File Source: JPAPropertiesManagerImpl.java
1
2 /*
3 * Licensed to the Apache Software Foundation (ASF) under one or more
4 * contributor license agreements. The ASF licenses this file to You
5 * under the Apache License, Version 2.0 (the "License"); you may not
6 * use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License. For additional information regarding
16 * copyright in this work, please see the NOTICE file in the top level
17 * directory of this distribution.
18 */
19 package org.apache.roller.weblogger.business.jpa;
20
21 import java.util.HashMap;
22 import java.util.Iterator;
23 import java.util.List;
24 import java.util.Map;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28
29 import org.apache.roller.weblogger.WebloggerException;
30 import org.apache.roller.weblogger.business.InitializationException;
31
32 import org.apache.roller.weblogger.business.PropertiesManager;
33 import org.apache.roller.weblogger.config.WebloggerRuntimeConfig;
34 import org.apache.roller.weblogger.config.runtime.ConfigDef;
35 import org.apache.roller.weblogger.config.runtime.DisplayGroup;
36 import org.apache.roller.weblogger.config.runtime.PropertyDef;
37 import org.apache.roller.weblogger.config.runtime.RuntimeConfigDefs;
38 import org.apache.roller.weblogger.pojos.RuntimeConfigProperty;
39 import org.apache.roller.weblogger.business.Weblogger;
40
41
42 /*
43 * JPAPropertiesManagerImpl.java
44 *
45 * Created on May 29, 2006, 2:06 PM
46 *
47 */
48 @com.google.inject.Singleton
49 public class JPAPropertiesManagerImpl implements PropertiesManager {
50
51 /** The logger instance for this class. */
/*
P/P * Method: org.apache.roller.weblogger.business.jpa.JPAPropertiesManagerImpl__static_init
*
* Postconditions:
* init'ed(log)
*/
52 private static Log log = LogFactory.getLog(
53 JPAPropertiesManagerImpl.class);
54
55 private final Weblogger roller;
56 private final JPAPersistenceStrategy strategy;
57
58
59 /**
60 * Creates a new instance of JPAPropertiesManagerImpl
61 */
62 @com.google.inject.Inject
/*
P/P * Method: void org.apache.roller.weblogger.business.jpa.JPAPropertiesManagerImpl(Weblogger, JPAPersistenceStrategy)
*
* Preconditions:
* log != null
*
* Postconditions:
* this.roller == roller
* init'ed(this.roller)
* this.strategy == strategy
* init'ed(this.strategy)
*/
63 protected JPAPropertiesManagerImpl(Weblogger roller, JPAPersistenceStrategy strategy) {
64 log.debug("Instantiating JPA Properties Manager");
65 this.roller = roller;
66 this.strategy = strategy;
67 }
68
69
70 /**
71 * @inheritDoc
72 */
73 public void initialize() throws InitializationException {
74
/*
P/P * Method: void initialize()
*
* Preconditions:
* init'ed(org/apache/roller/weblogger/config/WebloggerRuntimeConfig.configDefs)
* this.strategy != null
* this.strategy.threadLocalEntityManager != null
* (soft) log != null
* (soft) org/apache/roller/weblogger/config/WebloggerRuntimeConfig.log != null
* (soft) init'ed(org/apache/roller/weblogger/config/WebloggerRuntimeConfig.runtime_config)
* (soft) this.strategy.emf != null
*
* Postconditions:
* org/apache/roller/weblogger/config/WebloggerRuntimeConfig.configDefs == One-of{old org/apache/roller/weblogger/config/WebloggerRuntimeConfig.configDefs, &new RuntimeConfigDefs(unmarshall#1)}
* init'ed(org/apache/roller/weblogger/config/WebloggerRuntimeConfig.configDefs)
* new ArrayList(RuntimeConfigDefs#1) num objects <= 1
* new RuntimeConfigDefs(unmarshall#1) num objects <= 1
* init'ed(new RuntimeConfigDefs(unmarshall#1).configDefs)
*
* Test Vectors:
* java.util.Map:size(...)@79: {1..232-1}, {-231..0}
*/
75 Map props = null;
76 try {
77 props = this.getProperties();
78
79 if(props.size() < 1) {
80 // empty props table ... load defaults
81 props = initializeMissingProps(props);
82 } else {
83 // found existing props ... check for new props
84 props = initializeMissingProps(props);
85 }
86
87 // save our changes
88 this.saveProperties(props);
89 } catch (Exception e) {
90 log.fatal("Failed to initialize runtime configuration properties."+
91 "Please check that the database has been upgraded!", e);
92 throw new RuntimeException(e);
93 }
94
95 }
96
97
98 /**
99 * Retrieve a single property by name.
100 */
101 public RuntimeConfigProperty getProperty(String name) throws WebloggerException {
/*
P/P * Method: RuntimeConfigProperty getProperty(String)
*
* Preconditions:
* this.strategy != null
* this.strategy.threadLocalEntityManager != null
* (soft) this.strategy.emf != null
*
* Postconditions:
* init'ed(return_value)
*/
102 return (RuntimeConfigProperty) strategy
103 .load(RuntimeConfigProperty.class,name);
104 }
105
106
107 /**
108 * Retrieve all properties.
109 *
110 * Properties are returned in a Map to make them easy to lookup. The Map
111 * uses the property name as the key and the RuntimeConfigProperty object
112 * as the value.
113 */
114 public Map getProperties() throws WebloggerException {
115
/*
P/P * Method: Map getProperties()
*
* Preconditions:
* this.strategy != null
* this.strategy.threadLocalEntityManager != null
* (soft) this.strategy.emf != null
*
* Presumptions:
* java.util.Iterator:next(...)@127 != null
* javax.persistence.Query:getResultList(...)@117 != null
*
* Postconditions:
* return_value == &new HashMap(getProperties#1)
* new HashMap(getProperties#1) num objects == 1
*
* Test Vectors:
* java.util.Iterator:hasNext(...)@126: {0}, {1}
*/
116 HashMap props = new HashMap();
117 List list = (List) strategy.getNamedQuery("RuntimeConfigProperty.getAll").getResultList();
118 /*
119 * for convenience sake we are going to put the list of props
120 * into a map for users to access it. The value element of the
121 * hash still needs to be the RuntimeConfigProperty object so that
122 * we can save the elements again after they have been updated
123 */
124 RuntimeConfigProperty prop = null;
125 Iterator it = list.iterator();
126 while(it.hasNext()) {
127 prop = (RuntimeConfigProperty) it.next();
128 props.put(prop.getName(), prop);
129 }
130
131 return props;
132 }
133
134
135 /**
136 * Save a single property.
137 */
138 public void saveProperty(RuntimeConfigProperty property)
139 throws WebloggerException {
/*
P/P * Method: void saveProperty(RuntimeConfigProperty)
*
* Preconditions:
* this.strategy != null
* this.strategy.threadLocalEntityManager != null
* (soft) this.strategy.emf != null
*/
140 this.strategy.store(property);
141 }
142
143
144 /**
145 * Save all properties.
146 */
147 public void saveProperties(Map properties) throws WebloggerException {
148
149 // just go through the list and saveProperties each property
/*
P/P * Method: void saveProperties(Map)
*
* Preconditions:
* properties != null
* (soft) this.strategy != null
* (soft) this.strategy.emf != null
* (soft) this.strategy.threadLocalEntityManager != null
*
* Presumptions:
* java.util.Map:values(...)@150 != null
*
* Test Vectors:
* java.util.Iterator:hasNext(...)@151: {0}, {1}
*/
150 Iterator props = properties.values().iterator();
151 while (props.hasNext()) {
152 this.strategy.store((RuntimeConfigProperty) props.next());
153 }
154 }
155
156
157 /**
158 * This method compares the property definitions in the RuntimeConfigDefs
159 * file with the properties in the given Map and initializes any properties
160 * that were not found in the Map.
161 *
162 * If the Map of props is empty/null then we will initialize all properties.
163 **/
164 private Map initializeMissingProps(Map props) {
165
/*
P/P * Method: Map initializeMissingProps(Map)
*
* Preconditions:
* init'ed(org/apache/roller/weblogger/config/WebloggerRuntimeConfig.configDefs)
* (soft) log != null
* (soft) org/apache/roller/weblogger/config/WebloggerRuntimeConfig.log != null
* (soft) init'ed(org/apache/roller/weblogger/config/WebloggerRuntimeConfig.runtime_config)
*
* Presumptions:
* configDef.displayGroups@184 != null
* dGroup.propertyDefs@188 != null
* java.util.Iterator:next(...)@184 != null
* java.util.Iterator:next(...)@188 != null
* java.util.Iterator:next(...)@192 != null
* ...
*
* Postconditions:
* org/apache/roller/weblogger/config/WebloggerRuntimeConfig.configDefs == One-of{old org/apache/roller/weblogger/config/WebloggerRuntimeConfig.configDefs, &new RuntimeConfigDefs(unmarshall#1)}
* init'ed(org/apache/roller/weblogger/config/WebloggerRuntimeConfig.configDefs)
* return_value == One-of{props, &new HashMap(initializeMissingProps#1)}
* return_value != null
* new ArrayList(RuntimeConfigDefs#1) num objects <= 1
* new RuntimeConfigDefs(unmarshall#1) num objects == new ArrayList(RuntimeConfigDefs#1) num objects
* new HashMap(initializeMissingProps#1) num objects <= 1
* init'ed(new RuntimeConfigDefs(unmarshall#1).configDefs)
*
* Test Vectors:
* props: Inverse{null}, Addr_Set{null}
* java.util.Iterator:hasNext(...)@183: {0}, {1}
* java.util.Iterator:hasNext(...)@187: {0}, {1}
* java.util.Iterator:hasNext(...)@191: {0}, {1}
* java.util.Map:containsKey(...)@195: {1}, {0}
*/
166 if(props == null)
167 props = new HashMap();
168
169 // start by getting our runtimeConfigDefs
170 RuntimeConfigDefs runtimeConfigDefs =
171 WebloggerRuntimeConfig.getRuntimeConfigDefs();
172
173 // can't do initialization without our config defs
174 if(runtimeConfigDefs == null)
175 return props;
176
177 // iterator through all the definitions and add properties
178 // that are not already in our props map
179 ConfigDef configDef = null;
180 DisplayGroup dGroup = null;
181 PropertyDef propDef = null;
+ 182 Iterator defs = runtimeConfigDefs.getConfigDefs().iterator();
183 while(defs.hasNext()) {
184 configDef = (ConfigDef) defs.next();
185
186 Iterator groups = configDef.getDisplayGroups().iterator();
187 while(groups.hasNext()) {
188 dGroup = (DisplayGroup) groups.next();
189
190 Iterator propdefs = dGroup.getPropertyDefs().iterator();
191 while(propdefs.hasNext()) {
192 propDef = (PropertyDef) propdefs.next();
193
194 // do we already have this prop? if not then add it
195 if(!props.containsKey(propDef.getName())) {
196 RuntimeConfigProperty newprop =
197 new RuntimeConfigProperty(
198 propDef.getName(), propDef.getDefaultValue());
199
200 props.put(propDef.getName(), newprop);
201
202 log.info("Found uninitialized property " +
203 propDef.getName() +
204 " ... setting value to [" +
205 propDef.getDefaultValue() + "]");
206 }
207 }
208 }
209 }
210
211 return props;
212 }
213
214
/*
P/P * Method: void release()
*/
215 public void release() {}
216
217 }
SofCheck Inspector Build Version : 2.18479
| JPAPropertiesManagerImpl.java |
2009-Jan-02 14:25:28 |
| JPAPropertiesManagerImpl.class |
2009-Sep-04 03:12:31 |