Cyntech’s Tech Blog

Icon

Technical tidbits; coding and I.T.

How to open a Finder window from Terminal in OSX

Here’s a neat little trick I just learned.  If you’re in Terminal and need a Finder window of the folder you’re in, just type the following:

$> open .

Very handy if you need to use some Finder functions and you’re deep in a folder tree (like a java package).

What the hell is causing this Servlet Exception?

Writing a Java web application?  Keep getting a servlet exception? Can’t work out what the bloody hell is causing it?  Well, I have news for you!

Use this little snippet of code to get to the root cause of the exception.

try {

    // Your code

} catch (ServletException e) {

    Throwable t = e.getRootCause();
    System.err.println("Root Cause of Servlet Exception: " +
         t.getMessage());
    t.printStackTrace();

}

This does require to be placed in the area that actually uses Servlet code, like Request filters or HTTP processing (obviously to be able to catch it, you have to be where it is thrown).

MD5 Hashing Made Simple (Checksum)

I needed a way to compare large blocks of text efficiently for a project I am on at the moment.  I did not want to compare these strings when they could be in excess of 4000 characters or more.  Comparing such strings can be inefficient and system intensive.  So, a simple way of comparing large streams of any type of data is creating an MD5 hash of the data and then comparing the hashes.  It is very unlikely any two sources, unless identical, would have the same hash.  It is also extremely unlikely that even the slightest change (one character) would preserve it’s hash.

If you’re new to MD5, generating an MD5 hash from a string will return a 32-byte hexadecimal representation of that string.

I found this example on how to do it.

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class AeSimpleMD5 {

    private static String convertToHex(byte[] data) {
        StringBuffer buf = new StringBuffer();
        for (int i = 0; i < data.length; i++) {
            int halfbyte = (data[i] >>> 4) & 0x0F;
            int two_halfs = 0;
            do {
                if ((0 <= halfbyte) && (halfbyte <= 9))
                    buf.append((char) ('0' + halfbyte));
                else
                    buf.append((char) ('a' + (halfbyte - 10)));
                halfbyte = data[i] & 0x0F;
            } while(two_halfs++ < 1);
        }
        return buf.toString();
    }

    public static String MD5(String text)
    throws NoSuchAlgorithmException, UnsupportedEncodingException  {
        MessageDigest md;
        md = MessageDigest.getInstance("MD5");
        byte[] md5hash = new byte[32];
        md.update(text.getBytes("iso-8859-1"), 0, text.length());
        md5hash = md.digest();
        return convertToHex(md5hash);
    }
}

This example uses the security package within Java’s core, specifically the MessageDigest class.  Once set to use the MD5 algorithm, you can create a ‘digest’ of the source string which is then passed to a method that coverts the digest to a Hexidecimal string.

To use this example you simply call the class directly:

String MD5_ad1 = AeSimpleMD5.MD5("Mary had a little lamb");
String MD5_ad2 = AeSimpleMD5.MD5("Jack and Jill went up the hill");

This example also pointed out that there may be difficulties while processing string data in encodings other than iso-8859-1, but otherwise you should be right with it.

My first World of Warcraft Addon – WhisperDKP

So I wrote my first WoW addon last night and named it WhisperDKP (with the help of a good friend of mine).  It’s an extremely simple addon for my first attempt, intended for guilds that use eqDKP to monitor points for end game content raiding.  A user sends a ‘whisper’ with the text ‘dkp’ to the player with the addon installed, a whisper would be then returned with the point value of the players current DKP (dragon kill points).

There is still room for improvement, as the only way to provide the DKP information to the addon is to manually extract the values from the eqDKP website.  I’ll probably have to write a simple Java application to automate this.  After speaking with my friends in the guild, they want to be able to keep track of on-the-fly changes mid-raid as well as display a list of users in order of DKP so they can use it to choose the winner of an item.

For my current knowledge level, I can probably do this easily in a text based format, going into graphical windows is a new step and might take me a bit longer, maybe I’ll do it textual first then step into a graphical window.

Can’t get that underlying connection?

While trying to create CLOBs for an Oracle database table, I was getting an Invalid Argument(s) error when trying to call CLOB.createTemporary().  I beat my head against a wall trying to figure out why I was getting null when you request the underlying connection from a Pooling Data Source but then I found some information that solved it for me.

For it to work, you need to enable access to the underlying connection to be able to get an instance of it.

There are two ways of doing this;

In your Tomcat configuration (or other Application Server), by enabling access to it in the <Context> of your web application:

<Context path="/ariweb" docBase="ariweb.war">
    <Resource name="jdbc/ari" auth="Container" type="javax.sql.DataSource"/>
    <ResourceParams name="jdbc/ari">
    <parameter>
        <name>driverClassName</name>
        <value>oracle.jdbc.driver.OracleDriver</value>
    </parameter>
    <parameter>
        <!-- NOTE: This is necessary to enable access to the Oracle connection object -->
        <name>accessToUnderlyingConnectionAllowed</name>
        <value>true</value>
    </parameter>
    <!-- Other configuration parameters -->
    ...
    </ResourceParams>
</Context>

If you’re using Spring, you can also set it there in the parameters for your data source:

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName"><value>oracle.jdbc.driver.OracleDriver</value></property>
        <property name="url"><value>jdbc:oracle:thin:@dbhost:1521:DBSID</value></property>
        <property name="username"><value>user</value></property>
        <property name="password"><value>pass</value></property>
        <property name="accessToUnderlyingConnectionAllowed"><value>true</value></property>
</bean>

This means that when you use the getDelegate() or getInnermostDelegate() methods, you will get the underlying connection type rather than a null.

How to use Commons Logging

I’ve spent the whole of today trying to find information on how to properly configure commons logging in my java classes, specifically with Jdk14Logger.  It’s been a labourous task as a) there’s not much out there and b) Apache’s documentation isn’t brilliant.  I finally found one page on Tigris.org explaining commons logging (from the view point of a change in their software), which helped me greatly.

Due to the lack of decent documentation out there, I’m writing this article to try and help others.

Read the rest of this entry »

Oracle, Timestamps and Timezones

I recently had a problem with retrieveing timestamps from an Oracle table (you might remember my previous post on Dates and Timestamps), where I would get an error such as this: Read the rest of this entry »

Getting back into the habit…

I’ve been getting back into some php web coding this week, looking at updating an application that a friend wrote a couple of years ago.  It’s a CHPP (Certified Hattrick Product Provider) program which is an interface to Hattrick that ‘predicts’ what a match’s result could have been given the ratings.  The guru’s over at HT changed the login system, so it needs updating for it to work.  I’m getting some practice in before writing an application later on for the Oceanian national team, which should be fun.

Java Timezones

I found a blog post indicating that the available list of timezones in java.util.TimeZone isn’t documented.  They listed just the specific United States timezones as the auther was American.

I decided to post the full list here if anyone else was like me and needing the full list without having to write a quick test class just to print them out.

Read the rest of this entry »

Java, Oracle and Date columns

I spent a few days wrestling with Dates with Oracle and Java this week. I thought I’d post my findings here to save others the trouble.

When using PreparedStatement objects to interface with the Database, using PreparedStatement.setDate( int parameter, java.sql.Date field ) will truncate the time off the date.

If you require the time as well as the date, use java.sql.Timestamp.

Read the rest of this entry »