Clover coverage report - Coverage timestamp: Sa Jul 7 2007 09:11:40 CEST FRAMES NO FRAMES |
file stats: | LOC: | 189 |  | Methods: | 8
| NCLOC: | 87 |  | Classes: | 1
Source file | Conditionals | Statements | Methods | TOTAL
Config.java | 45% | 62,8% | 62,5% | 57,7% |

1 |  | /*
2 |  | * Copyright (c) 2002-2007 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.IOException;
11 |  | import java.io.InputStream;
12 |  | import java.net.URL;
13 |  |
14 |  | import java.util.Properties;
15 |  |
16 |  | /**
17 |  | * Responsible for holding the Cache configuration properties. If the default
18 |  | * constructor is used, this class will load the properties from the
19 |  | * <code>cache.configuration</code>.
20 |  | *
21 |  | * @author <a href="mailto:fabian.crabus@gurulogic.de">Fabian Crabus</a>
22 |  | * @version $Revision: 412 $
23 |  | */
24 |  | public class Config implements java.io.Serializable {
25 |  |
26 |  | private static final transient Log log = LogFactory.getLog(Config.class);
27 |  |
28 |  | /**
29 |  | * Name of the properties file.
30 |  | */
31 |  | private final static String PROPERTIES_FILENAME = "/oscache.properties";
32 |  |
33 |  | /**
34 |  | * Properties map to hold the cache configuration.
35 |  | */
36 |  | private Properties properties = null;
37 |  |
38 |  | /**
39 |  | * Create an OSCache Config that loads properties from oscache.properties.
40 |  | * The file must be present in the root of OSCache's classpath. If the file
41 |  | * cannot be loaded, an error will be logged and the configuration will
42 |  | * remain empty.
43 |  | */
44 | 0 | public Config() {
45 | 0 | this(null);
46 |  | }
47 |  |
48 |  | /**
49 |  | * Create an OSCache configuration with the specified properties.
50 |  | * Note that it is the responsibility of the caller to provide valid
51 |  | * properties as no error checking is done to ensure that required
52 |  | * keys are present. If you're unsure of what keys should be present,
53 |  | * have a look at a sample oscache.properties file.
54 |  | *
55 |  | * @param p The properties to use for this configuration. If null,
56 |  | * then the default properties are loaded from the <code>oscache.properties</code>
57 |  | * file.
58 |  | */
59 | 285 | public Config(Properties p) {
60 | 285 | if (log.isDebugEnabled()) {
61 | 0 | log.debug("OSCache: Config called");
62 |  | }
63 |  |
64 | 285 | if (p == null) {
65 | 145 | this.properties = loadProperties(PROPERTIES_FILENAME, "the default configuration");
66 |  | } else {
67 | 140 | this.properties = p;
68 |  | }
69 |  | }
70 |  |
71 |  | /**
72 |  | * Retrieve the value of the named configuration property. If the property
73 |  | * cannot be found this method will return <code>null</code>.
74 |  | *
75 |  | * @param key The name of the property.
76 |  | * @return The property value, or <code>null</code> if the value could
77 |  | * not be found.
78 |  | *
79 |  | * @throws IllegalArgumentException if the supplied key is null.
80 |  | */
81 | 2427 | public String getProperty(String key) {
82 | 2427 | if (key == null) {
83 | 5 | throw new IllegalArgumentException("key is null");
84 |  | }
85 |  |
86 | 2422 | if (properties == null) {
87 | 0 | return null;
88 |  | }
89 |  |
90 | 2422 | return properties.getProperty(key);
91 |  | }
92 |  |
93 |  | /**
94 |  | * Retrieves all of the configuration properties. This property set
95 |  | * should be treated as immutable.
96 |  | *
97 |  | * @return The configuration properties.
98 |  | */
99 | 0 | public Properties getProperties() {
100 | 0 | return properties;
101 |  | }
102 |  |
103 | 206 | public Object get(Object key) {
104 | 206 | return properties.get(key);
105 |  | }
106 |  |
107 |  | /**
108 |  | * Sets a configuration property.
109 |  | *
110 |  | * @param key The unique name for this property.
111 |  | * @param value The value assigned to this property.
112 |  | *
113 |  | * @throws IllegalArgumentException if the supplied key is null.
114 |  | */
115 | 0 | public void set(Object key, Object value) {
116 | 0 | if (key == null) {
117 | 0 | throw new IllegalArgumentException("key is null");
118 |  | }
119 |  |
120 | 0 | if (value == null) {
121 | 0 | return;
122 |  | }
123 |  |
124 | 0 | if (properties == null) {
125 | 0 | properties = new Properties();
126 |  | }
127 |  |
128 | 0 | properties.put(key, value);
129 |  | }
130 |  |
131 |  | /**
132 |  | * Load the properties from the specified URL.
133 |  | * @param url a non null value of the URL to the properties
134 |  | * @param info additional logger information if the properties can't be read
135 |  | * @return the loaded properties specified by the URL
136 |  | * @since 2.4
137 |  | */
138 | 145 | public static Properties loadProperties(URL url, String info) {
139 | 145 | log.info("OSCache: Getting properties from URL " + url + " for " + info);
140 |  |
141 | 145 | Properties properties = new Properties();
142 | 145 | InputStream in = null;
143 |  |
144 | 145 | try {
145 | 145 | in = url.openStream();
146 | 145 | properties.load(in);
147 | 145 | log.info("OSCache: Properties read " + properties);
148 |  | } catch (Exception e) {
149 | 0 | log.error("OSCache: Error reading from " + url, e);
150 | 0 | log.error("OSCache: Ensure the properties information in " + url+ " is readable and in your classpath.");
151 |  | } finally {
152 | 145 | try {
153 | 145 | in.close();
154 |  | } catch (IOException e) {
155 | 0 | log.warn("OSCache: IOException while closing InputStream: " + e.getMessage());
156 |  | }
157 |  | }
158 |  |
159 | 145 | return properties;
160 |  | }
161 |  |
162 |  | /**
163 |  | * Load the specified properties file from the classpath. If the file
164 |  | * cannot be found or loaded, an error will be logged and no
165 |  | * properties will be set.
166 |  | * @param filename the properties file with path
167 |  | * @param info additional logger information if file can't be read
168 |  | * @return the loaded properties specified by the filename
169 |  | * @since 2.4
170 |  | */
171 | 145 | public static Properties loadProperties(String filename, String info) {
172 | 145 | URL url = null;
173 |  |
174 | 145 | ClassLoader threadContextClassLoader = Thread.currentThread().getContextClassLoader();
175 | 145 | if (threadContextClassLoader != null) {
176 | 145 | url = threadContextClassLoader.getResource(filename);
177 |  | }
178 | 145 | if (url == null) {
179 | 145 | url = Config.class.getResource(filename);
180 | 145 | if (url == null) {
181 | 0 | log.warn("OSCache: No properties file found in the classpath by filename " + filename);
182 | 0 | return new Properties();
183 |  | }
184 |  | }
185 |  |
186 | 145 | return loadProperties(url, info);
187 |  | }
188 |  |
189 |  | }