Beware of JavaScript's parseInt function - 010 does not equal 10...

I use JavaScript & jQuery quite a bit in my day job helping to add a bit of 'shiny' to our web applications. One feature that I added recently kept a track of the percentages that you had typed in the form.  It told you at the bottom of the page how much percentage you had left - I used JavaScript's parseInt function to help me in this respect.

As the application passed through system test it was found that if you typed '020' as a percentage my application said that you had 84% left to assign rather than the expected 80% - any ideas what's going on?

Yes that's right - the leading zero was causing parseInt to treat the number as an octal (base 8) number and so 020 was two lots of 8 = 16. It appears that this octal recognition is being deprecated but who knows when it will actually go so for now I've had to add ", 10" (i.e. a decimal radix) to the end of all of my parseInt calls:

parseInt(percentage, 10);
Double check any code that you use which uses parseInt to make sure it doesn't trip you up in the same way that I way!

Do 'is' boolean methods work in JSPs with JSTL?

When coding in JSTL you often want to use conditional logic (I'm thinking and the like) to be able to structure your page correctly. Auto-generated getter & setter methods will normally create isX() methods for the getters of boolean values (at least in Eclipse it does) but can you access this method directly from JSTL without writing a getX() version?

Does JSTL support 'is' boolean methods though? Ask a random poll of Java we developers and you will get conflicting answers so I decided to investigate, want the short answer?

YES - JSTL does support accessing isX() methods directly as if you were accessing a getX() method, but only if the return type of the isX() method is a primative boolean. If you return an object of any kind (such as Boolean isObjectBooleanTrue()) then JSTL fails to find the method and will give you a rather nasty JSP exception:

javax.el.PropertyNotFoundException: The class 'com.andrewbeacock.BooleanTest' does
not have the property 'objectBooleanTrue'.
So yes, 'is' methods work in JSTL but make sure you ONLY return primitive booleans from them.

How to stop Eclipse reformating your Java enumerations & comments

Eclipse is a wonderful IDE for the Java language and I’ve used it daily for at least the past 4 years but it does have some 'issues'. One is regarding it’s code formatting (or reformatting) support, normally it does a great job of putting stuff in the right place but there are occasions where it just fails to get it right.

Enumerations
When I write enums I like to have each item on it's own row, I find it's easier to read and amend in the future:

public enum Family {
    MOTHER,
    FATHER,
    DAUGHTER,
    SON;
}
But Eclipse has other ideas and formats it so that it looks like this:
public enum Family {
    MOTHER, FATHER, DAUGHTER, SON;
}
A way to get around this (and any other times where you have a few lines of code that you don't want collapsing into one is to add the double-slash style code comments to the end of each line:
public enum Family {
    MOTHER, //
    FATHER, //
    DAUGHTER, //
    SON;
}
Block comments

I recently wanted to have a decent sized chunk of XML stored within Java's block comments (/* ... */) so that I could refer to it as I coded a mapping class. Everytime I saved the class Eclipse reformatted my XML so that it looked like someone had been sick on the page.

After a little digging I found this gem buried in the original coding conventions document from Sun back in 1997:
Block comments can start with /*-, which is recognized by indent(1) as the beginning of a block comment that should not reformatted.
    Taken from section 5.1.1 of Java Code Conventions

Now I'm pretty damn sure that Eclipse doesn't use indent for it's layout but I tried it anyway and it works a treat! Simply add the minus sign to the start of the block comment section and it will leave the whole block comment alone:
/*-
    
        
            XML which we don't want
        
        
    
*/

Always note DNS server settings offline

Had a strange experience this evening - complete loss of the internet. My router was suggesting that I had ASDL connectivity, and even an IP address but I wasn't able to load any websites. After a bit of debugging with the help of a remote friend I figured out that I wasn't able to connect to my OpenDNS name servers and so google.com wasn't resolving for me.

I updated my router with Google's DNS settings and was back in play - just goes to show that what appears to be an ISP outage can be a case of name servers not being available at that time.

Make sure you make a note of a few free DNS server details (i.e. both OpenDNS and GoogleDNS) offline so you can try them out if you ever end up 'offline'!

Misleading wiring messages with aliased Spring DataSources

When accessing databases in Spring you commonly use a dataSource.xml file of some description to hold the XML stanzas describing the connection details to various databases or schemas.

When dealing with multiple dataSource requirements you might find that more than one logical dataSource bean name actually points to the same physical connection. Rather than define multiple datasource stanzas with exactly the same details, Spring allows the use of the element to point easily to an existing bean but use a different name:

<bean id="personDataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
        <property name="url" value="jdbc:oracle:thin:@database:port:SID" />
        <property name="username" value="USER" />
        <property name="password" value="PASS" />
        <property name="validationQuery" value="select 1 from dual"/>
    </bean>

    <alias alias="customerDataSource" name="personDataSource"/>
BUT (this is the whole point of this blog post really!)

If you get wiring errors when Spring tried to wire data sources into other beans it DOESN'T see aliased beans as first class citizens, when the error reports which data sources are available it won't list the aliased names, suggesting that you actually have less data sources than you really do!

So although aliases are great (and have saved me loads of lines of duplicated XML config) make sure you consider that they won't be shown in lists of 'available data sources'.

Sound advice for a source code commit frequency

A colleague of mine commented recently in a discussion on how often one should commit their source code:

commit on keyup
    - always tends to keep everything as up to the minute as possible
Classic! and yes it was tongue in cheek advice!

Using & comparing enumerations (enums) with JSTL

Often in your JSTL pages you will want to test a value of a particular variable before displaying something on the page. Often you are comparing against primitive types or other objects but what if you want to compare against an enumerated type?

Attempting to access the enumeration directly as Colour.BLUE doesn't work as the class/enum isn't available but what you can do it compare objects against their label or enum name.

If we have a Colour enumeration:

public enum Colour {
    RED, GREEN, BLUE
}
and we have a car object which has a getColour() method on it (returning the enumerated type) we can test against it in JSTL by using the specific name:
<c:if test="${car.colour eq 'BLUE'}">

Keyboard shortcut for 'paste as plain text' in Pidgin

I use Pidgin at work for communicating with my remote colleagues and I regularly paste code snippets and log file output into the Pidgin window. Often the text formats completely wrong and you end up sending the recipient a page of garbage rather than the real text.


Pidgin has a right-click context menu option for getting round this called 'paste as plain text' which normally does the trick but what if you normally use CTRL-V to paste your text in? After 2 seconds of experimentation today I found that CTRL-SHIFT-V is the keyboard shortcut for 'paste as plain text', I now feel complete...