First, apologies for repeating a variation of an often asked question. However, I am struggling to understand the best approach here. I have an application that uses Hibernate and Glassfish 3. Like others, I would like to be able to load Hibernate properties from outside the war file. The only solution that I have managed to get to work is the following. It loads the properties from the domains/domain1/config folder.
private Properties getLocalHibernateProperties() {
Properties properties = new Properties();
try {
File pf = new File(System.getProperty("user.dir"), PROPERTIES_FILE_NAME);
InputStream inStream = new FileInputStream(pf);
properties.load(inStream);
}
catch (Exception e) {
e.printStackTrace();
}
return properties;
}
However, I have also found and example How to read properties file placed outside war? that uses the ServletContext. Combining the various parts:
in web.xml <listener-class>foo.bar.startup.HibernatePropertiesLoader</listener-class>
public class HibernatePropertiesLoader implements ServletContextListener {
public void contextInitialized(ServletContextEvent event){
ServletContext context = event.getServletContext();
context.setAttribute("settings", new HibernatePropertiesReader(context));
}
public void contextDestroyed(ServletContextEvent event){}
}
public class HibernatePropertiesReader {
ServletContext ctx = null;
public HibernatePropertiesReader(ServletContext ctx) {
this.ctx = ctx;
}
public Properties getLocalHibernateProperties() {
Properties properties = new Properties();
try {
InputStream inStream = ctx.getResourceAsStream(PROPERTIES_FILE);
properties.load(inStream);
}
catch (Exception e) {
e.printStackTrace();
}
return properties;
}
}
I can understand that the second solution will search the CLASSPATH of the Servlet – (is this correct?) and is therefore more flexible than my original solution. However, I don’t understand how to use (access) the properties from my HibernateUtils class. I guess that this means that there is something fundamental missing in my understanding of ServletContext….
any assistance, advice, better solutions appreciated…
The ServletContext.getResourceAsStream is typically used to load a file from the web app itself. For example, if you place your properties files as:
Then you can load the file using
The method can also be used to load “resources” from jar files but the first method is probably better for getting a configuration file. The JavaDocs are quite helpful in this case.
==UPDATE==
I see your problem now is, how do you get the properties object out of the servlet context and into your Hibernate configuration. This depends on what your application layout is like. Are you using Spring?
Assuming you are, a quick and dirty solution would be to have HibernateUitl implement the ServletContextAware interface (assuming HibernateUtil is constructed by Spring) and have code like the following
This couples HibernateUtil to the Servlet API though, which may be undesirable. A slightly cleaner solution might be to create a HibernatePropertiesSource interface and then have a ServletContextHibernatePropertiesSource implementation which has the above methods and inject that into HibernateUtil.
Again, this is all assuming you are using Spring.