All | General | Java | J2EE | JUnit | Eclipse | Security
Tuesday February 14, 2006

Adding metadata to Spring beans. / Java : 21:12 EST

Previous post reminded me some interesting use cases for when code runs in the managed environment. Containers like Spring allow to wire any classes and even apply interceptors to them. Unfortunately such configuration is not very reusable since interceptor definition has to be coded for each and every bean. When you own the actual code you can add some metadata (e.g. Java 5 annotations) and would be discovered in the run time and allow container to apply an appropriate advices. However that won't work if there is no code or the same bean reused several times and require slightly different metadata for each instance.

I think it would be really neat to be able to define such metadata in Spring config and then retrieve it trough the BeanFactory for that bean. Then factory post processors could automatically wrap those beans into proxy with preconfigured interceptors. This could be also used to define scoped beans that Bob Lee mentioned another day.

Bean definition could look something like this:
  <bean id=""... >
    <ann class="Async"/>
    <ann class="Remote">
      <property name="name1" value="something"/>
      <property name="name2"><value><ann class="..."/></value></property>
The above definition could reuse similar semantics as bean definition and actual data could be available trough dynamically created proxies for the annotation interfaces (or even regular Java 5 annotations). It should not be quite easy to implement and if you like the idea please vote for SPR-1711.

BTW, some time ago I blogged about adding annotatations/metadata to 3rd party classes. That time Alex Vasseur suggested to use AspectJ 5 to introduce annotations and that was more or less acceptable solution for me.

Transparent transactions, persistence and clustering on a standard JVM. / Java : 13:10 EST

Vincent van Beveren blogged about Uber JVM, the one that could provide transparent clustering, transparent persistence and transactions. In his eyes it could look like this:
public persistent class Account {
  public void transfer(double amount, Account toAccount)
      throws AccountException {
    transaction {
      try {
        amount = withdraw(amount, "Transfer to " + toAccount);
        toAccount.deposit(amount, "Transfer from " + this);
      } catch (CreditException ce) {
        throw ce;
Would be nice, isn't it? Though I think it is not really good idea to put such stuff into the language. Besides, the metadata introduces in Java 5 (and its surrogate JavaDoc-based replacement for earlier JVM's) has been used to implement such features in a more generic way. Basically you could annotate Account class with @Perstist annotation and transfer() method with @Transactional annotation, so infrastructure could use those hints to behave accordingly. This is quite easy to implement using one of the existing AOP frameworks, such as AspectWerkz or AspectJ. The only trouble is that it will work only down to the method and you could not apply such aspects on something under method.

I've blogged about this while back and tried to propose to allow annotations with CLASS retention policy for any code block, but it doesn't seem there was much interest.

As of clustering, there are several products that allow to make it transparent for the application code and still run it on a standard JVM. One of them, Terracotta Distributed Shared Objects is extending semantics of synchronized blocks to support cluster-wide locking.

Sunday February 12, 2006

Short and wrong way to define class / Java : 19:40 EST

Matthias Ernst is challenging everybody and asking if you can write shorter code to define class.

Well, his solution maybe short, but it has a glitch. Basically it defines a class in a scope of the temporary classloader which is immediately thrown away, so there could be some nasty issues if other code with different class loader scope would try to use this class. Also, this way you can't define several dependent classes. So, the right way to define class is to do it on the current classloader instance, and unless it is your own classloader you'll have to use reflection to call protected defineClass() method.

Friday February 10, 2006

Sun developers are moving to IDEA / Java : 15:12 EST

Romain Guy (the Swing fame) is now using IDEA and think that it is really cool. Heh, too bad for Sun's own IDE product. I believe that the only way to make some tool really great is to use it for your own activities. Well, Romain is not part of the NetBeans team, but it is still not a good sign that he prefer Eclipse and IDEA for things he is working on.

Wednesday February 08, 2006

Distributed TestNG is not quite distributed yet. / Java : 19:05 EST

Cedric Beust explained new TestNG feature for distributed testing. Apparently current implementation assumes that slave test runners have all classes in the classpath. So, it may help to speedup some long running test suites, but nearly useless for development testing.

It would be really neat to have local project code incrementally replicated to the slave test runners including all the classes, dependencies and required resources, so it will be completely self configuring. Especially if there will be some mechanism to authomatically resolve existing slaves without explicitly configuring their hosts.

Another thing that could be really handy is to use existing continuous build servers as a slave test runners. I believe Continuum has multi-server support since recently, so it could be interesting to hook into it.

Wednesday February 01, 2006

MDB, AOP and Spring / J2EE : 11:25 EST

BEA dev2dev just published an article about non-XA messagging with MDB and Spring we wrote with Dmitri. We are using Spring AOP framework to implement loosely coupled components, which provide once-and-only-once processing quality of service when assembled together.

Tuesday January 31, 2006

It seems Bob Lee don't get transactions either. / General : 08:00 EST

Bob Lee somehow got frustrated with Spring framework. In comments to that post he went even further:
Why don't people just start and stop a tx in a servlet filter? Some say that would be too coarse grained, but is that a real issue, or are you just optimizing too early? I don't see how you could get any simpler than mapping a filter and forgetting about txs until the performance becomes an issue or you really need to think about them (if you're using JMS for example).
I am sorry to say that idea to set transactional boundaries in servlet filter has several holes. First of all this is explicitly prohibited by J2EE (you should NOT manipulate transactions in filters). But even if it wasn't, there are lot of applications that have no servlets or require fine grained transaction control. I don't want to get into the argument if Spring is good or bad, but its AOP features allow to easily apply transactional boundaries to practically any component managed by Spring. That of course could be done with other AOP frameworks, but none of them as easy as Spring to use and deploy.

Monday January 30, 2006

Detecting crypto algorithms. / Security : 11:00 EST

Ilfak Guilfanov posted an interesting entry about detection of the crypto algorithms used in application code. Though, with Java, in most of the cases you would just check if application is using one of the popular frameworks like javax.crypto or Bouncy Castle Crypto API and then search for string literals representing known algorithm names. However, Ilfak's approach would work for an obfuscated code that is using custom or inlined crypto library (unless these constants are also obfuscated).

Looking trough the glass fish. / General : 08:00 EST

This weekend I drove to Monteray Bay and stopped at the Aquarium. It is amazing how some fishes can hide right on the open space.
Actually, that is not a glass fish, but its transparency trick probably work even better.

Monday January 23, 2006

When not to use switch statement / Java : 10:15 EST

Christian Stein wrote an entry about generating switch statements for enums. Basically he came up with the following:
enum Letter {
  A, B, C, ... Z
class OneMinuteIntro {
  Printer printer = Escape.compile(Printer.class, this);
  public void print(Letter letter) {
  public void printA() {
  public void printZ(letter) {
    System.out.println("Zet #" + letter.ordinal());
In above code Escape.compile() will generate something like this:
public void print(Letter letter) {
    switch(letter) {
      case A: printA(); break;
      case B: printB(); break;
      case C: printC(); break;
      case Z: printZ(letter); break;
It look cool from a first glance, but there is a simple way. Because enum is regular class (which implicitly extending java.lang.Enum) you can write something like this:
enum Letter {
  A {
    public void print() {
  B {
    public void print() {
  Z {
    public void print(letter) {
      System.out.println("Zet #" + ordinal());
  abstract void print();
You may say that this is more verbose then version with the switch and there are anonymous class generated for each enum value, but you can't argue that this way all Letter behaviour is encapsulated in the right class.

Switch is very handy when you don't want encapsulation or when you need heavily optimized code, but this can't be combined with dlegation. Generally it is a bad idea to use switch.

Changing job / General : 10:00 EST

Friday was my last day on the team I've been working with for over 2 years. The project was great success and I got lots of best wishes from coworkers. Well, it was time for me to go and for next two weeks I am at San Francisco starting at new job, which I am very exited about.

PS: by the way, if you know the city and area, I'd appreciate some advise about sightseeing.

Wednesday January 18, 2006

Inverting inversion of control / J2EE : 12:27 EST

One of the recent blog posts reminded me idea I come with while writing an article about integration Spring and EJB. I haven't actually had a chance to implement it but created issue in Spring Jira. The idea is to create special factory bean (perhaps very similar to FieldRetrievingFactoryBean), which would take child components declared in that bean and inject them into the caller. Then developer can configure this factory in Spring context and then call it from ejbCreate() method.

Since Spring already has convenient abstract classes for implementing EJBs this logic can actually reside in those classes and will allow developer to enjoy inversion of control on regular EJB and configure their dependencies declaratively.

Sunday January 15, 2006

Beware of free tips / Eclipse : 22:49 EST

JavaLobby published an Eclipse tip which is suggesting to manually edit Eclipse's .classpath to reorder spource folder and libraries in Package Explorer view. I was really surprised to see such suggestion from somebody who used Eclipse for longer then one week. Actually there is an UI to reorder project layout and this functionality has been there for ages.
From other hand you wouldn't need to worry about this at all if you use Mylar.

Friday January 13, 2006

Eclipse compiler supports Java6 / Eclipse : 12:07 EST

Olivier Thomann committed support for Java 6 StackMapTable attribute for Eclipse compiler. That make possible to build classes that will be verified by Type Checking Verifier.
This cannot be activated in the UI yet since there is no compliance 6.0 available in the compiler preference page. However the batch compiler now supports -1.6 to enable the 1.6/6.0 mode.
Soon it will be available in nightly and integration builds and will also go into Eclipse 3.2M5 (due to Feb. 17, 2006).

Thursday January 12, 2006

UseThreadPriorities and other related JVM flags. / Eclipse : 13:48 EST

In some cases JVM threads There are few options in JVM to control thread priorities. Though these options are not very well documented and the most information you can find is JVM bug reports 4910535 and 5101898 and Eclipse bug 118335.

So, if you are running Eclipse on JVM earlier then 1.5.0_06 it is a good idea to use the following JVM options (e.g. add them into eclipse.ini):

Thursday December 22, 2005

Using the Spring AOP Framework with EJB Components / Java : 11:26 EST

BEA dev2dev portal published my article "Using the Spring AOP Framework with EJB Components". Though this may not be the most common usage of the Spring AOP and the Spring Framework itself I've tried to illustrate how to apply basic AOP principles on existing business application. The same principles can be used for non-EJB components and even in non-web applications. Article also explain how custom AOP components (e.g. advices) can be tested in isolation, which makes test cases much more manageable.

Friday December 09, 2005

Silent releases. / Eclipse : 21:19 EST

Today Eclipse Web Tools Platform (WTP) team quietly published 1.0RC2 build and there is only one week left before 1.0 release.

Another exiting Eclipse plugin release, kudos to Spring IDE team, happens yesterday. Live from The "Spring Experience" the Version 1.2.4 of Spring IDE has been released. It require WTP 1.0RC1 and I wonder if there are know issues with WTP 1.0RC2...

Update: it looks like Spring IDE 1.2.4 does work with WTP 1.0RC2, so given that few other plugins somehow painful to run with Eclipse 3.2M3 (primarily because not all plugins, including AJDT, have update sites for 3.2 streams) I may consider to downgrade to Eclipse 3.1.1. How sad it is... and I even scared to think what will be broken in 3.2M4 release next week.

GlassFish had been ASMified. / Java : 13:18 EST

Recent interview with GlassFish persistence developers confirmed that they will use Oracle TopLink for EJB3 persistence engine. TopLink is using ASM bytecode framework to weave in necessary transformations into the users classes.

By the way, Hibernate is using ASM as well, but indirectly trough CGLIB.

If your project also benefit from features and simplicity of the ASM framework, please let me know, so we can add it to the ASM users page along with AspectWerkz, Groovy, BeanShell, CGLIB and other products from several vendors including BEA, IBM, Terracotta and others. I am aware of dozen tools and frameworks that are using ASM, but not listed on this page and it would be nice to see them up there.

Thursday December 08, 2005

What will be the next iPod? / General : 09:57 EST

Well, we had iPod shuffle, nano, photo and now there are iPod that is capable to play video. Some people claim to be good in predicting what the next big thing from Apple is going to be. It seems there is not much left now. How about iPod "vibro"? ;-)

Disclaimer: I've never been big fan of Apple stuff, neither own anything from the iSomething line.

Wednesday December 07, 2005

Show me the variables / Java : 12:12 EST

Bytecode waving is a powerful technique that gained popularity in a last few years. Well known tools are using it with great success, that include Hibernate, CGLIB, AspectWerkz, AspectJ, you name it...

Unfortunately, in many cases these tools introduce additional challenge to developers who need to debug an instrumented code. Waving adds an additional behavior that is not shown in the source code and it may also add additional state that is hard to trace, especially if it is stored in method variables. To solve this, instrumentation could add a debug logging, but it would be additional overhead on the bytecode size, instrumentation and runtime performance. However, with very little effort waving tools could make debugging of the instrumented methods with new variables much easier.

Basic idea is to create debugging information about new variables, so debugger would be able to match it with the actual state and even if those variables are not in the source code user will see them nicely in the debugger.

Let's say we have instrumentation that adds some bytecode equivalent to the following Java code at the beginning of every method.
  long time = System.currentTimeMillis();
Here is how such waving could be implemented with ASM framework. Note that it is using LocalVariablesSorter from commons package to make it easier to add new variable added at the beginning of the method and shift existing variables.
  public static class NewVariableMethodAdater 
        extends LocalVariablesSorter implements Opcodes {
    private Label timeVarLabel;
    private int timeVar;
    public NewVariableMethodAdater(MethodVisitor mv, 
          int access, String desc) {
      super(access, desc, mv);
    public void visitCode() {
      timeVar = newLocal(2);
      timeVarLabel = new Label();
          "java/lang/System", "currentTimeMillis", "()J");
      mv.visitVarInsn(LSTORE, timeVar);
    public void visitMaxs( int maxStack, int maxLocals) {
      Label endLabel = new Label();
      mv.visitLocalVariable("time", "J", null, 
          timeVarLabel, endLabel, timeVar);
      mv.visitMaxs(maxStack, maxLocals);
Now if you set breakpoint in the source file and then run an instrumented class you would see time variable in the debugger and can inspect its value.

« February 2006

Recent Comments

  • [2] Short and wrong way to define class
  • [9] Sun developers are moving to IDEA
  • [8] Distributed TestNG is not quite distributed yet.
  • [1] It seems Bob Lee don't get transactions either.
  • [1] When not to use switch statement
  • [5] Changing job
  • [7] Beware of free tips
  • [1] Using the Spring AOP Framework with EJB Components
  • [2] Silent releases.
  • [3] GlassFish had been ASMified.
  • [2] What will be the next iPod?


  • Today's page hits: 398

  • G1 FAIL - Application at context path /test could not be started in tomcat
  • G1 configure.cvs call of duty%26hl=es%26lr=
  • G1 The ipod cannot be updated. The disk could not be read from or written to.
  • G10 commons-lang "target release 1.1 conflicts with default source release 1.5"
  • G1 "maven site.xml"
  • G1 Schema text failed: could not instantiate id generator
  • G1 The Ipod "ipod" cannot be updated. The disk could not be read from or written to
  • G1 "Error 503: Failed to load target servlet" ldap
  • G1
  • G1 myeclipse cannot build class files
  • G13 call of duty error bad key code
  • Y The iPod cannot be u
  • G13 how to call helper class and methods from helper class in JSP
  • G1 spring context read text file
  • G1 Cactus error Failed to load test suite
  • G1 eclipse.ini JVM parameter -encoding
  • G1 DispatchAction getResource
  • G1 "cannot be updated" "the disk cannot be read from or written to"
  • G1 Installation Instruction for BouncyCastle
  • G1 Unable to initialize the CVS process: The system cannot find the file specified.
  • G1 NitroX Studio 3.0 M1 down
  • G1 WTP Publishing Performance
  • G1 maven2 buildnumber manifest
  • G1 pojo in action pdf
  • G1 "cannot be resolved to a type" JSP java bean
  • G1 jbuilder junit4 support
  • G9 ant javac: invalid target release: 1.5
  • G1 INTERNAL ERROR on Browser End: Could not get the plugin manager
  • G1 counter strike source g15 plugin
  • G1 "maven site.xml"
  • G1 the iPod cannot be updated. The disk could not be read from or written to
  • G1 the ipod could not be updated. the disk could not be read or written to
  • G9 call of duty error 1628
  • G1 IOError: File not found - -gui: The system cannot find the file specified wlst
  • G1 cannot be resolved. It is indirectly referenced
  • G1 "the :extssh: access method is not installed on this system"
  • G1 ICONST_4
  • G1 sun/security/provider/SHA oracle
  • G1 "sun.text.resources.LocaleElements" javadoc
  • G1 cvs error Unable to initialize the CVS process: The system cannot find the file specified
  • G7 eclipse junit breakpoint fork
  • G6 taglib identification source
  • G1 configuring Myeclipse with Weblogic
  • G5 illegal key size jdk1.5
  • G1
  • G1 mdb multitreaded
  • G1 Cobertura Eclipse Plugin
  • G1 Counter-Strike 1.6 patch 21
  • G1 Billy Newport API-less
  • G1 eclipse "key scheme" idea intellij
  • G1 Syntax error%2C annotations are only available if source level is 5.0
  • G1 "Head First" "JAVA books" download ;pdf
  • G1 myeclipse weblogic j2ee classpath
  • G1 reading pgp keys and converting it to xml in .net rsa
  • G1 "Unable to locate JRE meeting specification"
  • G1 unsing XML Documentation C 2005
  • G1 Tomcat "FAIL - Application at context path could not be started"


  • Login
  • Technorati

This work is licensed under a Creative Commons License.
Java and the Java Get Powered logo are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries.