24 December 2008

Maven not copying empty folders under src\main\webapp folder

I noticed that Maven does not copy empty folders [even after doing a mvn clean package] under src\main\webapp folders. I had created a empty folder under src\main\webapp called exhibit which will be populated with images that are generated during runtime of the web application. So as a workaround, I placed an empty file called markerfile.txt so that Maven started packing the empty folders.

The configuration used is as below.
Maven version: 2.0.9
Java version: 1.6.0_10
OS name: "linux" version: "2.6.27-7-generic" arch: "amd64" Family: "unix"


22 December 2008

Getting row count in JPA using EntityManager and Query

We can make use of the following code snippet to get row count in JPA.

      Query query=em.createNativeQuery("select count(*) as rowcount from candidate where loginid=?1 or email=?2");
        query.setParameter(1,getCandidate().getLoginid().trim());
        query.setParameter(2,getCandidate().getEmail().trim());
        if( ((BigInteger)query.getSingleResult()).intValue() > 0){
            return true;
        }

Configuring log4j for a SEAM based application in Tomcat

By default Tomcat uses JDK logging. I used the following in log4j.xml [which should go under WEB-INF\classes folder] to enable log4j for a SEAM based web application [infact also for any other web application] in Tomcat. In Maven please place it under, src\main\resources
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="false">

<appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">

<param name="Target" value="System.out"/>

<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{HH:mm:ss,SSS} %-5p [%c{1}] %m%n"/>
</layout>
</appender>
<appender name="FILE" class="org.apache.log4j.DailyRollingFileAppender">

<param name="File" value="${catalina.base}/logs/yourwebappname.log"/>

<param name="DatePattern" value="'.'yyyy-MM-dd"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{HH:mm:ss,SSS} %-5p [%c{1}] %m%n"/>
</layout>
</appender>

<root>
<priority value="INFO"/>

<appender-ref ref="FILE"/>
<appender-ref ref="CONSOLE"/>
</root>

</log4j:configuration>

Note: Please ensure log4j jar is available in the classpath. In a Maven based application add the below dependency to include log4j jar.
<dependency>
<groupId>apache-log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.14</version>
</dependency>

20 December 2008

How to populate a select box (drop down) in Seam with values in List

This post is using Seam 2.1.1. Seam alleviates the pain to deal with UISelect and in the example below, we display a list of exams and the user selects an exam.

The code shown below is part of  the Seam backing bean with the name examscheduler. It retrieves list of exams and populates in examsToSelect List. Also please note that I have omitted the getter setter for private Exam exam here.

    @Out(required=false)
    List<Exam> examsToSelect;

    @Factory("examsToSelect")
    public void populateValues(){
        examsToSelect=em.createNamedQuery("Exam.findAll").getResultList();
    }
   private Exam exam;

Now the UI part,
<h:selectOneMenu id="exam" value="#{examscheduler.exam}">
 <s:selectItems value="#{examsToSelect}" var="exam" label="#{exam}"
           noSelectionLabel="select..." />
     <s:convertEntity />
</h:selectOneMenu>

And finally the configuration in component.xml
<ui:jpa-entity-loader entity-manager="#{em}" />
and the xmlns at the top as an attribute of components (if we have not specified one already for ui)
xmlns:ui="http://jboss.com/products/seam/ui"


Note: I had overridden the default name entityManager

19 December 2008

How to enable copy paste when using Terminal Server Client in Ubuntu 8 plus

Go to Applications-->Accessories-->Terminal just type
rdesktop machinenameOrIP -r clipboard:PRIMARYCLIPBOARD

17 December 2008

How to solve Could not create Component: org.jboss.seam.ui.EntityConverter

If you have migrated to 2.1.x or above and getting the above Exception whose root cause is as below, please read on.
Caused by: java.lang.IllegalArgumentException: no such field: org.jboss.seam.ui.EntityConverter.entityManager

Solution:
If you have something similar to the following in your component.xml
    <component name="org.jboss.seam.ui.EntityConverter">
        <property name="entityManager">#{em}</property>
    </component>
please change it to
<ui:jpa-entity-loader entity-manager="#{em}" />

Dont forget to add xmlns:ui="http://jboss.com/products/seam/ui" on the top of the file to components

Where to download latest SEAM reference manual?

You can download the PDF for 2.1.1.GA by clicking here
You can choose other versions from this link

16 December 2008

Setup Unit and Integration Test Environment using embedded JBoss for Maven based SEAM applications

This post explains how to setup and run unit and integration tests in a Maven based SEAM project. The server used is Tomcat [the same configuration is valid for JBoss but the scope of few dependencies should be made as provided. I will cover the setup with JBoss in the next post].We will also learn to run test coverage reports using Cobertura.

The dependencies required for embedded JBoss server are as below. Please copy these dependencies to your project POM.

        <dependency>

            <groupId>org.jboss.embedded</groupId>

            <artifactId>jboss-embedded-all</artifactId>

            <version>beta3.SP4</version>

            <exclusions>

                <exclusion>

                    <groupId>org.jboss.embedded</groupId>

                    <artifactId>jboss-embedded</artifactId>

                </exclusion>

                  <exclusion>

                    <groupId>org.jboss.microcontainer</groupId>

                    <artifactId>jboss-deployers-client-spi</artifactId>

                </exclusion>

                <exclusion>

                    <groupId>org.jboss.microcontainer</groupId>

                    <artifactId>jboss-deployers-core-spi</artifactId>

                </exclusion>

            </exclusions>

            <scope>test</scope>

        </dependency>

        <dependency>

            <groupId>org.jboss.embedded</groupId>

            <artifactId>hibernate-all</artifactId>

            <version>beta3.SP4</version>

            <scope>test</scope>

        </dependency>

        <dependency>

            <groupId>org.jboss.embedded</groupId>

            <artifactId>thirdparty-all</artifactId>

            <version>beta3.SP4</version>

            <scope>test</scope>

        </dependency>

        <dependency>

            <groupId>org.jboss.embedded</groupId>

            <artifactId>jboss-embedded</artifactId>

            <version>beta3.SP4</version>

            <scope>test</scope>

            <exclusions>

                <exclusion>

                    <groupId>org.jboss.microcontainer</groupId>

                    <artifactId>jboss-deployers-client-spi</artifactId>

                </exclusion>

              </exclusions>

        </dependency>

Please add the following for TestNG and el-api for parsing the expression language
<dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>

            <version>5.7</version>
            <scope>compile</scope>
            <classifier>jdk15</classifier>
        </dependency>

        <dependency>
            <groupId>javax.el</groupId>
            <artifactId>el-api</artifactId>
            <version>1.2</version>
            <scope>test</scope>
        </dependency>

Download Embedded JBoss and unzip the following folders (that are under bootstrap folder) under src/test/resources of your application as in the screenshot . Note that I created WEB-INF folder and placed web.xml under it.
We need not copy any other jar files that are in the embedded JBoss as we have already specified them as dependencies in pom.xml.


Now comes the plugins to be added. The below will work both in J2SE 5.0  and Java SE 6.0.

            <plugin>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
               
                    <execution>
                        <configuration>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>javax.xml.bind</groupId>
                                    <artifactId>jaxb-api</artifactId>
                                    <version>2.1</version>
                                </artifactItem>
                            </artifactItems>
                            <outputDirectory>${project.build.testOutputDirectory}/endorsed</outputDirectory>
                        </configuration>
                        <id>download-jaxb-api</id>
                        <phase>process-test-resources</phase>
                        <goals>
                            <goal>copy</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>

    
                    <childDelegation>true</childDelegation>
                    <useSystemClassLoader>true</useSystemClassLoader>
                    <argLine>-Djava.endorsed.dirs=${project.build.testOutputDirectory}/endorsed -Dsun.lang.ClassLoader.allowArraySyntax=true</argLine>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>cobertura-maven-plugin</artifactId>
                <version>2.1</version>
                <configuration>
                    <check>
                        <haltOnFailure>false</haltOnFailure>
                        <regexes>
                            <regex>
                                <pattern>com.thea.*</pattern>
                                <branchRate>90</branchRate>
                                <lineRate>90</lineRate>
                            </regex>
                    
                        </regexes>
                    </check>
                    <instrumentation>
                        <includes>
                            <include>com/thea/**/*.class</include>
                           
                        </includes>
                    </instrumentation>
                </configuration>
                <executions>
                    <execution>
                        <id>clean</id>
                        <phase>pre-site</phase>
                        <goals>
                            <goal>clean</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>instrument</id>
                        <phase>site</phase>
                        <goals>
                            <goal>instrument</goal>
                            <goal>cobertura</goal>
                            <goal>check</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>


To execute tests, please use mvn test command. To run cobertura please use mvn cobertura:cobertura

Now, when we try to run mvn test we may be greeted with NoSuchMethod error related to javassist. This is due to incompatible version of javassist that comes in as a result of transitive dependency of SEAM. Hence exclude them as below.

         <dependency>
            <groupId>org.jboss.seam</groupId>
            <artifactId>jboss-seam</artifactId>
            <version>2.1.1.CR2</version>
                     <exclusions>
            <exclusion>
            <groupId>javax.el</groupId>
                              <artifactId>el-api</artifactId>
            </exclusion>
                             <exclusion>
            <groupId>javassist</groupId>
                    <artifactId>javassist</artifactId>
            </exclusion>

12 December 2008

Solution for Commit failed (details follow): Unrecognized URL scheme for ''

I was using NetBeans 6.5 and suddenly I got the error below.
Commit failed (details follow): Unrecognized URL scheme for ''

Restarting NetBeans and re trying to commit did not work. Finally managed to commit by committing individual files.

I googled and found that many other developers are also facing the same issue with other SVN clients, but none of the reasons prove the root cause [atleast for me :)]

Added on 14-Dec-2008: One way of reproducing this issue is when we copy svn version controlled files from yet another repository. Let us assume we have project1 situated on svn server1 repo and project2 on svn server2 repo. We find reusable files [say Java file or javascript file] in project1 and we wish to copy and commit into project2.
Now when copying if we do not exclude the .svn folder [subversion specific files], we may get the above error when trying to commit.