Monday, October 20, 2008

Google App Engine will support Java ? Cool !

Well, I don't believe yet, but it seems to be that Java will be supported on GAP !

At the recently Google Developers Day in Bangalore, Keynote speaker Prasad Ram said that Google App Engine will now support Java.

Source here and here

Thursday, October 09, 2008

JBoss EJB3 Integration testing with Maven2 and Cargo

One of the most boring things in EJB development is that we need container. Compile-Build-Deploy-Run_some_testing_code - this is to long cycle to repeat it frequently without some kind of automation. I'll show you one of the possible ways to simplify this stuff. There are others, but I didn't tried them (I mean jboss microcontainer here). It's not complex, but it works and probably could be portable solution for not only my case.

So, my working environment is the following : Maven 2, JBoss 4.2.2 GA, jUnit 3.8.1 and of course EJB3. Build lifecycle with Maven 2 consists of some phases, like "...compile,test,package,integration-test...". Ok, with package and compile phases all is clear. Test... we need jboss to be up and running before we can test anything. How to start and stop jboss automatically during build ? The answer is Cargo with Maven2 plugin.

Cargo is a thin wrapper around existing containers (e.g. J2EE containers). It provides different APIs to easily manipulate containers.

Now we can assign "start container" and "stop container" goals to some Maven 2 build phase. First of all, we are interested in integration-test. Also we need to skip junit tests before integration test. To achieve this I'm using this config for cargo plugin :

<plugin>
<groupId>org.codehaus.cargo</groupId>
<artifactId>cargo-maven2-plugin</artifactId>
<version>0.3-SNAPSHOT</version>
<configuration>
<container>
<containerId>jboss4x</containerId>
<home>${jboss.home}</home>
<append>false</append>
</container>
<configuration>
<type>existing</type>
<home>${jboss.home}/server/default</home>
<properties>
<cargo.jboss.configuration>default</cargo.jboss.configuration>
<cargo.rmi.port>1099</cargo.rmi.port>
<cargo.logging>high</cargo.logging>
</properties>
</configuration>
<wait>false</wait>
</configuration>
<executions>
<execution>
<id>start-container</id>
<phase>pre-integration-test</phase>
<goals>
<goal>start</goal>
</goals>
</execution>
<execution>
<id>stop-container</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>

And this for surefire - to be able to run junit tests on integration-test phase, after container started at pre-integration-test phase :

<plugin>                                      
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
<executions>
<execution>
<id>surefire-it</id>
<phase>integration-test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<skip>false</skip>
</configuration>
</execution>
</executions>
</plugin>
I haven't any luck with "standalone" type configuration. I should say that when"existing" jboss installation is used here, overall process is quite fast:

[INFO] -------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] -------------------------------------------
[INFO] Total time: 42 seconds
[INFO] Finished at: Thu Oct 09 15:15:39 EEST 2008
[INFO] Final Memory: 9M/19M
[INFO] -------------------------------------------
42 seconds to compile code, package jar, copy jar to deploy dir, start jboss, run junit test, stop jboss and all this by doing just "mvn install" !

Complete source code available via svn here.

PS.
Time can be very different. My hardware is HP 6820s laptop. BTW, executing "mvn -o install" takes 23 sec :)

Friday, October 03, 2008

Build EJB3 with Maven 2 example with source code

My posts (the first and the second) about how to build EJB3 using Maven 2 was very popular. This post is some kind of update, but now, with complete source code. BTW, I'm going to publish some of sources I mentioned at my blog posts, since readers are asking for them. I mean source code from this post at minimum.


So, here it is !
In my further posts I'm going to highlight some of pitfalls one can face with.

Wednesday, August 27, 2008

Yet another Java Puzzle

No words here, just the code :)

class String
{
public static void main(String args[])
{
String String = new String();
}
}

Friday, July 25, 2008

Wow ! DZone.com using Acegi Security !

I've seen a very interesting alert box with exception message at dzone.com when tried to vote up for the post. It was something like "org.acegisecurity....". Hey, wait a minute, it is Acegi ! Really, login form looks as so

<form method="post" action="/links/j_acegi_security_check" 
class="loginform" id="addlinkform">
<div>
<label>Username:</label>
<input type="text" class="txt" name="j_username" size="20"/>
</div>
<div>
<label>Password:</label>
<input type="password" class="txt" name="j_password" size="20"/>
<label/>
<a href="http://zones.dzone.com/user/password"
class="forgotpass">
Forgot password?</a>
</div>
<div>
<label class="cbx">
<input type="checkbox" name="_acegi_security_remember_me"
checked=""/>
remember me
</label>
</div>
<div class="submit">
<input type="submit"
onsubmit="lightbox.prototype.deactivate();" value="Login"/>
<a rel="deactivate" class="lbAction" href="#">
<input type="button" value="Cancel"/></a>
</div>
</form>

j_acegi_security_check, j_username, j_password, _acegi_security_remember_me - it seems to be really Acegi !

Tuesday, July 22, 2008

Struts 2 : Deprecate or remove Dojo plugin

Sometimes it is very useful to read struts-dev mailing list. See here. In fact I'm very happy of that, because I was going to use some AJAX with Struts 2 and Dojo plugin. One thing I couldn't figure out is why Dojo ?? Why not Prototype ? Why not jQuery ? Well, OK. If developers using Dojo, I will do the same. Now the situtation is more clear and I'm not going to use Dojo anymore. Maybe Prototype ? :)

Friday, July 18, 2008

A little bit of dependency injection with Struts 2 and Spring 2

I'm using Struts 2 + Hibernate 3 + Spring 2 for my current project. I have to admit that Struts+Spring integration is really easy. The key integration point is Struts 2 Spring plugin. I'll not repeat the docs here. Only one thing I should admit that I prefer to handle spring dependencies by myself because of little bit different version of Spring - 2.0.5 in plugin and 2.0.8 in my project. So, I have this lines in my pom.xml (Maven 2 build file)

<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-spring-plugin</artifactId>
<version>2.0.11.1</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</exclusion>
</exclusions>
</dependency>
and
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
etc. I'm injecting Spring powered services into Struts 2 actions, but I had never had a chance to inject some kind of static list. Now I have, so the story about it.

I have a static list of countries. I think it doesn't make sense to store this info in database. The number of countries is not that large. If so, I need to store this data in some data structure and place it in memory. It could be List or Map - it depends on situation. And init this data structure once application starts. In web app when war file being deployed, Spring's WebApplicationContext initialized. So, when we define our list as Spring bean, it will be initialized at the application start and will be ready to other manipulations. I've defined it as so :

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-2.0.xsd">

<util:list id="countryList" list-class="java.util.LinkedList">
<value>item1</value>
<value>item2</value>
<value>item3</value>
</util:list>

</beans>
Here is the one question on Spring forum and official docs about it. For now we have a LinkedList filled with countries. We are going to inject it into Spring action and then show it at the "view" jsp.

Please note that id of our list is countryList. In fact it is a name of the bean. It's name really important since Struts 2 Spring pluring's default autowiring strategy is by name (I'm using default autowiring type and happy with that). It means that we can use setter injection this way
public class SomeStruts2Action extends ActionSupport
{
public List countryList;

public void setCountryList(List countryList) {
this.countryList = countryList;
}
.......
}
Please note that id and class member name with type List are equal. That is all ! But actually not all :) We have to tell where is our list definition is. In my case it's at one of jars so I have this line in web.xml :

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext-myCountryList.xml,
/WEB-INF/appContext-web.xml</param-value>
</context-param>

Have fun !