KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > blandware > atleap > webapp > util > core > GlobalProperties


1 /*
2  * Copyright 2004 Blandware (http://www.blandware.com)
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package com.blandware.atleap.webapp.util.core;
17
18 import com.blandware.atleap.common.Constants;
19 import com.blandware.atleap.model.core.GlobalProperty;
20 import com.blandware.atleap.service.core.GlobalPropertyManager;
21 import com.blandware.atleap.service.exception.BeanNotFoundException;
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24 import org.springframework.context.ApplicationContext;
25 import org.springframework.web.context.support.WebApplicationContextUtils;
26
27 import javax.servlet.ServletContext JavaDoc;
28 import java.util.Properties JavaDoc;
29
30 /**
31  * <p>Wrapper class for GlobalPropertyManager. Holds and synchronizes dynamic and static properties</p>
32  * <p><a HREF="GlobalProperties.java.htm"><i>View Source</i></a></p>
33  *
34  * @author Sergey Zubtcovskii <a HREF="mailto:sergey.zubtcovskii@blandware.com">&lt;sergey.zubtcovskii@blandware.com&gt;</a>
35  * @version $Revision: 1.11 $ $Date: 2006/02/07 10:47:55 $
36  */

37 public final class GlobalProperties {
38
39     /**
40      * Servlet context key under which instance of this class is saved
41      */

42     private static final String JavaDoc INSTANCE_KEY = "com.blandware.atleap.webapp.util.GlobalProperties.INSTANCE";
43
44     /**
45      * Commons Logging instance for this class
46      */

47     private transient final Log log = LogFactory.getLog(GlobalProperties.class);
48
49     /**
50      * Static properties
51      */

52     private Properties JavaDoc staticProperties;
53
54     /**
55      * Dynamic properties
56      */

57     private Properties JavaDoc dynamicProperties;
58
59     /**
60      * Global property manager we're using
61      */

62     private GlobalPropertyManager globalPropertyManager = null;
63
64     /**
65      * Creates new instance of GlobalProperties and initializes internal fields
66      *
67      * @param servletContext Servlet context
68      */

69     private GlobalProperties(ServletContext JavaDoc servletContext) {
70         ApplicationContext applicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
71         globalPropertyManager = (GlobalPropertyManager) applicationContext.getBean(Constants.GLOBAL_PROPERTY_MANAGER_BEAN);
72         synchronizeCache();
73     }
74
75     /**
76      * Returns instance of GlobalProperties
77      *
78      * @param servletContext Servlet context to search by
79      * @return Instance of GlobalProperties
80      */

81     public static GlobalProperties getInstance(ServletContext JavaDoc servletContext) {
82         GlobalProperties globalProperties = (GlobalProperties) servletContext.getAttribute(INSTANCE_KEY);
83         if ( globalProperties == null ) {
84             globalProperties = new GlobalProperties(servletContext);
85             servletContext.setAttribute(INSTANCE_KEY, globalProperties);
86         }
87         return globalProperties;
88     }
89
90     /**
91      * Load data from database into local cache
92      */

93     public void synchronizeCache() {
94         staticProperties = globalPropertyManager.getStaticProperties();
95         dynamicProperties = globalPropertyManager.loadDynamicProperties(false);
96
97         if ( log.isInfoEnabled() ) {
98             log.info("Global properties synchronized");
99         }
100     }
101
102     /**
103      * Retrieves global property with specified name as string.<br />
104      * This method first searches in dynamic properties and then in static.
105      * So, if you have static and dynamic property with the same name, note that
106      * static one will never be returned.
107      *
108      * @param propertyName Name to search by
109      * @return Value or <code>null</code> if no property with specified name exists
110      */

111     public String JavaDoc getString(String JavaDoc propertyName) {
112         return getString(propertyName, null);
113     }
114
115
116     /**
117      * Retrieves global property with specified name as string. If nothing
118      * found, default value is used.<br />
119      * This method first searches in dynamic properties and then in static.
120      * So, if you have static and dynamic property with the same name, note that
121      * static one will never be returned.
122      *
123      * @param propertyName Name to search by
124      * @param defaultValue The value that will be returned if nothing is found
125      * @return Value or default if no property with specified name exists
126      */

127     public String JavaDoc getString(String JavaDoc propertyName, String JavaDoc defaultValue) {
128         String JavaDoc value = dynamicProperties.getProperty(propertyName);
129         if ( value == null ) {
130             value = staticProperties.getProperty(propertyName);
131         }
132         return value != null ? value : defaultValue;
133     }
134
135     /**
136      * Retrieves global property with specified name as integer.<br />
137      * This method first searches in dynamic properties and then in static.
138      * So, if you have static and dynamic property with the same name, note that
139      * static one will never be returned.<br />
140      * If an error occur when trying to convert property to integer,
141      * <code>null</code> is returned.
142      *
143      * @param propertyName Name to search by
144      * @return Value or null if no property with specified name exists or
145      * if property value cannot be converted to integer
146      */

147     public Integer JavaDoc getInteger(String JavaDoc propertyName) {
148         return getInteger(propertyName, null);
149     }
150
151     /**
152      * Retrieves global property with specified name as integer. If nothing
153      * found, default value is used.<br />
154      * This method first searches in dynamic properties and then in static.
155      * So, if you have static and dynamic property with the same name, note that
156      * static one will never be returned.<br />
157      * If an error occur when trying to convert property to integer,
158      * <code>null</code> is returned.
159      *
160      * @param propertyName Name to search by
161      * @param defaultValue The value that will be returned if nothing is found
162      * @return Value or default if no property with specified name exists or
163      * if property value cannot be converted to integer
164      */

165     public Integer JavaDoc getInteger(String JavaDoc propertyName, int defaultValue) {
166         return getInteger(propertyName, new Integer JavaDoc(defaultValue));
167     }
168
169     /**
170      * Retrieves global property with specified name as integer. If nothing
171      * found, default value is used.<br />
172      * This method first searches in dynamic properties and then in static.
173      * So, if you have static and dynamic property with the same name, note that
174      * static one will never be returned.<br />
175      * If an error occur when trying to convert property to integer,
176      * <code>null</code> is returned.
177      *
178      * @param propertyName Name to search by
179      * @param defaultValue The value that will be returned if nothing is found
180      * @return Value or default if no property with specified name exists or
181      * if property value cannot be converted to integer
182      */

183     public Integer JavaDoc getInteger(String JavaDoc propertyName, Integer JavaDoc defaultValue) {
184         String JavaDoc value = null;
185         if ( defaultValue != null ) {
186             value = getString(propertyName, defaultValue.toString());
187         } else {
188             value = getString(propertyName, null);
189         }
190
191         Integer JavaDoc result = null;
192         if ( value != null ) {
193             try {
194                 result = Integer.valueOf(value);
195             } catch ( Exception JavaDoc e ) {
196                 if ( log.isErrorEnabled() ) {
197                     log.error("Error while retrieving integer value of property '" + propertyName
198                             + "'. Failed to convert string '" + value
199                             + "' to java.lang.Integer. Cause: "
200                             + e.getClass().getName() + ": " + e.getMessage());
201                 }
202                 result = defaultValue;
203             }
204         }
205         return result;
206     }
207
208     /**
209      * Updates global property
210      *
211      * @param globalProperty Global property to be updated
212      * @throws BeanNotFoundException If no such global property exists in a
213      * persistent storage
214      */

215     public void updateProperty(GlobalProperty globalProperty) throws BeanNotFoundException {
216         globalPropertyManager.updateProperty(globalProperty);
217         dynamicProperties.put(globalProperty.getName(), globalProperty.getValue());
218     }
219
220     /**
221      * Retrieves global property with specified name
222      *
223      * @param name The name of global property to retrieve
224      * @return Global property with given name or <code>null</code> or nothing
225      * was found
226      */

227     public GlobalProperty retrieveGlobalProperty(String JavaDoc name) {
228         return globalPropertyManager.retrieveGlobalProperty(name);
229     }
230
231     /**
232      * Reloads all properties from configuration file
233      */

234     public void reloadProperties() {
235         dynamicProperties = globalPropertyManager.loadDynamicProperties(true);
236     }
237
238 }
239
Popular Tags