Sunday, April 24, 2011

My First Scala App

I've been looking at learning another development language for some time. Over the years I've dabbled in PHP, Groovy, and even C/C++. While I learned a lot in doing this, I never became an 'expert' in any of them. For the most part, I used them for the project in which they were needed or for the scenario where they were most appropriate and then went back to Java for my primary development platform.

The reason is that I'd become so familiar with the Java tool set, language, and available libraries. I was (and am) most productive in that environment. But Java is showing its age and limitations. Its becoming clear that some of the most vexing problems facing developers will need a full range of tools to solve them, and while Java's OO support is good, its functional support is not. I, therefore, decided that I needed to expand and really learn another language pretty well. I decided that language is going to be Scala.

Why Scala? Well, I'll go over that in a different post. This post is about my experience writing my first Scala app. So, here it goes.

The Application
Since I am changing jobs (again), I decided I would leave something behind for my co-workers.  In our current work environment, we have a shared drive where server error logs are stored in 'pseudo' XML format.  It was always annoying to me that we had to navigate the file structure, open a text file, and hunt through the file to find an error we would be looking for.  It was also difficult to determine how often an error was occurring.  Now, the easy way to solve this is to use the log4j XML appender and read the files with Apache Chainsaw, but this is a large company, and it decided to go with a proprietary format.  I decided to use Scala to write a little desktop app that the developers could use to view the log files in a tabular format, then select a row to see the stacktrace detail.

The Functionality
The application needed 3 essential things : a table to show rows, a TextArea to show the detail, and a FileChooser to select which log to view.  In the background it also needed the following processing capabilities:  read in a file, modify the text to make it valid XML, parse the XML tags.  That's about it.

The Results
Well, I whipped out the app pretty quickly.  It would have been much more quickly if I hadn't been learning Scala along the way.  There were some really nice things about Scala that I found out.  First, its swing wrapper classes are nice, and their pub/sub model is succinct.  Second, the XML processing is excellent.  The built in support for XML files allowed me to parse the logs in a couple of lines of code.  Finally, the Actor metaphor for multi threading was also very easy to use and implement.

I had a few frustrations along the way as well.  The Eclipse plugin was nice, but it still doesn't have code-complete (One of the biggest productivity tools in my mind).  The code-complete is supposed to be in the plugin, but for some reason, doesn't seem to work for me.  I've tried on 3 separate computers. This meant that I was reading a lot of documentation.  Its also hard to read the documentation.  Since Scala has so much support for so many features, the docs can get a little complicated to read.  I'm sure as I become more familiar with the libraries and syntax this will get easier, but I can see how it would be daunting for a junior developer.

Conclusion 
I'm by no means a Scala expert at this point, but I enjoyed the language enough, and it seems to have enough benefits that I'm looking forward to learning more.  Who knows, maybe one day Scala will be my primary language...

Saturday, February 5, 2011

GWT and SmartGWT - JNI and Unsatisfied Link Error

GWT uses the Java JNI mechanism in a really unique way.  JNI was originally intended to work with native code on the computer where you had your JDK, like C++.  I've had limited exposure to the JNI interface - I've only had to use it for a serial communication component.  Even then, I was using a library that relied on JNI, so I didn't generate the code, I just had to make sure the native library was on my classpath.

The Google Engineers applied this mechanism to Javascript, however.  If you want to write 'Native' Javascript in your GWT classes, its easy.  Just flag your method as native and put your javascript code, in comments, before your terminator.  Here's a sample from the Google Web site.


public static native void alert(String msg) /*-{
  $wnd.alert(msg);
}-*/;


Viola!  Instant native interface for your JavaScript CLIENT code.  That's right, just try running this method on the server.  While working on my current project, I ran into this issue accidently while trying to use a SmartGWT class.  I got a JNI error on the server because the VM couldn't find the native code.  Of course, there isn't any native code on the server.  In this case, the 'native' code is JavaScript.

I was trying to pass a SmartGWT ListGridRecord to the server so I could map it to a persistent bean.  Of course, once I saw the error, I followed the inheritance hierarchy for ListGridRecord all the way up the chain, and what did I find at the top?  JsObject.  Of course, I didn't even have to go that far.  After looking at the Record code, it relies on static methods in JSOHelper, which uses a lot of native JavaScript

In the end I had make transfer objects to use to send the data over GWT RPC. This was a good reminder, though, that while GWT looks like Java, what's being executed really isn't.

Sunday, January 30, 2011

SmartGWT and GWT RPC Integration

I am currently finishing my Capstone project for my MSE at Carroll University. While the application I am writing itself doesn't have a lot of twists in it, I decided to use this opportunity to experiment with some different technologies.  I'm using GWT, SmartGWT, and Hibernate in the application.  I've used Hibernate and GWT pretty extensively in my professional career, but the twists I've added for this development are to configure Hibernate using the JPA standard, and to use GWT Designer for mockups.  I've already posted about my experiences with GWT Designer.

I haven't really used SmartGWT before.  My experience has been with the ExtGWT library, which I loved.  I chose to use SmartGWT on this build because it is fully LGPL, while Ext is dual licensed.  While working with SmartGWT I've found some things that I was not expecting.

One of the selling points of SmartGWT is that it has built in support for 'Data Binding' with Tables and the like.  This is done through DataSource objects.  The problem is, the published DataSource implementations don't work with GWT RPC. And the DataSource base class has dozens of methods (Just take a look at the JavaDoc).  It really isn't clear how to define your own custom data source, or the work it will take to do so.  This was a sticking point for me because I really like the GWT RPC mechanism, and I had already come up with a nice security model, which I posted about here.

It took a while, but I finally found a post about integrating GWT RPC on the SmartClient Forums.  The post is a bit dated and says you need to compile from source, but that is not the case.  The version of SmartGWT I am using, 2.4 already has the clientCustom flag built in.  The developer who posted this also supplied 5 files, Including a GwtRpcDataSource abstract class, and a sample implementation.  The class comments well what you need to do to implement your datasource. With this setup, a new GWT data source can be defined by implementing 4 methods.  

I hope this post helps others find the solution much faster than I was able to.