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?
views:
665answers:
2May 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 thefindClassInternal
method.Configuring Tomcat to use the extended classloader.
This is done in the appropriate webapp configuration file under the Tomcatconf/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.
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
andsecond
, that should share thecommon
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 lineshared.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
.