Same webapp on Tomcat with different log4j config for each - spring

I've hunted and read, and I think there's no way...but just in case.
I need to deploy the same webapp to Tomcat multiple times, each with a different config that indicates which database to work with. I've figured out how to do that without modifying the contents of the war file.
In short, I copied WebApp.jar to WebAppDB1.jar, WebAppDB2.jar, and deploy these to the webapps folder, and use a custom context configuration for each in tomcat/conf/Catalina/localhost. So I'm good there.
But I would really like for each of these to log to a separate file. As it is, everything goes to catalina.out. It's a Spring app using log4j and slf4j. Every avenue I've explored gets me nowhere.
For now, I'm back to updating the actual war file, going into WEB-INF/classes and updating log4j.xml, but that makes it a manual process.
Any ideas?

You can put the log4j.xml files somewhere in the outside the webapp and use Tomcat's VirtualWebappLoader to load different log4.xml for each webapps.
http://tomcat.apache.org/tomcat-7.0-doc/config/loader.html#VirtualWebappLoader_Implementation
It looks like this.
${CATALINA_BASE}/conf/Catalina/localhost/WebAppDB1.xml:
<Context docBase="..." >
<Loader className="org.apache.catalina.loader.VirtualWebappLoader" virtualClasspath="/somewhere/WebAppDB1" />
</Context>
${CATALINA_BASE}/conf/Catalina/localhost/WebAppDB2.xml:
<Context docBase="..." >
<Loader className="org.apache.catalina.loader.VirtualWebappLoader" virtualClasspath="/somewhere/WebAppDB2" />
</Context>
And then put log4j.xml files into /somewhere/WebAppDB1 and /somewhere/WebAppDB2.
Hope this helps.

Related

Copy compiled jar into %TOMCAT%/lib automatically using Maven

I would like to know if there is a good way to build JAR files with Maven and after that copy them into %TOMCAT%/lib folder.
What I want to achieve is that every time when I deploy my WAR in the cloud (using Jenkins and Cloud Foundry), I get all JARs needed by Tomcat copied in its lib folder.
My use case is, my application uses Redis to store sessions (to be cloud ready), and I have added this configuration to %TOMCAT%/context.xml file
<!-- session persistence is handled by Redis Data Server -->
<Valve className="com.gopivotal.manager.SessionFlushValve" />
<Manager className="org.apache.catalina.session.PersistentManager"
host="localhost"
port="6379"
database="0"
maxInactiveInterval="60">
<Store className="com.gopivotal.manager.redis.RedisStore" />
</Manager>
Sessions are stored like a charm when I copy manually redis.jar, commons-pool.jar and jedis.jar in %TOMCAT%/lib, but I want to do that automatically based on the best practice.

Tomcat context.xml from META-INF folder for context root configuration

I have an application which is being deployed in Tomcat 8. This application needs to be the context root.
To do this, I put a root.xml file in conf\Catalina\localhost folder and it worked.
Now, the problem is that when i debug through IntelliJ idea (Remote host) it is not being deployed as the root application.
Seems like the only way to fix this problem is to add context.xml file in META-INF folder and specify the configuration there.
Now, when I do this, context root configuration from context.xml file is not being picked up.
Any thoughts on this? Ultimately, I just need to debug the application through a remote server.

Can't deploy site on tomcat on mac

I cannot figure out how to deploy a website (not app) on Tomcat on my mac. I just want to have Tomcat serve the file index.html. I tried creating the file myapp.xml in library/Tomcat/conf/Catalina/localhost with the content:
<Content path="/myapp" docBase="/Users/jerryk/Development/myapp" />
But when I attempt to access the site with
"http://localhost:8080/myapp" or "localhost:8080/myapp/index.html"
I get a resource not found error.
http://localhost:8080 brings up the Tomcat control panel.
Any thoughts on what I am doing wrong? FWIW, this works fine on windows.
Thanks,
Jerry
I'm not sure what's going on with Windows, but theoretically, the docBase must point either to a .war file, or a directory that has the same layout as a war file.
So, you can't just have an index.html in that directory, you also need at least a WEB-INF directory with a minimal web.xml in it.
See, for example, http://oreilly.com/java/archive/tomcat.html

Custom tomcat context.xml for Spring App

I have a Spring App, and I would like change the session cookie path. I know this can be done in the server's context.xml file, but I don't have permission to edit that file on my server. Is there another way to do this, maybe by adding another context.xml file somewhere?
Btw, I added a context.xml file to my project's META-INF folder, but it didn't work.
Bundle your context.xml with your war file.

Mapping a directory outside the web-app to URL in TOMCAT

I need to map an directory containing images which resides outside tomcat webapps folder, so that application can serve those images.
I am making a J2EE Web application running under tomcat 6. User can upload/delete images in the application. Currenly I store these images to a directory under application's WebContent folder, but I want to take it outside the tomcat (e.g. C:/test/images).
I need to know how to I configure tomcat so that if I access URL http://.com/images/abc.jpg , it serves the image from directory C:/test/images
Thanks,
Add a <Context> tag in server.xml, inside the <Host> tag:
<Context path="/images" docBase="C:/test/images/" />
Docs will be accessible at http://localhost:8080/images
in Tomcat8 you can also add PotsResources to you META-INF/context.xml as follow :
<Context>
<Resources allowLinking="false">
<PostResources readOnly="false"
className="org.apache.catalina.webresources.DirResourceSet"
base="path-to-your-local-folder"
webAppMount="/images"/>
</Resources>
...
</Context>
I had the same issue but found a solution.
If you are using Eclipse and a Tomcat plugin then please note that the Eclipse Tomcat plugin creates a separate CATALINA_BASE under the Eclipse workspace directory.
You can go to this location and you will find server.xml.
Use that server.xml and it will work.
My actual tomcat directory is:
C:\apache-tomcat-7.0.62x64\apache-tomcat-7.0.62\conf
and my Eclipse Tomcat server uses:
C:\workspace\JSF\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\conf
Use the workspace path and server.xml from this location.
Add this in server.xml inside the host tag:
< Context docBase="D:/personal" path="/images" />
and it will work if D:/personal has 1.png, and then the url http://localhost:8080/images/1.png will load the image.

Resources