Tag Archive for 'java'

EJB 3.0 Annotations Cheat Sheet - Reference 1.2.2

The sheet got it’s own url here.

Minor update: added @AssociationOverride from Java EE 5 API documentation. Thanks to Jonathan O’Connor for catching this one.

Just download it from here as pdf or OpenOffice Document.

EJB 3.0 Annotations Cheat Sheet - Reference 1.2.1

The sheet got it’s own url here.

Updates to the EJB 3.0 Annotations Cheat Sheet.

Changes

  • Title changed to EJB 2.1 and Ealier Client View
  • Update @RemoteHome and @LocalHome according to the latest spec.
  • Fixed spelling and typos (NamesQueries / SINGLE_TABLE)

Thanks to Jonathan O’Connor for pointing out this errors.

Just download it from here as pdf or OpenOffice Document.

Preview of (old) cheat sheet.

Interruptible RMI - Working with JBoss?

Neil O’Toole has a RMI Socket- and ThreadFactory which enables a RMI client to interrupt a RMI invokation. This seems like a nice addition to a fat swing client which accesses a JBoss server. What I wonder is if the library will be compatible with JBoss and EJB 3. Have to try out. More information available at Neil’s blog entry.

EJB 3.0 Annotations Cheat Sheet - 1.2

Update: The Annotations Cheat Sheet got it’s own page here

After a longer hibernation I found the time to update the EJB 3.0 Annotations Cheat Sheet. I hope I catched up with all the recent changes to the spec up to the proposed final draft. I’ll appreciate any error or suggestion that is reported to me.

Just download it from here as pdf or OpenOffice Document.

Preview of (old) cheat sheet.

Refactor-Safe passing of method names for callbacks

Reflection is a very helpful tool for decoupling and avoiding marker interfaces. A common pattern for registering callbacks at the obersever pattern makes use of passing the callback method name as a String to a Observable:

The Observable class uses reflection to extract the method “onEvent” at runtime and will invoke it to notify Observable that an event occurred. This approach shows several advantages over the use of marker interfaces like java.util.Observer. First, no marker interface is needed, which decouples the code and depending on the effort put into the observer, the callback method must not be defined as strictly. E.g. the observer could even handle a signature like “void onEvent()” and simple do not pass any argument to the method.

Drawback of this approach is that the method name is passed to the Observable as String which makes refactoring your code harder and leads to nasty runtime errors. There is not check if the methods signature matches the signature which Observable expects. These errors won’t occur until the Observable will try to make a callback to the Observer.

Inspired by EasyMock 2.0 I found a way to extract a method name at runtime without invoking the method. Create a proxy with the Cglib bytecode manipulation library which records the method called on the proxy. Later on, the method can be used to register an Observer. I made the following, experimental code with Proxytoys and Cglib.

All small test shows how it should work:

As you can see, this one is refactor-safe because the name of the method isn’t used anymore.

The full power of this with the oberserver pattern:

I must admit that there is another solution to the problem of callback interfaces using anonymous inner classes, too:

This implementation which abstains of making Bar implement any Observer interface, suffers from the famous inner-class-breaks-security-problem (see entry at c2 wiki). The reflection based solution above uses an inner class only the extract the method name.

More stuff to read:

[updated and fixed broken comment form]