Running OT/J inside Tomcat

Attention: This is a preliminary workaround for Tomcat 6.0 and still has to be worked out!

From a language perspective, servlets can be handled by OT/J as every other Java class. The difficulties lie in the different classloading strategies used in Tomcat.

The servlet application

First, create a "Dynamic Web Project" named "webTest" (configured for Tomcat 6.0) and a Servlet with this content:

package foo;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class MyServlet
 */
public class MyServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        PrintWriter pw = response.getWriter();
        pw.println("Hello World!<br />");
        for (Enumeration atts = request.getAttributeNames(); atts
                .hasMoreElements();) {
            String att = (String) atts.nextElement();
            pw.println("attribute: " + att + ":" + request.getAttribute(att)
                    + "<br />");
        }
    }

    private void logIt() {
        System.out.println(" -> This is a private message !!!");
    }
}

Second, create an "Object Teams Project" named "webTestOT" with the following team class:

package aspect;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import base foo.MyServlet;

public team class MyTeam {
    protected class ServletRole playedBy MyServlet {
        doGet <- replace doGet;

        callin void doGet(HttpServletRequest request, HttpServletResponse response) 
                throws IOException, ServletException {
            // injecting some attributes into the original request:
            request.setAttribute("foo", "bar");
            request.setAttribute("more", "information");
            // calling the original servlet behaviour:
            base.doGet(request, response);
            // adding to the response:
            PrintWriter pw = response.getWriter();
            pw.println("No Way!<br />");
            // calling a private base method via inferred callout:
            logIt();
        }
    }
}

To globally enable the team, you also need a file "team.config" in the OT project with the following line in it:

aspect.MyTeam

From within Eclipse

Create a new launch configuration of type "Apache Tomcat" and configure as follows (change location paths according to your settings):

Now run it and open  http://localhost:8080/webTest/MyServlet in a browser. You should see following output:

Hello World!<br />
attribute: more:information<br />
attribute: foo:bar<br />
No Way!<br />

The console should show -> This is a private message !!!

From the command-line

Export the web project as WAR file (webTest.war) and the OT project as JAR file (webTestOT.jar). Put the WAR into the <TOMCAT_HOME>/webapp folder and the JAR and team.config into <TOMCAT_HOME>. Put the OT libraries otre.jar, otre_min.jar, BCEL.jar and otre_agent.jar into <TOMCAT_HOME>/lib.

The following instructions are for Windows and have to be adatped for Linux.

Edit <TOMCAT_HOME>\bin\setclasspath.bat and change the following line

set CLASSPATH=%JAVA_HOME%\lib\tools.jar

to

set CLASSPATH=%JAVA_HOME%\lib\tools.jar;%CLASSPATH%

Then use the following script to start Tomcat (all one-liners):

@ECHO OFF

set JAVA_OPTS=-Xbootclasspath/a:D:\dev\tomcat\lib\otre_min.jar -javaagent:D:\dev\tomcat\lib\otre_agent.jar -Dot.teamconfig=D:\dev\tomcat\team.config

call bin\cpappend.bat D:\dev\tomcat\lib\otre.jar D:\dev\tomcat\lib\BCEL.jar D:\dev\tomcat\lib\servlet-api.jar D:\dev\tomcat\webTestOT.jar D:\dev\tomcat\webapps\webTest\WEB-INF\classes

call bin\catalina.bat run

Attachments