Saturday, August 1, 2009

Retirement of the blog

I do not have enough time to post articles in more than one place, so I will retire (at least temporarily) this blog in favor of my site definenull.

Hope to see you all there!

Cheers,
Dani

Thursday, June 18, 2009

Dude, where are my resources?

I was struggling with a stupid problem this week and it took me some time to realize what was going on, so I will post here the solution so nobody else needs to waste time on this matter again (and for future reference for myself as well ;)).

I wanted to read a .csv (comma separated value) text file that was inside one package in my application. Consider the simplified version of my code in the tree below.


src
|- main
|- java
|- com
|- App.java
|- resources
|- file.csv


The App.java had this (simplified) code:



public class App {

public static void main(String[] args) throws IOException {
URL url = App.class.getResource("com/resources/file.csv");
}
}



The value of the variable url was null. And there is one simple (and quite obvious once you find out) reason for this. The resource file was indeed in my src folder, but it was not in my destination folder where the classes were generated. As I am using maven, this would be the target folder.
And why is this obvious? Because the getResource method uses the ClassLoader of the calling class to find the resource you are looking for, and the realm of files the ClassLoader knows are in the classpath. As I was executing the code from my IDE, the classpath would be the compiled classes from sources. On top of that, the problem was that the text files not being copied to the destination folder.

Thanks to Maven the solution was quite simple. As it is based on convention over configuration, all I did was to move the resources folder to the root of the src/main folder like this:


src
|- main
|- java
|- com
|- App.java
|- resources
|- file.csv


The App.java had this (simplified) new version of the code:



public class App {

public static void main(String[] args) throws IOException {
URL url = App.class.getResource("file.csv");
}
}



In summary, just make the resource available somewhere in your classpath: in the target/classes folder or in the .jar you are generating.

Find this article also on definenull.com.

Thursday, June 11, 2009

Java in Ubuntu

Well, I am now just trying to use Ubuntu as a development environment. First thing I noticed is that there are more than one jre already installed by default in the system, and here is the evidence:


jars@jars-desktop:~$ locate /rt.jar
/usr/lib/jvm/java-6-openjdk/jre/lib/rt.jar
/usr/lib/jvm/java-6-sun-1.6.0.07/jre/lib/rt.jar


I really wanted to use the Sun JDK for my development instead of OpenJDK, so I installed the Sun Java.

Just type the command below and compare the results:

sudo apt-get install sun-java6-bin sun-java6-jre sun-java6-jdk



jars@jars-desktop:/etc/alternatives$ sudo apt-get install sun-java6-bin sun-java6-jre sun-java6-jdk
Reading package lists... Done
Building dependency tree
Reading state information... Done
sun-java6-bin is already the newest version.
sun-java6-jre is already the newest version.
sun-java6-jre set to manually installed.
Suggested packages:
sun-java6-demo sun-java6-doc sun-java6-source
The following NEW packages will be installed:
sun-java6-jdk
0 upgraded, 1 newly installed, 0 to remove and 14 not upgraded.
Need to get 17.4MB of archives.
After this operation, 55.7MB of additional disk space will be used.
Get:1 http://de.archive.ubuntu.com hardy-updates/multiverse sun-java6-jdk 6-07-3ubuntu2 [17.4MB]
Fetched 17.4MB in 3min23s (85.6kB/s)
Preconfiguring packages ...
Selecting previously deselected package sun-java6-jdk.
(Reading database ... 139447 files and directories currently installed.)
Unpacking sun-java6-jdk (from .../sun-java6-jdk_6-07-3ubuntu2_i386.deb) ...
sun-dlj-v1-1 license has already been accepted
Setting up sun-java6-jdk (6-07-3ubuntu2) ...


But which of the two JVMs that now reside in my system area going to be used? Here it comes the ass-saving (and brand new to me) Ubuntu command:


sudo update-java-alternatives -s java-6-sun


This will define Sun Java-6 as the default JVM in your system. If you prefer to do it step-by-step just type this:


jars@jars-desktop:~$ sudo update-alternatives --config java

There are 2 alternatives which provide `java'.

Selection Alternative
-----------------------------------------------
+ 1 /usr/lib/jvm/java-6-openjdk/jre/bin/java
* 2 /usr/lib/jvm/java-6-sun/jre/bin/java

Press enter to keep the default[*], or type selection number:


The next tricky step is to set the prefered order in which your JVMs shoud be used. To set this up just edit the /etc/jvm file and include your recently installed JVM as the first one, so your file looks more or less like this:


# This file defines the default system JVM search order. Each
# JVM should list their JAVA_HOME compatible directory in this file.
# The default system JVM is the first one available from top to
# bottom.

/usr/lib/jvm/java-6-sun
/usr/lib/jvm/java-gcj
/usr/lib/jvm/ia32-java-1.5.0-sun
/usr/lib/jvm/java-1.5.0-sun
/usr


Ok, not ready yet, but I am not willing to return to Windows without doing this until the end! :)
So, next step is setup the environment variables and if you want to set it system wide, edit the /etc/profile file and append the following lines at the end:


export JAVA_HOME=/usr/lib/jvm/java-6-sun
export PATH=$PATH:$JAVA_HOME/bin


You can also set the variables for only one user by editing /home/$user_name$/profile.

Now test it!!


jars@jars-desktop:/etc/alternatives$ java -version
java version "1.6.0_07"
Java(TM) SE Runtime Environment (build 1.6.0_07-b06)
Java HotSpot(TM) Client VM (build 10.0-b23, mixed mode, sharing)


You find this article also on definenull.com.

Wednesday, June 10, 2009

JavaOne 2009 Slides


You can download the slides from JavaOne 2009 conference from here. Just choose the track and find the slides.

You need to be signed at the Sun Developer Network (SDN) to view/download the pdf.

Cheers

Thursday, June 4, 2009

JavaOne 2009: RESTful Transaction Systems

Mark Little (JBoss CTO) and Michael Musgrove (Red Hat Tx)
RESTful Transaction Systems


  • Atomic Transactions should have a scoping mechanism that provides "all-or-nothing" semantics, enables shared resources to be protected from concurrent use and have ACID properties (Atomic, Consistent, Isolated, Durable)

  • WS-Transactions in the past vas very hard to achieve transactional interaction among WS services; even JTS implementations didn't interoperate but WS provide the opportunity to leverage unparalleled interoparability

  • On B2B interaction, one cannot affort to lock resources on behalf of an individual indefinitely and maybe you need to undo a subset of the worh that's been done

  • Possible Transaction Models for REST


    • Atomic Transaction (XA/XTA) - status: specified and implementd

    • Forward Compensation Base (WS-BP)- status: specified


  • Implementation for REST transaction for JBoss uses an uniform interface with HTTP and the usual methods (GET, PUT and POST) return the usual HTML code and some XML in the body

  • Example URLs:


    • GET <s>://<auth>/transaction-coordinator/active

    • POST <s>://<auth>/transaction-coordinator/begin


  • Demo is using the transaction-coordinator (with a Proxy in Tomcat) and some Web Services in Python, JBoss TM and RESTeasy

JavaOne 2009: Enterprise Application Integration Patterns

Andreas Egloff (Sun) and Bruce Snyder (SpringSource)
Enterprise Integration Patterns in Practice
"A Design Pattern is a formal way to document a solution to a design problem ina particular field of expertise" - from Wikipedia


  • The presentation is based on the "enterprise integration patterns" book

  • Stencils for Visual Studio in EAI Patterns web site

  • Flexibility x Productivity: too little explicit support out-of-the-box but explicit support out-of-the-box is too rigid (does not fit your case or you are forced to go around) - the solution would be to extend those use cases

  • Frameworks: Apache Camel and Project Fuji (from OpenESB v3)

  • Camel supports routing of messages in a simple manner like, for instance, from("A").to("B") and much more complex stuff like taking decisions in the route and where to route the message to; it also suppors a pipeline routing much like from("file://myfile.txt").choice().when().method("MyBean","matches").to("Q").end().pipeline("B","C","D")

  • Some Camel components: Atom, Bean, SEDA, SFTP, HTTP, FTP, Mock, XQuery, XSLT and much more (70+)

  • Project Fuji is the basis for OpenESB v3 and is a service platform to realize SOA based on Convention - Configuration - Code in that order; Light weight and OSGi based and not centred on JMS but on Services

  • The Project Fuji has a Web UI that allows the user to visually declare and configure services and program the connections and routing of services; it also offers a DSL (Integration Flow Language)

  • Bruce and Andreas went on some slides with examples of the patterns from EAI book and showed how those are implemented in Camel calls using Fuji interface to build them - it seems easy, but as they said in the beginning of the presentation, using out-of-the-box solutions might be too rigid; on the good side, you can write code using DSL and even pure Java, so it really might be an interesting solution for integration

  • After the slides, a quick live demo using Eclipse with some tools for Spring and Maven with archetypes for Apache Camel

Wednesday, June 3, 2009

JavaOne 2009: Coding REST and SOAP together

Martin Grebac and Jakub Podlesak from Sun Microsystems on Coding REST and SOAP together


  • Using METRO = JAXB + JAX-WS + WSIT and Jersey

  • No deployment descriptors needed (use annotations) but still available if required; part of Java EE 5 and 6

  • Why REST? Simplicity, Takes advantage of the existing caches and proxies, Serendipity, Scalability (HTTP load balancers)

  • Common JAX-WS/RS concepts should be supported

  • Heavy using of annotations: @Path, @WebService, @GET, @Produces, @WebMethod, @XmlRootElement, @XmlAttribute etc.

  • Take care: do not fall into RPC over HTTP (REST-RPC hybrid), use compatible security (Securing SOA does not automatically secure the REST), annotation hell

  • Think about the consumers of your services and ask yourself if you really need SOAP and REST; consider using REST only in some parts of your application

  • Use @WebMethod annotations to enable control over exposed SOAP methods