001 package com.thetransactioncompany.util;
002
003
004 import java.util.Enumeration;
005 import java.util.Properties;
006
007 import javax.servlet.ServletConfig;
008 import javax.servlet.ServletContext;
009
010
011 /**
012 * Static utility methods for converting servlet and context initialisation
013 * parameters to Java properties.
014 *
015 * @author Vladimir Dzhuvinov
016 */
017 public class PropertyConverter {
018
019
020 /**
021 * Converts the servlet initialisation parameters (typically specified
022 * in the {@code web.xml} file) to a Java properties hashtable. The
023 * parameter names become property keys.
024 *
025 * <p>Note regarding {@code web.xml} markup: The servlet initialisation
026 * parameters have an XML tag {@code <init-param>} and are defined
027 * within the {@code <servlet>} element.
028 *
029 * @param config The servlet configuration.
030 *
031 * @return The servlet initialisation parameters as Java properties.
032 */
033 public static Properties getServletInitParameters(final ServletConfig config) {
034
035 Properties props = new Properties();
036
037 Enumeration en = config.getInitParameterNames();
038
039 while (en.hasMoreElements()) {
040
041 String key = (String)en.nextElement();
042 String value = config.getInitParameter(key);
043
044 props.setProperty(key, value);
045 }
046
047 return props;
048 }
049
050
051 /**
052 * Converts the servlet context initialisation parameters (typically
053 * specified in the {@code web.xml} file) to a Java properties
054 * hashtable. The parameter names become property keys.
055 *
056 * <p>Note regarding {@code web.xml} markup: The context parameters
057 * have an XML tag {@code <context-param>} and are defined within the
058 * {@code <web-app>} element.
059 *
060 * @param ctx The servlet context.
061 *
062 * @return The servlet context parameters as Java properties.
063 */
064 public static Properties getServletCtxInitParameters(final ServletContext ctx) {
065
066 Properties props = new Properties();
067
068 Enumeration en = ctx.getInitParameterNames();
069
070 while (en.hasMoreElements()) {
071
072 String key = (String)en.nextElement();
073 String value = ctx.getInitParameter(key);
074
075 props.setProperty(key, value);
076 }
077
078 return props;
079 }
080 }