Category Archives: J2EE

Java static initializer question

I ran into a problem with a section of code that lives within a Java static initializer; I’m not sure that the problem is the static init. Let me step back and frame the problem first. I have a Struts action class that presents a form for collecting date information; said form includes a drop-down menu that is bound to an ArrayList of LabelValueBean objects that contains the months of the year (Jan|1,Feb|2,Mar|3, etc..). The ArrayList of LabelValueBean objects is retrieved from a data provider class using a static method:
Collection months = MyDateProvider.getMonths();
Here’s the pseudo-code inside the date provider class:
public class MyDateProvider {
  private static Collection months = new ArrayList();
  
  static {
    Calendar c = Calendar.getInstance();
    SimpleDateFormat formatter = new SimpleDateFormat("MMM");
    SimpleDateFormat monthformatter = new SimpleDateFormat("MM");
    for (int i=0; i
The problem is that somehow the ArrayList of LabelValueBean objects contained two instances of March and no February, so the dropdown list would look like this:
January
March
March
April
May
June
July
August
September
October
November
December

Restarting Tomcat temporarily fixed the problem (obviously because MyDateProvider was then reloaded), but my question is how did is it possible for the ArrayList to be missing February and contain two instances of March? Any takers?

Update: the problem has nothing to do with the static init and everything to do with the way I used the Calendar class. The Calendar.getInstance() method returns a Calendar object initialized with the current date and time, so when the web application was restarted on 11/30/2004, the Calendar object reflected that date/time. Looping from 0 to 11 and setting the month (c.set(java.util.Calendar.MONTH, i);) resulted in the month changing correctly EXCEPT during the month of February , which has only 29 days. The date "02/30/2004" is illegal and so the Calendar class helpfully bumps the month up, which resulted in two instances of March in my drop down. Thanks to Paul Hastings for pointing this out.

Commons Net FTP listFiles() returns null

Just bringing this to the top of the Google QueueTM, if you’re using the listFiles() method of the org.apache.commons.net.ftp.FTPFile class and you’re seeing that listFiles() returns null when it shouldn’t be, make sure to upgrade to the latest and greatest version of commons-net (which is 1.2.2 right now). Using version 1.0.0 was causing NPE’s all over the place. I think this bug on issues.apache.org discusses the issue, but it’s more specific than what I was seeing.

Automating Application Deployment with Tomcat

I’m asking a bunch of questions here so if you’re expecting answers, look elsewhere. In the past couple years, I’ve waded through a bunch of different ways to configure and build Java web applications. At first, I hardcoded connection strings, settings and message strings in the source code, compiled using Eclipse and copied over the JSP and class files to the web server (lame, I know, but you gotta start somewhere). As the applications I wrote got more complex and as I got smarter, I started using Ant to perform the build and I learned about properties files and web.xml. After unpacking alot of other open source Java applications, I’m now using log4j for logging and error messaging, JNDI for configuration (datasource and application configuration stored in Tomcat’s server.xml), resource bundles for storing internationalized message strings, JUnit for running tests and Ant for cleaning, testing and building my apps into a deployable format (war files).

And that’s where it stops being easy. Deployments suck. We have a pretty small environment (a couple test servers, a couple staging servers, a couple live servers) and deploying changes to those servers is tedious. For example, in my development environment, I like to have log4J append any and all information to the console, which lets me watch the system as it starts up and runs:

log4j.category.com.mycompany=INFO, STDOUT

But once I build the application and deploy it to the live environment, I only want error messages and I want them sent to the SMTPAPPENDER:

log4j.category.com.mycompany=ERROR, SMTPAPPENDER

so I’m stuck editing a text file every time we deploy an application. It’s not that big a of a deal, but I also have applications on each server that need to have the appropriate entries in Tomcat’s server.xml (environment entries, JDBC connections, etc..), sometimes Tomcat needs to be restarted after your deploy the war, applications are deployed to different directories on different machines, sometimes the application being rolled out doesn’t work and you need to roll back to the previous version, how do you keep track of all the live / staging / development servers? The environment I work in is pretty small, so all of this can be done by hand, but it’s tedious, boring and error prone. How do you guys that work in larger environments do it? How do you move the .war files from your staging environment to the live environment? Using Ant? Do you trigger Ant tasks on the live servers that check out source code from CVS and build the apps there? Do you restart Tomcat every time? Do you do one half of your machines at a time and then the next half? You can’t be doing this by hand! Any tips?

Scotty Cameron Studio Store: Struts, Hibernate, Quartz, Ant, Cewolf, etc..

On Monday we launched the online store I was working on this summer for ScottyCameron.com. We brought the ecommerce capabilities in-house (it was on Yahoo! Store) so that we could leverage some of the existing functionality I built for address verification, credit card fraud protection and taxation. I was able to use alot of Java related technology including Hibernate (for object persistence), Struts, Quartz (embeddable job scheduling), Ant, Cewolf (for graphing and charting), various commons projects (beanutils, httpclient, logging, etc..), Fedex API, dom4j (XML parsing all deployed successfully on Tomcat 5.0.25 across a couple webservers.

SQL Server Type 4 Drivers: jTDS

A couple months ago I wrote about problems I was having with Tomcat and SQL Server where I would get Connection reset by peer errors if the SQL Server was restarted. A couple people commented that instead of using the Microsoft provided JDBC SQL Server drivers, they had switched the open source jTDS driver available on sourceforge. I ignored them and went on my merry way, until yesterday when I ran into this really strange problem. I had a query written using Hibernate/HQL that would take approximately 10 seconds to run but the same query run in Query Analyzer would take less than 1 second. My code looked like this:

Query q = null;
Session session = HibernateFactory.currentSession();
String query = "select this and that from sometable";
q = session.createQuery(query);
List list = q.list();
for (int i = 0; i
Pretty simple, nothing out of the ordinary, it just took 10 seconds to go from the q.list() to the for {} block. I tried a bunch of different things: I modified the query, I tried using an Iterator rather than a List, I added named binding to the query. Nothing worked. Today I switched to the jTDS driver. Problem solved. I'll never use the Microsoft JDBC drivers again.

Java / Web Developer Job Opening: Southern Massachusetts

Don’t bother applying unless you live in or around southern Massachusetts or are willing to relocate to southern Massachusetts. I work for the largest golf company in the world and I’m looking for a partner in crime. Strong Java, Struts and Hibernate experience will get you in the door (proving that you know Java inside and out will work too); loving your work will get you a job. Email me your resume if you’re interested.

Conflicting mindsets of C# vs. Java: Part II

You all read the the ‘Conflicting mindsets of C# vs. Java‘ weblog post right? And you all noticed that the guys running the Lucene.NET project on sourceforge closed up shop, took all their toys and went on home right? I’m gonna go out on a limb and say that they’re related.

The way I see it, *in general* the .NET community conversation is dominated by talk about the latest and greatest that microsoft is putting out; there’s talk about MapPoint Location Server, SQL Server, Longhorn, ASP.NET 2.0 and Visual Studio; all products of Redmond. The same group of Java developers are talking about JBoss, Hibernate, Struts and Eclipse: none of which came out of the Silicon Valley.

Malcolm’s mindset #1 says that .NET developers accept the tools and services that are provided them by Microsoft and I think for the most part this is true. You don’t see .NET developers spending their cycles on persistence layers, web application frameworks or caching solutions probably because Microsoft has provided Microsoft solutions for all these problems. But if it’s just providing tools, then why aren’t JSF, JDO and NetBeans dominating the javablogs conversations? Seriously, take a look at ASP.NET and JSF. They aren’t that different and yet ASP.NET is widely used in conjunction with Visual Studio while JSF is rarely lauded and more often derided. I think he’s right, it’s really a mindset.

Which brings me back to the Lucene.NET guys. Why would they close up shop? Why not continue to donate their time and energy to an excellent cause? Maybe the Microsoft mindset has something to do with it. How about this: a search on google for ‘lucene’ within the weblogs.asp.net domain yields exactly 17 results. The same search on jroller.com yields 2570 results. Admittedly, Lucene has been around longer, but maybe one of the reasons that the Lucene.NET guys packed it up (and are now trying to sell their work) is that no one paid any attention to them because they were all too busy working with SQL Server full-text indexing, a tool given them by Microsoft (but one that costs thousands of dollars per processor). Another reason that a project like Lucene or Struts or Tomcat flourishes is because there is a certain amount of prestige working on a big open source project. If you work on open source projects for the prestige and you’re not getting the attention you think you deserve, you find another motivation. In their case money was a motivation, so they closed up project on sourceforge and they’re selling a personal edition and a business edition. They might make a couple bucks, but I bet in 1 year there won’t be many people writing about searchblackbox.com.

So what’s my point? That all .NET developers are greedy and don’t care about the community? Not really. I think it’s that the two communities have different bus drivers: .NET developers look to Microsoft to provide the tools they need to do their jobs… and if they look elsewhere or copy something else, Microsoft will eventually come in and make a product of their own that does the job, thereby negating any work the developers do in the meantime. Microsoft drives the bus. Java developers look at the products and specs that Sun puts out and then go and build their own tools or frameworks or applications to do the job. Sun will eventually put out something through the JCP that does the job…. but the developers in the Java community will only use it if they want too, witness the continued popularity of Struts and the lack of interest in JSF. In the Java camp, the developers drive the bus.

daily links

· The guys at Search Engine Watch have a blog now…

· Python date handling tutorial

· Java Open Single Sign-On Project: an open source J2EE-based SSO infrastructure aimed to provide a solution for centralized platform neutral user authentication.

· Better Software magazine: delivers relevant, timely information to tackle the challenges of building better quality software, regardless of your role in the software development lifecycle.

· Better Software magazine on Continuous Integration

· CruiseControl: I need to start using this…

· No balls. No babies.