Category: Programming



Adding Ivy to a J2EE project in Eclipse is pretty easy:

I am currently running Eclipse Galileo, but I am pretty sure this works with any other version above 3.0.

I will also use a Dynamic Web Application for these step by step directions.

  1. Create a Tomcat Server for your project.   If you haven’t done this before, follow this simple blog
  2. Download and install the Apache IvyDE plugin from within the Eclipse Updater.
  3. Restart Eclipse
  4. Create A new Dynamic Web project.
  5. Right click on your project and go to properties.
  6. Select Java Build Path
  7. Select the Libraries Tab
  8. Select Add Library
  9. Select IvyDE Managed Dependencies and hit next
  10. Accept the default on the next screen and click through the generated errors.
  11. Right click on your project and select New-Other
  12. Type Ivy in the wizard search box.
  13. Select the Ivy file wizard and hit next.
  14. On the next screen select browse and browse to your current project.
  15. Hit finish and this will create an ivy file at the root of your dynamic web project.
  16. Add a few dependencies into your ivy file to see what happens.
  17. A word of note, if your Ivy Dependency tree doesn’t change automatically you can either right click on the ivy.xml entry in your project and select the menu item resolve, or you can change the basic way that the IvyDE with resolve the dependencies.   By selecting Window-Preferences-Ivy.      I usually have Ivy set to trigger a refresh on Eclipse Startup, as well as  on project open.    Seeing that the dependency tree isn’t changed very much I don’t mind telling Ivy to resolve when I make a change to the ivy.xml file itself directly.
  18. Add a servlet to your application and configure it.
  19. Right click on your app, and select Run As, Run On Server
  20. Select your server
  21. Select Finish

BTW, here is the file I used for Ivy.

<?xml version="1.0" encoding="ISO-8859-1"?>
<ivy-module version="2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:noNamespaceSchemaLocation="http://ant.apache.org/ivy/schemas/ivy.xsd">
    <info
        organisation=""
        module=""
        status="integration">
	</info>
	<configurations defaultconfmapping="build->default;compile->compile(*),master(*);test,runtime->runtime(*),master(*)">
        <conf name="build"/>
        <conf name="compile"/>
        <conf name="test" extends="compile"/>
        <conf name="runtime" extends="compile"/>
    </configurations>
    <dependencies>
		<dependency org="axis" name="axis" rev="1.4" conf="runtime->default"/>
		<dependency org="log4j" name="log4j" rev="1.2.9" conf="runtime->default"/>
		<dependency org="javax.servlet" name="servlet-api" rev="2.4" conf="runtime->default"/>
		<dependency org="javax.servlet" name="jstl" rev="1.1.2" conf="runtime->default"/>
		<dependency org="org.hdiv" name="hdiv-jstl-taglibs-1.1.2" rev="2.0.3" conf="runtime->default">
			<exclude org="org.springframework" />
			<exclude org="taglibs"/>
		</dependency>
	</dependencies>
</ivy-module>

GWT and Maven, Oh The Agony!!

I promised I would put up another post on the new job, but unfortunately I have been so swamped at the new gig that I haven’t had the time to post anything.

Here is the run down.  I am working for a consulting firm in the Atlanta area doing J2EE development for a major hotel chain.    Technology wise, the project involves, GWT for the presentation layer, Spring(MVC and IOC) for the J2EE framework as well as the transaction management and hibernate for the data persistence layer.     We are using Agile for our project methodology, Eclipse Galileo for our IDE, and Oracle for our database.

As with most projects there were a few initial hurdles to get over.. such as making Maven and GWT 2.0 work nicely together, and being able to deploy to Artifactory.  We also had issues with getting the Embedded Jetty server (comes with the GWT plugin) running with JNDI.  We could never get this to work, so we just decided to use straight JDBC locally for the embedded server and deploy the app using JNDI to Artifactory for deployment to the other environments.

After getting a breather(some call this the weekend!) and refocusing on GWT and Maven without a deadline looming, I found this article on how to configure GWT 2.0 and the Maven Plugin within eclipse.

Maven GWT 2.0 and Eclipse

If this link works for you, let me know.. but for me I had many issues.

First of all, I typically run maven from the command line and not from within the Eclipse IDE.. maybe that’s not the ideal way to do things, but I feel it better models the current examples on the web which are IDE agnostic.

1.) Regarding the post, just changing gwt.version from 1.7.1 to 2.0 didn’t force Maven to do a refresh of the newer GWT modules.  I had to actually edit the version for the GWT plugins to cause a refresh to occur.  If there is a better way let me know and I will edit this post.

2.) I kept receiving an error referring to the GWTTestSimple class mentioning that this needed to be recompiled.  After some trial and error I realized I couldn’t just do a mvn clean to clean what was in my war/WEB-INF/lib directory.  The 1.7.1 versions keep staying around.  So I physically deleted them.

3.)  I had to add the following to my web.xml (why are my test cases being copied into the war?)

<servlet>
<servlet-name>jUnitHostImpl</servlet-name>
<servlet-class>com.google.gwt.junit.server.JUnitHostImpl</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>jUnitHostImpl</servlet-name>
<url-pattern>/com.theartoftech.gwt20example.Application/junithost</url-pattern>
</servlet-mapping>

4.) mvn clean will clean everything in the target directory(and delete this directory), but not in the war directory – as referenced in 2 – which is from where gwt:run will run the embedded server. You must either add an ant task to your pom.xml or delete it by hand.

5.) mvn gwt:compile will compile your gwt code into javascript and place this in your target directory. (Directory named target/[war snapshot name])

6.) mvn install will run your test cases, and move your WEB-INF and META-INF into the target directory under your [war snapshot name] directory.  This doesn’t however move anything into your war directory.

7.) mvn gwt:run will copy your client side javascript + htm/css to your war directory as well as your WEB-INF (including lib and classes).  Curiously there will be no web.xml file copied.

I received errors like this; Module declares 1 <servlet> declaration(s), but a valid ‘web.xml’ was not found at c:\some\path\to\my\war\WEB-INF\web.xml

8.) Enter Ant – probably not the best way to do this, but I have never figured out an easy way to copy files within maven that works better than this.  After making this change the web.xml gets copied to war/WEB-INF

<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<mkdir dir=”./war/WEB-INF” />
<copy overwrite=”true” todir=”./war/WEB-INF/”>
<fileset dir=”./src/main/webapp/WEB-INF”>
<include name=”web.xml” />
</fileset>
</copy>
</tasks>
</configuration>
</execution>
</executions>
</plugin>



My Pom.xml:

<?xml version=”1.0″ encoding=”UTF-8″?>
<project xmlns=”http://maven.apache.org/POM/4.0.0″ xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xsi:schemaLocation=”http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd”>
<!–
POM generated by gwt-maven-plugin archetype
–>
<modelVersion>4.0.0</modelVersion>
<groupId>com.theartoftech</groupId>
<artifactId>gwt20example</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>

<properties>

<!– convenience to define GWT version in one place –>
<gwt.version>2.0.0</gwt.version>

<!–  tell the compiler we can use 1.5 –>
<maven.compiler.source>1.5</maven.compiler.source>
<maven.compiler.target>1.5</maven.compiler.target>

</properties>

<dependencies>

<!–  GWT dependencies (from central repo) –>
<dependency>
<groupId>com.google.gwt</groupId>
<artifactId>gwt-servlet</artifactId>
<!–      <version>${gwt.version}</version>–>
<version>2.0.0</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.google.gwt</groupId>
<artifactId>gwt-user</artifactId>
<!–      <version>${gwt.version}</version>–>
<version>2.0.0</version>
<!–      <scope>provided</scope>–>
</dependency>

<!– test –>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.7</version>
<scope>test</scope>
</dependency>

</dependencies>
<build>
<outputDirectory>war/WEB-INF/classes</outputDirectory>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>gwt-maven-plugin</artifactId>
<version>1.2</version>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>generateAsync</goal>
<goal>test</goal>
</goals>
</execution>
</executions>
<configuration>
<runTarget>com.theartoftech.gwt20example.Application/Application.html</runTarget>
<source path=”client” excludes=”**/GwtTest*.java” />
</configuration>
</plugin>
<!–
If you want to use the target/web.xml file mergewebxml produces,
tell the war plugin to use it.
Also, exclude what you want from the final artifact here.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<webXml>target/web.xml</webXml>
<warSourceExcludes>.gwt-tmp/**</warSourceExcludes>
</configuration>
</plugin>
–>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<mkdir dir=”./war/WEB-INF” />
<copy overwrite=”true” todir=”./war/WEB-INF/”>
<fileset dir=”./src/main/webapp/WEB-INF”>
<include name=”web.xml” />
</fileset>
</copy>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>

Is it not true that as a Developer, you must have the patients of a saint?   Case in point;

I just spent a few hours trying to track down why I was getting the error that is the title of this post.   Apparently the context.xml within your app deployed to Tomcat 6.0 is case sensitive.  I really should have known this as we are talking about a Java based application.

I had this..

<?xml version=”1.0″ encoding=”UTF-8″?>
<context reloadable=”true”>
<Loader loaderClass=”org.springframework.instrument.classloading.tomcat.TomcatInstrumentableClassLoader”/>
<Resource name=”jdbc/dwspring2″ auth=”Container” type=”javax.sql.DataSource”
maxActive=”100″ maxIdle=”30″ maxWait=”10000″
username=”userid”
password=”password” driverClassName=”com.mysql.jdbc.Driver”
url=”jdbc:mysql://localhost:3306/springtutorial”/>
</context>

When I should of had this..

<?xml version=”1.0″ encoding=”UTF-8″?>
<Context reloadable=”true”>
<Loader loaderClass=”org.springframework.instrument.classloading.tomcat.TomcatInstrumentableClassLoader”/>
<Resource name=”jdbc/dwspring2″ auth=”Container” type=”javax.sql.DataSource”
maxActive=”100″ maxIdle=”30″ maxWait=”10000″
username=”userid”
password=”password” driverClassName=”com.mysql.jdbc.Driver”
url=”jdbc:mysql://localhost:3306/springtutorial”/>
</Context>

Do you know of a way to force better checking on this before it’s deployed?

I have recently started using a UML application to design a software system that I am building called StarUML. It’s highly addictive because it gives you many of the basic building blocks for designing a very detailed system, and it generates very good looking diagrams. So far I have created Use Case diagrams, Sequence Diagrams, and Class Diagrams with this tool, and they have turned out really well. The only real problem, is that it’s designed for Windows. However, lately I am becoming more of a convert that Linux can suffice as my primary desktop.

So here is what I did to get it running on UBuntu 9.10

Under Applications Select Ubuntu Software Center
Search for Wine
If you haven’t already Installed it – install.
wget
http://internap.dl.sourceforge.net/sourceforge/staruml/staruml-5.0-with-cm.exe
wget http://kegel.com/wine/winetricks
sh winetricks vcrun6 msxml3
wine staruml-5.0-with-cm.exe

That will install starUML onto your system to run within Wine.

My last post dealt with Websphere configuration for Hibernate with a Datasource for MySQL.  I also mentioned how I typically use Apache Tomcat to do a lot of my testing before deploying to Websphere.  Smart readers will know that the types of applications I am referring to do not include JMS or EJBs but are straight Servlet or Web Services type apps.  Development and Testing for full J2EE based applications should take place using other Application Servers, and or development environments.

With that said, I thought I would post the steps I went through to configure Tomcat for a Datasource, and what I had to do to configure my Hibernate configuration file for that source.

First off, I made no changes to my hibernate.cfg.xml from the one I deployed to Websphere.  So the following configuration parameters are still the same.

<property name=”current_session_context_class”>thread</property>
<property name=”hibernate.bytecode.use_reflection_optimizer”>false</property>
<property name=”hibernate.connection.datasource”>java:comp/env/jdbc/mysqlblogpostdataref</property>
<property name=”show_sql”>true</property>
<property name=”dialect”>org.hibernate.dialect.MySQLDialect</property>
<property name=”hibernate.bytecode.use_reflection_optimizer”>false</property>
<property name=”hibernate.connection.datasource”>java:comp/env/jdbc/mysqlblogpostdataref</property>
<property name=”show_sql”>true</property>
<property name=”dialect”>org.hibernate.dialect.MySQLDialect</property>

I also kept my web.xml the same as my deployment to Websphere.


<resource-ref>
<description>My Blog DataSource</description>
<res-ref-name>jdbc/mysqlblogpostdataref</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>

Now edit your context.xml file stored in the $CATALINA_HOME/conf directory and add a Resource entry.


<Context path="/DBTest" docBase="DBTest"
debug="5" reloadable="true" crossContext="true">

<!-- maxActive: Maximum number of dB connections in pool. Make sure you
configure your mysqld max_connections large enough to handle
all of your db connections. Set to -1 for no limit.
-->

<!-- maxIdle: Maximum number of idle dB connections to retain in pool.
Set to -1 for no limit.  See also the DBCP documentation on this
and the minEvictableIdleTimeMillis configuration parameter.
-->

<!-- maxWait: Maximum time to wait for a dB connection to become available
in ms, in this example 10 seconds. An Exception is thrown if
this timeout is exceeded.  Set to -1 to wait indefinitely.
-->

<!-- username and password: MySQL dB username and password for dB connections  -->

<!-- driverClassName: Class name for the old mm.mysql JDBC driver is
org.gjt.mm.mysql.Driver - we recommend using Connector/J though.
Class name for the official MySQL Connector/J driver is com.mysql.jdbc.Driver.
-->

<!-- url: The JDBC connection url for connecting to your MySQL dB.
-->

<Resource name="jdbc/mysqlblogpostdataref" auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
username="dbuser" password="yourpassword" driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/yourdatabase"/>
</Context>

The key here is to make sure your resource name in the context.xml matches your reference name in the web.xml, this is the only way Tomcat environments know how to map resources from your deployment descriptor to the context.xml.

Yes this is a bit of a tangent from my usual posts, but I like to geek out as much as the next guy, write some code, and deploy it to various Application servers.

I, as I suspect many programmers do, tend to develop my applications within Tomcat then build releases for other platforms such as WebSphere or Weblogic.   In doing so, I have come to the pretty obvious conclusion that it’s a good idea to use a Datasource references within my deployment descriptors, and allow the deployment managers to configure the AppServers for the given resources at deployment time. Over the last few days I have been struggling with how to properly configure a datasource within WebSphere and use the proper naming string within my hibernate.cfg.xml files.  I thought I would share my configuration steps in hopes that no one else has to go through this.

First You have to setup a JDBC Provider.  A JDBC provider is a JDBC driver that you can then use to connect to a database using a data source that you setup later in this example.

From the Administrative console, Open up the Resources pane, then expand JDBC.  Select JDBC Providers.

Figure 1. Depending on your scope selection, this screen shows all of the providers you have set up on your Websphere Server.

Select the Scope that you want this resource to be available at. Cell will make this resource available to all of the managed nodes and servers, but you can also make this resource available at the node or the server level.

Select New.

Figure 2. Set up your new resource.

Database Type: User Defined

Implementation Class Name: com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource

Name: That’s up to you.

Select Next, and setup your classpath to your mysql driver.

Figure 3. Setup Class Path

Hit Next then Finish.  Don’t forget to Save this setup to the master configuration.

Under the JDBC node, Select Data Providers.

Figure 4. Data Sources List

Select New.

Figure 5. New Data Source

Data Source Name: Up to you

JNDI Name: this is up to you, but the standard naming convention should be;  jdbc/datasourcerefname.  For this example we will use jdbc/mysqlblogpost.

Select Next

Figure 6. Select an Exisiting JDBC Provider

Make sure on this screen you select the JDBC provider you setup earlier.

Select Next

Figure 7. Leave the default as is.

Hit Next and then Finish.  — Remember to save this to your master configuration.

Figure 8. Your Data Source List should look something like this now.

Select the data source you just created and select the JAAS-J2C authentication data link to the side.

Figure 9. JAAS List

Select New.

Figure 9. New User/Password Reference

Hit Ok, and Save.

Go back to your Datasource and select Custom Properties.

Either set or add the following properties.

Property
Name
Type Value
databaseName java.lang.String mysqlDB
Port java.lang.String 3306
serverName java.lang.String <server-name>

After you add this..  Remember to save the changes to your master configuration.

Now lets get to your Application.

Setup a Resource Reference in your Deployment Descriptor.

When you deploy this application Websphere will ask you to map this resource.  You will be mapping it to the data source you just set up. jdbc/mysqlblogpost


<resource-ref>

<description>Your New DataSource</description>

<res-ref-name>jdbc/mysqlblogpostdataref</res-ref-name>

<res-type>javax.sql.DataSource</res-type>

<res-auth>Container</res-auth>

</resource-ref>

Then your Hibernate Configuration file can reference your web.xml’s reference.

Hibernate.cfg.xml
<property name="current_session_context_class">thread</property>
<property name="hibernate.bytecode.use_reflection_optimizer">false</property>
<property name="hibernate.connection.datasource">java:comp/env/jdbc/mysqlblogpostdataref</property>
<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>

That’s It!!  Happy Deploying!!

Hey everyone, this is my first Blog.  I have been a twitter fanatic for about a year now, and a facebook poster for well 2 years.   This is my first attempt to branch beyond that more personal social interaction and setup something a bit more about my professional life.   While there are many blogs out there, detailing all things Java, J2EE, Oracle and the like, there will be, and I promise this, nothing with the unique humor and outlook that I bring to the  table.

We are all unique right..  well I am a unique Geek.  I don’t really fit into your typical stereotype of your total geek, but yet I don’t fit totally into your image of a business guy either.  I straddle these two areas in my day to day life, and it’s my humor and my outlook that keeps me sane.

So as I increase my readership, I hope to learn from, as well as teach those who feel as if the world is an interesting and fascinating place, and that to operate in both the business and technical circles equally and with success, is an enjoyable experience.

Follow

Get every new post delivered to your Inbox.