name....Rafal Zaczynski
age.0808129501 seconds
longevity.2239056000 seconds
height....180 centimeters
weight.......80 kilograms
nationality.............polish

« .. Back home .. «

Java programming Tips - by Baton

  1. 21-04-2010
    EJB3 timers retaining their id after restart (JBoss)
  2. 10-10-2008
    IE JavaScript optimization - lots of dynamic elements
  3. 13-03-2008
    Centered DIV with position absolute
  4. 04-01-2008
    Embedded images in JavaMail emails
  5. 02-01-2008
    "..violation of SRV.8.2 and SRV.14.2.5.1" ServletException in JBoss/Tomcat
  6. 09-11-2006
    JSF page reload/refresh button (handling discarding of components submitted values)
  7. 28-09-2006
    Bound JSF component lifecycle
  8. 21-09-2006
    XML document access using XPath expression language in Java
  9. 04-09-2006
    Mapping *.jsf extension to Faces(JSF) requests

Working with EJB timers lets programmers develop some realy nice and clean solutions for problems. Unfortunatelly current implementation of these (timers) is very lame in JBoss. The main problem that pops up is the persistence of timer handles that are not being retained between server restarts (and of course crashes as well). Not sure if that shall not be a must for the J2EE compatibility/compliance (whatever version on whatever JBoss you pick) tests to be passed by AS implementation..

..anyway, the timer handle javax.ejb.TimerHandle obtained from any javax.ejb.Timer instance - when stored to any persistence storage (e.g. DB using entity bean attribute) - and read later shall lead back to the same javax.ejb.Timer instance that it was created by (so it could be later in time e.g. cancelled or updated). It does not. In more detail - all works fine until the AS is restarted. In that situation all the timers that were existing in AS during shutdown (of course assuming the persistence policy is used allowing the timers to be stored in a non volatile way - e.g. persistencePolicy=database) are being recreated during the AS startup and the recreation is done in a simplest possible way (explain me why is that so?) - deleting all of them from DB and reinserting them again. The small tweak here is that the identifiers of the timers being recreated are being updated (changed). Thanks to the change of the id's of the recreated timers they are not properly found/bound back from timer handles later on read from persistent storage. So we have a proper handle (which would work without server restart) but any call to getTimer() will lead to rising of the javax.ejb.NoSuchObjectLocalException.

All these problems are already noticed (in mid 2007 for JBoss 4.0 and late 2009 for JBoss 5.1) and noted as JBAS JIRA entries:

 • 
TimerHandle failed to retrieve timer after JBoss restart (entry for JBossAS-4.0.5.GA)
 • javax.ejb.NoSuchObjectLocalException: Timer not available - after restart (entry for JBossAS-5.1.0.GA)

These entries are existing a year already and are not fixed yet - and nobody is having a big problem with that I suppose. This could only mean that there is little code developed on JBoss using that pretty basic feature (why is that?).

On the other hand it seems the problem already was propagated to the JBoss Enterprise Application Platform where it's fixed already and waiting to be released in June 2010 (bravo!). Hopefully there fixes (as all the JIRA entries are marked as major bugs) will finally find their way to JBoss AS release.

Here are the JBPAPP JIRA entries I'm taking about. Please take a look there as there is explanation of the solution as well (different implementation of TimerIdGenerator - using UUID instead of sequence of big integers and of course probable reimplementation of EJBTimerServiceImpl.restoreTimers method).

 • EJBTimers do not retain their timer ID's over server restarts (entry for 4.2.0.GA_CP08, 4.3.0.GA_CP07)
 • EJBTimers do not retain their timer ID's over server restarts (entry for EAP 5.0.0, EAP 5.0.1)

Lately I stumbled upon a small efficiency problem in JavaScript run on IE browser. The problem appeared in a situation when a lot of dynamic DOM elements were created and removed from document. I was using MooTools JavaScript framework (v1.1) for all the stuff on the page so as well for all the DOM elements handling.

As the functionality of the site required that hundreds (in some extreme situations event 3k+) of such elements are created and removed the problem appeared immediately in IE. It appeared that creation is not very problematic but removal of huge number of elements is killing the browser. The empty() (from MooTools) method on container Element was called to remove all its children (1k+ in number). For IE this method was just taking ages to finish and in the meantime IE popped up the message box "Stop running this script? .. may become unresponsive.". Of course such popup for end user is a no-go!

As a solution such small patch was done to make it work smoothly: (JavaScript code)


	..
	var containerE = $( 'containerId' );
	if ( window.ie ) {                    // mootools added property to check if IE is running the script
		containerE.innerHTML = '';    // this works smooth in IE
	} else {
		containerE.empty();           // this one kills IE performance ( if any :) )
	}
	..
		

Sometimes its quite handy to center align an HTML DIV element which is positioned absolute.

Easy way of doing this is to use positioning with combination of pixel and percentage. One important thing in this case is that the DIV needs to be fixed in width - as the solution is based on that. The solution is to use left: 50% as one of positioning attributes. This one will cause the layer to be placed in the center of window. In fact, a left side of layer is placed exactly in the center of browser window. Now we only need to move the window left by half of its width. We do it using negative value for margin-left attribute. The value for it shall be half of the DIV overall width. Voila - there it is.


	/** css part */
	div#popup {
		position: absolute;
		top: 200px;
		left: 50%;
		width: 400px;
		margin-left: -200px;    /* negative half of the width value */
	}
		

How to embed images inside the email and use these images for layouting of this email contents?

First we have to have an email with a body part of type text/html where images can be used. Whenever we try to link any of such images to an external internet address it will be blocked by email client before displaying. What we need to do to overcome such problems is that we need to attach the images we want to use for layouting into the email.

Attaching of images can be done using JavaMail api methods like MimeBodyPart.attachFile( File ) but if you have the images deployed in your service EAR file it can be done as following:


	// create proper mesage container
	MimeMultipart ourMessage = new MimeMultipart( "related" );
	
	MimeBodyPart attachmentPart = new MimeBodyPart();
	URL urlToImg = class.getResource( "/com/package/../image_file.jpg" );
	attachmentPart.setDataHandler( new DataHandler( urlToImg ) );
	
	// set the ID of this attachment - so it can be referrenced from HTML
	attachmentPart.setContentID( "<image>" );
	
	// and add the part to message
	ourMessage.addBodyPart( attachmentPart );
		

Having such attachment we have to reference it somehow from the HTML code. We now that the attachment ID is image as we specified it using setContentID method. To reference that attachment from the HTML contents we have to use cid: prefix which email clients resolve to content identifier, so eg. referencing our image would mean using the following HTML code:


	<IMG SRC="cid:image"/>
		

Additionally all the body parts of the email need to be a part of a message of type multipart/related which can be seen as first line of the example code.

There is a great need of creating a button/action that leads to the same page it is displayed on, but needs to do some server side logic. To achieve this in without creation of additional XML navigation entry we can simply assign no action to a link component.

So the code like:


	<h:commandButton value="Refresh" actionListener="#{doThelogic}" immediate="true"/>
		
will do just fine - it will reload current page doing the business logic inside action listener.

The same can be done using an action attribute binding to a method which returns null.

Problem here is that the JSF view is not recunstructed but the previous one is used. This will lead to the situation when all the values entered by used to any input components will still be there in the page. To solve this problem and force discarding of all input components submitted values we can do the following..

Let's imagine the situation where we want to create a DB state refresh button. It shall load the state from the DB/model and present it on a page. Unfortunatelly using such refresh strategy I presented we will still have the problem of input components submitted values returning to a browser after page reload. If we need to discard the submitted values of all input components we will have to define the action attribute in a link component and additionally make a dummy refresh navigation entry in faces-config.xml file.

So the link component, which needs to discard all submitted values of input components will be defined like (e.g. the JSP file is /editMe.jsp):


	<h:commandButton value="Refresh" actionListener="#{doThelogic}" action="refresh" immediate="true"/>
		
and with navigation rule:

	<navigation-rule>
		<from-view-id>/editMe.jsp</from-view-id>
		<navigation-case>
			<from-outcome>refresh</from-outcome>
			<to-view-id>/editMe.jsp</to-view-id>
		</navigation-case>
	</navigation-rule>
		
the JSF engine will be forced to create a new view with id /editMe.jsp which will cause recreation of all input components with submitted values set to initial, null value.

The problem is how to access XML file easily, using XPath expression language.

There exists an open source library implementing XPath expression language interpreter. The library is called JXPath and is one of Jakarta commons libraries.

Let's dig into the stuff now. The code for accessing an XML document is easy and short:


  	URL xmlURL = getClass().getResource( xmlFile );
	org.apache.commons.jxpath.xml.DocumentContainer xmlContainer = 
		new org.apache.commons.jxpath.xml.DocumentContainer( xmlURL );
	org.apache.commons.jxpath.JXPathContext context = 
		org.apache.commons.jxpath.JXPathContext.newContext( xmlContainer );
	
	..
	
	java.util.Iterator vals = context.iterate( xpath );
		
First we create the URL to access XML file, then a proper Container is created, which is later on used as a root for XPath context.
Having that context we can now easily iterate over values using XPath expressions.

The API documentation of the Jakarta JXPath library can be found here.

The idea is to specify "<url-pattern>" as "*.jsf" and map it to Faces Servlet using "<servlet-mapping>".

In default implementation of Application and ViewHandler (JspViewHandler) the extension will be replaced with the extension defined as "javax.faces.DEFAULT_SUFFIX". The default value is set to ".jsp" so whenever the "xx.jsf" request comes it will be processed as "xx.jsp" underneath but the browser URL will stay "xx.jsf".

javax.servlet.ServletException: Original SevletResponse or wrapped original ServletResponse
	not passed to RequestDispatcher in violation of SRV.8.2 and SRV.14.2.5.1

This is because of more strict Servlet 2.5 specifications on JBoss (which is default for Tomcat 6+). This can be thrown on JBoss 4.2.0+ GA and can be fixed by adding the following directive to the server startup parameters:

-Dorg.apache.catalina.STRICT_SERVLET_COMPLIANCE=false