Friday, June 15, 2007

JBoss AOP 2.0.0.alpha5 released

The JBoss AOP team have just released JBoss AOP 2..0.0.alpha5, which now contains most
of the core functionality that will go into the GA release. Please download it and
help test it out, and keep the feedback coming via our user forum.
The release zip files from 2.0.0.alpha5 onwards will now be hosted on jboss.org instead of at
sourceforge.net (JBoss AOP 1.5.x will continue to be hosted at
sourceforge.net).



The release contains documentation and an AOP tutorial to quickly get you up and running with JBoss AOP. This release is packaged differently from previous releases.
To help you upgrade to JBoss AOP 2.0.0 in an existing JBoss Application Server instance, we provide Ant scripts under



  • jboss-40-install/jboss-aop-jdk14.deployer/
    (to replace the existing AOP 1.5.x. JDK 1.4 deployer)

  • jboss-40-install/jboss-aop-jdk50.deployer/
    (to replace the existing AOP 1.5.x. JDK 5 deployer)



Simply modify the jboss.properties file in one of these directories to point to the JBoss server you want to patch with the new release, before running Ant.


We obviously support all the features from previous alphas.
The main new standout feature this time is Before/After/Throwing/Finally advices which are more lightwight than the traditional Around advices since we avoid the allocation of
the Invocation object that drives the advice chain in the case of Around advices. A simple example of an around advice:



public class POJO{
public void method(){}
}


<aop><aspect class="AroundAspect"/>
<bind pointcut="execution(void POJO->method())">
<advice aspect="AroundAspect" name="around"/>
</bind>
</aop>


public class AroundAspect{
public Object around(MethodInvocation inv){
try{
System.out.println("Entering aspect");
return inv.invokeNext();
}finally{
System.out.println("Leaving aspect");
}
}

}

The same functionality can be implemented as a pair of Before and After advices:
public class POJO{
public void method(){}
}


<aop><aspect class="BeforeAfterAspect"/>
<bind pointcut="execution(void POJO->method())">
<before aspect="BeforeAfterAspect" name="before"/>
<after aspect="BeforeAfterAspect" name="after"/>
</bind>
</aop>


public class BeforeAfterAspect{
public void before(){
System.out.println("Entering aspect");
}

public void after(){
System.out.println("Leaving aspect");
}
}

We can also access things like the typed target method arguments and return type from the new style advice methods

public class POJO{
public static int multiply(int i1, int i2){
return i * l;
}
}

<aop>
<aspect class="BeforeAfterAspect"/>
<bind pointcut="execution(void POJO->multiply())">
<before aspect="BeforeAfterAspect" name="before"/>
<after aspect="BeforeAfterAspect" name="after"/>
</bind>
</aop>

public class BeforeAfterAspect{
public void before(@Arg int i, @Arg int j){
System.out.println("Multiplying " + i + " by " + j);
}

public int after(@Return int rtn){
//Let's always make the number positive
if (i < 0){
return i * (-1);
}
return i;
}
}

In the above example, if we call

int result = POJO.multiply(3, -10);

The advice method parameters need to be annotated. @Arg ensures that the first argument to BeforeAfterAspect.before() will
be 3, the second will be -10; The @Return annotation ensures that the rtn argument to POJO.after() will
contain the result of calling POJO.mulitply(), so it will have the value -30. The advice will override this value and make this possible
so result ends up being 30.

We also support other arguments to advice methods such as:

  • @JoinPoint - access to the method, constructor or field called

  • @Target - access to the target instance being called

  • @Thrown - access to the exception thrown in an after or finally advice

  • @Args - a mutable array of the arguments passed in to the target jounpoint

  • @Caller - the calling instance when using caller pointcuts




Please see the tutorials that come with the release for more information about how to use before/after/throwing/finally advice.

Thanks to Flavia Rainone for all her hard work on this release, and to all our users for the feedback on the forum. If you are using JBoss AOP, or just have
some time to kill, and you want to help out, get in touch via or design forum.
You can also mail Flavia or me directly.

1 comment:

szimano said...

congartulations ! Glad to see new jboss aop almost fiinshed ;-).

Good to see a blog as well...