views:

97

answers:

4

I have a GlassFish/j2ee application, and I develop on one box, and production is a remote box. I have a function that makes files, and I need the files' location to be different based on my dev box or production. What is an automatic way to do the switching so I do not have to edit the source file based on where it is being deployed?

+1  A: 

The easiest approach is to define a system property which specifies where the file system location for your data is. The production appserver would define one value (using java -D in the startup script), and your dev app server would define another value. Your application source would consult the system property value (using System.getProperty()) to discover the appropriate location.

skaffman
System properties are global. This is a bad habit to acquire (says one who had to undo the use of System.properties in a library coming from a stand alone application only being used by one, to a web application used by many - oh the memories)
Thorbjørn Ravn Andersen
Yes, they are global to the JVM. Are you telling me you'd mix production and non-production applications in the same JVM? In most things I'd agree, using system properties is a bad move, but this sort of thing is *exactly* what system properties are for.
skaffman
That would assume your application server only runs one web-app that needs to know a storage folder. Setting a property in the web-app context might be better.
rsp
Putting the config in the webapp context is little better than poutting it in the source code. You don't want to have to mess with application internals. If you have more than one webapp in the same JVM, then use different system properties to configure them.
skaffman
i added a a system property, "foo", and set the value as "/me/bar/".In code, i tride to access by: String dir= System.getProperty("foo");but it returned null.Am i using it properly?
bmw0128
How are you defining the property? I suggest posting a new question concerning this.
skaffman
A: 

Essentially you are trying to store arbitrary data that changes depending on your environment. If you you currently do that in your app (ie a database) I would just put it in there.

chotchki
Which will be a problem if you try to sync PROD database to DEV and blow up the customizations there.
Andrei Taranchenko
+1  A: 

Put the information you need in JNDI - that's what it is designed for.

Consider letting your application refuse to do anything if the information is not there.

Thorbjørn Ravn Andersen
+2  A: 

You can use a properties file and externalize environment specific stuff (like your "file location") in this configuration file. Put the adequate value in it at build time (e.g. using filtering and profiles with Maven). Include this file in the application or make it available somewhere on the classpath.

Pascal Thivent