Archive for June, 2008

Eclipse Ganymede is coming

Sunday, June 22nd, 2008

Some time ago I write about next Eclipse version called Ganymede. Also some time ago I have switched to Mac and InteliJ IDEA. The reasons was that eclipse has poor dynamic languages support and also SWT was far from native cooperation with Mac Cocoa.

First at all I read all of new features eclipse will provide. Next I downloaded version for j2ee development and start to look at new features. Good news eclipse SWT going to native Mac Cocoa support, even Command+F5 reads eclipse screen and reads all the code I have in active windows. Maybe it is not important for programmer, but we are able to write SWT application with such accessibility support. Keep going Eclipse, nice work.

Eclipse was always on the top having so much editing possibilities. Now they added very nice breadcrumb so you press Alt+Shift+B, on Mac the shortcut is Alt+Command+B. You see nice breadcrumb which allows you to quickly navigate, depending on which step you use, we can switch between methods, class in the same package, packages, directories.

breadcrumb

Items counts depends on the position where you press shortcut. One thing to mention is that it is experimental and from my point of view there is a minor bug, because eclipse shows everything on the disk so in my situation is .svn directory and also .DS_store file.

After switch from eclipse to InteliJ I miss for quick assist Ctrl+1, when I found it Alt+Enter (truly my colegue told me) I think it is one of coolest feature in IDE. Ganymede adds some more quick fixes. And of course on Mac it is Command+1 ;)

  • Create getter and setters for fields

getters_setters

  • Expression extract to method
  • Expression extract to variable
  • Convert to StringBuffer

quick_fix

In InteliJ I really like format modified lines, I can set up on committing files, or call it when I want. Now Eclipse has the same feature, It’s a pity it has no shortcut but as you find this attractive you can setup it.

It also good to point that rulers now are more configurable and you can configure it by right click on it

rulers

And some others improvements

  • There is new eclipse java compiler, it now much quicker on multi core machines as my Mac ;)
  • JUnit shows now execution time, which I found very nice feature, because I can focus on test which takes to much time.
  • Quick find occurrence Ctrl+Shift+U (of course on mac it is Command+Shift+U), depending on context it shows search form, or if only one search is possible it fires this search. Very nice feature. A one thing to mention is that there is possibility to choose different colors for write and read.

find_occurence

  • Close tabs with middle click, I use middle click a lot at work in Firefox, but on Mac I haven’t middle click at all ;)

And last but not least is DLTK that means Dynamic Languages Toolkit subproject, and I must look closely at it ;) . In my opinion Eclipse always was on the top and new Ganymede is great. I’ve got now hard days, because I’ve got from InteliJ my own License.

Little Snitch

Tuesday, June 17th, 2008

On my Mac I’ve got firewall so I should fill secure. But as we know there are so much application which sends data from our computer thought firewall. I found very nice application called “Little Snitch“. It’s cost just 29.95$ and you can evaluate it for free, there is only one problem application stops every 3 hours and than you can restart it.

Installation is quick, but after installation is finished restart is required. After restart my machine starts for very very very long time, but finally it has started. Little Snitch has predefined rules and this rules is good start.

LittleSnitch default rules

It also has nice monitor which shows current use of network.

LittleSnitch Monitor

Very nice and useful application for person who wants to make his mac more secure.

Java Specification – chapter three

Sunday, June 15th, 2008

I’ve just started to read Java Specification, and I suggest every java programmer should read it. I try to write some interesting facts in this post and future posts. In first two chapter I didn’t found anything interesting. I almost pass over chapter three, but fortunately I found some interesting information on the first page.

  • Programs are written using Unicode. Unicode version depends on java version and this match
    • Prior Java 1.1 – Unicode 1.1.5
    • Java 1.1 – Unicode 2.0
    • Java 1.1.7 – Unicode 2.1
    • J2SE 1.4 – Unicode 3.0
    • J2SE 1.5 – Unicode 4.0
  • Programs are written using Unicode, and as we know we rather code our programs in ASCII. There is a translation to Unicode. In this process every unicode escape \uXXXX where XXXX is hexadecimal value is converted to proper unicode character.
    • Note that \\uxxxx is not unicode escape and produce “ \ \ u x x x x”.
    • Note that after \ there can be more u character than one !!!. So \uuuuu005a produce Z character
    • Note that \u005c (character \) is not interpreted as further unicode escape. So \u005cu005a produce \ u 0 0 5 a characters. And produce compiler error !!!, because u is invalid character escape.
  • Unicode escapes are processed very early, it is not correct to write \u000a – linefeed, because in next step it will be treated as line terminator. The same with other special characters. We should use for them character escape (\r \n )
  • It good to know that white space is “space”, “horizontal tab”, “form feed” and line terminators LR and CR.
  • Unicode provides to use national characters to name variables. A character for first identifier letter can be any character for which Character.isJavaIdentifierStart return true. And for another part Character.isJavaIdentifierPart returns true.
  • Two identifiers are the same if they are identical that means that every character have the same unicode code. Be careful the same letter can have many unicode codes, so they are different – aVar is different aVar because first a can be LATIN_SMALL_LETTER \u0041 but second a can be CYRILLIC_SMALL_LATTER \u0430 which both look like ‘a’.
  • const and goto are reserved keywords which is not use at all, and in opposite true, false and null is not keywords.
  • L is suffix for long literal and is preferred from l because l is similar to 1.
  • It’s strange but the largest positive literal for int type is 2147483647 and 2147483648 can be only with negation operator. Otherwise compiler raise error “integer number too large” . The same situation is with long but the value is much larger 2^63.
  • If you want to know how java converts unicode floats and doubles values you should read Double.valueOf, Float.valueOf
  • Compile time error occurs when rounded conversion to internal representation becomes infinity. A program can represents infinity as 1f/0f or -1d/0d expressions or predefined constants POSITIVE_INFINITY and NEGATIVE_INFINITY in class Float and Double.

The last part was about String literals. To see unique instances use String.intern. And the rules for that are:

  • String literal represents always the same String object
  • String computed by constant expressions are computed at compile time and than treated as literals so “ala” + “ola” represents the same object as “alaola”
  • Strings computed by concatenation at run time are newly created and are different.

JPA vs Hibernate

Sunday, June 8th, 2008

As already everyone of you know, when you google “jpa vs hibernate” you find so much articles which of them are better. In our company we investigate which orm is better.

First at all, both ORMs now use xml mappings and annotations, deployer can override all of the properties, in jpa it is more convenient because all are do in one file. There are nearly the same. Going further hibernate is strict JPA, and provides some more extensions.

What’s more in Hibernate

  • Hibernate has more primary key generators.
  • Hibernate is no depend on entity classes so is strict POJO
  • Hibernate has more control over cascade behavior with Cascade.DELETE_ORPHAN and others.
  • Indexed collections
  • Criteria for nice query building.
  • Hibernate is JPA with EntityManager subproject.
  • Optimistic Locking strategy (OptimisticLockType.VERSION, OptimisticLockType.NONE, OptimisticLockType.DIRTY, OptimisticLockType.ALL)
  • BatchSize annotation sets batch size
  • Immutable annotation allows do some optimizations.
  • Filters and Filter annotations to apply on data
  • Cache annotation to define cache strategy and region of the cache

And some more, you can find it in hibernate annotation project documentation.

What’s new in JPA 2.0

JPA 2.0 (JSR 317) will be part of Java EE 6 platform. Although JPA 1.0 is rather great success, other products has better functionality as we write before.

  • Orphan removal – In parent-child relationship there can be a situation when child has no parent. One reason can be that parent was removed, so then every child is also removed, another situation is when child pointer to parent becomes null than this child should be removed.
  • Ordered collections – New annotation will be added to manage such collections. @OrderedColumn will automatically do all the stuff
  • Access Model – We can change access by @Access annotation, so now we can override access type both FIELD and PROPERTY in every point of hierarchy.

Some strategies

  1. Common properties such as id, uuid, audit data, can be put in some kind of base entity and mapped as MappedSupperclass. If for some reasons you have to redefine property you can do it using AttributeOveride annotation.
  2. Another good point is to create base dao using generics and use it to common task such as getById.
  3. One more is about audit data, and fields such create_date, last_update etc. For this propose you can use base dao or interceptors. Both solution has it’s pons and cons. For simple expectations dao is better approach.

Summary

In my opinion the best option is to use JPA whenever it is enough. Decide what JPA implementation you will be using, and when JPa isn’t enough use this JPA extension.
Spring Framework (TopLinkDialect, OpenJpaDialect, HibernateJpaDialect) supports changing JPA providers but I think you will never switch between them.
And what do you think about it?

Regards
Pedro

Decorators by Python

Monday, June 2nd, 2008

Lately, I’m using Django. So I also learn Python. And I found that python is very clean and nice language.

As we know Python allows that function can also be a parameter to another function. So code similar to this is ok.

def decorator(fun):
print fun.__name__
def my_fun():
print “hello world”

decorator(my_fun)

But Python provides some syntactic sugar so the code above is the same as this code:

def decorator(fun):
print fun.__name__
@decorator
def my_fun():
print “hello world”

So why it is so important, because I’m Java programmer and this allows me in elegant way to use something similar to Aspect. For example I can write cache decorator and apply it to every calculation intensive function.

class cache:
def __init__(self, function):
self.function = function
self.cache = {}
def __call__(self, *args):
try:
return self.cache[args]
except KeyError:
self.cache[args] = self.function(*args)
return self.cache[args]

Now for our propose I use a non optimal fibonacci recursive function.

def fib(n):
if n > 1:
return fib( n -1 ) + fib ( n -2 )
return 1

And simply measured that using enough large fibonacci number.

from time import gmtime, strftime
print “started %s” % strftime(“%a, %d %b %Y %H:%M:%S”, gmtime())
print fib(35)
print “ended %s” % strftime(“%a, %d %b %Y %H:%M:%S”, gmtime())

With and without @cache decorator. The output is

With @cache:

started Mon, 02 Jun 2008 20:32:41
14930352
ended Mon, 02 Jun 2008 20:32:41

and without:

started Mon, 02 Jun 2008 20:33:09
14930352
ended Mon, 02 Jun 2008 20:33:19

The conclusion is that it is great possibility to use such a class/function as I’m used to when using aspect with java.

about me

My name is Sebastian Pietrowski. I've finished Warsaw University as Master degree. I started my journey with Java 1.1 with Thread and JDBC programing in 1998 as I worked for merlin.pl. In 1999 I've passed Java Programer Certificate for Java 1.2, and was solution architect of merlin.pl infrastructure when we was moving from pl/sql to J2EE. It was great performance optimization with 10 times more req/sec than in requirements and 85 times faster as original solution.

Currently I work as Expert Software Development Java at F.Hoffmann-La Roche. The company was founded in 1896 and today, Roche employs over 80.000 people. After work I'm involved in activities related to Scala/Lift, Ruby/Rails/Merb, Python/Django. This is because I try to be pragmatic also I'm focused on application performance and tuning with success in my daily work.

My Yoda's motto: Do, or do not. There is no try.