Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
RE: [aspectj-users] Support for Tomcat 5

Hi Mark,

If you are using AspectJ 5 then you could use the load-time weaving support
to weave aspects into your JSP's. 

If not, then you need to plug in to Tomcat's ant task for auto-compiling
Servlets. Here's how I did it in past: One key step that I did that doesn't
appear in Wes's write up "Running AspectJ JSP's in Tomcat 4.x" is to use a
command editor so you can apply aspects to your JSP's through the compiler
adapter!

Here's a simple one I have used in past, which you are free to use:
/**
 * Copyright 2003 New Aspects of Software. This software is granted
 * for use under the terms of the Common Public License 1.0.
 *
 * @author Ron Bodkin
 *
 * Command class that adds aspect path settings based on the classpath.
 * This class uses a naming pattern approach: any jar on the classpath with
a name that ends 
 * in Weave (e.g., fooWeave.jar) will be added to the aspectpath. This
limitation was
 * made because the memory requirements for adding all the class jars to the
aspectpath were
 * found to be unacceptable for projects using many libraries.   
 * This editor requires classpath to be an argument, as it normally is in
 * the Ajc11TaskDef.
 *
 */
public class AutoAspectpathRewriter implements ICommandEditor {
	/**
     * 
     * Do the rewriting.
     * 
	 * @see
org.aspectj.tools.ant.taskdefs.ICommandEditor#editCommand(java.lang.String[]
)
	 */
	public String[] editCommand(String[] command) {
//        System.out.println("in editCommand");
//        for (int i=0; i<command.length; i++) {
//            System.out.println("command "+i+" = "+command[i]);
//        }

        // requires classpath to be an argument, since we don't have access
to the underlying task
        int matchPos;                       
        for (matchPos=0; matchPos<command.length; matchPos++) {
            if ("-classpath".compareToIgnoreCase(command[matchPos]) == 0) {
                break;
            }
        }
        if (matchPos < command.length-1) {
            String[] rewritten = new String[command.length+2];
            System.arraycopy(command, 0, rewritten, 0, command.length);
            rewritten[command.length] = "-aspectpath";
            StringBuffer holder = new StringBuffer();
            // walk the list of entries, and include any jars...
            int pos, nextPos;
            for (pos = 0;;pos = nextPos+1) {
                nextPos =
rewritten[matchPos+1].indexOf(java.io.File.pathSeparator, pos);
                if (nextPos != -1) {
    				String substring =
rewritten[matchPos+1].substring(pos, nextPos);
/*                    
                    java.io.File file = new java.io.File(substring);
                    if (file.exists() && !file.isDirectory()) {
                        // we could use a JarFile to verify this is a valid
jar, but we'll
                        // assume any file that's not a directory should
be...
*/
                    if (substring.endsWith("Weave.jar")) {
                        // use a naming convention - any jar named
*Weave.jar will
                        // be added to the aspectpath...
                        holder.append(substring);
                        holder.append(java.io.File.pathSeparator);
                        //System.out.println("added "+substring);

                    } else {                     
                        //System.out.println("skipped "+substring);
                    }
                } else {
                    break;				 
                }
			}
            rewritten[command.length+1] = holder.toString();            
    		return rewritten;
        } else {
            return command;
        }
	}
}


Here's the original note:
This note describes how to configure Tomcat 4.x to use AspectJ to compile
JSP files.

It uses the ajee.adapters.AutoAspectpathRewriter to
set up an aspectpath that includes any jars on the classpath that are
named *Weave.jar.

1. Server configuration:
<!-- in %TOMCAT_HOME%/conf/web.xml or in app/WEB-INF/web.xml -->
    <servlet>
        <servlet-name>ajsp</servlet-name>
        <servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class>
        <init-param>
            <param-name>logVerbosityLevel</param-name>
            <param-value>DEBUG</param-value>
        </init-param>
        <init-param>
            <param-name>compiler</param-name>
 
<param-value>org.aspectj.tools.ant.taskdefs.Ajc11CompilerAdapter</param-valu
e>
        </init-param>
        <init-param>
            <param-name>fork</param-name>
            <param-value>FALSE</param-value>
        </init-param>
        <load-on-startup>2</load-on-startup>
    </servlet>

<!-- in app/WEB-INF/web.xml for apps that use AspectJ with JSPs (or
in %TOMCAT_HOME%/conf/web.xml to always use AspectJ with JSPs)  -->
  <servlet-mapping>
    <servlet-name>ajsp</servlet-name>
    <url-pattern>*.jsp</url-pattern>
  </servlet-mapping>

2. Command line set up of variables (for Windows command processor):
Set
CATALINA_OPTS=-Dorg.aspectj.tools.ant.taskdefs.AjcTask.COMMAND_EDITOR=ajee.a
dapters.AutoAspectpathRewriter
copy %ASPECTJ_HOME%\lib\aspectjtools.jar %TOMCAT_HOME%\common\lib
copy %ASPECTJ_HOME%\lib\aspectjrt.jar %TOMCAT_HOME%\common\lib
copy eopSupportWeave.jar %TOMCAT_HOME%\common\lib

copy <your aspect jar>Weave.jar %TOMCAT_HOME%\common\lib
or
copy <your aspect jar>Weave.jar app\WEB-INF\lib

3. When you change your aspects, you will need to delete any
   precompiled jsp files; the Tomcat jsp engine (jasper) will not
   recognize dependency on aspects. E.g.,

del %TOMCAT_HOME%\work\Standalone\localhost\<app>\*.java
del %TOMCAT_HOME%\work\Standalone\localhost\<app>\*.class

________________________________________
From: aspectj-users-bounces@xxxxxxxxxxx
[mailto:aspectj-users-bounces@xxxxxxxxxxx] On Behalf Of Marc Candle
Sent: Monday, July 11, 2005 6:48 AM
To: aspectj-users@xxxxxxxxxxx
Subject: [aspectj-users] Support for Tomcat 5

Hi,

I'd like to do AOP over JSPs in Tomcat/5.5.9 but I have been unsuccessful at
getting it to run using the instructions at
http://eclipse.org/aspectj/sample-code.html#j2ee-tomcat4-servlets (which
target Tomcat version 4.1). Is there a step-by-step setup guide available?

Ideally, I would like Tomcat to weave my aspects in automatically every time
it recompiles the JSPs (i.e. I don't want to have to recompile the JSPs
manually every time).

Thanks for any help,

Mark



Back to the top