May 28 2007

Java Persistence API – Best Practices and Tips

This JavaOne 2007 session was packed the rim but I soon discovered I should have gone elsewhere. Everything in Java has some type of context and JPA is no different, the speaker was talking about the Persistence Context, blah, blah, Entity Manager, something or other. I found this session a bit hard to follow, a bit too technical and really dry. The slides looked like endless paragraphs from Tolstoy’s The Death of Ivan IIyich. A JPA junkie snapped pictures of the slides so that he can decipher and analyze them at a later time, or so I imagined, with the help of Java non the less.

Here are some general rules that I was able to grasp from this session… You need to manage the relationship of the parent and child object explicitly for each object individually. You should try to use left joins to fetch data in your queries. Using the @Version annotation attribute will help to detect parallel updates to a entity. You should use named queries whenever possible because they are cached by the JPA provider. Use fully qualified names for your queries such as ‘Employee.findByName’ to avoid named collisions. Concatenating strings in an attempt to make dynamic queries is not a best practice because it is open to SQL injection. Use named parameters in queries to avoid SQL injection. Try to avoid native SQL queries so that your application can be more database portable. That said, native queries might be required for vendor specific functionality or stored procedures.

Technorati Tags: , , , , , ,


May 28 2007

Using jMaki in a Visual Development Environment

The jMaki project started as a wrapping utility for JS libraries and widgets. The j stands for JavaScript and maki comes from the Japanese word for wrapping, maku. The intent of jMaki is to promote clean seperation between content, style, and javascript and do so in an abstraction layer that lends itself well to a component model. jMaki is a client-server framework with support in PHP, Rails, or Java web applications.

jMaki has nice IDE support in NetBeans with plenty of wizards to get you started. Since jMaki provides a nice component model you can use the visual editor to wire together the UI of a web application in your IDE. Since a big part of web application development is getting the layout right, jMaki comes with several layouts right out of the box, all you do is select the template you want to use from the IDE.

To wrap a custom JavaScript library with jMaki you will need to create a jMaki widget. A jMaki widget uses the convention, instead of configuration, which is basically a folder containing three files, a HTML template, a CSS style, and JavaScript behavior. jMaki already has support for popular JavaScript libraries such as ExtJS, YUI!, Dojo, etc.

Greg Murray stated that he developed jMaki because he “wanted the reload button to be the redeploy button.” Well, since HTML, CSS, and JS aren’t compiled, that is easy to do. One button redeploy is not the stregth of jMaki, what jMaki has going for itself is that you can visual develop a web application using NetBeans and that this wraps several libraries in a easy to use fashion. jMaki also allows you to mix and match different JavaScript libraries together and communicate with each other using Glue. jMaki provides a common ground for integrating multiple of the currently existing AJAX libraries.

There is a jMaki on Rails plugin available and a PHP library.

Technorati Tags: , , , , , , , , , , ,


May 28 2007

Java Puzzlers

The complete name of this JavaOne 2007 session was Java Puzzlers, Episode VI: The Phantom-Reference Menace/Attack of the Clone/Revenge of the Shift. This technical session was presented by perhaps the most effective Java developer Joshua Bloch and fellow puzzler William Pugh. Joshua and William presented 8 short programs with curious behavior. This session was basically a what does this program print interactive question and answer discussion.

The puzzles where like Google interview questions, a bunch of brain teasers which intended to preach a few moral and teach a few tips regarding interesting behavior at the language and JDK level. For example here are some tips I took away from the session, use URI instead of URL because the hashCode and equals method in URL is broken. Don’t mix short, long, int primitives and objects because autoboxing with knock you out. Autoboxing happens, when you least expect it. JUnit does not support concurrency. Wrapped primitives aren’t primitives. Watch out for circular class initialization when you mix statics and constructors, remember static code is process from top to bottom before constructors. If an API is broken, wrap it. For API designers, don’t violate the principle of least astonishment. Math.abs doesn’t guarantee non-negative results, for example Math.abs(Integer.MIN_VALUE) because Integer.MIN_VALUE == -1*Integer.MIN_VALUE. Avoid mixing types in the ?: ternary operator. Use find bugs, acording to Joshua and William Find bugs warned all of the questionable behavior.

Lastly, William said that the “Collections API where designed for what is type-safe, not what is sensible.”

Technorati Tags: , , , , , ,


May 28 2007

Evolutionary Java – General Session

Evolutionary Java was a general session held at noon on the first day of JavaOne 2007. This general session delved into some proposed features for Java SE 7. Some proposed features for Java SE 7 include modularization which will introduce the concept of super package, new byte code and careful evolution of the Java language, and better support for multiple and dynamic languages in the JVM.

During this session there where more demos including one of JRuby on Rails running a standard Rails application, Mefisto. Tor Norbye gave a demo of the new JRuby/Rails support in a beta version of NetBeans with code completion, code highlighting, and wizards for creating new projects. There was a great demo of GL Studio for Java. Basically the demo mashed up Google Earth-like earth simulation provided by NASA Ames and DiSTI 3D models of a realistic F-16 jet fighter. One of the best demo was Iris, a Flickr mashup with some great eye candy.

There was an additional demo and information on JavaFX Script (JFX). JavaFX Script is an object-oriented programming language with a declarative syntax. Think of JFX as a mini-DSL for effects, animations, and rollovers for the JVM. JFX is a programming language especially designed for effects and animations. It is interesting to note that most of the demos of JFX are ports of Flash applications. A common questions amongst those present was, why introduce another language? Why not just a Java-based library?

Technorati Tags: , , , , ,


May 28 2007

JRuby on Rails – Agility for the Enterprise

Charles Nutter and Thomas Enebo, Sun employees and key contributors to the JRuby project, gave the typical introduction into the Ruby programming language, JRuby implementation, and JRuby on Rails. Ruby is a pure Object-Oriented programming language with plenty syntactic sugar for arrays, maps, regular expressions, anonymous blocks, duck typing, and meta-programming. JRuby is a first class citizen in the JVM.

A common theme by the scripting folks, is their common dislike for regular expression in Java and how the leverage the libraries already available in Java. JRuby, in short combines the libraries and power of Java with the syntax and expressiveness of Ruby.

Charles stated that the benefits of JRuby over Ruby include better scaling, will soon be faster, native unicode support, Java libraries, easy adoption in the enterprise environment and existing applications. The benefits of JRuby over Java include Ruby language features and Ruby applications like Rails, Rake, Rave, and RSpec.

Charles Nutter, the lead on JRuby did state the the current focus is ‘compatibility over performance.’ By this, his focus is for JRuby to be equivalent to the C implementation of Ruby 1.8. Ruby does not have an official language specification, the C implementation is as close as you get to a language spec. Charles Nutter has stated that the lack of a official specification as a pain point in the development in JRuby.

There are some JRuby extras on RubyForge, include a Web Application Resource plugin recently renamed to Goldspike. Originally named Rails Intregation, the Goldspike plugin allows you to create a WAR file for a JRuby on Rails project which can be deployed on Tomcat, Jetty, or any other Servlet container. Other interesting projects include ActiveRecord-JDBC and a Java port of RMagick, the popular image package used in Rails applications.

The speakers also mentioned on the precepts and philosophy behind Rails such as Convention over Configuration, Don’t Repeat Yourself (DRY), and agile development. The typical quote around the JRuby on Rails peeps is that there is “less Rails code than Java application configuration.”

There was a question from an audience member about how JRails compares with Grails. The JRuby folks feel that Ruby on Rails currently has more books, more focus, and more developers. Grails was heavily inspired from Rails, and both projects have borrowed ideas from each other. I personally strongly recommend either Grails, and JRuby on Rails over some of the other available web application frameworks.

Technorati Tags: , , , , , , , ,


May 28 2007

JavaOne 2007: Tuesday General Session

John Gage, Sun co-founder and Chief Researcher, kicked off JavaOne 2007 by saying that there were 81-hours until the end of JavaOne. He recommended that we all take the time and meet the fellow developers and engineers sitting to each other at each session. Engineers, as a breed, tend to be introverts but John reminded us to not be shy. He recommended that you forget that you are Swedish, forget you are British, polite, and reserved. He said, “For the next 81 hours you are Brazilian.” To this declaration, the Brazilian contingent raised the roof with their cheers. John said that in that conference hall room are all the authors of every Java book, leader of many Open Source projects, and innovators in just about every front. He recommended that we all take advantage of that fact.

The big announcements at JavaOne 2007 from Sun has been JavaFX Script, formerly known as Form Follows Function (F3). I think of JavaFX Script as a scripting Domain Specific Language for Rich Internet Applications which begs the question, why a new language? Why not accomplish the same functionality using Groovy, JRuby, or a unified and simplified API? Recently Sun also made inroads into the mobile space by purchasing the SavaJe mobile phone technology. At JavaOne, Sun announced JavaFX Mobile. JavaFX Mobile is an iPhone-inspired Java-powered software system for mobile devices. Rich Green, Vice President of the Software division at Sun, said that JavaFX Mobile is some “serious mobile eye candy.”

The general session is also meant to inspire all those present to share ideas, contribute code, and to participate. To this end, Rich Green said, “This is not a read-only life style.” Scott McNealy, former CEO of Sun, made a brief appearance on stage to announce his current project, Curriki, a elementary to high school curriculum wiki. Scott McNealy also had time to joke that Rich Green is a ‘short sleeve version of Steve Jobs.’

Technorati Tags: , , , , ,