Success with JBoss AS + Struts + AOP

Tutorial how to set up JBoss, Struts and AOP together

It’ll be about JBoss Application Server and Aspect Oriented Programming and web applications.

As everyone who knows me know I develop simple, yet powerful web application. At least I try. There was time at the beginning of this adventure I’ve used Tomcat web container among with Struts Model-View-Controller framework.

After some time, Aspect Oriented Programming (thanks to Andrew) came into my developer’s life. It is JBoss AOP that I ‘play’ with.

I still need to develop web application. How to include AOP into web application? First of all I decided not to play with Tomcat any more. Main reason is I knew JBoss Application Server also has Tomcat embedded inside it. Also JBoss AS, as the JBoss company is the mother of JBoss AOP, has AOP included inside their product.

So I took the freshest JBoss 4.0.1 release of JBoss AS and unpacked it.

I use Ant for my application, so examples will use it. This “tutorial” is also valid for Java 1.4 version. I don’t know if there are any problems with 1.5.

I assume you know basics of JBoss AOP and how to deploy [Struts] web application on Tomcat.

Steps to successfully deploy AOP enabled web application on JBoss AS with Struts:

  1. download JBoss AS 4.0.1. Unpack. I use /usr/local/java/jboss/ directory.
  2. prepare your web applicationI mean get your *.war file that was running on Tomcat with Struts. I had to change nothing to the files.
  3. next step is to write your aspects and put them along with your plain Java source files
  4. next, write JBoss AOP configuration file. It is usually called jboss-aop.xml

    Multiple aspects’ configuration files *-aop.xml. Aspect library (libraries)

  5. now when we have all source files, configuration files, we need do compile sources
      AOP sources compilation process consists of four parts:

    • Get the libraries:Libraries needed to compile aspects are in these directories:
      • JBOSS_HOME/client/*.jar
      • JBOSS_HOME/server/default/deploy/jboss-aop.deployer/*.jar
      • qdox.jar
    • Compile your sources:compile all sources, including aspects with plain Java compiler (I use <javac>)
    • Compile JBoss AOP annotations (if you use them):

    • Compile all sources using AOP compiler:

  6. Ok. The next step is packaging all of it. So, basically, all we need is to pack all files related to web application into application.war file, and all files related to AOP into application.aop file. After doing this, put those two files into one file application.sar with one configuration file.So here is how the structure of my deploy/directory looks like:
                                    *.class, *MethodInvocation*.class
                            ant.jar, log4j-1.2.8.jar, javax.servlet.jar, ...
                        book.png, ...
                        inc-footer-links.jsp, ...
                        index.jsp, ...

    As you also see I’ve unpacked contents of all archives into directories with the same name. Though I believe it’s not necessary.

    The contents of jboss-service.xml is simply:

        <?xml version="1.0" encoding="UTF-8"?>
  7. [optional step] edit AOP configuration file of default/ configuration of JBoss AS to enable weaving process:We have to choices here: if we precompile sources with <aopc>, <annotationc> (as it is in my situation) then we do nothing and go to the next stepOtherwise we may not precompile sources using those two tasks! Aspects may be hot deployed. But for this, one must adapt configuration:
    Change JBOSS_HOME/server/default/deploy/jboss-aop.deployer/META-INF/jboss-service.xml and set variable EnableTransformer to true
  8. deploy your application by putting it under JBOSS_HOME/server/default/deploy/ directory. I just expand the file contents into JBOSS_HOME/server/default/deploy/application.sar/ directory
  9. run JBoss AS: JBOSS_HOME/bin/ -c default.For my needs I also update -Xmx value in run.conf file
  10. point your browser to http://localhost:8080/application/

That is all. Of course you may change the application name into some other value, it is just an example.

I hope it helped you :)

Any questions, feedback.. put below.