001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. The ASF licenses this file to You
004: * under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License. For additional information regarding
015: * copyright in this work, please see the NOTICE file in the top level
016: * directory of this distribution.
017: */
018: /*
019: * HibernatePropertiesManagerImpl.java
020: *
021: * Created on April 21, 2005, 10:40 AM
022: */
023:
024: package org.apache.roller.business.hibernate;
025:
026: import java.util.HashMap;
027: import java.util.Iterator;
028: import java.util.List;
029: import java.util.Map;
030: import org.hibernate.Criteria;
031: import org.hibernate.HibernateException;
032: import org.hibernate.Session;
033: import org.apache.commons.logging.Log;
034: import org.apache.commons.logging.LogFactory;
035: import org.apache.roller.RollerException;
036: import org.apache.roller.config.RollerRuntimeConfig;
037: import org.apache.roller.config.runtime.ConfigDef;
038: import org.apache.roller.config.runtime.DisplayGroup;
039: import org.apache.roller.config.runtime.PropertyDef;
040: import org.apache.roller.config.runtime.RuntimeConfigDefs;
041: import org.apache.roller.business.PropertiesManager;
042: import org.apache.roller.business.Roller;
043: import org.apache.roller.business.RollerFactory;
044: import org.apache.roller.pojos.RollerConfigData;
045: import org.apache.roller.pojos.RollerPropertyData;
046:
047: /**
048: * Hibernate implementation of the PropertiesManager.
049: */
050: public class HibernatePropertiesManagerImpl implements
051: PropertiesManager {
052:
053: static final long serialVersionUID = -4326713177137796936L;
054:
055: private static Log log = LogFactory
056: .getLog(HibernatePropertiesManagerImpl.class);
057:
058: private HibernatePersistenceStrategy strategy = null;
059:
060: /**
061: * Creates a new instance of HibernatePropertiesManagerImpl
062: */
063: public HibernatePropertiesManagerImpl(
064: HibernatePersistenceStrategy strat) {
065:
066: log.debug("Instantiating Hibernate Properties Manager");
067:
068: this .strategy = strat;
069:
070: // TODO: and new method initialize(props)
071: init();
072: }
073:
074: /**
075: * Retrieve a single property by name.
076: */
077: public RollerPropertyData getProperty(String name)
078: throws RollerException {
079: try {
080: return (RollerPropertyData) strategy.load(name,
081: RollerPropertyData.class);
082: } catch (HibernateException e) {
083: throw new RollerException(e);
084: }
085: }
086:
087: /**
088: * Retrieve all properties.
089: *
090: * Properties are returned in a Map to make them easy to lookup. The Map
091: * uses the property name as the key and the RollerPropertyData object
092: * as the value.
093: */
094: public Map getProperties() throws RollerException {
095:
096: HashMap props = new HashMap();
097:
098: try {
099: Session session = strategy.getSession();
100: Criteria criteria = session
101: .createCriteria(RollerPropertyData.class);
102: List list = criteria.list();
103:
104: /*
105: * for convenience sake we are going to put the list of props
106: * into a map for users to access it. The value element of the
107: * hash still needs to be the RollerPropertyData object so that
108: * we can save the elements again after they have been updated
109: */
110: RollerPropertyData prop = null;
111: Iterator it = list.iterator();
112: while (it.hasNext()) {
113: prop = (RollerPropertyData) it.next();
114: props.put(prop.getName(), prop);
115: }
116: } catch (HibernateException e) {
117: throw new RollerException(e);
118: }
119:
120: return props;
121: }
122:
123: /**
124: * Save a single property.
125: */
126: public void saveProperty(RollerPropertyData property)
127: throws RollerException {
128:
129: this .strategy.store(property);
130: }
131:
132: /**
133: * Save all properties.
134: */
135: public void saveProperties(Map properties) throws RollerException {
136:
137: // just go through the list and saveProperties each property
138: Iterator props = properties.values().iterator();
139: while (props.hasNext()) {
140: this .strategy.store((RollerPropertyData) props.next());
141: }
142: }
143:
144: private void init() {
145: Map 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 e) {
161: log
162: .fatal(
163: "Failed to initialize runtime configuration properties."
164: + "Please check that the database has been upgraded!",
165: e);
166: throw new RuntimeException(e);
167: }
168:
169: }
170:
171: /**
172: * Migrate data from the old roller config.
173: * This is called only if the existing runtime properties are empty.
174: */
175: private Map migrateOldRollerConfig(Map props) {
176: // try to get the old config
177: Roller roller = RollerFactory.getRoller();
178: RollerConfigData rollerConfig = null;
179:
180: try {
181: rollerConfig = roller.getConfigManager().getRollerConfig();
182: } catch (Exception e) {
183: // We currently treat any exception obtaining the roller config
184: // as if we had not found it.
185: log.error(e);
186: }
187:
188: if (rollerConfig != null) {
189: log
190: .info("Found old roller config ... doing migration to new runtime properties.");
191: // copy over data
192: props.put("site.name", new RollerPropertyData("site.name",
193: rollerConfig.getSiteName()));
194: props.put("site.description", new RollerPropertyData(
195: "site.description", rollerConfig
196: .getSiteDescription()));
197: props.put("site.adminemail", new RollerPropertyData(
198: "site.adminemail", rollerConfig.getEmailAddress()));
199: props.put("site.absoluteurl", new RollerPropertyData(
200: "site.absoluteurl", rollerConfig.getAbsoluteURL()));
201: props.put("site.linkbacks.enabled", new RollerPropertyData(
202: "site.linkbacks.enabled", rollerConfig
203: .getEnableLinkback().toString()));
204: props.put("users.registration.enabled",
205: new RollerPropertyData(
206: "users.registration.enabled", rollerConfig
207: .getNewUserAllowed().toString()));
208: props.put("users.themes.path", new RollerPropertyData(
209: "users.themes.path", rollerConfig.getUserThemes()));
210: props.put("users.editor.pages",
211: new RollerPropertyData("users.editor.pages",
212: rollerConfig.getEditorPages()));
213: props.put("users.comments.enabled", new RollerPropertyData(
214: "users.comments.enabled", "true"));
215: props.put("users.comments.autoformat",
216: new RollerPropertyData("users.comments.autoformat",
217: rollerConfig.getAutoformatComments()
218: .toString()));
219: props.put("users.comments.escapehtml",
220: new RollerPropertyData("users.comments.escapehtml",
221: rollerConfig.getEscapeCommentHtml()
222: .toString()));
223: props.put("users.comments.emailnotify",
224: new RollerPropertyData(
225: "users.comments.emailnotify", rollerConfig
226: .getEmailComments().toString()));
227: props.put("uploads.enabled", new RollerPropertyData(
228: "uploads.enabled", rollerConfig.getUploadEnabled()
229: .toString()));
230: props.put("uploads.types.allowed", new RollerPropertyData(
231: "uploads.types.allowed", rollerConfig
232: .getUploadAllow()));
233: props.put("uploads.types.forbid", new RollerPropertyData(
234: "uploads.types.forbid", rollerConfig
235: .getUploadForbid()));
236: props.put("uploads.file.maxsize", new RollerPropertyData(
237: "uploads.file.maxsize", rollerConfig
238: .getUploadMaxFileMB().toString()));
239: props.put("uploads.dir.maxsize", new RollerPropertyData(
240: "uploads.dir.maxsize", rollerConfig
241: .getUploadMaxDirMB().toString()));
242: /* no longer part of runtime config
243: props.put("aggregator.enabled",
244: new RollerPropertyData("aggregator.enabled", rollerConfig.getEnableAggregator().toString()));
245: props.put("aggregator.cache.enabled",
246: new RollerPropertyData("aggregator.cache.enabled", rollerConfig.getRssUseCache().toString()));
247: props.put("aggregator.cache.timeout",
248: new RollerPropertyData("aggregator.cache.timeout", rollerConfig.getRssCacheTime().toString()));
249: props.put("debug.memory.enabled",
250: new RollerPropertyData("debug.memory.enabled", rollerConfig.getMemDebug().toString()));
251: */
252: props.put("spam.blacklist", new RollerPropertyData(
253: "spam.blacklist", rollerConfig
254: .getRefererSpamWords()));
255: } else {
256: log
257: .info("Old roller config not found ... default values will be loaded");
258: }
259:
260: return props;
261: }
262:
263: /**
264: * This method compares the property definitions in the RuntimeConfigDefs
265: * file with the properties in the given Map and initializes any properties
266: * that were not found in the Map.
267: *
268: * If the Map of props is empty/null then we will initialize all properties.
269: **/
270: private Map initializeMissingProps(Map props) {
271:
272: if (props == null)
273: props = new HashMap();
274:
275: // start by getting our runtimeConfigDefs
276: RuntimeConfigDefs runtimeConfigDefs = RollerRuntimeConfig
277: .getRuntimeConfigDefs();
278:
279: // can't do initialization without our config defs
280: if (runtimeConfigDefs == null)
281: return props;
282:
283: // iterator through all the definitions and add properties
284: // that are not already in our props map
285: ConfigDef configDef = null;
286: DisplayGroup dGroup = null;
287: PropertyDef propDef = null;
288: Iterator defs = runtimeConfigDefs.getConfigDefs().iterator();
289: while (defs.hasNext()) {
290: configDef = (ConfigDef) defs.next();
291:
292: Iterator groups = configDef.getDisplayGroups().iterator();
293: while (groups.hasNext()) {
294: dGroup = (DisplayGroup) groups.next();
295:
296: Iterator propdefs = dGroup.getPropertyDefs().iterator();
297: while (propdefs.hasNext()) {
298: propDef = (PropertyDef) propdefs.next();
299:
300: // do we already have this prop? if not then add it
301: if (!props.containsKey(propDef.getName())) {
302: RollerPropertyData newprop = new RollerPropertyData(
303: propDef.getName(), propDef
304: .getDefaultValue());
305:
306: props.put(propDef.getName(), newprop);
307:
308: log.info("Found uninitialized property "
309: + propDef.getName()
310: + " ... setting value to ["
311: + propDef.getDefaultValue() + "]");
312: }
313: }
314: }
315: }
316:
317: return props;
318: }
319:
320: public void release() {
321: }
322:
323: }
|