Thursday, June 14, 2007

Code syntax highlighting on blogspot

For my last post I wanted to get syntax highlighting for my code examples. That was not as easy as I had hoped!
(I think I spent more time trying to fix that than I spent on examining the activeresource-API...)

Anyway I tried a lot to get SyntaxHighlighter to work. But I did not succeed.
What I did end up doing instead was to follow the advice from QuirkeyBlog and use the TextMate Bundle's "Create css from theme" and "Create HTML" from theme. I'm pretty pleased with the result that is visible on screen, but it demanded a lot of copy-pasting of html-code. I have to automate that process for next "code-example-post". If anybody out there have a better idea of how to get syntax highlighting to work on blogger, please tell me.

Wednesday, June 06, 2007

What is a RESTful application?

In the Rails community (and elsewhere) there is a lot of talk about "RESTful applications". But there is a lot of questions of what is really meant by REST. IMHO the most characteristic and interesting parts of REST is:

  • Everything is a resource, uniquely addressable using a URI.
  • There are a few well-defined operations to view and change state of a resource
For Rails, the REST-buzz started with David's keynote at RailsConf last year and the "RESTful" controllers/routing was part of Rails 1.2, where it is very easy to create a webapp with "RESTful" characteristics (the new routing etc.). The client side of the RESTful interface, Active Resource, was not part of rails 1.2 but now can be easily used anyway. I come back to how soon...

Maybe the easiest way to create a RESTful application in rails is using the "scaffold_resource" generator (since rails 1.2).

Example "service":
$ ./script/generate scaffold_resource result match_date:date home_team:string\
away_team:string home_score:integer away_score:integer
$ rake db:migrate
$ ./script/server

Apart from giving a web application that can be used right away this also gives a REST-service that can be used with for example curl. When at least one item has been added with the scaffold web app, it can look like the following.
$ curl http://localhost:3000/results/1.xml

<?xml version="1.0" encoding="UTF-8"?>
<result>
<away-score type="integer">0</away-score>
<away-team>Denmark</away-team>
<home-score type="integer">3</home-score>
<home-team>Sweden</home-team>
<id type="integer">1</id>
<match-date type="date">2007-06-02</match-date>
</result>


Ok, that's fine. There is a lot of stuff that we get for free when using scaffold_resource. But how can we have a little more sophisticated client than curl?
Here comes the fun part. As I said above, ActiveResource was not part of Rails 1.2, but we don't even need to run edge-rails to run it now. It has been released as a gem and can be easily downloaded and installed, see Ryan's post for more info.
  • sudo gem install activeresource --source http://gems.rubyonrails.org
If the activeresource gem is installed we only need a couple lines of ruby code to access and make changes to resources. We don't even need rails, a small ruby-client will do.

require 'rubygems' 
require 'active_resource'

class Result < ActiveResource::Base
self.site = "http://localhost:3000/"
end

result = Result.find(1)
p result.home_team



This will access my REST-service and print out the home team of the resource "results/1" => Sweden.

The really cool use of activeresource comes when I want to create a new record. When I add to my following code example:

Result.create :home_team=>"Sweden", :away_team=>"Iceland", :home_score=>5, :away_score=>0, :match_date=>Time.now


It creates a new entry in my railsapp, that instantly can be accessed by activeresource or in a browser with
http://localhost:3000/results/2

I think it is pretty amazing that you in just a few lines of ruby code can create both a REST-service and client. The next step for me will be to test how activeresource and REST might work for a larger application. How about error handling? Using other clients than Ruby towards my REST-service (like a Java-client)? Can I throw out my SOAP-solutions now? (say yes!)...

Thursday, May 24, 2007

Deploying Rails to tomcat just works...

So, RailsConf 2007 is over, and I am back in Sweden again.
A lot of people in the blogosphere has summarized the different talks at RailsConf, so I won't do that right now.

For me, one of the best experiences from RailsConf is JRuby. I went to the tutorial "Your First Day with JRuby on Rails" on Thursday.
Ola Bini has continuously told us in the Rails community in Stockholm about the development of JRuby. But still, I was impressed of how easy you can turn a regular railsapp into a self-contained war-file running in an appserver, and using ActiveRecord JDBC. This is done with the goldspike-plugin on rubyforge.

Ok, so after the tutorial, I knew the theory, but does it really work for a real application?
Let's checkout!
I used a normal rails-app we use internally at my company.

Recipe
Here is a quick recipe for what I did to make it work:
(The assumption is that there is already a functioning railsapp using mysql on the computer, and that Java and ant, and tomcat is also installed.)

1. Install JRUBY (You can download a binary, or install it from source as is described here)
$ svn co svn://svn.codehaus.org/jruby/trunk/jruby
$ ant test
Add jruby to path:
$ export JRUBY_HOME =/Users/andreas/dev/jruby
$ export PATH=$PATH:$JRUBY_HOME/bin

2. $ cd "your-existing-railsapp-dir"

3. $ jruby script/plugin install svn://rubyforge.org/var/svn/jruby-extras/trunk/rails-integration/plugins/goldspike

4. Add support for mysql-jdbc:
Add to config/environments.rb:
require 'jdbc_adapter'

Add a new file config/war.rb with the following content:

maven_library 'mysql', 'mysql-connector-java', '5.0.5'

Add the following to each environment-description in config/database.yml :
adapter: jdbc
url: jdbc:mysql://localhost/blp_development
driver: com.mysql.jdbc.Driver


5. $ rake war:standalone:create
This will create a war file including a lot of java libraries (The jar-files that are needed are collected from maven repositories, including the mysql JDBC-driver specified in war.rb)

5. $ cp appname.war /opt/local/apache-tomcat-5.5.23/webapps/ (or wherever your tomcat is installed)

6. http://localhost:8080/blp

Voila, it works the same as the normal railsapp would, running in mongrel.
There is still some issue with static content, like css, but I am sure it will probably get fixed real soon. I haven't got any feeling of performance (it seems a bit slower then running it in mongrel), and other problems yet. But I am sure that backed up by Sun etc., the JRuby team will put a lot of energy in that (if it is an issue).

So why?
So, it works, but why would you use JRuby instead of standard c-ruby for railsapps?
Well, many customers I have shown small railsapps for has been impressed by the productivity boosts and how easy you can do things. But then they say "We can not have rails in our production environment, we are a Java shop".
My friend Peter has written a blog post that I think summarize "the why" pretty well.

The benefits of Jruby

  • Can use existing deployment environment in an organisation (if any, like Weblogic, jboss...)
  • Can use all existing Java-libraries out there from your railsapp...
Cons
  • Can not use native-c plugins , like ferret, rmagick... (This might be a problem if there are no alternative Java-libraries)

So, I look forward to see how this will effect the Rails-deployment strategies, rails adoption etc...

Thursday, May 10, 2007

RailsConf, here I come!

I am really excited of going to RailsConf next week.
It hasn't been much rails coding recently (because I am involved 100% i a Java project), but I started to play with the RESTful stuff the other day, and it is really cool, and the scaffold_resource generator lets you get started in seconds. More to come on that topic, when I have the time...

With Dr. Nic's good app myconfplan, I publish my preliminary conference schedule

Look forward for some posts on various aspects from RailsConf (I know I have atleast 5 readers out there ;-) !

Tuesday, January 30, 2007

I don't want to be a "regression test slave" !

Yesterday I went to Jfokus, a one-day Java conference in Stockholm.
One of the best talks, in my opinion, was the opening keynote by Simon Phipps, Chief Open Source Officer at Sun Microsystems. He was talking about the motivation of Open source, and there was a few phrases that got stuck in my memory (I hope I am quoting him correctly).

- "Earn a living by giving back" - By giving back you the community, you can feel good about making your own living out of that product/project. I like that phrase.

- "Regression test slave" - someone that is using open source, but does not commit back their patches and new additions to the community. For the complete lifecycle of that OS project they have to maintain their codebase, and make sure they dare to upgrade to a newer version. Doesn't sound so good. So there is a selfish reason to help the community too.

There was a lot of other good content in his talk (regarding licenses etc. ), but I don't recall everything right now, hopefully the slides will be available somewhere soon.

So, what about the rest of the day? I went to the following talks:

Intro Java EE5, EJB 3.0
Mike Keith, Oracle
- A bit to basic (but again, it was supposed to be an intro)
Mike was talking about the "news" of EJB 3.0
I was talking to a collegue about the usefulness of comparing EJB 3 to EJB 2 (which is a technique nobody in the would even consider using nowadays), it would have been much more interesting to compare it agaist for example a Spring/Hibernate solution.
I had a bit of a deja vu feeling too ("I have heard exacly this talk before..."), and when I got home a couldn't help myself from verifying that it was almost the same slides that Mike Keith used at JAOO 2005...

Testing a Java system using Ruby
Ola Bini, from "Karolinska institutet" (and JRuby committer) was talking about how you can use rspec (and Jruby) to test Java code as easy as if it was ruby code.
An hour was a little bit short time to try to fit in an intro of Ruby, Jruby, rspec AND demo the testing framework at KI. But I think that Ola did a good job showing the benifits.
I really like the idea of a "runnable specification", and will see if I can use similar ideas in my projects.

OSGi - intro
I would have preferred a more hands-on talk, telling what you could use OSGi for, but for me, that didn't no much about OSGi before, it was kind-of ok. The best part was the demo the last 10 minutes (using eclipse), and when Rickard Öberg was answering a few questions at the end, of how he can see JBoss is moving towards using OSGi.

Spring Web Services
Arjen Poutsma, of Interface21, was talking about different ways of creating and consuming a web service.
The most interesting part was that he advocates "Contract first", that means that you are specifying the XML-schema/WSDL before the java class, and not by just adding an annotation to a regular java method.
In my post "A few gotchas from consuming .net-webservices with a Linux/Java-client" I talked about the problems that generating a WSDL without thinking about the return types etc. can have. So I personally can really see the point of "contract first" for interoperability reasons, if you have a web serivice that is supposed to support clients written in different languages for instance.

To summarize, it was a day with varying quality of content (some high, some low), and I had a good time and met a lot of people I have worked with before.

Monday, December 18, 2006

TDD abstinence

- Yes, I admit it, I am a TDD-junkie. I have become addicted to TDD. If you have once tried it, you are stuck!

After having used test-driven development for the last projects, I find it really hard to live without the tests. Recently I started to work for a client with the job description "Do Java development, integrating an ERP system". There was very few unit tests in the codebase, and like a drug-addict I felt severe abstinence, feeling restless, and started to sweat heavily, until I wrote my first unit tests, set up test coverage, and starting to feel that satisfying feeling of control. After a week I started to preach TDD, and last week they wanted me to set up Continuous Integration, and teach the development team the basics about TDD and mock testing.
So like a drug dealer I sold them TDD, and soon they will be as addicted as I am. And it will be my fault... But it feels good!

Monday, November 06, 2006

A few gotchas from consuming .net-webservices with a Linux/Java-client

Webservice-integration and SOAP is easy, right? Well I wouldn't really agree with the S in SOAP standing for "simple"...
I have been working for a client, building a Java(Tomcat) web application that integrates with a Microsoft back-end using SOAP web services. I have been working with SOAP frameworks a lot before, mostly Apache Axis, and I didn't think I was going to run into so many problems, but...

The setup:
Java/Tomcat/Axis on Linux --> c#/.net on Windows Server

The first obstacle was to even generate Java-classes from the WSDL. Apparently Axis didn't support operation overloading (and a bit of googling on the subject told me that it wasn't really clear whether it was supported in the specification or not. If I got it right it was not supported in SOAP 1.0, but supported in SOAP 1.1 (but didn't really work there either), and then...not supported again in SOAP 1.2).
Anyway, other people have had the same problem and have fixed it (if you need it, you can download an enhanced version of wsdl4j.jar from: http://www.xwebservices.com/Resources/wsdl4j-esigma.zip)

The second obstacle was that the web service I was suppose to use returned .net-datasets (which I could not find any solution to use). After a lot of discussion I managed to have the web services rewritten to return standard "complexTypes".

The third obstacle does not have to do with SOAP but with Windows authentication. The client I worked with had a policy to use Windows Integrated Security for authentication (NTLM) as the only way to authenticate, and you had to use NTML version 2. I don't really know the details of that protocol but when searching the web for a solution to use it from Java on Linux I only found frustrated questions but no answers. Apparently Sun has licensed to have the JRE Http client work with NTLM, but only on the Windows platform. So that didn't work. Next try was to use Apache Commons Http Client, but the documentation was pretty clear on the support for NTLM.
From jakarta.apache.org:
"NT Lan Manager (NTLM) authentication is a proprietary, closed challenge/response authentication protocol for Microsoft Windows. Only some details about NTLM protocol are available through reverse engineering. HttpClient provides limited support for what is known as NTLMv1, the early version of the NTLM protocol. HttpClient does not support NTLMv2 at all."
Well...after a lot of searching I found a commercial http-client from oakland software that managed to handle the ntlm-protocol. And it finally it worked fine after some tweaking.

My advices:
1. Don't let tools like Visual Studio generate web services for you, without specifying the interface yourself (in particular do not return .net datasets if you want to service other than .net clients)
2. Don't use Windows Integrated Security (NTLM) for authentication from other than Windows clients (unless really, really necessary)
3. If you do choose to use NTLM, and want to use a commercial client, consider Oakland software's http-client, they had excellent support (and fixed Axis support in just a couple of days)...
4. SOAP is not (S)imple...Consider REST instead of SOAP where applicable... (after this project I am really looking forward to try out REST-support (and Active resources) in Rails 1.2. That might be my next blog post.