KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > roller > business > hibernate > HibernatePropertiesManagerImpl


1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. The ASF licenses this file to You
4 * under the Apache License, Version 2.0 (the "License"); you may not
5 * 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. For additional information regarding
15 * copyright in this work, please see the NOTICE file in the top level
16 * directory of this distribution.
17 */

18 /*
19  * HibernatePropertiesManagerImpl.java
20  *
21  * Created on April 21, 2005, 10:40 AM
22  */

23
24 package org.apache.roller.business.hibernate;
25
26 import java.util.HashMap JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Map JavaDoc;
30 import org.hibernate.Criteria;
31 import org.hibernate.HibernateException;
32 import org.hibernate.Session;
33 import org.apache.commons.logging.Log;
34 import org.apache.commons.logging.LogFactory;
35 import org.apache.roller.RollerException;
36 import org.apache.roller.config.RollerRuntimeConfig;
37 import org.apache.roller.config.runtime.ConfigDef;
38 import org.apache.roller.config.runtime.DisplayGroup;
39 import org.apache.roller.config.runtime.PropertyDef;
40 import org.apache.roller.config.runtime.RuntimeConfigDefs;
41 import org.apache.roller.model.PropertiesManager;
42 import org.apache.roller.model.Roller;
43 import org.apache.roller.model.RollerFactory;
44 import org.apache.roller.pojos.RollerConfigData;
45 import org.apache.roller.pojos.RollerPropertyData;
46
47
48 /**
49  * Hibernate implementation of the PropertiesManager.
50  */

51 public class HibernatePropertiesManagerImpl implements PropertiesManager {
52     
53     static final long serialVersionUID = -4326713177137796936L;
54     
55     private static Log log = LogFactory.getLog(HibernatePropertiesManagerImpl.class);
56     
57     private HibernatePersistenceStrategy strategy = null;
58     
59     
60     /**
61      * Creates a new instance of HibernatePropertiesManagerImpl
62      */

63     public HibernatePropertiesManagerImpl(HibernatePersistenceStrategy strat) {
64         
65         log.debug("Instantiating Hibernate Properties Manager");
66         
67         this.strategy = strat;
68         
69         // TODO: and new method initialize(props)
70
init();
71     }
72     
73     
74     /**
75      * Retrieve a single property by name.
76      */

77     public RollerPropertyData getProperty(String JavaDoc name) throws RollerException {
78         try {
79             return (RollerPropertyData) strategy.load(name, RollerPropertyData.class);
80         } catch (HibernateException e) {
81             throw new RollerException(e);
82         }
83     }
84     
85     
86     /**
87      * Retrieve all properties.
88      *
89      * Properties are returned in a Map to make them easy to lookup. The Map
90      * uses the property name as the key and the RollerPropertyData object
91      * as the value.
92      */

93     public Map JavaDoc getProperties() throws RollerException {
94         
95         HashMap JavaDoc props = new HashMap JavaDoc();
96         
97         try {
98             Session session = strategy.getSession();
99             Criteria criteria = session.createCriteria(RollerPropertyData.class);
100             List JavaDoc list = criteria.list();
101             
102             /*
103              * for convenience sake we are going to put the list of props
104              * into a map for users to access it. The value element of the
105              * hash still needs to be the RollerPropertyData object so that
106              * we can save the elements again after they have been updated
107              */

108             RollerPropertyData prop = null;
109             Iterator JavaDoc it = list.iterator();
110             while(it.hasNext()) {
111                 prop = (RollerPropertyData) it.next();
112                 props.put(prop.getName(), prop);
113             }
114         } catch (HibernateException e) {
115             throw new RollerException(e);
116         }
117         
118         return props;
119     }
120     
121     
122     /**
123      * Save a single property.
124      */

125     public void saveProperty(RollerPropertyData property) throws RollerException {
126         
127         this.strategy.store(property);
128     }
129
130     
131     /**
132      * Save all properties.
133      */

134     public void saveProperties(Map JavaDoc properties) throws RollerException {
135         
136         // just go through the list and saveProperties each property
137
Iterator JavaDoc props = properties.values().iterator();
138         while (props.hasNext()) {
139             this.strategy.store((RollerPropertyData) props.next());
140         }
141     }
142
143     
144     private void init() {
145         Map JavaDoc props = null;
146         try {
147             props = this.getProperties();
148             
149             if(props.size() < 1) {
150                 // empty props table ... try migrating, then load defaults
151
props = migrateOldRollerConfig(props);
152                 props = initializeMissingProps(props);
153             } else {
154                 // found existing props ... check for new props
155
props = initializeMissingProps(props);
156             }
157             
158             // save our changes
159
this.saveProperties(props);
160         } catch (Exception JavaDoc e) {
161             log.fatal("Failed to initialize runtime configuration properties."+
162                     "Please check that the database has been upgraded!", e);
163             throw new RuntimeException JavaDoc(e);
164         }
165         
166     }
167     
168     
169     /**
170      * Migrate data from the old roller config.
171      * This is called only if the existing runtime properties are empty.
172      */

173     private Map JavaDoc migrateOldRollerConfig(Map JavaDoc props) {
174         // try to get the old config
175
Roller roller = RollerFactory.getRoller();
176         RollerConfigData rollerConfig = null;
177         
178         try {
179             rollerConfig = roller.getConfigManager().getRollerConfig();
180         } catch (Exception JavaDoc e) {
181             // We currently treat any exception obtaining the roller config
182
// as if we had not found it.
183
log.error(e);
184         }
185         
186         if (rollerConfig != null) {
187             log.info("Found old roller config ... doing migration to new runtime properties.");
188             // copy over data
189
props.put("site.name",
190                     new RollerPropertyData("site.name", rollerConfig.getSiteName()));
191             props.put("site.description",
192                     new RollerPropertyData("site.description", rollerConfig.getSiteDescription()));
193             props.put("site.adminemail",
194                     new RollerPropertyData("site.adminemail", rollerConfig.getEmailAddress()));
195             props.put("site.absoluteurl",
196                     new RollerPropertyData("site.absoluteurl", rollerConfig.getAbsoluteURL()));
197             props.put("site.linkbacks.enabled",
198                     new RollerPropertyData("site.linkbacks.enabled", rollerConfig.getEnableLinkback().toString()));
199             props.put("users.registration.enabled",
200                     new RollerPropertyData("users.registration.enabled", rollerConfig.getNewUserAllowed().toString()));
201             props.put("users.themes.path",
202                     new RollerPropertyData("users.themes.path", rollerConfig.getUserThemes()));
203             props.put("users.editor.pages",
204                     new RollerPropertyData("users.editor.pages", rollerConfig.getEditorPages()));
205             props.put("users.comments.enabled",
206                     new RollerPropertyData("users.comments.enabled", "true"));
207             props.put("users.comments.autoformat",
208                     new RollerPropertyData("users.comments.autoformat", rollerConfig.getAutoformatComments().toString()));
209             props.put("users.comments.escapehtml",
210                     new RollerPropertyData("users.comments.escapehtml", rollerConfig.getEscapeCommentHtml().toString()));
211             props.put("users.comments.emailnotify",
212                     new RollerPropertyData("users.comments.emailnotify", rollerConfig.getEmailComments().toString()));
213             props.put("uploads.enabled",
214                     new RollerPropertyData("uploads.enabled", rollerConfig.getUploadEnabled().toString()));
215             props.put("uploads.types.allowed",
216                     new RollerPropertyData("uploads.types.allowed", rollerConfig.getUploadAllow()));
217             props.put("uploads.types.forbid",
218                     new RollerPropertyData("uploads.types.forbid", rollerConfig.getUploadForbid()));
219             props.put("uploads.file.maxsize",
220                     new RollerPropertyData("uploads.file.maxsize", rollerConfig.getUploadMaxFileMB().toString()));
221             props.put("uploads.dir.maxsize",
222                     new RollerPropertyData("uploads.dir.maxsize", rollerConfig.getUploadMaxDirMB().toString()));
223             /* no longer part of runtime config
224             props.put("aggregator.enabled",
225                 new RollerPropertyData("aggregator.enabled", rollerConfig.getEnableAggregator().toString()));
226             props.put("aggregator.cache.enabled",
227                 new RollerPropertyData("aggregator.cache.enabled", rollerConfig.getRssUseCache().toString()));
228             props.put("aggregator.cache.timeout",
229                 new RollerPropertyData("aggregator.cache.timeout", rollerConfig.getRssCacheTime().toString()));
230             props.put("debug.memory.enabled",
231                 new RollerPropertyData("debug.memory.enabled", rollerConfig.getMemDebug().toString()));
232              */

233             props.put("spam.blacklist",
234                     new RollerPropertyData("spam.blacklist", rollerConfig.getRefererSpamWords()));
235         } else {
236             log.info("Old roller config not found ... default values will be loaded");
237         }
238         
239         return props;
240     }
241     
242     
243     /**
244      * This method compares the property definitions in the RuntimeConfigDefs
245      * file with the properties in the given Map and initializes any properties
246      * that were not found in the Map.
247      *
248      * If the Map of props is empty/null then we will initialize all properties.
249      **/

250     private Map JavaDoc initializeMissingProps(Map JavaDoc props) {
251         
252         if(props == null)
253             props = new HashMap JavaDoc();
254         
255         // start by getting our runtimeConfigDefs
256
RuntimeConfigDefs runtimeConfigDefs =
257                 RollerRuntimeConfig.getRuntimeConfigDefs();
258         
259         // can't do initialization without our config defs
260
if(runtimeConfigDefs == null)
261             return props;
262         
263         // iterator through all the definitions and add properties
264
// that are not already in our props map
265
ConfigDef configDef = null;
266         DisplayGroup dGroup = null;
267         PropertyDef propDef = null;
268         Iterator JavaDoc defs = runtimeConfigDefs.getConfigDefs().iterator();
269         while(defs.hasNext()) {
270             configDef = (ConfigDef) defs.next();
271             
272             Iterator JavaDoc groups = configDef.getDisplayGroups().iterator();
273             while(groups.hasNext()) {
274                 dGroup = (DisplayGroup) groups.next();
275                 
276                 Iterator JavaDoc propdefs = dGroup.getPropertyDefs().iterator();
277                 while(propdefs.hasNext()) {
278                     propDef = (PropertyDef) propdefs.next();
279                     
280                     // do we already have this prop? if not then add it
281
if(!props.containsKey(propDef.getName())) {
282                         RollerPropertyData newprop =
283                                 new RollerPropertyData(propDef.getName(), propDef.getDefaultValue());
284                         
285                         props.put(propDef.getName(), newprop);
286                         
287                         log.info("Found uninitialized property "+propDef.getName()+
288                                 " ... setting value to ["+propDef.getDefaultValue()+"]");
289                     }
290                 }
291             }
292         }
293         
294         return props;
295     }
296     
297     
298     public void release() {}
299     
300 }
301
Popular Tags