As to the first doubt, the default Java EE architecture is for the application to declare in web.xml
what Datasources it depends on and the configuration is performed on the application server at the time of deployment by the client or responsible.
For cases where the client may want two instances of the application pointing to different Datasources, it is up to it to properly configure the server.
Unfortunately, each application server has its own configuration. For example, Tomcat creates a file context.xml
for each application with its Datasources. In Websphere and Weblogic it is possible to link resource dependencies to Datasources through the administrative panel.
On the other hand, it is quite possible to ignore the container’s Datasources and leave them to load the application. A very simple way is to load the persistence.xml
from a directory when manually instantiating the EntityManagerFactory
. The configuration of this directory could be done by environment variable, so the client would still have the flexibility to point to any directory. Being possible to define specific variables for each JVM or for each application (on some servers at least), it would still be possible to have different configuration files for each instance of the application.
Data as user and password are always customer’s responsibility, your application should never load this data within the package.
As for your second doubt, environment-dependent settings should be stored somewhere in the environment.
The vast majority of enterprise applications store system settings or "parameters" in a database. This facilitates management by the client, after all it is only necessary to know a little SQL to do maintenance when necessary.
Another common way is to store the settings in XML, Json or properties files (the latter being more common because it is a Java platform standard). I do not recommend pointing directly to a directory, but using the same technique described above, that is, using an environment variable. The environment variable can be set on several levels: operating system, OS user, JVM, Application Server or for a single application.
In any case, a very important item is to have a roadmap for initial installation and system upgrade.
It is also important to do a kind of application homologation on the various application servers. Unfortunately there are huge differences between them and compatibility is not always so easy to achieve. Some JEE servers come with a library truck that may conflict with what your application has, each has a way to resolve these conflicts.
Updating
Specific settings in Tomcat
No Tomcat, the context.xml
is the ideal configuration file to point to Data Sources, mainly with regard to databases. Just use the tag <Resource>
, in accordance with documentation.
You can also use the file context to define context parameters or application-specific environment variables. See documentation.
Specific settings in Jboss
In Jboss you can configure the Data Sources in the archive standalone.xml
, located in a server subdirectory.
There are other files for specific application settings, see documentation here.
These configuration files are inside WAR and as well as the file context.xml
of the Tomcat, are called Deployment Descriptors, because they are files that configure the deploy application on a specific server. Each server should have its own, with more or less flexibility.
How to load a specific file per application
You can also upload a different external configuration file for each application or application instance as follows:
- Set an environment variable by pointing to a configuration directory, for example:
/path/to/conf
.
- Create a
ServletContextListener
to load the settings during the startup of the system.
- Read the configuration using the configured directory, plus the context path as file name.
Thus if there are two instances of the same application in different contexts, for example /myappinstance1
and /myappinstance2
, each will access its own configuration, respectively:
/path/to/conf/myappinstance1.properties
/path/to/conf/myappinstance2.properties
To solve the second item, I usually distribute a file config.properties.model with the default settings (I do not include in any package config.properties). Then, in the installation the client removes the .model the name of the file and customize its content; and during the update the client file will never be replaced as the overwritten file will only be the template file.
– Caffé
Welcome to SOPT! I removed the greetings and thanks from your question (http://meta.pt.stackoverflow.com/a/851/3907) and made it more objective. Please read this Meta question to better understand why I did it.
– fpg1503