Created
July 14, 2011 18:35
-
-
Save agentgt/1083089 to your computer and use it in GitHub Desktop.
Simple way to do configuration for a web app.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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