I have following configuration:
Wildfly 8.1.0
multi-domain environment
in each domain I've deployed the same EAR, but with different INI files
EAR contains Configuration.jar with config.ini file and RestWS.war with WEB-INF/classes/shiro.ini
I would like to protect these INI files from overwriting. Is it possible to specify external files instead of these from EAR?
The article How to put an external file in the classpath describes how to do this.
Related
We are using Websphere 9 application server. We want some of the configuration files such as xml and properties files in a separate directory of Websphere server and want them too see accessible by ear/war file during the run time. I heard about shared libraries approach, but it apppears that only class and jar files can be used as shared libraries, but not xml and other files. Can anyone tell me an alternative solution where the external xml configuration files be made available for war/ear file during run time or in class path?
If you add a directory as a shared library path, the directory itself will be added as a class path entry to any class loader referencing the shared library (along with any jar/zip files within it), so you'll have access to loose files such as XML files through the getResource() API.
Note that the argument to getResource() needs to be relative to the location within the directory. For example, if you have the file test.xml, you could add it to the directory /sharedlib, created a shared library named "library1", and associate it with your EAR or WAR, and then your application could use use this to get at the file:
this.class.getResource("test.xml");
That would return you a URL pointing at /sharedlib/test.xml.
I compiled a Java application into a WAR file, there is a configuration file inside the WAR file. The configuration file is required to change something after deployed to the production server, because it still contains the UAT server parameters.
However, I don't know where to edit the configuration file after deployed in Tomcat. Please help. Thanks.
It doesn't sound like a correct design. You should -
Load configuration file based on some System parameter (e.g. -Denvironment=UAT or PROD). This will be the decision factor for loading the right configuration file.
Do not package the file inside war itself, if possible externalize it to some other directory where amending is lot easier.
According to what I know Java Web applications are deployed on a server by deploying the .war file of the web application. Is this correct?
I have a Web application that has some configurations to allow the server administrator to configure the application at the time of deployment. Ex. Set the path where log files are to be saved, Set the location to store uploaded documents etc..
I defined such parameters in a .properties file and hardcode the path to it. Thereafter I was able to get the web application to read the values in the .properties file without recompiling the source code (as the .properties file path did not change).
Now I want to know how to do this without hardcoding the path to the .properties file so I can deploy this on any server without worrying about its directory structure.
So, is it possible to provide a .properties file along with the .war file when the web application is being deployed so that the server administrator can edit the .properties configuration file, deploy the .war file (without recompiling the web application) on a server and let it read the values in the .properties file?
If so how can the web application know where the .properties file is?
Best practise is to put the properties file at a location where it can be found on the classpath. Your application loads the file with
this.getClass().getClassLoader().getResourceAsStream( "myConfig.properties");
If the file is located in a non standard classpath location you can finetune your applications classpath using tomcats configuration possibilites. See e.g. Understanding The Tomcat Classpath
I'd like to create a desktop standalone application from my Java/Spring web application. I created MSI-installer that copy all required files to C:\Program Files (x86)\App. But tomcat doesn't have permission and can't write to its own folder. How I can configure tomcat so it would write all app-specific data to other folder? I wouldn't like to install my app to C:\App or user dir.
java.io.FileNotFoundException: C:\Program Files (x86)\App\tomcat\logs\catalina.2016-06-18.log (Access denied)
By setting the environment variable CATALINA_BASE to another directory in your tomcat start script you can configure Tomcat to read/put the working data, configuration and stuff from/to another location. If CATALINA_BASE is set, Tomcat will use the folders %CATALINA_BASE%/bin, %CATALINA_BASE%/conf, %CATALINA_BASE%/logs, %CATALINA_BASE%/temp, etc. for the current instance of Tomcat. This is described in more detail in the Advanced Configuration - Multiple Tomcat Instances section of the RUNNING.txt file in Tomcat's root folder.
If you are planning to ship Tomcat with your application and put all Tomcat files to some user choosable folder you should set CATALINA_HOME to this folder. Tomcat will then use this folder as base directory for everything.
However I think, as you mentioned to ship a standalone application based on Spring, you should seriously take a look at Spring Boot. This will allow you to ship a single fat jar containing all of your application's dependencies (including Tomcat). And this application can simply be started by executing the jar file.
If it's just about the log and temp files, you can set the Java system properties java.util.logging.config.file and java.io.tmpdir in the setenv.bat file under %CATALINA_BASE%/bin to make Tomcat use a custom logging configuration and a different temp dir, respectively. That is the file would look something like this:
set CATALINA_OPTS="-Djava.util.logging.config.file=file:///c:/path/to/log/config.properties"
set CATALINA_OPTS="%CATALINA_OPTS% -Djava.io.tmpdir=c:/path/to/temp/dir"
Find and update all occurrences of "$CATALINA_BASE"/logs/catalina.out to custom path in catalina.sh script.
My requirement is I have to refer the jars present in other war file in my web application.
I am thinking to edit the manifest file of my web application to add the jars of other war file, but I do not know exactly what to add in the manifest file.
Could any one please help me on this.
You web application should be running inside at least a Servlet Container, right?
So you have to move your second webapp jars inside the library directory of your Application Server (e.g. lib for Tomcat AS) and that way the classes under those jars will be available for both web application (even all web application running in you AS).
Note: It is already a bad practice to package a common dependencies under a war archive as those should be shared among multiple contexts, so you should review you packaging strategy.
Edit: How to declare a Shared classpath in Tomcat
To define a shared classpath in Tomcat AS so that all your contexts have access to:
Navigate to your server home ($CATALINA_HOME) and open the catalina.properties file under the conf/ directory.
Look for the shared class loader section and add / modify shared loader to look as follows:
shared.loader="/path-to-shared-classes"
You need to look for the classpath form rules, yours may be "$CATALINA_HOME/webapps/middlewar/WEB-INF/lib/*.jar"