KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > roller > business > PropertiesManagerImpl


1 /*
2  * PropertiesManagerImpl.java
3  *
4  * Created on April 21, 2005, 10:43 AM
5  */

6
7 package org.roller.business;
8
9 import org.apache.commons.logging.Log;
10 import org.apache.commons.logging.LogFactory;
11 import org.roller.RollerException;
12 import org.roller.model.PropertiesManager;
13 import org.roller.model.Roller;
14 import org.roller.model.RollerFactory;
15 import org.roller.pojos.RollerConfigData;
16 import org.roller.pojos.RollerPropertyData;
17
18 import java.util.Iterator JavaDoc;
19 import java.util.Map JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import org.roller.config.RollerRuntimeConfig;
22 import org.roller.config.runtime.ConfigDef;
23 import org.roller.config.runtime.DisplayGroup;
24 import org.roller.config.runtime.PropertyDef;
25 import org.roller.config.runtime.RuntimeConfigDefs;
26
27 /**
28  * Abstract PropertiesManager implementation.
29  *
30  * @author Allen Gilliland
31  */

32 public abstract class PropertiesManagerImpl implements PropertiesManager
33 {
34
35     protected PersistenceStrategy mStrategy;
36
37     private static Log mLogger =
38         LogFactory.getFactory().getInstance(PropertiesManagerImpl.class);
39
40
41     /**
42      * Creates a new instance of PropertiesManagerImpl
43      */

44     public PropertiesManagerImpl(PersistenceStrategy strategy)
45     {
46         this.mStrategy = strategy;
47         init();
48     }
49
50     private void init()
51     {
52         Map JavaDoc props = null;
53         try
54         {
55             props = this.getProperties();
56             
57             if(props.size() < 1) {
58                 // empty props table ... try migrating, then load defaults
59
props = migrateOldRollerConfig(props);
60                 props = initializeMissingProps(props);
61             } else {
62                 // found existing props ... check for new props
63
props = initializeMissingProps(props);
64             }
65             
66             // save our changes
67
this.store(props);
68         }
69         catch (Exception JavaDoc e)
70         {
71             mLogger.fatal("Failed to initialize runtime configuration properties."+
72                     "Please check that the database has been upgraded!", e);
73             throw new RuntimeException JavaDoc(e);
74         }
75
76     }
77
78     /**
79      * Save a single property
80      */

81     public void store(RollerPropertyData property) throws RollerException
82     {
83         this.mStrategy.store(property);
84     }
85
86     /**
87      * Save all properties
88      */

89     public void store(Map JavaDoc properties) throws RollerException
90     {
91         // just go through the list and store each property
92
Iterator JavaDoc props = properties.values().iterator();
93         while (props.hasNext())
94         {
95             try
96             {
97                 this.mStrategy.store((RollerPropertyData) props.next());
98             }
99             catch (RollerException re)
100             {
101                 mLogger.error("Couldn't store Roller property", re);
102                 throw re;
103             }
104         }
105     }
106
107     public void release()
108     {
109     }
110
111
112     /**
113      * Migrate data from the old roller config.
114      * This is called only if the existing runtime properties are empty.
115      */

116     private Map JavaDoc migrateOldRollerConfig(Map JavaDoc props)
117     {
118         // try to get the old config
119
Roller roller = RollerFactory.getRoller();
120         RollerConfigData rollerConfig = null;
121
122         try
123         {
124             rollerConfig = roller.getConfigManager().getRollerConfig();
125         }
126         catch (Exception JavaDoc e)
127         {
128             // We currently treat any exception obtaining the roller config
129
// as if we had not found it.
130
mLogger.error(e);
131         }
132
133         if (rollerConfig != null)
134         {
135             mLogger.info("Found old roller config ... doing migration to new runtime properties.");
136             // copy over data
137
props.put("site.name",
138                 new RollerPropertyData("site.name", rollerConfig.getSiteName()));
139             props.put("site.description",
140                 new RollerPropertyData("site.description", rollerConfig.getSiteDescription()));
141             props.put("site.adminemail",
142                 new RollerPropertyData("site.adminemail", rollerConfig.getEmailAddress()));
143             props.put("site.absoluteurl",
144                 new RollerPropertyData("site.absoluteurl", rollerConfig.getAbsoluteURL()));
145             props.put("site.linkbacks.enabled",
146                 new RollerPropertyData("site.linkbacks.enabled", rollerConfig.getEnableLinkback().toString()));
147             props.put("users.registration.enabled",
148                 new RollerPropertyData("users.registration.enabled", rollerConfig.getNewUserAllowed().toString()));
149             props.put("users.themes.path",
150                 new RollerPropertyData("users.themes.path", rollerConfig.getUserThemes()));
151             props.put("users.editor.pages",
152                 new RollerPropertyData("users.editor.pages", rollerConfig.getEditorPages()));
153             props.put("users.comments.enabled",
154                 new RollerPropertyData("users.comments.enabled", "true"));
155             props.put("users.comments.autoformat",
156                 new RollerPropertyData("users.comments.autoformat", rollerConfig.getAutoformatComments().toString()));
157             props.put("users.comments.escapehtml",
158                 new RollerPropertyData("users.comments.escapehtml", rollerConfig.getEscapeCommentHtml().toString()));
159             props.put("users.comments.emailnotify",
160                 new RollerPropertyData("users.comments.emailnotify", rollerConfig.getEmailComments().toString()));
161             props.put("uploads.enabled",
162                 new RollerPropertyData("uploads.enabled", rollerConfig.getUploadEnabled().toString()));
163             props.put("uploads.types.allowed",
164                 new RollerPropertyData("uploads.types.allowed", rollerConfig.getUploadAllow()));
165             props.put("uploads.types.forbid",
166                 new RollerPropertyData("uploads.types.forbid", rollerConfig.getUploadForbid()));
167             props.put("uploads.file.maxsize",
168                 new RollerPropertyData("uploads.file.maxsize", rollerConfig.getUploadMaxFileMB().toString()));
169             props.put("uploads.dir.maxsize",
170                 new RollerPropertyData("uploads.dir.maxsize", rollerConfig.getUploadMaxDirMB().toString()));
171             /* no longer part of runtime config
172             props.put("aggregator.enabled",
173                 new RollerPropertyData("aggregator.enabled", rollerConfig.getEnableAggregator().toString()));
174             props.put("aggregator.cache.enabled",
175                 new RollerPropertyData("aggregator.cache.enabled", rollerConfig.getRssUseCache().toString()));
176             props.put("aggregator.cache.timeout",
177                 new RollerPropertyData("aggregator.cache.timeout", rollerConfig.getRssCacheTime().toString()));
178             props.put("debug.memory.enabled",
179                 new RollerPropertyData("debug.memory.enabled", rollerConfig.getMemDebug().toString()));
180             */

181             props.put("spam.referers.ignorewords",
182                 new RollerPropertyData("spam.referers.ignorewords", rollerConfig.getRefererSpamWords()));
183         }
184         else
185         {
186             mLogger.info("Old roller config not found ... default values will be loaded");
187         }
188         
189         return props;
190     }
191
192     
193     /**
194      * This method compares the property definitions in the RuntimeConfigDefs
195      * file with the properties in the given Map and initializes any properties
196      * that were not found in the Map.
197      *
198      * If the Map of props is empty/null then we will initialize all properties.
199      **/

200     private Map JavaDoc initializeMissingProps(Map JavaDoc props) {
201         
202     if(props == null)
203         props = new HashMap JavaDoc();
204
205         // start by getting our runtimeConfigDefs
206
RuntimeConfigDefs runtimeConfigDefs =
207                 RollerRuntimeConfig.getRuntimeConfigDefs();
208         
209         // iterator through all the definitions and add properties
210
// that are not already in our props map
211
ConfigDef configDef = null;
212         DisplayGroup dGroup = null;
213         PropertyDef propDef = null;
214         Iterator JavaDoc defs = runtimeConfigDefs.getConfigDefs().iterator();
215         while(defs.hasNext()) {
216             configDef = (ConfigDef) defs.next();
217             
218             Iterator JavaDoc groups = configDef.getDisplayGroups().iterator();
219             while(groups.hasNext()) {
220                 dGroup = (DisplayGroup) groups.next();
221                 
222                 Iterator JavaDoc propdefs = dGroup.getPropertyDefs().iterator();
223                 while(propdefs.hasNext()) {
224                     propDef = (PropertyDef) propdefs.next();
225                     
226                     // do we already have this prop? if not then add it
227
if(!props.containsKey(propDef.getName())) {
228                         RollerPropertyData newprop =
229                                 new RollerPropertyData(propDef.getName(), propDef.getDefaultValue());
230                         
231                         props.put(propDef.getName(), newprop);
232                                 
233                         mLogger.info("Found uninitialized property "+propDef.getName()+
234                                 " ... setting value to ["+propDef.getDefaultValue()+"]");
235                     }
236                 }
237             }
238         }
239         
240         return props;
241     }
242     
243 }
244
Popular Tags