Archive for the ‘Java’ category

Musings of a SpringOne 2009 Attendee – Day 4

March 10th, 2010

This is the last and final part on my SpringOne 2009 experience. It’s late catching up to the 3 earlier posts but it’s here now. This post summarizes the sessions I attended from day 4 and wraps up with a summary of my take aways. If you want to catch up here are the three earlier posts:

  1. Musings of a SpringOne 2009 Attendee Day 1
  2. Musings of a SpringOne 2009 Attendee Day 2
  3. Musings of a SpringOne 2009 Attendee Day 3

Read on for day 4.

» Read more: Musings of a SpringOne 2009 Attendee – Day 4

Rendering Global t:messages After Redirect

March 8th, 2010

A common problem when working with JSF is getting global info messages  via <t:messages globalOnly="true"> or <f:messages globalOnly="true"> to display messages set in the previous request when you have a <redirect/> in your faces-config for a particular page You will not see your <t:messages> that are set on the previous page.

The Problem

For instance, say you have two pages – page1.xhtml and page2.xhtml. In your faces-config.xml, you will have 2 entries.

<navigation-case>
	<from-outcome>page1</from-outcome>
	<to-view-id>/pages/page1.xhtml</to-view-id>
	<redirect/>
</navigation-case>
 
<navigation-case>
	<from-outcome>page2</from-outcome>
	<to-view-id>/pages/page2.xhtml</to-view-id>
	<redirect/>
</navigation-case>

Let’s say page1Bean is called in page1 and you want it to display messages in page2. So your bean would have something like this:

public class page1Bean {
	...
	public String page1Action{
		...
		/** Add message */
		FacesMessage facesMessage = new FacesMessage();
		...
		FacesContext.getCurrentInstance().addMessage(null, facesMessage);
		...
		return "page2";
	}
	...
}

Your page2.xhtml has like the following somewhere in it.

<t:messages globalOnly="true">

One would expect that this would display the message on the next page, however these messages are request scoped and so are not available in this second request and you do not see your message unless you remove the <redirect/> on page2.

Fortunately, there is a nifty and quick solution. Basically, it is a phase listener that saves the messages from the previous request and then restores them just before the RENDER_RESPONSE phase of the second request.

The Solution

To solve the problem, save the following class into some package where your faces-config will be able to access it

package com.myproject.web.jsf.phaselistener;
 
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
 
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.event.PhaseEvent;
import javax.faces.event.PhaseId;
import javax.faces.event.PhaseListener;
 
/**
 * Enables messages to be rendered on different pages from which they were set.
 * To produce this behaviour, this class acts as a <code>PhaseListener</code>.
 *
 * This is performed by moving the FacesMessage objects:
 *
	<li>After each phase where messages may be added, this moves the messages from
 * the page-scoped FacesContext to the session-scoped session map.
 *</li>
	<li>Before messages are rendered, this moves the messages from the session-scoped
 * session map back to the page-scoped FacesContext.
 *</li>
 * Only messages that are not associated with a particular component are ever
 * moved. These are the only messages that can be rendered on a page that is different
 * from where they originated.
 *
 * To enable this behaviour, add a <code>lifecycle</code> block to your
 * faces-config.xml file. That block should contain a single <code>phase-listener</code>
 * block containing the fully-qualified classname of this file.
 *
 * EDIT: This code was minimally modified by Max Kuipers to address some of the Java 1.6
 * compiler warnings.  All code was originally written by Jesse Wilson.
 *
 * @author <a href="mailto:jesse@odel.on.ca">Jesse Wilson</a>
 * @author <a href="mailto:mkuipers@sourceallies.com">Max Kuipers</a>
 */
 
public class MessageHandler implements PhaseListener {
	private static final long serialVersionUID = 1L;
 
	/**
	 * a name to save messages in the session under
	 */
	private static final String sessionToken = "MULTI_PAGE_MESSAGES_SUPPORT";
 
	/**
	 * Return the identifier of the request processing phase during which this
	 * listener is interested in processing PhaseEvent events.
	 */
	public PhaseId getPhaseId() {
		return PhaseId.ANY_PHASE;
	}
 
	/**
	 * Handle a notification that the processing for a particular phase of the
	 * request processing lifecycle is about to begin.
	 */
	public void beforePhase(PhaseEvent event) {
 
		if(event.getPhaseId() == PhaseId.RENDER_RESPONSE) {
			FacesContext facesContext = event.getFacesContext();
			restoreMessages(facesContext);
		}
	}
 
	/**
	 * Handle a notification that the processing for a particular phase has just
	 * been completed.
	 */
	public void afterPhase(PhaseEvent event) {
 
		if(event.getPhaseId() == PhaseId.APPLY_REQUEST_VALUES ||
				event.getPhaseId() == PhaseId.PROCESS_VALIDATIONS ||
				event.getPhaseId() == PhaseId.INVOKE_APPLICATION) {
 
			FacesContext facesContext = event.getFacesContext();
			saveMessages(facesContext);
		}
 
	}
 
	/**
	 * Remove the messages that are not associated with any particular component
	 * from the faces context and store them to the user's session.
	 *
	 * @return the number of removed messages.
	 */
	private int saveMessages(FacesContext facesContext) {
		// remove messages from the context
		List messages = new ArrayList();
		for(Iterator i = facesContext.getMessages(null); i.hasNext(); ) {
			messages.add(i.next());
			i.remove();
		}
		// store them in the session
		if(messages.size() == 0) {
			return 0;
		}
		Map sessionMap = facesContext.getExternalContext().getSessionMap();
		// if there already are messages
		@SuppressWarnings("unchecked")
		List existingMessages = (List) sessionMap.get(sessionToken);
		if(existingMessages != null) {
			existingMessages.addAll(messages);
		}
		else {
			sessionMap.put(sessionToken, messages); // if these are the first messages
		}
 
		return messages.size();
	}
 
	/**
	 * Remove the messages that are not associated with any particular component
	 * from the user's session and add them to the faces context.
	 *
	 * @return the number of removed messages.
	 */
	private int restoreMessages(FacesContext facesContext) {
		// remove messages from the session
		Map sessionMap = facesContext.getExternalContext().getSessionMap();
		@SuppressWarnings("unchecked")
		List messages = (List)sessionMap.remove(sessionToken);
		// store them in the context
		if(messages == null) {
			return 0;
		}
		int restoredCount = messages.size();
		for(Iterator i = messages.iterator(); i.hasNext(); ) {
			facesContext.addMessage(null, i.next());
		}
 
		return restoredCount;
	}
}

And then add something like the following in your faces-config.xml. Be sure to replace the example package location with your own.

<lifecycle>
	<phase-listener>com.myproject.web.jsf.phaselistener.MessageHandler</phase-listener>
</lifecycle>

The forum post that documented this solution is here.

Developing a multithreaded test harness

March 5th, 2010

You can’t ignore the fact that web servers are multithreaded. We can hide as much as we want, but sooner or later you’ll find yourself in the situation where your application works fine during development and testing; but once it hits production you start hearing about “funny” things happening. While there are plenty of tools that can be used to simulate multiple users, they aren’t always the easiest to run locally and they seem to take to much time to modify while hot on the trail of a multithreading bug. Here I’ll discuss the approach I took when I was recently faced with this situation.
» Read more: Developing a multithreaded test harness

Environment Specific Properties in Spring

March 1st, 2010

On many occasions I want to be able to inject environment specific property values into my Spring managed beans. These may be things like web service endpoints, database URLs, etc. Values I know for each environment at build time, but I want to use the same WAR/EAR file in each environment. I would like to keep the actual values separate from the Spring config files themselves. And I would really like to manage a set of default values for each property, so that I do not need to specify a value for every property in every environment (ex. my credit card processing URL for dev, test, uat is the same, but for production it is different.)

» Read more: Environment Specific Properties in Spring

Replacing and Patching Java Application and Core classes

February 15th, 2010

Why would you ever need that?

Say you get a jar file. After using the jar for a while you realise that there is a bug in a class in the jar file. Unfortunately you also find out that the jar is no longer supported and there is no way you will get a fix from the author (who is long gone fishing).

In order to solve this issue, you first need to get the source of the class. If you are lucky enough and the author did not obfuscate the class file you can decompile it with a decompiler (my favourite one is JD-GUI).

» Read more: Replacing and Patching Java Application and Core classes

Taking Advantage of Spring MVC’s Default Behavior

February 11th, 2010

Over the last several months I have worked on several content heavy websites for one of our clients. When I say “content heavy”, I mean that 80%-90% of the pages in the application are static, or at least mostly static, a customer name, membership number, etc may need to be floated in, but not big data tables with dynamic data being pulled from the database. The marketing department manages this content with their content management system and publish fully formed HTML pages (layout, look and feel, etc is controlled in the CMS) which are then pulled into our /WEB-INF/jsp/content directory by our build process.

Our applications treat these HTML pages as JSPs (simple rename in the build script). This lets the marketing team work from a cheat sheet of EL expressions such as ${customerName} and keeps the IT department out of the day to day content management work. One of our goals with these systems was to easily and seamlessly deal with both static pages and very dynamic pages requiring custom controllers to be built. With just a little bit of work Spring MVC makes it easy to provide this functionality and also provides a sane set of defaults for building out these web sites one page at a time.
» Read more: Taking Advantage of Spring MVC’s Default Behavior

Sonar – Code Quality Analysis Tool

February 9th, 2010

What is Sonar?

Sonar is a web based code quality analysis tool for Maven based Java projects. It covers a wide area of code quality check points which include: Architecture & Design, Complexity, Duplications, Coding Rules, Potential Bugs, Unit Test etc. Sonar has a rich set of features like what you would get with different tools such as Covertura, PMD, FindBugs, Check Styles combined.

http://sonar.codehaus.org/

Setting up Sonar

Hibernate Embeddable Objects

January 18th, 2010

Hibernate Embeddable Objects are a really neat way to organize your data model.  Especially, if you have the same few columns in a number of different tables, that all pertain to the same thing. The example commonly used is Addresses.  You may have a number of tables that each have fields pertaining to address information and you don’t want to have to do all the mappings for each entity again and again.  If the column names are the same across each table, you can just add an @Embeddable annotation.
» Read more: Hibernate Embeddable Objects

Keep your dataTable clean with a custom popup

January 13th, 2010

The basic idea is to output some data to a user in a table and allow them to take an action on each row individually. A fairly straightforward solution is to create a separate page to link to, passing the necessary row information along. If the action is simple enough, like a single checkbox, you could just embed the necessary component(s) in each row of the table. Too many components, however, can bloat the table and make the UI cumbersome to the user. Instead we can create a popup window to overlay our page, containing whatever components are needed, and activate it by a link embedded in our table. Passing the row information is a little trickier, but the result is a cleaner interface and a better user experience.
» Read more: Keep your dataTable clean with a custom popup

Spring’s refreshable beans

January 8th, 2010

A couple of days ago I found out about a really nice feature in Spring, called ‘refreshable bean’.

Spring’s vision a refreshable bean is a dynamic-language-backed bean that monitors changes to its source code and then reloads itself when changes occur. And it is all this is done without restarting/re-deploying entire app. Sweet!
» Read more: Spring’s refreshable beans