How to load property file from glassfish config's folder
Tech-Today

How to load property file from glassfish config's folder


In seam you can define a component class in components.xml that will load 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 class (this can be extended and more logic can be added such as caching of properties):
public class PropertyBean {
private static final Logger log = Logger.getLogger(PropertyBean.class);
private String _propertyFile;
private Properties properties;
private static PropertyBean instance = null;

private 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();
}

public static PropertyBean getInstance(String propertiesName) {
setInstance(new PropertyBean(propertiesName));
}

public void initialize() {
if (_propertyFile.startsWith("file:")) {
_propertyFile = _propertyFile.substring(5);
}

FileInputStream propertyFile = null;
try {
Properties pr = new Properties();
pr.load(new FileInputStream(_propertyFile));
setProperties(pr);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (propertyFile != null) {
try {
propertyFile.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}

public void setProperties(Properties new_properties) {
properties = new_properties;
}
}
In glassfish the same can be achieve, but there's no component.xml where we could define the property bean, so instead we need to define a producer class, but first we need 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 })
public @interface ConfiguredBy {
@Nonbinding
public 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
*/
public class PropertyProducer {
@Inject
Logger log;

Properties propertyCache;

/**
* Glassfish instace root. The glassfish domain where the application is running.
*/
private static String glassfishInstanceRootPropertyName = "com.sun.aas.instanceRoot";

/**
* Glassfish configuration folder.
*/
private static String glassfishDomainConfigurationFolderName = "config";

/**
* Reads a property file from glassfish config folder.
* @param fileName
* @return
* @throws FileNotFoundException
*/
private static File readFileFromGlassfishDomainConfigFolder(
final String fileName) throws FileNotFoundException {
// Instance Root folder
final String instanceRoot = System
.getProperty(glassfishInstanceRootPropertyName);
if (instanceRoot == null) {
throw new FileNotFoundException(
"Cannot find Glassfish instanceRoot. Is the com.sun.aas.instanceRoot system property set?");
}

// Instance Root + /config folder
File configurationFolder = new File(instanceRoot + File.separator
+ glassfishDomainConfigurationFolderName);
File configFile = new File(configurationFolder, fileName);

// return the given file
return configFile;
}

/**
* Produces a java.util.Properties object based on the qualifier
* ConfiguredBy and the injection point.
*
* Behavior is as follows: - the value can be a System property or a path. -
* If it's a system property, we convert it to the value of that property
* first
*
* - Then we check to see if the value now is an absolute path or a
* classpath entry - We try both.
*/
@Produces
@ConfiguredBy("")
public Properties produceProperties(InjectionPoint ip) {
Properties props;
props = createProperties(ip);

return props;
}

/**
* Read the application configuration property.
* @param ip
* @return
*/
private Properties createProperties(InjectionPoint ip) {
Properties p = new Properties();
String value = ip.getAnnotated().getAnnotation(ConfiguredBy.class)
.value();

try {
File configFile = readFileFromGlassfishDomainConfigFolder(value);
InputStream is = new FileInputStream(configFile);
p.load(is);
is.close();
} catch (Exception e) {
log.debug("Problem reading the file, ignoring.");
}
return p;
}
}
The ConfigBean where we will inject the property 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
public class ConfigBean {
@Inject
@ConfiguredBy("/ipiel.properties")
private Properties properties;

@Inject
private Logger log;

public String getProperty(String propertyName) {
String result = properties.getProperty(propertyName);
if (result == null) {
log.warn("No property value found for a propety {}", propertyName);
}
return result;
}
}
Then we can inject ConfigBean on any class we have to access the properties, example:
public class Foo {
@Inject
private ConfigBean configBean;
}




- Do Some Initialization Work After The Web Application Initialization Process
We can achieve this by using a webListener. See code below: package com.czetsuya; import java.text.MessageFormat; import java.util.ResourceBundle; import javax.inject.Inject; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener;...

- Jax-rs 2.0 Security Tutorial In Javaee And Jboss
This tutorial will summarize how the author was able to call a secured rest webservice using resteasy. We will not go into detail on how we build the entire project since the code is already pushed at github. Basically we will just note down the most...

- How To Create A Custom Bean Validation In Javaee6
Bean validation has been mentioned on many articles online but I found few that explain the actual class that implements the constraint validator. For example: the bean validation article from  oracle javaee6 documentation: http://docs.oracle.com/javaee/6/tutorial/doc/gkfgx.html....

- Loading Locale From Post/get Parameter
This page will summarize how to load a resource bundle dynamically from a GET/POST parameter.  1.) In your mail xhtml template, make sure that you define f:view locale before the h:body tag: <f:view locale="#{languageBean.currentLocale}" />...

- How To Get Started With Hibernate In Java. Create A Simple Class And Database Schema For Demostration.
Since, I've always been using different database sometimes I get confused how to implement the others. Like hibernate, where the configuration must be properly set. *This help assumes that you have already created a java project in eclipse that has...



Tech-Today








.