Rick

Rick
Rick

Friday, March 26, 2010

Customize what parts of the object you want in the JSON Stream

This page has examples of how to customize what parts of the Java object gets output to the JSON stream.

in reference to: JacksonMixInAnnotations - FasterXML Wiki (view on Google Sidewiki)

test

test

in reference to:

"So in this case we have installed Hibernate as object-relational mapping (ORM)-provider. Hibernate is one of three ORM providers which Roo currently offers. EclipseLink and OpenJPA represent the alternative choices. In a similar fashion we have chosen the Hypersonic in-memory database database as our target database. Hypersonic is a convenient database for Roo application development because it relieves the developer from having to install and configure a production scale database."
- Chapter 2. Beginning With Roo: The Tutorial (view on Google Sidewiki)

Using ROO with MySQL and Maven Tomcat plugin

I spent some time trying to get Roo to work with both MySQL and the Tomcat plugin in such a way that I could do integration tests from Eclipse and deploy to Tomcat and use JNDI.

This posting is really more about how to use the Maven Tomcat plugin, it just so happens that I ran across this whilst using Roo.

The Roo tutorial seems a bit terse when it comes to these topics, which is a shame. I think Roo would be more useful if they helped setup the Maven Tomcat plugin and tried to configure the JDBC bits. Perhaps this will help document the problem and at least on possible solution.

I used Roo to setup MySQL (see guide) and then I tried to run mvn tomcat:run. It failed. Then I spent some time trying to figure out how to best configure the Maven Tomcat plugin and without breaking integration tests that I wrote.

Along the way, I learned quite a bit about the maven tomcat plugin, Roo and Spring 3. I figured I would share this with like minded folks on my team and beyond.

Here is what I came up for the pom.ml.

I could not get the XML stuff to show up correctly on blogger.. go here to see the code listings:

http://www.jroller.com/RickHigh/entry/using_roo_with_mysql_and

Roo added the following to pom.xml:



com.mysql.jdbc
com.springsource.com.mysql.jdbc
5.1.6



By the way Roo fills your application with artifact ids that are prepended with com.springsource. I am not sure why it does this, but it probably has something to do with having their own repositories and wanting to control the jar files that get used for customer support reason. It is either that or a secret plot for taking over the world.

Roo also added a tomcat plugin. The problem is that I need a context.xml file for tomcat so I can setup my JNDI datasource.

I reconfigured the tomcat plugin as follows in pom.xml:


org.codehaus.mojo

tomcat-maven-plugin

1.0-beta-1



tomcat





testing

yes











com.mysql.jdbc

com.springsource.com.mysql.jdbc

5.1.6









Notice that I add mysql jar file to the list of dependencies for the plugin so that Tomcat can find mysql and I can avoid the dreaded Class not found exception for the MySQL driver.

By the way, I tried many things before getting to this point. This is just the first one that worked. I am sure there are other ways, and even possible better ways. I got the above through trial and error and reading the maven tomcat plugin documents (sparse) and the tomcat manual.


Also notice that I have a tomcat property set. This tomcat folder will be under the project root directory, ${project.dir}/tomcat. This was the key for adding the JNDI datasource to my database.

In the ${project.dir}/tomcat dir I have a conf directory. The conf directory has the following files:


$ pwd
/Users/rickhightower/proto/application-catalog/tomcat/conf

$ ls
context.xml tomcat-users.xml web.xml


The tomcat-users.xml and web.xml were generated by the tomcat plugin under /target/tomcat. I just copied them here.

The context.xml is where I set up the JDBC as follows:




WEB-INF/web.xml

type="javax.sql.DataSource"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/mydb?autoReconnect=true"
username="mydb" password="mydb"
maxActive="20" maxIdle="10" maxWait="-1" />







(Now would be a good time to add the applicationContext.xml file to the watched resources (note to self)).

Notice that I have configured the Resource for my database.

In my application context file, I just add the following:




The problem is that now my code is somewhat tied to running in a container which is exactly what I don't want. I want to be able to run this application context without change in my integration tests.

I want to use this dataSource when I am testing and such.








To achieve this in Spring 3, I can use SPeL (Spring Expression Language) as follows:


ref="#{ systemProperties['ac.testing']=='yes' or systemProperties['ac.development']=='yes' ? 'dataSourceDBCP' : 'dataSourceJNDI'}"/>


The above basically states that if ac.testing or ac.development system properties are set to yes then use dataSourceDBCP otherwise use dataSourceJNDI.

I like SPeL.

Then to run with dataSourceDBCP instead of dataSourceJNDI (for whatever reason), you can pass a system environment variable as follows:


$ mvn tomcat:run -Dac.testing="yes"


You could setup a maven profile or something some such so that you don't ever have to pass this whilst running this from maven.

Tuesday, February 9, 2010

Maven 2.0.9 or higher

I wonder if Roo works with 2.2.1 which is the latest release of Maven.

If the latest version was 2.0.12 and you said 2.0.9 or higher I would not even blink.

But....

Also has anyone tested Roo against Maven 3.0 branch?

in reference to: http://static.springsource.org/spring-roo/reference/html/intro.html#intro-installation (view on Google Sidewiki)

Read the Roo manual now trying the tutorial (stay tuned)

I read the Roo manual (cover to cover). Now I want to try out the tutorial. I am still evaluating Roo. I am looking for a set of modern best practices for using the Spring Framework(for a client).

I watched a talk by Ben Alex on InfoQ wrt Roo. He stated that Roo uses the state-of-the-art best practice.

Not sure where to get started on this... I am thinking about installing on either my Debian instance (VMWare), Unbuntu instance (VMWare), or Windows XP (host OS).

I need to install Eclipse, Spring STS Eclipse plugins, Maven, etc. I'd like to do this on a relatively clean environment.

I think I will use Windows XP since it is the host OS and I will get the most RAM and CPU out of it (on this box).

Looks like on my windows box, I have JDK 1.6 and Netbeans but no Eclipse and no Maven.
I need something more than Maven 2.0.9 to run Roo. Time to download, download, download.

It seems you can download
with Eclipse from Spring source.

The Roo docs say to use Maven 2.0.9 or later. The latest released version of Maven is 2.2.1.
I wonder if this is ok. I am about to find out.

I downloaded maven 2.2.1

Next I downloaded Spring Roo...
Then I setup my .bashrc file as follows (cygwin):

export JAVA_HOME="/cygdrive/C/Program Files/Java/jdk1.6.0_18"
export MAVEN_HOME=/home/rhightower/tools/maven/apache-maven-2.2.1
export ROO_HOME=/home/rhightower/tools/roo/spring-roo-1.0.1.RELEASE
export PATH=$PATH:$JAVA_HOME/bin:$MAVEN_HOME/bin:$ROO_HOME/bin

I am using cygwin since I have to switch back and forth between Linux, MacOS and Windows. It is easier on my brain.

Maven ran fine. The JDK javac command ran fine, but Roo did not run.

rhightower@cmsjdl23 ~
$ mkdir sample

rhightower@cmsjdl23 ~
$ cd sample/

rhightower@cmsjdl23 ~/sample
$ ls

rhightower@cmsjdl23 ~/sample
$ roo.sh
Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/r
oo/bootstrap/Bootstrap
Caused by: java.lang.ClassNotFoundException: org.springframework.roo.bootstrap.B
ootstrap
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)

Seems like it is time to reread the Roo install docs.

Ok reread. It seems like I am doing everything right. (I did go ahead and add the symbolic link to /usr/bin/roo so I can just type roo).

Seems someone else has ran into my cygwin pain.


Ben Alex says the magic cygwin light is here...

He also suggests that if you come up with a cygwin fix for run.sh that it will be included in the distribution... (I know run.sh for Jboss and mvn.sh for maven both support cygwin... so perhaps... I will reverse that they do and include it....)

For now... I will take 10 minutes and try to hack the roo.sh to run on cygwin.

I followed Ben's link. I noticed that both JBoss and mvn support cygwin so I looked how they did it and then hacked roo.sh into submission.

Here is what I came up with.... (this roo.sh should work under Unix, Linux, MacOS and cygwin)...

roo.sh that is cygwin friendly...

#!/bin/sh

PRG="$0"

while [ -h "$PRG" ]; do
ls=`ls -ld "$PRG"`
link=`expr "$ls" : '.*-> \(.*\)$'`
if expr "$link" : '/.*' > /dev/null; then
PRG="$link"
else
PRG=`dirname "$PRG"`/"$link"
fi
done
ROO_HOME=`dirname "$PRG"`

# Absolute path
ROO_HOME=`cd "$ROO_HOME/.." ; pwd`

# echo Resolved ROO_HOME: $ROO_HOME
echo "ROO HOME $ROO_HOME"
echo "JAVA_HOME $JAVA_HOME"

cygwin=false;
case "`uname`" in
CYGWIN*)
cygwin=true
;;
esac

if [ "$cygwin" = "true" ]; then
export ROO_HOME="`cygpath -wp $ROO_HOME`"
export JAVA_HOME="`cygpath -wp "$JAVA_HOME"`"
export EXT_DIR=""$ROO_HOME\\dist";"$ROO_HOME\\lib";"$ROO_HOME\\work";"$JAVA_HOME\\jre\\lib\\ext""
echo "ROO HOME $ROO_HOME"
echo "JAVA_HOME $JAVA_HOME"
echo "EXT_DIR $EXT_DIR"
else
export EXT_DIR="$ROO_HOME/dist:$ROO_HOME/lib:$ROO_HOME/work:$JAVA_HOME/jre/lib/ext"
fi

while true; do
java -Djava.ext.dirs="$EXT_DIR" $ROO_OPTS -Droo.home="$ROO_HOME" org.springframework.roo.bootstrap.Bootstrap "classpath:roo-bootstrap.xml" $@
EXITED=$?
# echo Exited with $EXITED
if [ $EXITED -ne 100 -a $EXITED -ne 200 ]; then
break
fi
done

So to recap,,, I downloaded and installed SpringSource STS (with Eclipse), maven 2.2.1 and Roo 1.0.1. I had to hack roo.sh to get it to work with cygwin. I posted my hack on the Spring forum and on my twitter... Hopefully future version of roo will support cygwin.

Now I can actually go through the tutorial...

Ok.. I am up to the part where I running the selenium tests. It is taking a while. It seems maven is downloading dependencies.

Still downloading...

Still downloading...

Ahhh... I miss maven.. I think I drink a lot more coffee whenever I use maven.

[INFO] -----------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] -----------------------------------------
[INFO] Total time: 5 minutes 45 seconds
[INFO] Finished at: Tue Feb 09 16:11:54 PST 2010
[INFO] Final Memory: 16M/38M

Ok.. I am up to mvn selenium:selenese

Ok...

This runs for a while...

Ok.. Firefox has decided to update itself (I don't use firefox.. I usually use Chrome.. I am a big google fan.)

I had to restart firefox a few times. Then I gave up and updated firefox to the latest... Now things really don't work.

$ mvn selenium:selenese
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building tenminutes
[INFO] task-segment: [selenium:selenese]
[INFO] ------------------------------------------------------------------------
[INFO] [selenium:selenese {execution: default-cli}]
log4j:WARN No appenders could be found for logger (org.openqa.selenium.server.Se
leniumServer).
log4j:WARN Please initialize the log4j system properly.
[INFO] ------------------------------------------------------------------------
[ERROR] FATAL ERROR
[INFO] ------------------------------------------------------------------------
[INFO] Unable to delete file C:\Documents and Settings\rhightower\customProfileD
ir502545\parent.lock
[INFO] ------------------------------------------------------------------------
[INFO] Trace
Unable to delete file C:\Documents and Settings\rhightower\customProfileDir50254
5\parent.lock
at org.apache.tools.ant.taskdefs.Delete.handle(Delete.java:624)
at org.apache.tools.ant.taskdefs.Delete.removeDir(Delete.java:683)
at org.apache.tools.ant.taskdefs.Delete.execute(Delete.java:543)
at org.openqa.selenium.server.browserlaunchers.LauncherUtils.recursively
DeleteDir(LauncherUtils.java:72)
at org.openqa.selenium.server.browserlaunchers.LauncherUtils.deleteTryTr
yAgain(LauncherUtils.java:80)
at org.openqa.selenium.server.browserlaunchers.LauncherUtils.deleteTryTr
yAgain(LauncherUtils.java:84)
at org.openqa.selenium.server.browserlaunchers.LauncherUtils.deleteTryTr
yAgain(LauncherUtils.java:84)
at org.openqa.selenium.server.browserlaunchers.LauncherUtils.deleteTryTr
yAgain(LauncherUtils.java:84)
at org.openqa.selenium.server.browserlaunchers.LauncherUtils.deleteTryTr
yAgain(LauncherUtils.java:84)
at org.openqa.selenium.server.browserlaunchers.LauncherUtils.deleteTryTr
yAgain(LauncherUtils.java:84)
at org.openqa.selenium.server.browserlaunchers.LauncherUtils.deleteTryTr
yAgain(LauncherUtils.java:84)
at org.openqa.selenium.server.browserlaunchers.FirefoxChromeLauncher.rem
oveCustomProfileDir(FirefoxChromeLauncher.java:238)
at org.openqa.selenium.server.browserlaunchers.FirefoxChromeLauncher.clo


I tried manually deleting the lock file. (I had to kill Firefox first.). Then I reran the selinium tests. It still does not work. I think I will skip this step for now.

Going through #roo docs. ten-minutes example worked (took longer than 10 minutes).. clinic sample did not work (circular ref). Going on to tutorial.

stay tuned... more to come...



Monday, February 8, 2010

completion not completio

This:

the first time you press TAB you'd see the inline help and the completio options

should read

the first time you press TAB you'd see the inline help and the completion options

Side note:

Inbuilt? I have heard of builtin, but inbuilt seems an odd word.

in reference to: http://static.springsource.org/spring-roo/reference/html/usage.html#usage-shell (view on Google Sidewiki)

Roo itself do not require Maven

"Roo itself do not require Maven."

should read

"Roo itself does not require Maven."

Unless Roo is like sheep and can also be plural.

in reference to: http://static.springsource.org/spring-roo/reference/html/intro.html#intro-installation (view on Google Sidewiki)
Kafka and Cassandra support, training for AWS EC2 Cassandra 3.0 Training