KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > blandware > atleap > service > core > impl > GlobalPropertyManagerImpl


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.service.core.impl;
17
18 import com.blandware.atleap.model.core.GlobalProperty;
19 import com.blandware.atleap.persistence.core.GlobalPropertyDAO;
20 import com.blandware.atleap.service.core.GlobalPropertyManager;
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23
24 import java.util.HashSet JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Map JavaDoc;
28 import java.util.Properties JavaDoc;
29 import java.util.Set JavaDoc;
30
31 /**
32  * <p>Implementation of GlobalPropertyManager interface.
33  * </p>
34  * <p><a HREF="GlobalPropertyManagerImpl.java.htm"> <i>View Source </i> </a>
35  * </p>
36  *
37  * @author Sergey Zubtcovskii <a HREF="mailto:sergey.zubtcovskii@blandware.com">&lt;sergey.zubtcovskii@blandware.com&gt;</a>
38  * @author Andrey Grebnev <a HREF="mailto:andrey.grebnev@blandware.com">&lt;andrey.grebnev@blandware.com&gt;</a>
39  * @version $Revision: 1.10 $ $Date: 2005/08/02 14:53:40 $
40  */

41 public class GlobalPropertyManagerImpl extends BaseManagerImpl implements GlobalPropertyManager {
42
43     protected final Log log = LogFactory.getLog(GlobalPropertyManagerImpl.class);
44
45     /**
46      * Global property DAO
47      */

48     protected GlobalPropertyDAO globalPropertyDAO;
49
50     /**
51      * Static properties
52      */

53     protected Properties JavaDoc staticProperties;
54
55     /**
56      * Dynamic properties
57      */

58     protected Properties JavaDoc dynamicProperties;
59
60     /**
61      * Original dynamic properties if user wants to perform reload
62      */

63     protected Properties JavaDoc originalDynamicProperties;
64
65     /**
66      * Sets DAO for operating with global properties
67      *
68      * @param dao the DAO to set
69      */

70     public void setGlobalPropertyDAO(GlobalPropertyDAO dao) {
71         this.globalPropertyDAO = dao;
72     }
73
74     /**
75      * Sets static properties (those that can't be changed in application)
76      *
77      * @param staticProperties static properties to set
78      */

79     public void setStaticProperties(Properties JavaDoc staticProperties) {
80         this.staticProperties = staticProperties;
81     }
82
83     /**
84      * Sets dynamic properties (those that can be changed in application)
85      *
86      * @param dynamicProperties dynamic properties to set
87      */

88     public void setDynamicProperties(Properties JavaDoc dynamicProperties) {
89         this.dynamicProperties = dynamicProperties;
90     }
91
92     /**
93      * Gets static properties (those that can't be changed in application)
94      *
95      * @return static properties
96      */

97     public Properties JavaDoc getStaticProperties() {
98         return staticProperties;
99     }
100
101     /**
102      * Gets dynamic properties (those that can be changed in application)
103      *
104      * @return dynamic properties
105      */

106     public Properties JavaDoc getDynamicProperties() {
107         return dynamicProperties;
108     }
109
110     /**
111      * @see com.blandware.atleap.service.core.GlobalPropertyManager#loadDynamicProperties(boolean)
112      */

113     public Properties JavaDoc loadDynamicProperties(boolean overrideDynamicValues) {
114         // Find the properties that are both static and dynamic (to warn user)
115
Set JavaDoc intersection = new HashSet JavaDoc(staticProperties.keySet());
116         intersection.retainAll(dynamicProperties.keySet());
117         if ( !intersection.isEmpty() ) {
118             // warn user
119
if ( log.isWarnEnabled() ) {
120                 for ( Iterator JavaDoc i = intersection.iterator(); i.hasNext(); ) {
121                     String JavaDoc name = (String JavaDoc) i.next();
122                     log.warn("Property with name '" + name + "' declared as static and dynamic simultaneously. Static value will never be returned.");
123                 }
124             }
125         }
126
127         // delete properties which names are not presented in the list
128
List JavaDoc existentProperties = listDynamicProperties();
129         for ( Iterator JavaDoc i = existentProperties.iterator(); i.hasNext(); ) {
130             GlobalProperty globalProperty = (GlobalProperty) i.next();
131             if ( !this.dynamicProperties.keySet().contains(globalProperty.getName()) ) {
132                 globalPropertyDAO.deleteGlobalProperty(globalProperty);
133             }
134         }
135
136
137         Properties JavaDoc properties = new Properties JavaDoc();
138
139         // create unexistent properties and update dynamic properties map
140
for ( Iterator JavaDoc i = this.dynamicProperties.entrySet().iterator(); i.hasNext(); ) {
141             Map.Entry JavaDoc property = (Map.Entry JavaDoc) i.next();
142             String JavaDoc name = (String JavaDoc) property.getKey();
143             String JavaDoc value = (String JavaDoc) property.getValue();
144             if (log.isDebugEnabled()) {
145                 log.debug("Creating unexistent property with name='" + name + "' and value='" + value + "'");
146             }
147             GlobalProperty globalProperty = globalPropertyDAO.retrieveGlobalProperty(name);
148             if ( globalProperty == null ) {
149                 globalProperty = new GlobalProperty(name, value);
150                 globalPropertyDAO.createGlobalProperty(globalProperty);
151             } else if ( overrideDynamicValues && !value.equals(globalProperty.getValue()) ) {
152                 globalProperty.setValue(value);
153                 globalPropertyDAO.updateGlobalProperty(globalProperty);
154             } else {
155                 value = globalProperty.getValue();
156                 if (value == null) {
157                     value = "";
158                 }
159             }
160             properties.put(name, value);
161         }
162         return properties;
163     }
164
165     /**
166      * @see com.blandware.atleap.service.core.GlobalPropertyManager#retrieveGlobalProperty(String)
167      */

168     public GlobalProperty retrieveGlobalProperty(String JavaDoc propertyName) {
169         return globalPropertyDAO.retrieveGlobalProperty(propertyName);
170     }
171
172
173     /**
174      * @see com.blandware.atleap.service.core.GlobalPropertyManager#updateProperty(com.blandware.atleap.model.core.GlobalProperty)
175      */

176     public void updateProperty(GlobalProperty globalProperty) {
177         // remove global property from cache in order to prevent Hibernate from assigning new version number
178
globalPropertyDAO.removeFromCache(globalProperty);
179
180         if ( log.isDebugEnabled() ) {
181             log.debug("Updating global property with name '" + globalProperty.getName() + "'...");
182         }
183
184         globalPropertyDAO.updateGlobalProperty(globalProperty);
185
186         if ( log.isDebugEnabled() ) {
187             log.debug("Global property has been updated successfully");
188         }
189     }
190
191     // ~ Additional methods ================================================================
192

193     /**
194      * @see com.blandware.atleap.service.core.GlobalPropertyManager#listDynamicProperties()
195      */

196     public List JavaDoc listDynamicProperties() {
197         return globalPropertyDAO.listDynamicProperties();
198     }
199
200 }
Popular Tags