JBoss AS 7 Application-Specific Properties File

I have several independent Java EE modules (WAR applications and EJB JAR modules) that I deploy to JBoss 7.1.1 AS. I want:

  • Centralize the configuration of these modules in a single * .properties file.
  • Make this file available in the classpath.
  • Keep the installation / configuration of this file as simple as possible. It would be ideal to just put it in some JBoss folder, for example: $ {JBOSS_HOME} / stand-alone / configuration file.
  • Make changes to this file without restarting the application server.

Is it possible?

I already found this link: How to put an external file in the classpath , which explains that the preferred way to do this is to make a static JBoss module. But I have to depend on this static module in every module of the application that I am deploying, which is a kind of combination that I am trying to avoid.

+7
source share
3 answers

Perhaps a simple solution is to read the file from a singleton or static class.

private static final String CONFIG_DIR_PROPERTY = "jboss.server.config.dir"; private static final String PROPERTIES_FILE = "application-xxx.properties"; private static final Properties PROPERTIES = new Properties(); static { String path = System.getProperty(CONFIG_DIR_PROPERTY) + File.separator + PROPERTIES_FILE; try { PROPERTIES.load(new FileInputStream(path)); } catch (MalformedURLException e) { //TODO } catch (IOException e) { //TODO } } 
+5
source

Here is a complete example using only CDI taken from this site .

This configuration will also work for JBoss AS7.

  • Create and fill in the properties file inside the WildFly configuration folder

     $ echo 'docs.dir=/var/documents' >> .standalone/configuration/application.properties 
  • Add a system property to the WildFly configuration file.

     $ ./bin/jboss-cli.sh --connect [ standalone@localhost :9990 /] /system-property=application.properties:add(value=${jboss.server.config.dir}/application.properties) 

This will add the following to your server configuration file (standalone.xml or domain.xml):

 <system-properties> <property name="application.properties" value="${jboss.server.config.dir}/application.properties"/> </system-properties> 
  1. Create a singleton bean session that downloads and saves application properties in general

     import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.util.HashMap; import java.util.Map; import java.util.Properties; import javax.annotation.PostConstruct; import javax.ejb.Singleton; @Singleton public class PropertyFileResolver { private Logger logger = Logger.getLogger(PropertyFileResolver.class); private String properties = new HashMap<>(); @PostConstruct private void init() throws IOException { //matches the property name as defined in the system-properties element in WildFly String propertyFile = System.getProperty("application.properties"); File file = new File(propertyFile); Properties properties = new Properties(); try { properties.load(new FileInputStream(file)); } catch (IOException e) { logger.error("Unable to load properties file", e); } HashMap hashMap = new HashMap<>(properties); this.properties.putAll(hashMap); } public String getProperty(String key) { return properties.get(key); } } 
  2. Create a CDI qualifier. We will use this annotation for the Java variables that we want to introduce.

     import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import javax.inject.Qualifier; @Qualifier @Retention(RetentionPolicy.RUNTIME) @Target({ ElementType.METHOD, ElementType.FIELD, ElementType.CONSTRUCTOR }) public @interface ApplicationProperty { // no default meaning a value is mandatory @Nonbinding String name(); } 
  3. Create a producer method; this generates the object to be inserted

     import javax.enterprise.inject.Produces; import javax.enterprise.inject.spi.InjectionPoint; import javax.inject.Inject; public class ApplicaitonPropertyProducer { @Inject private PropertyFileResolver fileResolver; @Produces @ApplicationProperty(name = "") public String getPropertyAsString(InjectionPoint injectionPoint) { String propertyName = injectionPoint.getAnnotated().getAnnotation(ApplicationProperty.class).name(); String value = fileResolver.getProperty(propertyName); if (value == null || propertyName.trim().length() == 0) { throw new IllegalArgumentException("No property found with name " + value); } return value; } @Produces @ApplicationProperty(name="") public Integer getPropertyAsInteger(InjectionPoint injectionPoint) { String value = getPropertyAsString(injectionPoint); return value == null ? null : Integer.valueOf(value); } } 
  4. Finally, add the property to one of your CDI beans

     import javax.ejb.Stateless; import javax.inject.Inject; @Stateless public class MySimpleEJB { @Inject @ApplicationProperty(name = "docs.dir") private String myProperty; public String getProperty() { return myProperty; } } 
+2
source

./standalone.sh --server-config = standalone-gdrais.xml --properties = / home / user / programs / jboss / jboss-eap-6.0 / standalone / configuration / file.properties -b 0.0.0.0

0
source

All Articles