Moving from Tomcat 6 to Jboss AS5 – notes

May 18th, 2009

I’m moving a bunch of single WAR sites from Tomcat 6 to Jboss AS5. Here are the configuration steps that need to be taken.

Assuming this is your tomcat config file:
%TOMCAT_HOME%/conf/server.xml:

<Host appBase="/home/wirelust/wirelust.com" autoDeploy="false"
     debug="0" deployXML="true" liveDeploy="true"
     name="domainname.com"
     unpackWARs="true">
 
    <Alias>www.wirelust.com</Alias>
 
     <Context cachingAllowed="true" cookies="true" crossContext="true" debug="0"
             displayName="wirelust" docBase="." path="" privileged="false"
             reloadable="true" swallowOutput="false" useNaming="true">
             <Resource auth="SERVLET" name="wirelustDatasource" scope="Shareable" type="javax.sql.DataSource"
                     username="username"
                     password="password"
                     driverClassName="net.sourceforge.jtds.jdbc.Driver"
                     url="jdbc:jtds:sqlserver://sql.wirelust.com/database"
                     removeAbandoned="true"
                     removeAbandonedTimeout="30"
                     logAbandoned="true"
             />
     </Context>
</Host>

Becomes the embedded tomcat config file:
%JBOSS_HOME%/server/default/deploy/jbossweb.sar/server.xml

<Host appBase="/home/wirelust/deploy/wirelust.com.war" autoDeploy="false"
     debug="0" deployXML="true" liveDeploy="true"
     name="domainname.com"
     unpackWARs="true">
 
    <Alias>www.wirelust.com</Alias>
</Host>

Notice that the appBase is now in a folder called “deploy”. You have to add this deploy folder to the config file:
%JBOSS_HOME%/server/default/conf/bootstrap/profile.xml

<bean name="BootstrapProfileFactory" class="org.jboss.system.server.profileservice.repository.StaticProfileFactory">
    <property name="bootstrapURI">${jboss.server.home.url}conf/jboss-service.xml</property>
    <property name="deployersURI">${jboss.server.home.url}deployers</property>
    <property name="applicationURIs">
        <list elementClass="java.net.URI">
            <value>${jboss.server.home.url}deploy</value>
 
            <value>file:/home/wirelust/deploy</value>
        </list>
    </property>
    <property name="attachmentStoreRoot">${jboss.server.data.dir}/attachments</property>
    <property name="profileFactory"><inject bean="ProfileFactory" /></property>
</bean>

Make sure your exploded directory ends in .war or jboss won’t see it.

Then in that new deploy directory, create a datasource file, in this case called “wirelust-ds.xml” with these contents:

<?xml version="1.0" encoding="UTF-8"?>
 
<!DOCTYPE datasources
    PUBLIC "-//JBoss//DTD JBOSS JCA Config 1.5//EN"
    "http://www.jboss.org/j2ee/dtd/jboss-ds_1_5.dtd">
<datasources>
    <local-tx-datasource>
        <jndi-name>wirelustDatasource</jndi-name>
        <connection-url>jdbc:jtds:sqlserver://sql.wirelust.com/wirelust</connection-url>
        <use-java-context>false</use-java-context>
        <driver-class>net.sourceforge.jtds.jdbc.Driver</driver-class>
        <user-name>username</user-name>
        <password>password</password>
 
        <Pool.LogAbandoned>false</Pool.LogAbandoned>
        <Pool.RemoveAbandoned>false</Pool.RemoveAbandoned>
        <Pool.RemoveAbandonedTimeout>50000</Pool.RemoveAbandonedTimeout>
   </local-tx-datasource>
</datasources>

In order to bind to the correct virtual host, you have to create a new file in your exploded WEB-INF directory called jboss-web.xml with these contents:

<!DOCTYPE jboss-web PUBLIC
    "-//JBoss//DTD Web Application 4.2//EN"
    "http://www.jboss.org/j2ee/dtd/jboss-web_4_2.dtd">
 
<jboss-web>  
    <context-root>/</context-root>  
    <virtual-host>wirelust.com</virtual-host>
</jboss-web>

Then the last change you have to make is how you are looking up your datasource in the application.
If you have code like this in your tomcat application it will fail because your jndi is not bound to java:comp/env:

Context ctx = new InitialContext();
Context envCtx = (Context) ctx.lookup("java:comp/env");
DataSource ds = (DataSource)envCtx.lookup("wirelustDatasource");
con = ds.getConnection();

This of course can be fixed with some further configuration and resource-ref settings but I couldn’t get that to work so I just changed the code to work either way:

Context ctx = null;
Context envCtx = null;
DataSource ds = null;
Connection con = null;
 
try {
	ctx = new InitialContext();
} catch (NamingException e) {
	e.printStackTrace();
}
 
if (ctx != null) {
	try {
		// look for the datasource in the tomcat location
		envCtx = (Context) ctx.lookup("java:comp/env");
		ds = (DataSource)envCtx.lookup("wirelustDatasource");
	} catch (javax.naming.NameNotFoundException nnfe) {
		try {
			// look for it in the jboss location
			ds = (DataSource)ctx.lookup(wirelustDatasource);
		} catch (Exception e) {
			e.printStackTrace();
		}
	} catch (Exception e) {
		e.printStackTrace();
	}
}
 
 
// open the connection
if (ds != null) {
	try {
		con = ds.getConnection();
	} catch (Exception e) {
		e.printStackTrace();
	}
}

Converting binary to signed decimal in Actionscript

April 8th, 2009

If you’re like me, you may have been living these past few years taking advantage of the luxury of new sleek programming languages that rarely require you to do low level operations. One of the old skills I seem to always let get rusty enough is bitwise operation. Most code written with the help of frameworks doesn’t require this type of code and most programmers probably don’t even bother to learn it anymore.

I came into an instance yesterday where I needed to do some bitwise work in actionscript. The code was reading a bytestream from a binary file and converting to an array of numbers. Since this project was ported from Java (by someone else), a simple operation was overlooked. It was taking the binary number and converting it to a signed short. Since actionscript doesn’t have the same casting mechanisms conversion isn’t as straight forward in java.

In java the operation looked like this:

//  binary: 1111 1111 1111 0001
int hexValue = 0xFFF1;
 
short value = (short)(hexValue);
// value = -15 

Since short in java is always stored with a signed bit, the cast appropriately set the value at -15.

The way this was translated into Actionscript looked liked this:

//  binary: 1111 1111 1111 0001
var hexValue:int = 0xFFF1;
 
var value:int = int(hexValue);
// value = 65521  // Wrong! 

Actionscript has no concept of casting for primitives so the int(hexValue) doesn’t do anything at all.

Here is the workaround I came up with, I suspect there is a shorter way to get the same result but I can’t think of one right now.

//  binary: 1111 1111 1111 0001
var hexValue:int = 0xFFF1;
 
// get everything except the signed bit "111 1111 1111 0001"
var unsignedValue:Number = (hexValue & 0x7FFF);  
// unsignedValue now equals 32,753
 
var signedValue:Number = unsignedValue;
 
// if the signed flag is set, flip the value
if ((hexValue >> 15) == 1) {
    signedValue = unsignedValue - 0x8000;   // 0x800 =  32,768 (maximum 15 bit number)
}
 
// signedValue = -15 

Picture Pump

April 6th, 2009

About five years ago Ben Sisto had an idea for an application that I thought would be useful. The idea being that if you want to download images from Google image search — it takes forever to go through the results, wait for them to load, and save the images that aren’t 404. The application Ben envisioned, would simply download every result and ignore the ones that were 404 or took too long to load. Once you have all the results on your hard drive it is a lot easier to sift through them.

I quickly banged out a release that was workable in a few days and never really looked at it again.

This week I had an exact need for this application again. When I fired it up, it seems Google changed their site so it wasn’t working anymore. I spent some time tonight to fix it up and got it working much better than the original.

So here it is. v1.0 (i guess) release of Picture Pump (named after the site Ben ran at the time – HoneyPump):

Click launch to start/install the application. OSX users should be all set, windows users you might have to get java first.

also, hey look. source code

Email me with any suggestions or patches.

UPDATE: 10/24/2009
– fixed my mime settings so that launch button works again (sorry moved servers and forgot to set it)
– fixed a few bugs
– added support for safe search
– added filtering by image type and license
– source code is now under the Apache Public 2.0

Font Reader

April 2nd, 2009

I’ve been doing some work with Five3D lately. I extended it so I could render text with proper kerning pairs. I hit a bit of a wall though with converting my fonts to as3 since the way Mathieu does it doesn’t lend itself well to getting kerning pairs from flash itself.

Looking for an alternative, I started taking a look at Font Reader which is a port of a project called Typecast I saw a few years back.

Anyway. Long story short, I’ve been up all night playing around with and extending Font Reader (for my current project, but also because it’s fun to play with).

The version that is posted on the site uses a custom written path renderer which seems to have some bugs. In particular it seems to clip at the end of a logical path segment.

The custom objects were very close to the new stuff in the Flex 4 gumbo stuff from Adobe, so I was able to easily swap them out and use the Adobe classes instead. Since that was so easy, I started working on getting editable point handles so I can start editing the paths within the application. I have this about 75% done, but I can’t help quell that feeling that there must be a reusable class out there someone else wrote. Anyone know of one?

Here’s a screen shot of my current working build for comparison:

If I get this working by next week, I’ll post the code.

Howto set the cookie policy with Apache http client 4.0

March 29th, 2009

The Apache Http Components client changed quite a bit from 3.0 to 4.0 beta. It wasn’t entirely clear to me from the documentation how to set request headers and cookie policies with the new client. After a lot of fishing around I was able to figure it out, here is the code for anyone trying to do the same thing.

Downloading a page with the 3.0 client:

HttpClient httpClient = new HttpClient();
 
GetMethod get = new GetMethod("http://www.twitter.com/teacurran");
 
// set a faux User-agent
get.setRequestHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; .NET CLR 1.1.4322)");
 
// set the cookie policy
get.getParams().setCookiePolicy(CookiePolicy.RFC_2109);
 
int responseCode = httpClient.executeMethod(get);
System.out.println(get.getResponseBody());
 
get.releaseConnection();

here is the same thing with the 4.0 client:

DefaultHttpClient httpClient = new DefaultHttpClient();
 
HttpGet get = new HttpGet("http://www.twitter.com/teacurran");
 
// set a faux User-agent
get.addHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; .NET CLR 1.1.4322)");
 
// set the cookie policy
get.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.RFC_2965);
 
HttpResponse response = httpClient.execute(get);
 
int responseCode = int code = response.getStatusLine().getStatusCode();
HttpEntity entity = response.getEntity();
 
System.out.println(EntityUtils.toString(entity));

Finding a good web based Excel-like grid

February 25th, 2009

I’ve been searching for a good Excel-like grid widget that can be embedded in web-apps. I essentially need something like Google Docs but open so I can alter and self host it.

Here is my wishlist:

  • Looks nice
  • Allows inline cell editing
  • good Javascript API or extensibility
  • can plop into an existing project without committing to a whole framework (sorry RichFaces)
  • Supports as many functions as possible, Excel functions would be nice
  • Open source (LGPL or something similar)

I looked at a ton of projects. These grids looked great for displaying data but didn’t have any support for functions so they were out. I’m putting the list here to remind myself to keep them in mind for other projects.

In the end it came down to these three projects:

Sigma Grid:

This is a nice grid for structured data and supports pretty much everything I would need for most projects. It even has chart, print, and calendar support which would make it great for end users. It supports formulas but only ones defined in the code on the page, not ones the user enters, so it wouldn’t work for this particular project.

Simple Spreadsheet

This spreadsheet appears to be the most capable Excel like clone I could find. It supports charts, formulas, and custom macros. I ended up not using this because the GUI is lacking. Out of the box my users would be forced to know some html. There also seemed to be some weird behavior when you are editing a cell with a formula in it and click on another cell. I think this would be a great project if it got a makeover and a nice user-friendly toolbar. Try it out Here.

Social Calc

I don’t remember how I found this project because their website is the least flashy and really doesn’t show much about the grid at all. Social Calc could also use some help on the GUI front, and I will have to clean it up a little before I put it in my project – but it seems to be a solid reliable control. Once you get past the UI differences from Excel — you can’t right click or select rows or columns — the editing is pretty smooth and natural. For client side formulas I would have been happy with =SUM() and =AVG() functions and was pleased to find =VLOOKUP() and most others working. In fact, it seems to support most Excel functions with the exact syntax that business guys love.
It doesn’t seem to be able to do nested formula calls. I am going to look into how difficult it would be to add that if I end up using it.

I couldn’t find a demo of Social Calc anywhere on their site. I put their demo up here if you want to try it out.

While I’m thinking about proxys.

February 23rd, 2009

Sloppy is a tool I use all the time to test out sites in. It will load any site and simulate a slower connection than you are currently using. It used to be great to see how your site loaded on a 56k modem — now I use it to test how my sites load on a cell phone connection.

It also works cross platform without any install thanks to Java Web Start.

Proxy your connections for debugging

February 23rd, 2009

One of the tricky things about having flash or ajax client applications that request data on their own is knowing what they are requesting and when. I went nuts today trying to figure out if my ajax was making the right calls and getting back data, 404 messages, or nothing at all. That was until i remembered Squid.

Squid is a simple proxy server used in the unix world for logging or filtering web content but it can be very handy as a desktop tool for debugging rich internet applications.


Looking around, I found this great installer and GUI front end for Squid for OSX called SquidMan.

When you first launch SquidMan it will prompt you to install the Squid subsystem, just click OK enter your password and it will bring up the settings:

You can leave all of the settings as the default

Then from the main dialog, click “Start Squid”

In firefox, find the network settings under the Advanced tab:

Enter your local computer 127.0.0.1 as the proxy host:

This will now force all of your Firefox traffic to go through the squid proxy. From the main SquidMan application settings, you can now choose “Tools” and see a log of every URL that is requested by javascript or flash within your browser:

Compiling opencv statically on OSX

February 4th, 2009

OpenCV is rapidly changing and there is no bundled version that comes with OSX. I’m working on some command line apps and don’t feel like explaining to my users how to install Macports so I decided I would need to statically compile it into my application. There is a OSX Framework available, but I’m not making an application bundle so that was a no-go.

Here is what needed to be done:

First off, this document on the OpenCv Wiki doesn’t seem to work anymore if you are pulling directly from subversion.

Build System – Open CV seems to have 3 types of build scripts in subversion: Make, Autotools, and CMake. It took a lot of trial and error before I realized that only CMake appears to be working, at least on my system.

You can get a copy of CMake here here.

Before you run CMake, you have to change the following files:

  • src/cxcore/CMakeLists.txt
  • src/highgui/CMakeLists.txt
  • src/cvaux/CMakeLists.txt
  • src/cv/CMakeLists.txt
  • src/ml/CMakeLists.txt
  • tests/cxts/CMakeLists.txt

Look for the add_library lines and change this:

add_library(${the_target} DYNAMIC ${lib_srcs} ${lib_hdrs})

to this:

add_library(${the_target} STATIC ${lib_srcs} ${lib_hdrs})

Once you have made that change, follow the CMake instructions here

In Your Own Project
Assuming the above worked well, you can now statically compile your application pointing to the .a files generated by your opencv build.

One thing to keep in mind, is that your application now has to be linked to OSX system frameworks. You can resolve this by adding the frameworks to your linker in your Makefile:

-framework Carbon -framework CoreFoundation -framework QuickTime -framework QuartzCore

The whole web is malware today

January 31st, 2009

Is it just me, or is Google reporting all sites as Malware for everyone today?

screenshot of google