How To Charge Holding File From Glassfish Config's Folder

In seam yous tin go define a element shape inwards components.xml that volition charge the properties from the JBOSS_CONFIG folder. Add the ff lines:
<component name="paramBean" class="com.ipil.PropertyBean" scope="application" auto-create="true" startup="true">  <property name="filename">myproject.properties</property>  <property name="reload">@debug@</property> </component> 
Then the PropertyBean shape (this tin go live extended too to a greater extent than logic tin go live added such every bit caching of properties):
public shape PropertyBean {     someone static lastly Logger log = Logger.getLogger(PropertyBean.class);     someone String _propertyFile;         someone Properties properties;     someone static PropertyBean illustration = null;          someone PropertyBean(String name) {         super();         if (System.getProperty(name) != null) {             _propertyFile = System.getProperty(name);         } else {    //load from jboss.server.config.url                         _propertyFile = System.getProperty("jboss.server.config.url") + name;         }         initialize();     }          world static PropertyBean getInstance(String propertiesName) {   setInstance(new PropertyBean(propertiesName));     }       world void initialize() {   if (_propertyFile.startsWith("file:")) {    _propertyFile = _propertyFile.substring(5);   }      FileInputStream propertyFile = null;   endeavour {    Properties pr = novel Properties();    pr.load(new FileInputStream(_propertyFile));    setProperties(pr);     } grab (Exception e) {    e.printStackTrace();   } finally {    if (propertyFile != null) {     endeavour {      propertyFile.close();     } grab (Exception e) {      e.printStackTrace();     }    }   }  }    world void setProperties(Properties new_properties) {         properties = new_properties;     } } 
In glassfish the same tin go live achieve, merely there's no component.xml where nosotros could define the holding bean, too thence instead nosotros ask to define a producer class, merely outset nosotros ask an annotation:
package com.ipiel.commons.web;  import static java.lang.annotation.ElementType.FIELD; import static java.lang.annotation.ElementType.METHOD; import static java.lang.annotation.ElementType.PARAMETER; import static java.lang.annotation.ElementType.TYPE; import static java.lang.annotation.RetentionPolicy.RUNTIME;  import java.lang.annotation.Retention; import java.lang.annotation.Target;  import javax.enterprise.util.Nonbinding; import javax.inject.Qualifier;  /**  * @author Edward P. Legaspi  * @since Jun 15, 2012  */ @Qualifier @Retention(RUNTIME) @Target({ METHOD, FIELD, PARAMETER, TYPE }) world @interface ConfiguredBy {  @Nonbinding  world String value(); } 
Then the producer class:
package com.ipiel.commons.web;  import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.InputStream; import java.util.Properties;  import javax.enterprise.inject.Produces; import javax.enterprise.inject.spi.InjectionPoint; import javax.inject.Inject;  import org.slf4j.Logger;  /**  * Property file loader.  *   * @author Edward P. Legaspi  * @since Jun 15, 2012  */ world shape PropertyProducer {  @Inject  Logger log;   Properties propertyCache;   /**   * Glassfish instace root. The glassfish domain where the application is running.   */  someone static String glassfishInstanceRootPropertyName = "com.sun.aas.instanceRoot";   /**   * Glassfish configuration folder.   */  someone static String glassfishDomainConfigurationFolderName = "config";    /**   * Reads a holding file from glassfish config folder.   * @param fileName   * @return   * @throws FileNotFoundException   */  someone static File readFileFromGlassfishDomainConfigFolder(    lastly String fileName) throws FileNotFoundException {   // Instance Root folder   lastly String instanceRoot = System     .getProperty(glassfishInstanceRootPropertyName);   if (instanceRoot == null) {    throw novel FileNotFoundException(      "Cannot abide by Glassfish instanceRoot. Is the com.sun.aas.instanceRoot organisation holding set?");   }    // Instance Root + /config folder   File configurationFolder = novel File(instanceRoot + File.separator     + glassfishDomainConfigurationFolderName);   File configFile = novel File(configurationFolder, fileName);    // supply the given file   supply configFile;  }   /**   * Produces a java.util.Properties object based on the qualifier   * ConfiguredBy too the injection point.   *    * Behavior is every bit follows: - the value tin go live a System holding or a path. -   * If it's a organisation property, nosotros convert it to the value of that holding   * outset   *    * - Then nosotros banking concern agree to meet if the value straight off is an absolute path or a   * classpath entry - We endeavour both.   */  @Produces  @ConfiguredBy("")  world Properties produceProperties(InjectionPoint ip) {   Properties props;   props = createProperties(ip);    supply props;  }   /**   * Read the application configuration property.   * @param ip   * @return   */  someone Properties createProperties(InjectionPoint ip) {   Properties p = novel Properties();   String value = ip.getAnnotated().getAnnotation(ConfiguredBy.class)     .value();    endeavour {    File configFile = readFileFromGlassfishDomainConfigFolder(value);    InputStream is = novel FileInputStream(configFile);    p.load(is);    is.close();   } grab (Exception e) {    log.debug("Problem reading the file, ignoring.");   }   supply p;  } } 
The ConfigBean where nosotros volition inject the holding file:
package com.ipiel.pg;  import java.util.Properties;  import javax.enterprise.context.ApplicationScoped; import javax.inject.Inject;  import org.slf4j.Logger;  import com.ipiel.commons.web.ConfiguredBy;  @ApplicationScoped world shape ConfigBean {  @Inject  @ConfiguredBy("/ipiel.properties")  someone Properties properties;   @Inject  someone Logger log;   world String getProperty(String propertyName) {   String outcome = properties.getProperty(propertyName);   if (result == null) {    log.warn("No holding value institute for a propety {}", propertyName);   }   supply result;  } } 
Then nosotros tin go inject ConfigBean on whatsoever shape nosotros bring to access the properties, example:
public shape Foo {   @Inject   someone ConfigBean configBean; } 
Next
Previous
Click here for Comments

0 komentar:

Please comment if there are any that need to be asked.