From: matrix of open source technologies: neo translation [2003/09/27]
Access Metadata In order to use metadata, it must be up to run. The metadata of the class is accessible through the Invocation object. In order to use it in our example, TracingInterceptor must modify a little bit.
Public Class TracingInterceptor Implements Interceptor
{
Public string getname () {return TracingInterceptor;
Public InvocationResponse Invoke (Invocation Invocation)
Throws throwable
{
String filter = (string) Invocation.getMetadata (Tracing, Filter);
IF (Filter! = null && filter.equals (true))
Return invocation.invokenext ();
String message = null;
IF (Invocation.gettype () == InvocationType.Method)
{
Method method = methodInvocation.getMethod (Invocation);
Message = Method: method.getname ();
}
ELSE IF (Invocation.gettype () == InvocationType.constructor)
{
Constructor c = constructorinvocation
.getconstructor (invocation);
Message = constructor: c.toString ();
}
Else
{
// do nothing for fields. Just Too Verbose.
Return invocation.invokenext ();
}
System.out.println (Entering Message);
// Continue on. Invoke The Real Method or Constructor.
InvocationResponse RSP = Invocation.Invokenext ();
System.out.println (Leaving Message);
Return RSP;
}
}
Operation Example 2: The POJO class will extends a little, increase the GET () and set () methods.
Public Class Pojo
{
Public pojo () {}
Public void helloworld () {system.out.println (Hello World!);
Private int counter = 0;
Public int getCounter () {return counter;}
Public void setCounter (int val) {counter = val;}
Public static void main (string [] args)
{
Pojo pojo = new pojo ();
Pojo.helloworld ();
Pojo.setCounter (32);
System.out.println (Counter is: Pojo.getCounter ());
}
}
TracingInterceptor will intercept main (), pojo (), and helloworld () calls. The output should look as follows: Entering Construction: Public Pojo ()
Leaving constructor: Public Pojo ()
Entering Method: HelloWorld
Hello World!
Leaving method: HelloWorld
You can download JBoss AOP and ion code here. Compile and execute:
$ CD OREILLY-AOP / EXAMPLE2
$ export classpath = .; jboss-common.jar; jboss-aop.jar; javassist.jar
$ javac * .java
$ java -djava.system.class.loader = org.jboss.aop.standalone.
SystemClassLoader Pojo
Example 3. Urban introduction If we can turn off and open for a specific instance, it will be cool. The JBoss AOP has an API that binds the metadata to an object instance, but let us disguise an actual tracking API is a better solution. In this case, we will change the definition of the Pojo class by using a note. We will force the POJO class to implement a tracking excuse and provide a mixed class, which processes the new tracking API. This will be tracking excuses:
Public interface Tracing
{
Public void enabletracing ();
Public void disabletracing ();
}
Define a mixed class Tracing interface will be implemented in the mixing class. When a POJO is an example, a mixed object mix class will bind to the Pojo class. The following is true:
Import org.jboss.aop.advised;
Public Class TracingMixin IMPLEments TRACING
{
Advised advised;
Public TracingMixin (Object Obj)
{
this.advised = (advised) OBJ;
}
Public void enableTraacing ()
{
Advised._getinstanceAdvisor (). getMetadata (). AddMetadata
"Tracing", "Filter", True;
}
Public void disableTraacing ()
{
Advised._getinstanceAdvisor (). getMetadata (). AddMetadata
"Tracing", "Filter", False;
}
}
EnableTraacing () Method Binds the Filter property to the object instance. The DisableTraacing () method is the same, but the Filter property is developed to false. These two methods are how metadata can be used for more than one class. Metadata can also instance level applications. Metadata application in instance level. Bind a bet, so we define the trace interface and implement this mixing class. The next step is to apply the introduction to the POJO class. Like interceptors, we must define a Ponitcut in XML. Let us look at what this item.
XML Version = "1.0" encoding = "UTF-8">
mixin>
Introduction-Pointcut>
aop>
The above POINTCUTS will force the POJO class to implement the TRACING interface. Now, when a Pojo instance is initialized, a TracingMixin will also be instantiated. TracingMixin is initialized to be defined in the label. You can put the Java code you want to place in the label. Operation Example 3 Pojo Class In order to display TracingAPI to be accessed, it has been expanded. TracingInterceptor is still the same as the example 2.
Public Class Pojo
{
Public pojo () {}
Public void helloworld () {system.out.println (Hello World!);
Public static void main (string [] args)
{
Pojo pojo = new pojo ();
TRACING TRACING = (Tracing).
Pojo.helloworld ();
System.out.println ("Turn Off Tracing.");
Trace.disableTracing ();
Pojo.helloworld ();
System.out.Println ("Turn on Tracing.");
Trace.enableTracing ();
Pojo.helloworld ();
}
}
Note We convert the Pojo to the Tracing interface. The output should look like this:
Entering Construction: Pojo ()
Leaving constructor: Pojo ()
Entering Method: HelloWorld
Hello World!
Leaving method: HelloWorld
Turn OFF TRACING.
Entering Method: DisableTracing
Leaving method: disableTracing
Hello World!
Turn on traacing.
Entering Method: HelloWorld
Hello World!
Leaving method: HelloWorld
Note that Interceptor-Pointcut added to TracingInterceptor is also applied to those methods imported through Tracing. In order to compile and run this example:
$ CD OREILLY-AOP / EXAMPLE3
$ export classpath = .; jboss-common.jar; jboss-aop.jar; javassist.jar
$ javac * .java
$ java -djava.system.class.loader = org.jboss.aop.standalone.
SystemClassLoader Pojo
Conclusion Aspects of programming is a powerful new tool for software development. In order to make your software development process more dynamic and smooth, you can implement your own interceptors, metadata and inscriptions. See the site www.jboss.org more detailed documentation.