tags:

views:

665

answers:

2

I have two tomcat web applications that need to share information using a singleton. I have already made it work by placing the jared classes in the tomcat common directory. Each webapp then gets the same copy of the singleton. What I would like to do is to integrate this behavior within eclipse. I would like the common classes to be a single project that gets incorporated into the tomcat common class loader every time I start the tomcat server within eclipse. Anyone knows how to configure eclipse to do this?

+1  A: 

May be one possibility could be to extend the tomcat class loader in order for that class loader to search in other directories than WEB-INF/lib, this by:

  • Extending org.apache.catalina.loader.WebappClassLoader and override the findClassInternal method.

  • Configuring Tomcat to use the extended classloader.
    This is done in the appropriate webapp configuration file under the Tomcat conf/Catalina/hostname path with the following element:

    ...

Then in eclipse, you could set your common project on the "Required projects on the build path", which makes it part of the classpath.

That means your extended classloader must be able to look for other classe:

  • either in a fixed pre-defined path
  • or in a pre-defined path within the classpath.

Not tested myself, but may be that can give you a lead on this issue.


A much simpler solution is proposed by noselasd in the comments, taking advantage of the GlobalNamingResources Component of Tomcat.

However, the FAQ does mentions:

When you create a new Tomcat server in Eclipse, the New Server wizard assumes it is not safe to affect the current behavior of the Tomcat installation that this new server will use.

WTP is able to avoid affecting the behavior of the installed Tomcat by using Tomcat's ability to run multiple server instances from a single installation. Thus, the default configuration for each new Tomcat sever you create will be a new server instance of the Tomcat installation associated with the Tomcat runtime selected in the wizard.
If you expect the new Tomcat server in Eclipse to run the same instance that the default batch files in your Tomcat installation run, you will likely be surprised when the Tomcat server in Eclipse doesn't behave as expected.

The Tomcat server configuration can be changed so that it does run the same instance as your Tomcat installation.

You will find here how to modify the server.xml in WTP.

VonC
Should not be needed to be so involved, at all. Tomcat has server wide settings, and you can define your own global resources in server.xml that can be shared with all webapps. http://tomcat.apache.org/tomcat-5.5-doc/config/globalresources.html You can use your own classes, rather than an Integer as in the example ofcourse. But I don't know how to play around with server.xml and the global lib/ directory in Eclipse.
nos
A: 

I've managed to get it working. Here is what I did:

  • Created a common project in the eclipse workspace.
  • Created the two web applications, called first and second, that should share the common project.

When the web applications are created a Servers project is created with the tomcat configuration.

  • Change catalina.properties inside the Servers project and add the line shared.loader=/path-to-workspace/common/bin.

This works perfectly for development. Every time a new build is created everything is in sync. For deployment You need to convert the common project into a common.jar and place it in ${catalina.home}/lib.

rmarimon