Use custom paths to store config files in Tomcat - java

In embedded-tomcat can we use our custom web.xml, server.xmlandcontext.xml files? If it is possible, can we use a custom location to store them and point that location?
It would be great if someone can point an example
I'm using Tomcat version 8.0.20.

My understanding is that these configuration files are not directly supported for an embedded Tomcat implementation. The answer to https://stackoverflow.com/questions/32958412/passing-a-custom-server-xml-file-to-standalone-tomcat-embedded-tomcat has more detail.

Related

Real path to a virtual directory

I setup a mapping to my images directory in Weblogic.xml by using the virtual-directory-mapping tag. How can I read the value of the real path so that my application can access that virtual directory for write access?
JMX seems to be of no help here since the WebServerMBean (or any other MBean) doesn't seem to provide access to the virtual-directory-mapping property.
Java Servlet API also has not yielded result since calling getRealPath() is only appending the url-pattern to the deployment directory of the application and not giving the correct path.
<virtual-directory-mapping>
<local-path>/home/wlsadm/images</local-path>
<url-pattern>help/specimens/*</url-pattern>
<url-pattern>*.xml</url-pattern>
</virtual-directory-mapping>
I've done a fair amount of research on this, reading vendor documentation, reading blogs, forums, etc. As the OP indicated, JMX use to be an approach to get this information, but this is no longer an option in current Weblogic versions.
The only solution I think you're left with is to create a utility that reads the information from the weblogic.xml file on the classpath. Something like an application listener that reads it on application startup and makes it available as a servlet attribute, etc.
Would love to hear how you solved it, though.

Best way for storing Java application name and version properties

Using Eclipse IDE.
I need to store my application's name and version as they are used by the app itself.
I could hard-code them as static final fields somewhere in the codebase. A better solution I know about may be storing them in the manifest file distributed with the application JAR.
Storing these properties in the manifest file feels better to me, but the problem is that the properties (obtained via Package methods) are not accessible during the development phase - running/debugging from IDE.
Is there a way to define them in Eclipse Run/Debug configurations? Is there a better way of storing such pieces of information. (I can also imagine using the Preferences API, although they are not "preferences" per se).
Thanks.
Use properties file. Here is a good start: http://www.mkyong.com/java/java-properties-file-examples/

Which JAR contains the weblogic.servlet.security.ServletAuthentication class?

I trying to create a servlet to logout from multiple JSF applications running on Oracle Glassfish. I tried the following code,
weblogic.servlet.security.ServletAuthentication.logout(request);
weblogic.servlet.security.ServletAuthentication.invalidateAll(request);
weblogic.servlet.security.ServletAuthentication.killCookie(request);
but I am not able to find the JAR file containing the required class in order to get the code to compile. I downloaded Oracle WebLogic and tried various of its JARs, but no one of them made the code to compile.
Which JAR does contain the required class and where can I find it?
The class you are looking for is in a jar named wls-api.jar.
However, you're probably searching for the wrong thing because if you are developing applications to run on Glassfish you should stick with the standard techniques (e.g. JSR-196 described here or here).
Because it isn't obvious what you are trying to implement I can't give any further detailed advice but there is plenty of documentation available about Java EE Security.
I think u need this jar file:
<JDeveloper Home>\Middleware\wlserver_10.3\server\lib\weblogic.jar
The file is located at <WLS_HOME>\middleware\wlserver\server\lib\wls-api.jar

Where/how to store persistent data with tomcat?

Where should I store persistent files in a Tomcat web app ?
javax.servlet.context.tempdir is not feasible, it's erased when the app is redeployed/removed
Don't want to use an absolute path in e.g. servlet init parameters
Storing the files in a database is not an option
Our team does this a lot. A general rule we follow is outside the web app and outside Tomcat.
Our sysadmin set up a directory on our server that the tomcat user has rw permissions to (e.g. /var/tomcat/persist). We have a built a directory structure under this that tomcat uses to store files, read app-specific init files, etc.
If you don't want to use an absolute path in your init-params for your servlet, consider setting a system property when tomcat is started up. The good thing about that is every application running under tomcat will have access to it. The bad thing about that is every application running under tomcat will have access to it. You could set a property named base.persist.dir and build subdirectories for each application underneath it. We set system properties in the setenv.sh script in the bin/ directory under the CATALINA_OPTS environment variable.
Answering the title of the question, what about using a database, a DataSource and JDNI? Even in a web only context, writing to files using java.io is not really recommended because of concurrency, threading, security, clustering, portability issues. Some of these problems can be "workarounded" but still, this is not really a best practice. The standard approach is to use a database and I'd suggest to reconsider this option, throwing "file-based" lightweight database like HSQLBD or JavaDB into the mix.
(EDIT: For an unknown reason, database is not an option. Using JNDI or context parameters or init parameters to pass an absolute path - which are the less worse options IMHO - is excluded too. For a relative path, maybe look at user.home or user.dir then - or any other system property that you could pass on the command line. I don't like it, I wouldn't do it, and this doesn't solve the issues previously mentioned, but it's your choice after all.)
Storing the files in a webapp directory under the home directory of the user running Tomcat is a good and convenient option. It is outside of Tomcat, which means it will survive redeployment, and it is usually a writable directory (because it is created under the users' home dir).
But it is always a good idea to allow overriding the location of such directory via system property.
Generally, this would go to the database. But since the OP insists on not using a database, I'd try a different approach:
Filesystem path which is known: ${user.home}/.myapp. Applications sometimes use this for e.g. search indices which can be recalculated based on data in the database. Might be okay for your use case to use the user's home.
Store the configurable filesystem path in a configuration repository such as the database or perhaps Java Preferences (if you don't like to use servlet init params). Commercial applications such as Atlassian JIRA use a configurable (but absolute) filesystem path where they store issue attachments. If they don't know a better way, i don't know who does :)
I generally would suggest to use a database to store persistent data and expose it via a DataSource.
If you don't want to do that, I guess you could consider using the "user.home" system property (I have seen this used in a few circumstances). But... there are no guarantees that your servlet will be run with permission to write access unless you configure that yourself.

Use Tomcat to serve a directory?

I have a directory on a linux box that I want to make publicly readable using Tomcat (5.5). I think this is easy to set up but can't find the appropriate documentation. Is there a simple way to accomplish this?
It is possible by defining that directory as an web application, but it's not really what Tomcat is designed to do, other servers are far better at serving static content.
The way to define a directory as a webapp is to either
put it into $TOMCAT_HOME/webapps,
configure it in $TOMCAT_HOME/conf/server.xml or
provide a context .xml file and put it in $TOMCAT_HOME/conf/Catalina/localhost (by default, depends on your configuration).
You can just link it to a folder under webapps as a new "web application".
ln -s /path-to-real-folder /path-to-tomcat/webapps/publicfoldername
If I remember correctly, directory listing is enabled by default in tomcat, so the dir would be reachable. If not, this can be fixed in web.xml
Although Tomcat is a good web server, it's not particularly made to list directories.
You might want to look at a web server like the Apache web server instead, it's more designed for this type of thing.

Categories