FTP Online
Search:
Locator+ Code:
FTPOnline Channels Conferences Resources Hot Topics Partner Sites Magazines About FTP RSS 2.0 Feed

Make Your Applications Operations Friendly With AOP (Continued)

JBossAOP
JBoss, creator of one of the most advanced J2EE servers in the industry, also developed an AOP interface for the JBoss server. JBossAOP works in a fundamentally different way from AspectJ. JBossAOP performs code weaving in the classloader rather than the compiler. Also, aspects are defined in XML files rather than Java-like files.

The JBoss server configures aspects like other software components. It uses a Java Management Extensions (JMX) MBean (see "Improve Application Management With JMX") to load aspects, which you can hot-deploy by dropping a file with the signature *-aop.xml into the server's deploy directory. You can use JBossAOP outside the JBoss server, but you must use it with the JBossAOP classloader.

In JBossAOP, advices and pointcuts are declared in an XML file placed in the JBoss server's deploy directory. Here's an example of a JBossAOP advice, called an interceptor, and a method pointcut. Note that the pointcut refers to the name of the interceptor. The interceptor logic executes whenever a get() method in MyClass is called:

<aop>
   <interceptor name="Logging" 
      class="LoggingInterceptor">
   </interceptor>

   <method-pointcut class="MyClass"
      methodName="get.*">
       <interceptors>
         <interceptor-ref name="Logging"/>
       </interceptors>
   </method-pointcut>
</aop>

The declaration in the XML file names a Java class, which implements the JBossAOP Interceptor interface. The logic, which executes when the pointcut is triggered, is placed into the invoke() method:

// LoggingInterceptor class
public class LoggingInterceptor 
   implements Interceptor {

   public String getName() { };

   public InvocationResponse invoke
     (Invocation invocation)
      throws Throwable { 
   }
}

The JBossAOP example demonstrates a different approach to AOP. But in the next section, I'll use AspectJ to develop a solution to the problem I brought up at the beginning of this article.

Add an Aspect to a Java Application
You can use AOP to define a crosscutting behavior and solve the problem of poor logging design. You can add to the application the new behavior: logging information about Exceptions. The logging statements in the aspect use the Jakarta Common logging interface:

  1. Define an exception handler pointcut.
    You can define either named or anonymous pointcuts. The previous section's LogPoints aspect used anonymous pointcuts. This example uses a named pointcut:

        pointcut exceptionHandler(Exception e): 
           handler(Exception+) && args(e);

    The pointcut's name follows the pointcut keyword. The pointcut identifier is the handler(Exception+) part of the declaration. This pointcut specifies a joinpoint of the invocation of any exception handler for Exception or any of its subclasses. This definition specifies a named joinpoint in the program that can be referenced by an advice.

  2. Define the advice for the exception handler pointcut.
    The advice references the name of the exceptionHandler pointcut. It contains a block of code that writes a log message for the exception:

        after(Exception e): exceptionHandler(e) {
           log.error("Exception: " + e.toString());
        }
  3. Create an aspect that puts together the advice and the pointcut.
    If you add the pointcut and advice to the LogPoints aspect, you have a logging interface that creates log entries for before and after the execution of init() and doGet() methods, and whenever an exception handler for Exception or any one of its subclasses is invoked (see the resulting aspect in Listing 2).
  4. Compile and run HelloWorldServlet (see Listing 1) and the LogPoints aspect.
    The external interface to the ajc compiler is the same as the javac compiler. You can execute it at a command line or within an Ant script. The AspectJ distribution includes examples and Ant scripts. You also can compile it with the AspectJ Browser. The HelloWorldServlet gets a "count" request parameter and converts it into an integer. Entering any non-numeric character will cause a NumberFormatException, and the exception handler will be invoked. If it runs and generates the exception, these log entries are written:

    2004-02-28 15:21:56,527 INFO  HelloWorldServlet : 
       Exit: call(void HelloWorldServlet.
          init(ServletConfig))
    2004-02-28 15:21:56,543 INFO  HelloWorldServlet : 
       Enter: call(void HelloWorldServlet.
          doGet(HttpServletRequest, HttpServletResponse))
    2004-02-28 15:21:56,543 ERROR HelloWorldServlet : 
       Exception: java.lang.NumberFormatException: 
          For input string: " 4"
    2004-02-28 15:21:56,574 INFO  HelloWorldServlet : 
       Exit: call(void HelloWorldServlet.
          doGet(HttpServletRequest, 
             HttpServletResponse))

    The log entries show that a NumberFormatException was thrown within the HelloWorldServlet class's doGet() method.

You can use AOP to improve the manageability of your existing applications. It allows you to add crosscutting behaviors to existing applications, making it well-suited to implementing operational concerns such as logging, security, authentication, and performance monitoring that cut across a system. These types of concerns are often overlooked during development because they are separate from an application's core concern. Using AOP, you can add these crosscutting concerns in separate modules so they aren't tangled and scattered throughout an application. With multiple production-quality, open-source implementations to choose from, AOP is worth considering as part of an application upgrade.

About the Author
Robert Swarr is an independent software consultant specializing in open-source software and Java development in New Britain, Conn. You can reach him by e-mail at robert.swarr@agoralogos.net.



Back to top



ADVERTISEMENT

Java Pro | Visual Studio Magazine | Windows Server System Magazine
.NET Magazine | Enterprise Architect | XML & Web Services Magazine
VSLive! | Thunder Lizard Events | Discussions | Newsletters | FTP Home