Archive for the ‘Java’ Category

Implement “Search results” using Hibernate

September 29, 2006

Hibernate Criteria API: Multi-Criteria Search Made Easy

Powerful, Elegant and Definitely Worth a Try,

that’s what the article says & it’s mighty true. Have seen quiet some people constructing these kind of queries using StringBuffer & stuff, but this is waaaay more elegant :)

..
Criteria criteria
= session.createCriteria(Accommodation.class);
if (startDate != null) {
criteria.add(Expression.ge("availabilityDate",
startDate);
}
if (endDate != null) {
criteria.add(Expression.le("availabilityDate",
endDate);
}

This beats

if (startDate != null) {
queryBuf.append(firstClause ? " where ": " and ");
queryBuf.append("a.availabiliyDate >=:startDate");
parameters.put("startDate",startDate);
firstClause = false;
}

any time !!

Must never do Antipatterns – Exception Handling

September 28, 2006

From java.net: Exception-Handling Antipatterns

Antipatterns
Log and Throw

Example:

catch (NoSuchMethodException e) {
LOG.error(“Blah”, e);
throw e;
}

or

catch (NoSuchMethodException e) {
LOG.error(“Blah”, e);
throw new MyServiceException(“Blah”, e);
}

or

catch (NoSuchMethodException e) {
e.printStackTrace();
throw new MyServiceException(“Blah”, e);
}

All of the above examples are equally wrong. This is one of the most annoying error-handling antipatterns. Either log the exception, or throw it, but never do both. Logging and throwing results in multiple log messages for a single problem in the code, and makes life hell for the support engineer who is trying to dig through the logs.

I see this in so many “commercial” products that I want my money back !!; makes debugging a pain the neck. Seeing the same trace logged from every tier again & again is not pretty.

Hidden variables in DisplayTag

September 27, 2006

If you are using DisplayTag and need to have hidden variables like I do (I’m using Struts) then this is a nice way

…setting the ’s class and headerClass attributes to hidden, which is a CSS style whose display property is none. This is a simple but effective method for keeping data available in the request scope without having to display it to the user.

Code snip :

<display:column property=”id” title=”ID” class=”hidden” headerClass=”hidden” media=”html” />

Source : Creating Highly Functional Tables in JSP Using DisplayTag and JavaScript

EclipseUML

November 29, 2005

Lately I’ve been looking at UML modelling tools. Tried quite a few of them (freeware, shareware etc.). Even downloaded the new freely available Java Studio Enterprise, but didn’t like the fact that I had to leave Eclipse ( . Now I don’t have to ) thanks to EclipseUML from Omondo.

This is what plugin architecture is all about !! I have used the Free Edition so far and it’s amazing. No major hiccups, would have liked to install through an update site, but their installer was OK too.I liked the fact that they have maintained the overall look and feel and icon set remains faithful to Eclipse. Auto-layout is not very smart though and the export is to SVG only (the commercial edition add more export formats). I like the fact that I can version-control this along with my source code so that I can keep them updated so that there is no “lag” between design and development. I am going to go all crazy with use cases and sequence diagrams too )

If all goes well I want to suggest that we use this plugin in all our teams and maybe I can push for the commercial version too.

PS : I am using w.bloggar to write this post and the spell check suggests “unholy” as a correction for UML ;) appropriate you say !!!

TimeZone on Red Hat Linux

October 20, 2005

On my development box I was facing a weird problem of java (1.4.2_04) Timezone not being what it should be. The good part is that this is not happening on the client box. So all this was ok as the only place it was getting used was in the log4j logs I was just offsetting the time to my time in my head ;) (Too lazy I know). But recently we were adding some code that was dependent on the timezone in that the time was the content of a mail sent to users and not everybody was prepared for my “mental offset” solution.

Googling for an answer I found this link http://minaret.biz/tips/timezone.html which describes the exact problem I had and a solution that works for me (export TZ=). I basically stuck this export TZ="whatever" to my bash profile.

Upgrading the java version to 1.5.0_02 doesn’t make a difference either and the Linux time zone is also set perfectly and the date command works as it should.

One thing I would like to add is that use the complete name “America/Los_Angeles” and not an ID like PST or JST. Setting the ID works for Java but the date command doesn’t get it, so if you have native apps then they would start acting up because of this.

Oh yeah, if I ever find out what the problem is to begin with, it will be posted here too :) for now this fix will have to make do.

Update: Check your /etc/localtime ;)

Swing based Pet Store

October 5, 2005

Is there a Swing based Rich client implementation of the Petstore application or something similar? I wanted to see the J2EE design patterns and blueprints put to practice in a Swing based rich client. The blue prints at http://java.sun.com/reference/blueprints/ and http://corej2eepatterns.com/ primarily focus on Web based applications but other than Presentation Tier everything should remain the same for a Swing based one. Macromedia has done something similar http://www.macromedia.com/devnet/blueprint/, pretty neat. Guess I could get cracking on this one.

Eclipse RCP … really cool platform

July 26, 2005

The Eclipse RCP does really look like the “Really cool Platform” that it’s been touted to be. I have been looking it into it on and off (read: every time somebody blogs about it or there is a eclipse release). I really wish that they had something like this for the average Swing Joe (like me).

Just thought of building a Outlook Express like application for reading the bloglines just as a quick hack project. But boy does it take a while to get off the ground. At work we already have a kind of framework that takes care of all the menu, status bar etc. well kind of. But if I start off from scratch (using all of JDNC and jgoodies) it would take me a couple of days just to get a decent skeleton in and then finally to the nitty-gritty. As opposed the RCP seems to provide all this out of the box and with all the run in background coolness (think it’s the Job API). All this and plus some more.This really should have been done swing already, considering that it’s been around like forever, maybe everybody has developed something proprietary in-house.

I hear that Java is making a come back in the desktop market (well we’ve been in here for quite some time), but without such a framework I am not sure it will sustain the interest. The whole point is to be quick off the ground, lots of apps => lots of source code etc. ! I think where eclipse did right was come out with quick results to sustain the interest that it had generated and has kept it going. I don’t say there are no efforts here JDNC is doing quite a lot of work and they have a Action framework that seems to have the workings that can be used in a contributable framework like in eclipse/RCP but leaving the actual work for somebody else or as a TBD might not workout.

Now being a Swing’er for life I am actually considering looking into the whole deal. Not sure how long and how much effort will be needed and more than that how much of a mind-set change will this need (I have my share of FUD being from the other “side”), but about a year ago I had persuaded my organization to switch to Eclipse as the standard IDE (after using it for 2 years myself) as the standard IDE from textpad’s, notepad’s and some other IDE’s in the market and it was such a huge success.

Now I am out looking for material, I see a lot of tutorials(plug-ins/RCP/SWT/JFace), videos but a lot of it is scattered around, now wishing for some more would be plain evil now would it ?

Eclipse on JRockit

June 21, 2005

Eclipse was getting real sluggish on my Thinkpad so I started looking around for ways to get it going faster. I tried the Keep Resident plugin but not much difference. Then I read someplace that the JRockit JVM is supposed to be faster than the Sun one, so I tried the 1.4 and 1.5 versions of it, hoping to be "rocked" by it's performance but actually came out disappointed. The Sun VM seems to be comparatively really good with the default options. Is JRockit not the best for client side / GUI applications ? Does it need some tweaking and tuning ? For now I do not have the time to look it up, maybe I'll come back to the Rockit some other day.

As UGLI as it gets ??

April 6, 2005

With so many libraries and applications depending on JCL for logging can this actually let you sleep at night ? (how can you !!!)

That's when I found UGLI

The Universal and Generic Logging Interface or UGLI is intended to serve as a simple abstraction of various logging APIs allowing to plug in the desired implementation at deployment time.

I was actually trying to use JDK 1.4 logging for one of my web apps. The most important in this was that I had written a custom formatter but turns out I hit this bug :( .Oh well who needs this !!

Let me use my google chakra to find what can go wrong with UGLI before I actually use it. I mean with a name like that something's got to be worng :D

Logged out !

August 19, 2004

JDK 1.4 introduced logging with 1.4 of the JDK. I for my part was pathetically mired in System.out's and System.err's (hey I am still on JServ !!! yeah ol timer ;) ).

I am a eclipse user and now a "eclipse evangelist" at my company :D . So for you Eclipse users stuck on err's or out's there is a way out. There is this cool eclipse plugin log4E that takes me to the promised land !

So now that I have no excuse I have started using the 1.4 logging. We had this homegrown logging system to use in our runtime application that was really slow, so I thought let's use 1.4 logging ! Hurray I am a genius. Now that I want to do that I realize why we wrote our own logging system in the first place (duh!). The existing system has a asynchronous logging system. What this means is that the application sends the log message and continues with normal execution, now the logging system will do the expensive operation of actually writing the message to file/database etc. Now that is clever isn't it !! So folks at Sun should be atleast as smart .. right ? Noooo :( , such a thing is non-existent in the JDK! But now that I was sold on the architecture I decided to implement a Handler that does the same for 1.4 logging. So a google on the topic gave me a link to AsyncAppender in the Log4j project, now that got me "inspired" and I hacked it into a Handler. It also needed 1 other significant change too. The Appender uses a Bounded FIFO whereas I could not live with the Bound constraint. So I had to put together a Queue (I used a linked list). So now I have a decent port of the Appender to Handler :) . Now all that is left is integration and testing (why O why!). Will it make the cut ??