Your Maven Java WEB project in Eclipse WTP

Saturday 2011-01-29

In our company, all Java projects are setup with Maven configuration so that after a “mvn eclipse:eclipse” any developer is generally good to go. One of these projects was a web project but would not transform into a WTP project. By running “mvn eclispe:eclipse” it became a Java project, but could not be added to a Server in Eclipse. It was not a WTP project.

I learned that the author of the project tried but never got the WTP plugin to work properly. Using the Google, I found more people who are having the same problem converting their existing Maven Java Web projects in Eclipse into a WTP project. There are even a few desperate articles describing how to edit your .project and .classpath files. Oh dear. This calls for an article on http://www.rolfje.com.

Read the rest of this entry »


Simple Strict Date Parsing

Saturday 2010-03-06

In Java, the DateFormat.parse() method is a funny little critter. It helps you by trying to figure out what date you actually meant when you typed in “35/12/2O10″ (note the letter “O” in 2O10). In this case, it will parse the date without errors or warnings, and returns the date “11/12/04″ (November 12th, 0004). That’s because it thinks “35” is a month, and “2” is the year, ignoring everything after the letter “O”.

DateFormat tries to convert the “35th month” into 2 years and 11 months, and correct the date accordingly. df.setLenient(false) prevents this, but that still leaves the problem of the parsing stopping at the first wrong character without warning.

I needed a much stricter way of parsing dates, and yesterday I found an elegant solution to this problem. It’s so small I was able to tweet it in less than 140 characters, but I thought it deserved a decent blogpost so here it goes:

public Date parseDateString(String inputDateString) 
         throws ParseException {
  DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT);
  Date parsedDate = df.parse(inputDateString);

  if (!inputDateString.equals(df.format(parsedDate))) {
    throw new ParseException("Invalid Date", 0);
  }
  return parsedDate;
}

The brilliance here is in the comparing of the formatted date with the original input. The method returns a normal ParseException so you can perfectly replace your original df.parse() calls with it, making them more strict.

Thanks to Bas for this elegant and simple solution.


Ibatis Inline Parameter Maps

Tuesday 2010-02-16

Today I (re)discovered a feature in the Ibatis data mapper framework which was clearly documented, but for some reason was not being used in our project. The feature is called “inline parameter maps” and combined with a wrapper bean it can clean up a lot of clutter in the code and in the SqlMaps. Please feel free to share this example with your fellow Ibatis Data Mapper 2 framework users.

Read the rest of this entry »


Sonar “Close Connection” warning workaround.

Tuesday 2009-10-06

When you use Spring and Ibatis and SQLTemplates, you could have code in your project which looks somewhat like this:

Connection connection = DataSourceUtils.getConnection(getDataSource());
...<do connection stuff here>...
DataSourceUtils.releaseConnection(connection, getDataSource());

Sonar will report that you did not close the connection, while in fact, Spring did that for you. You can not just add a “connection.close()” to the code because the whole point of calling “releaseConnection()” is to have Spring handle all the smart stuff on committing, closing, and returning the connection to the pool if needed.

Read the rest of this entry »


Eclipse 3.4.1, OSX and Java 1.6

Sunday 2008-12-28

Eclipse application icon

I recently downloaded Eclipse 3.4.1 for Mac OSX, and tried starting it using the eclipse.app icon. It did not work, and I noticed a message in /var/log/system.log telling me that the JVM could not be loaded.

Read the rest of this entry »


Follow

Get every new post delivered to your Inbox.

Join 29 other followers