Friday, December 28, 2012

Jetty JTA via JNDI with Spring

Finally got Jetty 8 to have JTA transactions available to Spring via JNDI lookup using Atomikos 3.8 after a lot of trouble. I'm not 100% sure if this is correct but it seems to be working for me.

My main driver was not wanting to have Atomikos classes hardcoded in the spring config. Instead we can just register the Atomikos UserTransactionManager and UserTransactionImp classes in JNDI and have Spring look them up, just like if we were using Websphere, Weblogic, etc. Now I can use Jetty for local development instead of Websphere without any change to my actual code base. (Be warned that I'm not using this in prod.)

1) Jetty 8 install
    Download, unzip, setup JETTY_HOME var for IntelliJ to use
2) Atomikos 3.8 install
    Download, unzip
    copy from the Atomikos lib/ directory into Jetty lib/ext/:
    copy all the JARs from the Atomikos dist/ directory into Jetty lib/ext/.
    (Hat tip to this blogpost for helping)
3) Jetty config for JTA (Ref posting that helped)

4) Spring JTA lookup
    Spring will lookup using standard JNDI locations of java:/TransactionManager and java:comp/UserTransaction

Tuesday, September 18, 2012

iPhone Gmail Notes sync to Mac

After setting up Mountain Lion on my Mac, I went and tried the new Notes sync feature. I've been syncing my notes from my iphone to Gmail for months now (or so I had thought).

But going into the Notes app on my Mac I did not see anything newer than 6-8 months ago.

Then I checked gmail and the Notes label showed the same. And a few google searches turned up empty.

The fix for me was to go into iPhone settings > Mail, Contacts, Calendars > Fetch New Data > Advanced > and change my Gmail Notes account from Fetch to Manual.

Then I relaunched the Notes app on my phone and everything magically synced.

Scary thing is I think none of my Notes were backed up, even though I thought they were.... Hopefully this will help someone else out there.

Monday, August 06, 2012

CSS word-break and word-wrap

Finding a cross-browser CSS solution to force breaks in long words inside of table-cells is not an easy task. My issue was specifically with email addresses formatted like The snag is that support is decent inside of block-level elements like divs, but only hacks seem to exist for table cells.

The widely supported white-space will instruct the browser to wrap on normal wrapping characters, but the browsers will not break a word. There are two newer CSS properties to try: word-wrap and word-break.

word-break:break-all will force a break anywhere but ignores spaces. It works well if you know your table cell will only contain a single term or word, and you don’t care about wrapping on commas, hyphens, or spaces.

word-wrap:break-word will honor commas, hyphens, and spaces to wrap first and then will force a break in the middle of words when needed. The catch is it only works cross-browser on block-level elements and not on table cells. Also a width or max-width must be specified.

One promising hack sets a table to be table-layout:fixed but this did not work with the jQuery datatables plugin.

So here is my cross browser solution, tested in Chrome, Firefox, and IE9. It unfortunately requires wrapping your td contents inside of a div. But it does work with the jQuery datatables plugin.

/* forces wraps in middle of words when necessary */
div.force-wrap {
  white-space: normal;
  word-wrap: break-word;
}, div.force-wrap {
  width: 30em;

  <td class="”email”">
<div class="”forcewrap”">,

Additional References

Wednesday, June 27, 2012

jQuery to Disable IE MS Lync phone number icons

Corporate environments have been pushing out MS Lync as the IM successor to Office Communicator. With that comes a click-to-call plugin that automatically puts a little phone icon next to any detected phone number on a web page. I can only assume that clicking the icon will make a phone call, as if anyone really wants that feature to be on by default.

Unfortunately for web developers, the icon is rendered by inserting markup into the webpage. This is a problem for a number of reasons:

  1. The icon looks like it is part of your page, but it's really not. 
  2. It even prints with the page. 
  3. It screws up your layout because the icon is inserted AFTER the page is rendered and onLoad javascript executes! This plays double issues when using a scrolling tables plugin like jQuery datatables because now the header doesn't lineup with the columns.
  4. There is no meta tag to disable the plugin. The only option to disable the plugin is up to the user or administrator, and we know we can't count on users to disable it for us.

Our phone numbers were in the common US format of (555) 555-5555. After some trial-and-error I found that MS Lync does not detect phone numbers that use a non-breaking hyphen (&#8209) instead of the normal hyphens (&#45 and &#4208) (big thanks to this Dashes and Hyphens page).

Now I could have changed our code server-side to render the new hyphen, but this caused issues with our exports to Excel, Word, and PDF. So instead I created a simple jQuery plugin that will replace the common &#45 hyphen with a &#8209 non-breaking hyphen and foil the MS Lync plugin from detecting our phone numbers.

Tuesday, June 19, 2012

JQuery UI datepicker IE focus fix

The jquery ui datepicker control is quite slick and easy to use, and as you see here it can be customized through various events.

At my current client we have multiple controls on the page that listen for onblur and onchange events to notify of changes to the page (most notably an html5 placeholder enabler). But the datepicker does not send the blur event when a selection happens. Also, our users wanted focus to return the text input field after the date selection. So we need to setup some event handlers to call the blur and focus events upon date selection.

First let’s start with a simple datepicker that is applied to any input field having css class dateInput, with a few extra options:

  changeMonth: true,
  changeYear: true,
  showAnim: "fadeIn",
  yearRange: 'c-30:c+30',
  showButtonPanel: true

Adding a blur and change event on datepicker selection is rather easy:

  changeMonth: true,
  changeYear: true,
  showAnim: "fadeIn",
  yearRange: 'c-30:c+30',
  showButtonPanel: true,

  /* blur needed to correctly handle placeholder text */
  onSelect: function(dateText, inst) {

But adding focus is a little more difficult because of a difference in browser behavior. Simply adding a .focus() to onSelect and onClose will suffice for Chrome and Firefox but IE will reopen the datepicker once it receives the focus. In order to handle IE we can simply implement the beforeShow event handler, returning false when we reach a case where IE should not reopen the datepicker window. I’ve added a fixFocusIE variable to track this:

Hopefully this helps someone else searching for a way to focus on the original input field after selecting a date with the JQuery UI Datepicker control.

Note: A little writeup cross-posted on the OPI blog about making the jquery UI datepicker send blur events when dates change, while handling a quirk with IE:


Thursday, January 19, 2012

Creating a Spring @StrictDateTimeFormat Annotation

Spring Formatters and Converters make it easy to annotate fields for conversion from Objects to Strings, and are especially useful in web apps. But there is no easy or straightforward way to strictly validate the String before parsing into an object, without creating a custom Formatter. Here is a reusable solution that uses a RegexParserDecorator to decorate any Spring Formatter to apply a regex pattern, in turn creating a @StrictDateTimeFormat annotation as an example implementation.

A little background: Spring 3.0 brought the Converter and Formatter framework with a concise @DateTimeFormat annotation, simplifying the Date to Object conversion that previously took custom binders or other wiring code. With @DateTimeFormat you can easily supply a String pattern used to parse and print a Date (or joda DateTime) object. However, the annotation does not strictly validate the String before converting to a Date. For instance, supplying a MM/dd/yyyy pattern does NOT enforce a 4 digit year. Instead a 2 digit year will be accepted and parsed using the SimpleDateFormat rules. Similar loose checking goes for 1 digit days and months, and also the slash character used as a separator. It would be easy to just throw the @Pattern tag onto your Date field except that @Pattern is only allowed on String fields. Combining @Pattern and @DateTimeFormat is what drove the creation of @StrictDateTimeFormat:

//sample usage using defaults for regex and pattern
private DateTime birthday;

Read more below for a discussion and snippets of code, and the entire codeset with comments is available on github.

The RegexParserDecorator: The first step is to create a Regex Parser class that will apply a regex pattern to validate a String for us. Creating this as a Decorator gives the added benefit that you can easily wrap any Spring Formatter to apply Regex patterns. The constructor takes a Parser to wrap and a regex to apply; and the parse method first validates against the regex before passing onto the decorated Parser:

public RegexParserDecorator(Parser parser, String regex) {
	this.parser = parser;
	this.regexPattern = Pattern.compile(regex);
public T parse(String text, Locale locale) throws ParseException {
	if (!regexPattern.matcher(text).matches()) {
		throw new IllegalArgumentException("Text does not match regex: " + text);
	return parser.parse(text, locale);

The @StrictDateTimeFormat Annotation: Next step is to setup the annotation interface class. It is very similar to DateTimeFormat but adds the field to hold a regex. The default regex allows 1 or 2 digit days and months, requires a forward slash as the separator, and enforces a 4 digit year. This can be easily overriden when applying the annotation to a field by supplying your own (regex="", pattern="") extension. A pattern is still required so make sure your pattern and regex are paired appropriately.

@Target({ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER})
public @interface StrictDateTimeFormat {
    public static final String REGEX_DEFAULT = "^(0?[1-9]|1[012])/(0?[1-9]|[12][0-9]|3[01])/dddd$";
    public static final String PATTERN_DEFAULT = "MM/dd/yyyy";
    String regex() default REGEX_DEFAULT;
    String pattern() default PATTERN_DEFAULT;

Spring then requires a StrictDateTimeFormatAnnotationFormatterFactory to wire the annotation with the parser. Nothing fancy here as it borrows heavily upon Spring's own JodaDateTimeFormatAnnotationFormatterFactory. The getParser method applies our regex to the DateTimeFormat functionality:

public class StrictDateTimeFormatAnnotationFormatterFactory implements
		AnnotationFormatterFactory {
	public Parser getParser(StrictDateTimeFormat annotation, Class fieldType) {
		DateTimeParser parser = new DateTimeParser(forPattern(annotation.pattern()));
		return new RegexParserDecorator(parser, annotation.regex());
	private DateTimeFormatter forPattern(String pattern) {
		return org.joda.time.format.DateTimeFormat.forPattern(pattern);

Hooking it all together: Here is the snippet from my applicationConfig.xml showing how the annotation is registered into Spring:

<mvc:annotation-driven conversion-service="myConversionService" />
<bean id="myConversionService" class="">
	<property name="formatters">
			<bean class="jeffsheets.util.format.StrictDateTimeFormatAnnotationFormatterFactory" />

Hopefully this information is helpful in creating a reusable regex validating DateTime formatter for use in your own web application.

 Originally published by me on the Object Partners blog: