001: /*
002: * Copyright (c) 2002-2007 by OpenSymphony
003: * All rights reserved.
004: */
005: package com.opensymphony.oscache.base;
006:
007: import org.apache.commons.logging.Log;
008: import org.apache.commons.logging.LogFactory;
009:
010: import java.io.IOException;
011: import java.io.InputStream;
012: import java.net.URL;
013:
014: import java.util.Properties;
015:
016: /**
017: * Responsible for holding the Cache configuration properties. If the default
018: * constructor is used, this class will load the properties from the
019: * <code>cache.configuration</code>.
020: *
021: * @author <a href="mailto:fabian.crabus@gurulogic.de">Fabian Crabus</a>
022: * @version $Revision: 412 $
023: */
024: public class Config implements java.io.Serializable {
025:
026: private static final transient Log log = LogFactory
027: .getLog(Config.class);
028:
029: /**
030: * Name of the properties file.
031: */
032: private final static String PROPERTIES_FILENAME = "/oscache.properties";
033:
034: /**
035: * Properties map to hold the cache configuration.
036: */
037: private Properties properties = null;
038:
039: /**
040: * Create an OSCache Config that loads properties from oscache.properties.
041: * The file must be present in the root of OSCache's classpath. If the file
042: * cannot be loaded, an error will be logged and the configuration will
043: * remain empty.
044: */
045: public Config() {
046: this (null);
047: }
048:
049: /**
050: * Create an OSCache configuration with the specified properties.
051: * Note that it is the responsibility of the caller to provide valid
052: * properties as no error checking is done to ensure that required
053: * keys are present. If you're unsure of what keys should be present,
054: * have a look at a sample oscache.properties file.
055: *
056: * @param p The properties to use for this configuration. If null,
057: * then the default properties are loaded from the <code>oscache.properties</code>
058: * file.
059: */
060: public Config(Properties p) {
061: if (log.isDebugEnabled()) {
062: log.debug("OSCache: Config called");
063: }
064:
065: if (p == null) {
066: this .properties = loadProperties(PROPERTIES_FILENAME,
067: "the default configuration");
068: } else {
069: this .properties = p;
070: }
071: }
072:
073: /**
074: * Retrieve the value of the named configuration property. If the property
075: * cannot be found this method will return <code>null</code>.
076: *
077: * @param key The name of the property.
078: * @return The property value, or <code>null</code> if the value could
079: * not be found.
080: *
081: * @throws IllegalArgumentException if the supplied key is null.
082: */
083: public String getProperty(String key) {
084: if (key == null) {
085: throw new IllegalArgumentException("key is null");
086: }
087:
088: if (properties == null) {
089: return null;
090: }
091:
092: return properties.getProperty(key);
093: }
094:
095: /**
096: * Retrieves all of the configuration properties. This property set
097: * should be treated as immutable.
098: *
099: * @return The configuration properties.
100: */
101: public Properties getProperties() {
102: return properties;
103: }
104:
105: public Object get(Object key) {
106: return properties.get(key);
107: }
108:
109: /**
110: * Sets a configuration property.
111: *
112: * @param key The unique name for this property.
113: * @param value The value assigned to this property.
114: *
115: * @throws IllegalArgumentException if the supplied key is null.
116: */
117: public void set(Object key, Object value) {
118: if (key == null) {
119: throw new IllegalArgumentException("key is null");
120: }
121:
122: if (value == null) {
123: return;
124: }
125:
126: if (properties == null) {
127: properties = new Properties();
128: }
129:
130: properties.put(key, value);
131: }
132:
133: /**
134: * Load the properties from the specified URL.
135: * @param url a non null value of the URL to the properties
136: * @param info additional logger information if the properties can't be read
137: * @return the loaded properties specified by the URL
138: * @since 2.4
139: */
140: public static Properties loadProperties(URL url, String info) {
141: log.info("OSCache: Getting properties from URL " + url
142: + " for " + info);
143:
144: Properties properties = new Properties();
145: InputStream in = null;
146:
147: try {
148: in = url.openStream();
149: properties.load(in);
150: log.info("OSCache: Properties read " + properties);
151: } catch (Exception e) {
152: log.error("OSCache: Error reading from " + url, e);
153: log.error("OSCache: Ensure the properties information in "
154: + url + " is readable and in your classpath.");
155: } finally {
156: try {
157: in.close();
158: } catch (IOException e) {
159: log
160: .warn("OSCache: IOException while closing InputStream: "
161: + e.getMessage());
162: }
163: }
164:
165: return properties;
166: }
167:
168: /**
169: * Load the specified properties file from the classpath. If the file
170: * cannot be found or loaded, an error will be logged and no
171: * properties will be set.
172: * @param filename the properties file with path
173: * @param info additional logger information if file can't be read
174: * @return the loaded properties specified by the filename
175: * @since 2.4
176: */
177: public static Properties loadProperties(String filename, String info) {
178: URL url = null;
179:
180: ClassLoader threadContextClassLoader = Thread.currentThread()
181: .getContextClassLoader();
182: if (threadContextClassLoader != null) {
183: url = threadContextClassLoader.getResource(filename);
184: }
185: if (url == null) {
186: url = Config.class.getResource(filename);
187: if (url == null) {
188: log
189: .warn("OSCache: No properties file found in the classpath by filename "
190: + filename);
191: return new Properties();
192: }
193: }
194:
195: return loadProperties(url, info);
196: }
197:
198: }
|