How to externalize configurations for multiple webapps inside a tomcat server? - java

I'm running a standalone tomcat webserver, with multiple different web applications (war files).
Question: I want to externalize some sensitive application.properties fields like database accounts, each separated for every application.
I could provide the following property at startup:
--Dspring.config.additional-location=/etc/java/
Problem: each webapp would load the same /etc/java/application-production.properties file then! So that doesn't help.
Question: how can I separate them by application, so each of the webapps loads it's own additional external file?
Like on the testserver:
/etc/java/myapp1/application-test.properties
/etc/java/myapp2/application-test.properties
Prod:
/etc/java/myapp1/application-production.properties
/etc/java/myapp2/application-production.properties

one way to go is, you create key-value files, like test1.env, test2.env.... The content would be:
DB_URL=test1DB
DB_USR=hi
DB_PWD=pwd
...
In your application.properties you can use placeholder like db.url=${DB_URL}
When you start your application, using a start (shell) script, export the variables in *.env, and source the given env file, so that the right values can be filled in application.properties.

Create a new file that only contains the sensitive data:
/src/main/resources/config/application-production.properties
And add a .gitignore so that this specific file will not be commited for version control.

Related

Error Reading custom configuration file in dropwizard Application

So we have a normal batching application that we decided to port over to dropwizard. We want to use dropwizard for lot of benefits that it gives us out of the box like HealthCheck, metrics etc.
We might add some REST admin end points later but right now we are just using DropWizard Managed Service to start our application in a separate thread. We created a sample config yml file but it's mostly just a skeleton.
This porting over is just mostly lift and shift and we want to avoid huge refactor in our code. The application is currently tied with two different properties file and we want to keep it that way.
We have figured a way out to deploy our app using a custom deployer that generates these property files "application.properties" and "system.properties"
For some reason the app can't read those files and is throwing NPE. This is a java application. All the jars are under lib directory and the config files are under config directory. It just doesn't seem to resolve those files. I have tried searching the dropwizard docs but didn't find anything useful.
The code just simply expects the file in a particular directory
private static final String APP_PROPERTIES_FILE = "/application.properties";
File file = new File(DataSourceFactory.class.getResource(filename)
.getFile());
The dropwizard version : 1.2.2
And this is how I'm running the app :
java -cp "deployed/App/lib/*:deployed/App/config/*" com.commercehub.app.MainClass server config.yml
Any comments are really appreciated!
The issue was with my classpath. I didn't need the trailing /* for the config folder.
java -cp "deployed/App/lib/*:deployed/App/config" com.commercehub.app.MainClass server config.yml

how to run several instances same role of the same jhipster war file with different config values

I would like to run a war file on a amazon server several times, each time with slightly different properties like server port or database schema. Right now I have to rebuild for each case... I.e. how can I feed a spring-boot properties file at runtime to a jhipster generated war file?
Edited to clarify:
all I want is to be able to run two or more copies of the same war-file by specifying parameters (port, db name,...) at run time.
The instances are supposed to run on an Amazon system (Linux)
They are started at boot by a service script in /etc/init.d that also is used to start and stop in case of software mods/parameter changes.
As it stands now the application developed using JHipster only reads the file propagated thru the JHipster flow building with Maven (uses the -prod profile)
-Using a separate properties file does not work, it is not read
-Setting properties using -D results in the same: not read
Does anybody have any advise? It looks like JHipster has its own property reader...
To start the application on a different port, you should launch it with additional parameter:
--server.port=8081
or add parameters using -D:
-Dspring.datasource.url=whatever.you.want
or when you need multiple parameters:
-Dserver.port=10000 -Dspring.datasource.url=whatever.you.want

Add and Replace placeholder in XML from property files

I am developing a web application which will be deployed in different environments like Dev, Test and Production. Some of the configurations will be specific to environments and so I need to change the configurations in the property files with respect to the environments where the project is deployed.
I added the Environment variable in machine and obtained it using the method
System.getEnv("Environment")
which would return the value like, dev, test and prod and I load the respective property files (File names are dev-config.properties, test-config.properties).
I am implementing the PicketLink libraries in jBoss AS to integrate the application with SSO-SAML. One of the configurations is to add a file named picketlink-idfed.xml, where I mention the IDP URL and application url. Both the URLs will be different for environments. I cannot have different file names like dev-picketlink-idfed.xml and so I cannot follow the above approach of using different files.
Question is that I need to use the same file picketlink-idfed.xml, just by adding the placeholder and replace it with the respective values from the property files. Attached the sample content of picketlink-idfed.xml.
<IdentityURL>https://idfed.test.com.au/adfs/ls/idpinitiatedsignon.aspx?LoginToRP=https%3A%2F%2Fapp.test.com.au%2Fdisplays%2FHome.action
</IdentityURL>
<ServiceURL>https://app.test.com.au/displays/Home.action
</ServiceURL>
In the above line the values for tags IdentityURL and ServiceURL will be changed for different environments. Can you please tell me on how to address this issue?
Note: For time being, I am replacing the contents for every deployment, which is not good practice. Also, I am not using Maven or Ant build files. I cannot use these due to insufficient time.

Deployment in Weblogic 10.3, how to change properties

I have a spring application packed in war file. If i deploy in tomcat, i can go to file directory and find properties and xml files, and i can make changes, for example the name of the JNDI conexion, i restart the server, and everything is ok with new value. But now i have to deploy my app in weblogic 10.3, i can deploy and it runs, but if i want to change some properties of one properties file, i have to make a new war file and deploy again.
So my question is, how can i find this files without making a new war file? What is the best option to save this properties than can be changed in the future?
I want to make only one deployment, and after this, if changes properties are needed, any user can make this changes as easily as possible, for example changing a value of properties file.
Thanks!
You're probably looking for Weblogic deployment plans.
Read http://docs.oracle.com/cd/E11035_01/wls100/deployment/config.html#wp1065363 for details.

Java webapp - where to put application-generated files?

I'm working on a Java web application that needs to store uploaded files in one directory, and an embedded Neo4j database in another directory. I'm deploying the warfile to Tomcat to serve the application, and the application needs to be runnable under Tomcat in either Linux or Windows.
Where exactly should I be putting these two directories on the host system's filesystem?
I'm confused since I'm accustomed to storing information in databases specified via a URL, etc. Thanks for the help.
Is there a chance webapp have two or more instances running at the same time, say in a same Tomcat with two /path names?
Java has system property user.home you could always create a subfolder on it. Current user is the one running Tomcat server. Print properties to sysout for debug purpose.
Reading a webapp name at runtime you can use servletContext.getRealPath("/") function. You get a filename path to $tomcat/webapps/mywebapp and use last folder entry. Define ServletContextListener in web.xml so you can read webapp name at startup.
Use naming convention ${user.home}/tomcat/${webappname}/ and store any file you please.
Or define a webapp context-param variable in web.xml file and let deployer create an appropriate folder.
http://docs.oracle.com/javase/tutorial/essential/environment/sysprop.html
http://docs.oracle.com/javaee/6/api/javax/servlet/ServletContextListener.html
http://docs.oracle.com/javaee/6/api/javax/servlet/ServletContext.html#getInitParameter%28java.lang.String%29

Categories