Wednesday, April 25, 2012

Java Regex Pattern and "^M"

A simple pattern:
 Pattern title = Pattern.compile("^Alert\\s*Digest.*", Pattern.CASE_INSENSITIVE);
When I tried to match this string. It failed:
Alert Digest No 6 of 2012^M 
 Yes, you can see that "^M" in the string. This is a "line terminator" which "." will not match by default.

So the solution is:
 Pattern title = Pattern.compile("^Alert\\s*Digest.*", Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
 This is the description from API document:

DOTALL

public static final int DOTALL
Enables dotall mode. In dotall mode, the expression . matches any character, including a line terminator. By default this expression does not match line terminators.

 

Invalid property 'packagesToScan' of bean class [org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean]

This is a maven webapp project with m2eclipse integrated into eclipse. In this project there are some hibernate dependencies used in spring-hibernate API. This is the related snippets:

    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="packagesToScan" value="com.seanlinxs.inventory.domain" />
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.cache.provider_class">org.hibernate.cache.NoCacheProvider</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
            </props>
        </property>
    </bean>




After changing the hibernate dependencies with m2e pom.xml GUI editor in eclipse many times, i.e. changing versions or something else like vendors. I found that junit runs well but tomcat deployment fail with this:

org.springframework.beans.NotWritablePropertyException: Invalid property 'packagesToScan' of bean class [org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean]: Bean property 'packagesToScan' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?



This is not a real issue with  AnnotationSessionFactoryBean, It is actually a jar conflict!
 E.g. In m2e pom.xml GUI editor when you see "ONE" hibernate-core, it might be "MANY" versions in the target WAR. You "remove" one dependency, then "save" the pom.xml, then add a different one, then "save" again. What you get in target WAR is two versions! You didn't really remove it though you might think you did. Run mvn clean will "REALLY" clean(empty) the target library directory and when you run mvn tomcat:deploy again everything should be WYSIWYG as expected.