Wednesday, August 25, 2010

System.currentTimeMillis();

I ran across an interesting issue today involving JVM internals, and how they behave differently based on the host OS.

The issue was with the following code snippet:

MyEntity a = new MyEntity();
a.setUid("foo" + System.currentTimeMillis());
a.setName("my entity");
entityService.save(a);

This was part of some object creation code I was using in a unit test, which ran successfully every time on my macbook but was failing on other's windows machines. When I found out the reason behind this, I was definitely surprised.

Believe it or not, it is more or less a known issue that the resolution of currentTimeMillis() on windows machines hovers around 10-15ms (linux and mac have a resolution of around 1ms). This was causing duplicate foreign key violations to occur.

So, as a general rule of thumb, it is a bad idea to rely on currentTimeMillis() to have a millisecond-resolution on windows, especially if you are using it to generate unique things like file names or db IDs. Looks like System.nanoTime() might tick faster.

Here are some links I found on StackOverflow about this:

On Windows, you'll more or less be limited to 10 ms resolution at best. Here's a bit from the Inside Windows NT High Resolution Timers article from TechNet:

Windows NT bases all of its timer support off of one system clock interrupt, which by default runs at a 10 millisecond granularity. This is therefore the resolution of standard Windows timers.

In my experience, using System.currentTimeMillis method gives about 15-16 ms resolution on Windows. Getting better time than the operating system timer will probably require more exotic methods.

Here's the StackOverflow question which helped me solve this issue:
http://stackoverflow.com/questions/351565/system-currenttimemillis-vs-system-nanotime

At the end of the day, I decided it was simpler for my test to use a static synchronized counter variable rather than depending upon currentTimeMillis() for generating my UIDs. Another possible solution could be to use UUID.randomUUID().toString().

Wednesday, August 11, 2010

Fixing ClassNotFoundExceptions when working with Eclipse WTP, Maven, Tomcat

Lately, I've been struggling off and on with ClassNotFoundExceptions when starting tomcat from within Eclipse (Springsource Tool Suite, to be exact). My projects are all dependency-managed by the Maven m2eclipse plugin (side note: its really important to install the m2eclipse WTP add-on, found here: http://m2eclipse.sonatype.org/sites/m2e-extras so that the web dependencies are all configured correctly).

Anyways, I would tend to encounter this error after restarting Eclipse. When I would investigate, by looking into the [tomcat_home]/wtpwebapps/[app]/WEB-INF/lib folder, it would of course be empty (hence, class not found). I would always flail about aimlessly for hours until it would magically start working again.

I have in the past week discovered a new trick, which has worked *both* times I've tried it: when I got the class not found error, I stopped tomcat, right clicked on my project in the Package Explorer, and clicked Maven -> Update Dependencies. For whatever reason, this caused WTP to publish the dependent jars into WEB-INF/lib, and everything worked again. :-)

Hope that helps save some debugging hours for some!

Thursday, June 3, 2010

DOM Events for HTML Radio Buttons

An interesting bug came up today, and it had to do with the way IE 8 processes DOM events differently than the other browsers.

Here's the scenario:

We were attaching to a radio button's onchange event to do some processing. When you click on an option, we show you a particular div relevant to your selected option. This worked just fine on Firefox and Chrome, but was wacky on IE8. It turns out that IE8 doesn't seem to fire the onchange DOM event until after the element *loses focus*, meaning that our divs didn't show up until we clicked somewhere else on the screen.

We fixed the issue by switching to the onclick event, which fires nicely, on the click, as you would expect, on all three browsers.

In summary: when you want to trigger something when a user selects one of your radio buttons, attach to the onclick event, and not the onchange event, which has variable behavior across browsers.

Friday, February 19, 2010

Unsigned Java Primitives

This one's just a quick note about unsigned primitives and how to work with them in Java, which technically has no notion of 'unsigned'.

If you need to find the unsigned value of a primitive (e.g. you are porting code in another language, or dealing with byte-level networking), you basically need to promote it to the next higher data type, and mask out the bits you don't need (anything above the size of your unsigned data type).

Examples...

To find the unsigned value of a byte (8 bits):


byte b = ...;
short unsignedB = (0x00FF & ((short)b));


And for a short (16 bits):


short s = ...;
int unsignedShort = (0x0000FFFF & ((int)s));


And so on.

You can read more on this and other exciting topics such as *-endian byte ordering, and why Java doesn't have unsigned in the first place here: http://www.darksleep.com/player/JavaAndUnsignedTypes.html

(hint: Gosling didn't think Java developers could understand them!)

Monday, November 16, 2009

ISO Language Codes

Every so often, I need to obtain a full list of ISO language codes for use in an application. I thought I'd write a reference post for future reference, and whomever else might find this kind of thing useful.

Languages are given a two-character code in ISO-639-2, which the Library of Congress is nice enough to host for us here.

I wrote a simple bit of code to generate a 'Language' table for MySQL, and posted it here:
http://www.fiestacabin.com/files/iso-lang-codes.sql.txt

Feel free to use this snippet; the table I created looks like:

CREATE TABLE IF NOT EXISTS Language (
id unsigned int not null auto_increment,
code char(2) not null,
description varchar(64),
primary key (id),
unique index iLanguage_code (code)
) ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;


Just for fun, I am including my Java snippet which generated the SQL; critique at will!

package sandbox;

import java.io.File;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.text.StrTokenizer;

public class ISOLangCodes {
public static void main(String[] args) throws Exception {
List<String> lines = (List<String>) FileUtils.readLines(new File("c:/temp/iso-lang-codes.txt"), "UTF-8");

for( String line : lines ){
StrTokenizer s = StrTokenizer.getCSVInstance(line).setDelimiterChar('|');
String langCode = s.getTokenArray()[2];
String langDesc = s.getTokenArray()[3];

if( StringUtils.isNotBlank(langCode) )
System.out.println(
"INSERT INTO Languages (code, description) VALUES ('" + langCode +
"', '" + StringEscapeUtils.escapeSql(langDesc) + "');");
}
}
}

Wednesday, October 14, 2009

Recursively Scanning Packages for Marked Classes with Spring

I thought I'd share a neat snippet of code which allows you to recursively scan all of the classes in a given package for those which have been marked with a particular annotation. This could be useful, for example, if you have a static factory class that wanted to know all of the classes which could be instantiated for a particular purpose, without having to add them in some sort of static block at the top of the factory.

String pkg = MyBaseClass.class.getPackage().getName();
String pkgPath = pkg.replace('.', '/');

List<Class<?>> classes = new ArrayList<Class<?>>();

PathMatchingResourcePatternResolver resourceResolver = new PathMatchingResourcePatternResolver();
Resource[] packageClasses = resourceResolver.getResources("classpath*:" + pkgPath + "/**/*.class");

for( Resource res : packageClasses ){
classes.add(Class.forName(pkg + '.' + FilenameUtils.getBaseName(res.getFilename())));
}

for( Class<?> c : classes ){
MyAnnotation myAnnotation = c.getAnnotation(MyAnnotation.class);

if( myAnnotation != null ){
// this class was marked with @MyAnnotation; handle accordingly
}
}



I could devote a whole post to Spring's resource loading capabilities; they are the nicest I've used. The above snippet utilizes Spring's PathMatchingResourcePatternResolver, which is able to list resources which match a given ant-style path description. The secret is "classpath*:some/path"; note the asterisk before the colon. This searches *all* accessible jars on the classpath, instead of stopping in the first one in which we find a match. One gotcha here is that you can't do something like "classpath*:*/service/*Service.class"; it will choke if you attempt a leading wildcard search.

Sorting internationalized lists in JSP

One topic which seems to keep coming up for me lately is sorting values for a web application which have been extracted into a resource bundle, for internationalization. Everything I've read on the web says the preferred place to do the sorting is in the servlet / controller class, with the view (in this article, JSP) merely responsible for iterating over that sorted list and displaying it in its native order.

This is all well and good for basic, non-internationalized applications, but seems to break down when dealing with resource bundles and keys. The problem, in my opinion, is that the controller should not have to worry about extracting messages from the resource bundle in order to properly sort, especially when most of the other logic for displaying resource bundle messages is already at the JSP level:

<fmt:message key="${my.label.key}">

Since the view already has a dependency on the resource bundle resolver, resolving those messages in the controller for the purpose of sorting feels wrong. The code I usually see shoe-horned into controllers is ugly, and in the worst cases, I've seen it even thrown in the service layer (*gasp*). It requires knowledge of the current locale, language, bundle, etc.

In an effort to maintain the purity of controllers everywhere, I propose some kind of "sort by resolved message" functionality in the view layer, and will present an example implementation below, as an adaptation of the JSTL c:forEach tag construct.

package com.abstractbits.web.taglib;

import java.util.Comparator;
import java.util.SortedMap;
import java.util.TreeMap;

import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.jstl.fmt.LocaleSupport;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.taglibs.standard.tag.rt.core.ForEachTag;

/**
* Implementation of a loop tag construct which iterates over a given collection
* of items which have been sorted by the resolved message from a resource bundle
* for a given bundle key.
*
* http://abstractbits.blogspot.com
*
* @author Jared Stehler
*/
public class SortedForEachTag extends ForEachTag {

private enum SortOrder {asc, desc};

private String bundleKeySortProperty;

private SortOrder order = SortOrder.asc;

/**
* Property path notation to get at the bundle key in each object in the given collection
*
* @param bundleKeyProperty
*/
public void setBundleKeySortProperty(String bundleKeyProperty) {
this.bundleKeySortProperty = bundleKeyProperty;
}

/**
* The order in which to sort the bundle messages. {"asc", "desc"}
*
* @param sortOrder
*/
public void setSortOrder(String sortOrder) {
this.order = SortOrder.valueOf( sortOrder.toLowerCase() );
}

/* (non-Javadoc)
* @see javax.servlet.jsp.jstl.core.LoopTagSupport#prepare()
*/
@Override
protected void prepare() throws JspTagException {
super.prepare();

try {
SortedMap<String, Object> sortedValues = new TreeMap<String, Object>(new Comparator<String>() {
public int compare(String o1, String o2) {
int result = o1.compareTo(o2);
return SortedForEachTag.this.order == SortOrder.asc ? result : -result;
}
});

while( this.items.hasNext() ){
Object nextItem = this.items.next();

String bundleKey = (bundleKeySortProperty == null) ? (String) nextItem : BeanUtils.getProperty(nextItem, bundleKeySortProperty);
String resolvedMessage = LocaleSupport.getLocalizedMessage(this.pageContext, bundleKey);

sortedValues.put(resolvedMessage, nextItem);
}

this.items = toForEachIterator(sortedValues.values());

} catch( Exception e ){
throw new JspTagException("Error sorting items", e);
}
}
}

I extended the ForEachTag class from the JSTL standard implementation, and found that I only needed to override the prepare() method to implement my custom sorting. This custom tag uses the standard method of resolving resource messages from bundle keys, and then sorts them in ascending / descending fashion, depending on the order specified. If the bundleKeyProperty is not set, I assume the given collection is a list of Strings, each of which is a bundle key.

Here is the corresponding TLD definition snippet for this tag (I copied this from c.tld, found within the standard taglib implementation jar):

<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
version="2.0">

<description>
JSTL extension tag library
</description>

<tlib-version>1.0</tlib-version>
<short-name>x</short-name>

<tag>
<description>
Adaptation of the classic iteration tag, supporting the notion of sorting based
on the resolved message values of the given objects' bundle keys.
</description>
<name>sortedForEach</name>
<tag-class>com.abstractbits.web.taglib.SortedForEachTag</tag-class>
<tei-class>org.apache.taglibs.standard.tei.ForEachTEI</tei-class>
<body-content>JSP</body-content>
<attribute>
<name>bundleKeySortProperty</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
<type>boolean</type>
</attribute>
<attribute>
<name>sortOrder</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<description>
Collection of items to iterate over.
</description>
<name>items</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
<type>java.lang.Object</type>
</attribute>
<attribute>
<description>
If items specified:
Iteration begins at the item located at the
specified index. First item of the collection has
index 0.
If items not specified:
Iteration begins with index set at the value
specified.
</description>
<name>begin</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
<type>int</type>
</attribute>
<attribute>
<description>
If items specified:
Iteration ends at the item located at the
specified index (inclusive).
If items not specified:
Iteration ends when index reaches the value
specified.
</description>
<name>end</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
<type>int</type>
</attribute>
<attribute>
<description>
Iteration will only process every step items of
the collection, starting with the first one.
</description>
<name>step</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
<type>int</type>
</attribute>
<attribute>
<description>
Name of the exported scoped variable for the
current item of the iteration. This scoped
variable has nested visibility. Its type depends
on the object of the underlying collection.
</description>
<name>var</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<description>
Name of the exported scoped variable for the
status of the iteration. Object exported is of type
javax.servlet.jsp.jstl.core.LoopTagStatus. This scoped variable has nested
visibility.
</description>
<name>varStatus</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
</tag>

</taglib>

That's all there is to it! I like this tag because it simplifies sorting resolved resource bundle messages, which is something I tend to need to do often, and would otherwise require an unnecessary dependency of the controller class on the resource bundle resolver. Controllers are cleaner when they only deal with bundle keys, in my opinion. I do feel like the name of the tag could be better; any suggestions? Does this already exist somewhere, and I've simply missed it?