Skip to content

Instantly share code, notes, and snippets.

@agentgt
Created July 14, 2011 18:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save agentgt/1083089 to your computer and use it in GitHub Desktop.
Save agentgt/1083089 to your computer and use it in GitHub Desktop.
Simple way to do configuration for a web app.
package com.evocatus.util;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class SimpleContextListenerConfig /*extend ResourceBundle */ implements ServletContextListener{
private ServletContext servletContext;
@Override
public void contextInitialized(ServletContextEvent sce) {
servletContext = sce.getServletContext();
servletContext.setAttribute(getClass().getCanonicalName(), this);
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
}
public static String getProperty(ServletContext sc, String propName, String defaultValue) {
SimpleContextListenerConfig config = getConfig(sc);
return config.getProperty(propName, defaultValue);
}
public static SimpleContextListenerConfig getConfig(ServletContext sc) {
SimpleContextListenerConfig config =
(SimpleContextListenerConfig) sc.getAttribute(SimpleContextListenerConfig.class.getCanonicalName());
return config;
}
public String getProperty(String propName, String defaultValue)
{
/*
* TODO cache properties
*/
String property = null;
if (property == null)
property = servletContext.getInitParameter(propName);
if (property == null)
System.getProperty(propName, null);
//TODO Get From resource bundle
if (property == null)
property = defaultValue;
return property;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment