-
1. Re: Configuration properties outside application
ericjava.eric.chiralsoftware.net Oct 17, 2008 7:01 PM (in response to vdweij)I ran into exactly this question / problem and I did find a good solution. Use MBeans. I set it up so that I can access a configuration MBean through the JMXConsole of JBoss and set values which a) are persistent in the container (JBoss) and b) visible to the application.
I can post the code for how I did this. It's not too complex but it did involve some figuring out. Once you have the code in place it's very easy to use.
You are right, an EAR file should be an application file which isn't modified.
It would be handy if this were in the Seam examples because it's something that every application needs.
-
2. Re: Configuration properties outside application
vdweij Oct 22, 2008 10:56 AM (in response to vdweij)I would like to see the code of the MBean solution. Could you please post it.
-
3. Re: Configuration properties outside application
vdweij Oct 22, 2008 10:57 AM (in response to vdweij)Putting the seam.properties file in the conf directory did not do the trick...
-
4. Re: Configuration properties outside application
duien Oct 22, 2008 7:55 PM (in response to vdweij)I would also like to see this code. I'm trying to solve the same problem but so far most of the solutions I've seen have been for Spring or Struts. Thanks in advance for sharing your solution.
-
5. Re: Configuration properties outside application
rmoore2112 Nov 11, 2008 2:34 PM (in response to vdweij)Try integrating commons-configuration .. the solution below will give you a flexible way to manage configuration in/out from any component (seam,ejb3 or pojo). Configuration files are externalized for environment portability, but can also be packaged with the application.
In the application ear/META-INF, create a file named config.xml
<?xml version="1.0" encoding="ISO-8859-1" ?> <configuration> <xml fileName="config-file1.xml" optional="true" /> <xml fileName="config-file2.xml" /> <xml fileName="config-file3.xml" /> </configuration>
Add this line to the jboss-service.xml file located int the ${jboss.server.home}/conf directory
<classpath codebase="${jboss.server.lib.url:lib/config}" archives="*.xml"/>
Create a directory named config under ${jboss.server.home}/lib/
Create a configuration file named config-file1.xml in the ${jboss.server.home}/lib/ directory
example config-file1.xml
<configuration> <prop1>prop1value</prop1> <prop2>prop2value</prop2> </configuration>
Properties can always be accessed from any class using a configuration factory. This is an old-school singleton for older libraries and unit tests. A more modern approach would use the @Resource annotation combined with a Seam component that initialized the configuration factory.
public class Config { private static Config instance; private static Configuration configuration; public static Config getInstance() { if (instance == null) { try { instance = new Config(); ConfigurationFactory factory = new ConfigurationFactory(); URL configURL = Config.class .getResource("/META-INF/config.xml"); factory.setConfigurationURL(configURL); configuration = factory.getConfiguration(); } catch (ConfigurationException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return instance; } }
One nice feature of commons-config is the jndiConfiguration factory,
Adding the jndi prefix directive to a config file will bind all properties in the naming directory.
<configuration> <jndi prefix="java:comp/env"/> <prop1>prop1value</prop1> <prop2>prop2value</prop2> </configuration>
You can use the @Resource annotation to inject configuration values:
@Resource(name="java:comp/env/prop1") private String prop1;
It would be nice to have a seam component that initialized the commons configuration factory at start-up. I belive this would be trivial using the @Startup annotation. I will try to post a component later this week.
-
6. Re: Configuration properties outside application
vladimir.kovalyuk Dec 9, 2008 3:01 PM (in response to vdweij)Aede van der Weij,
did you try putting components.xml into conf/META-INF folder?
Since conf is in classpath META-INF/components.xml should be loaded by Seam and processed successfully. Moreover, settings in META-INF/components.xml would override settings in .ear.
-
7. Re: Configuration properties outside application
vladimir.kovalyuk Dec 9, 2008 3:03 PM (in response to vdweij)Hmm ... one disadvantage of all those - setttings taken from classpath would affect all the applications deployed into the app server.
-
8. Re: Configuration properties outside application
modoc Feb 23, 2009 9:12 PM (in response to vdweij)For what it's worth, this works perfectly. Thanks!