1 package com.pow2.resources;
2
3
4 import java.util.HashMap;
5 import java.util.Properties;
6
7 import javax.servlet.ServletException;
8
9 import com.pow2.util.FileUtil;
10 import com.pow2.util.Util;
11
12
13 /***
14 * Properties init servlet.
15 *
16 * Define the following servlet in the web.xml file for your web-application.
17 *
18 * <pre><code>
19 * <servlet>
20 * <servlet-name>log4j-init</servlet-name>
21 * <servlet-class>com.pow2.resources.PropertiesInit</servlet-class>
22 *
23 * <init-param>
24 * <param-name>properties-init-fileKey</param-name>
25 * <param-value>myProperties</param-value>
26 * </init-param>
27 *
28 * <init-param>
29 * <param-name>properties-init-fileValue</param-name>
30 * <param-value>WEB-INF/myProperties.properties</param-value>
31 * </init-param>
32 *
33 * <load-on-startup>1</load-on-startup>
34 * </servlet>
35 * </code></pre>
36 *
37 *
38 * CVS info:
39 * $Id: PropertiesInit.java,v 1.1.1.1 2004/08/31 20:22:46 foxat Exp $
40 *
41 * @author Fossato
42 * @created 2 maggio 2002
43 */
44 public class PropertiesInit extends InitServlet
45 {
46 private static HashMap propertiesMap = new HashMap();
47
48
49 /***
50 * Initialize this servlet.
51 *
52 * @exception ServletException if any error occurs
53 */
54 public void init() throws ServletException
55 {
56 super.init();
57
58 String key = getInitParameter("properties-init-fileKey");
59 String file = getInitParameterFilePath("properties-init-fileValue");
60
61
62 if (Util.isNull(key) || Util.isNull(file))
63 {
64 String err = "cannot configure the properties object. Properties config key or file is null";
65 cat.error("::init - " + err);
66 throw new ServletException(err);
67 }
68
69 try
70 {
71 setProperties(key, FileUtil.getProperties(file));
72 }
73 catch (Exception e)
74 {
75 cat.error("::init - cannot set a new properties object into the propertiesMap", e);
76 throw new ServletException(e.getMessage());
77 }
78
79 cat.info("::init - properties object successful added to the propertiesMap using key, file [" + key + ", " + file + "]");
80 }
81
82
83 /***
84 * Gets the properties object stored into the propertiesMap identified by the input key
85 *
86 * @param key the key to use to retrieve the Properties object from the propertiesMap
87 * @return The properties object
88 */
89 public static Properties getProperties(String key)
90 {
91 return (Properties)propertiesMap.get(key);
92 }
93
94
95 /***
96 * Sets the properties object into the propertiesMap using the input key
97 *
98 * @param key the key to use to store the Properties object into the propertiesMap
99 * @param newProperties The new properties object to store
100 */
101 public static void setProperties(String key, Properties newProperties)
102 {
103 propertiesMap.put(key, newProperties);
104 }
105 }