|
|||||||||||||||||||
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
Config.java | 43.8% | 63.6% | 57.1% | 57.1% |
|
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;
|
|
11 |
|
|
12 |
import java.util.Properties;
|
|
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.2 $
|
|
21 |
*/
|
|
22 |
public class Config implements java.io.Serializable { |
|
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 PROPERTIES_FILENAME = "/oscache.properties"; |
|
29 |
|
|
30 |
/**
|
|
31 |
* Properties map to hold the cache configuration.
|
|
32 |
*/
|
|
33 |
private Properties 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 | 0 |
public Config() {
|
42 | 0 |
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 | 54 |
public Config(Properties p) {
|
57 | 54 |
if (log.isDebugEnabled()) {
|
58 | 54 |
log.debug("Config() called");
|
59 |
} |
|
60 |
|
|
61 | 54 |
if (p == null) { |
62 | 51 |
loadProps(); |
63 |
} else {
|
|
64 | 3 |
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 | 481 |
public String getProperty(String key) {
|
79 | 481 |
if (key == null) { |
80 | 3 |
throw new IllegalArgumentException("key is null"); |
81 |
} |
|
82 |
|
|
83 | 478 |
if (properties == null) { |
84 | 0 |
return null; |
85 |
} |
|
86 |
|
|
87 | 478 |
String value = properties.getProperty(key); |
88 | 478 |
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 | 0 |
public Properties getProperties() {
|
98 | 0 |
return properties;
|
99 |
} |
|
100 |
|
|
101 | 28 |
public Object get(Object key) {
|
102 | 28 |
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 | 0 |
public void set(Object key, Object value) { |
114 | 0 |
if (key == null) { |
115 | 0 |
throw new IllegalArgumentException("key is null"); |
116 |
} |
|
117 |
|
|
118 | 0 |
if (value == null) { |
119 | 0 |
return;
|
120 |
} |
|
121 |
|
|
122 | 0 |
if (properties == null) { |
123 | 0 |
properties = new Properties();
|
124 |
} |
|
125 |
|
|
126 | 0 |
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 | 51 |
private void loadProps() { |
135 | 51 |
if (log.isDebugEnabled()) {
|
136 | 51 |
log.debug("Getting Config");
|
137 |
} |
|
138 |
|
|
139 | 51 |
properties = new Properties();
|
140 |
|
|
141 | 51 |
InputStream in = null;
|
142 |
|
|
143 | 51 |
try {
|
144 | 51 |
in = getClass().getResourceAsStream(PROPERTIES_FILENAME); |
145 | 51 |
properties.load(in); |
146 | 51 |
log.info("Properties " + properties);
|
147 |
} catch (Exception e) {
|
|
148 | 0 |
log.error("Error reading " + PROPERTIES_FILENAME + " in CacheAdministrator.loadProps() " + e); |
149 | 0 |
log.error("Ensure the " + PROPERTIES_FILENAME + " file is readable and in your classpath."); |
150 |
} finally {
|
|
151 | 51 |
try {
|
152 | 51 |
in.close(); |
153 |
} catch (Exception e) {
|
|
154 |
// Ignore errors that occur while closing file
|
|
155 |
} |
|
156 |
} |
|
157 |
} |
|
158 |
} |
|
159 |
|
|