KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > oscache > base > Config


1 /*
2  * Copyright (c) 2002-2003 by OpenSymphony
3  * All rights reserved.
4  */

5 package com.opensymphony.oscache.base;
6
7 import org.apache.commons.logging.Log;
8 import org.apache.commons.logging.LogFactory;
9
10 import java.io.InputStream JavaDoc;
11
12 import java.util.Properties JavaDoc;
13
14 /**
15  * Responsible for holding the Cache configuration properties. If the default
16  * constructor is used, this class will load the properties from the
17  * <code>cache.configuration</code>.
18  *
19  * @author <a HREF="mailto:fabian.crabus@gurulogic.de">Fabian Crabus</a>
20  * @version $Revision: 1.1 $
21  */

22 public class Config implements java.io.Serializable JavaDoc {
23     private static final transient Log log = LogFactory.getLog(Config.class);
24
25     /**
26      * Name of the properties file.
27      */

28     private final static String JavaDoc PROPERTIES_FILENAME = "/oscache.properties";
29
30     /**
31      * Properties map to hold the cache configuration.
32      */

33     private Properties JavaDoc properties = null;
34
35     /**
36      * Create an OSCache Config that loads properties from oscache.properties.
37      * The file must be present in the root of OSCache's classpath. If the file
38      * cannot be loaded, an error will be logged and the configuration will
39      * remain empty.
40      */

41     public Config() {
42         this(null);
43     }
44
45     /**
46      * Create an OSCache configuration with the specified properties.
47      * Note that it is the responsibility of the caller to provide valid
48      * properties as no error checking is done to ensure that required
49      * keys are present. If you're unsure of what keys should be present,
50      * have a look at a sample oscache.properties file.
51      *
52      * @param p The properties to use for this configuration. If null,
53      * then the default properties are loaded from the <code>oscache.properties</code>
54      * file.
55      */

56     public Config(Properties JavaDoc p) {
57         if (log.isDebugEnabled()) {
58             log.debug("Config() called");
59         }
60
61         if (p == null) {
62             loadProps();
63         } else {
64             this.properties = p;
65         }
66     }
67
68     /**
69      * Retrieve the value of the named configuration property. If the property
70      * cannot be found this method will return <code>null</code>.
71      *
72      * @param key The name of the property.
73      * @return The property value, or <code>null</code> if the value could
74      * not be found.
75      *
76      * @throws IllegalArgumentException if the supplied key is null.
77      */

78     public String JavaDoc getProperty(String JavaDoc key) {
79         if (key == null) {
80             throw new IllegalArgumentException JavaDoc("key is null");
81         }
82
83         if (properties == null) {
84             return null;
85         }
86
87         String JavaDoc value = properties.getProperty(key);
88         return value;
89     }
90
91     /**
92      * Retrieves all of the configuration properties. This property set
93      * should be treated as immutable.
94      *
95      * @return The configuration properties.
96      */

97     public Properties JavaDoc getProperties() {
98         return properties;
99     }
100
101     public Object JavaDoc get(Object JavaDoc key) {
102         return properties.get(key);
103     }
104
105     /**
106      * Sets a configuration property.
107      *
108      * @param key The unique name for this property.
109      * @param value The value assigned to this property.
110      *
111      * @throws IllegalArgumentException if the supplied key is null.
112      */

113     public void set(Object JavaDoc key, Object JavaDoc value) {
114         if (key == null) {
115             throw new IllegalArgumentException JavaDoc("key is null");
116         }
117
118         if (value == null) {
119             return;
120         }
121
122         if (properties == null) {
123             properties = new Properties JavaDoc();
124         }
125
126         properties.put(key, value);
127     }
128
129     /**
130      * Load the properties file (<code>oscache.properties</code>)
131      * from the classpath. If the file cannot be found or loaded, an error
132      * will be logged and no properties will be set.
133      */

134     private void loadProps() {
135         if (log.isDebugEnabled()) {
136             log.debug("Getting Config");
137         }
138
139         properties = new Properties JavaDoc();
140
141         InputStream JavaDoc in = null;
142
143         try {
144             in = Config.class.getResourceAsStream(PROPERTIES_FILENAME);
145             properties.load(in);
146             log.info("Properties " + properties);
147         } catch (Exception JavaDoc e) {
148             log.error("Error reading " + PROPERTIES_FILENAME + " in CacheAdministrator.loadProps() " + e);
149             log.error("Ensure the " + PROPERTIES_FILENAME + " file is readable and in your classpath.");
150         } finally {
151             try {
152                 in.close();
153             } catch (Exception JavaDoc e) {
154                 // Ignore errors that occur while closing file
155
}
156         }
157     }
158 }
159
Popular Tags