1 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 ; 19 import java.util.Map ; 20 import java.util.HashMap ; 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 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 44 public PropertiesManagerImpl(PersistenceStrategy strategy) 45 { 46 this.mStrategy = strategy; 47 init(); 48 } 49 50 private void init() 51 { 52 Map props = null; 53 try 54 { 55 props = this.getProperties(); 56 57 if(props.size() < 1) { 58 props = migrateOldRollerConfig(props); 60 props = initializeMissingProps(props); 61 } else { 62 props = initializeMissingProps(props); 64 } 65 66 this.store(props); 68 } 69 catch (Exception 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 (e); 74 } 75 76 } 77 78 81 public void store(RollerPropertyData property) throws RollerException 82 { 83 this.mStrategy.store(property); 84 } 85 86 89 public void store(Map properties) throws RollerException 90 { 91 Iterator 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 116 private Map migrateOldRollerConfig(Map props) 117 { 118 Roller roller = RollerFactory.getRoller(); 120 RollerConfigData rollerConfig = null; 121 122 try 123 { 124 rollerConfig = roller.getConfigManager().getRollerConfig(); 125 } 126 catch (Exception e) 127 { 128 mLogger.error(e); 131 } 132 133 if (rollerConfig != null) 134 { 135 mLogger.info("Found old roller config ... doing migration to new runtime properties."); 136 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 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 200 private Map initializeMissingProps(Map props) { 201 202 if(props == null) 203 props = new HashMap (); 204 205 RuntimeConfigDefs runtimeConfigDefs = 207 RollerRuntimeConfig.getRuntimeConfigDefs(); 208 209 ConfigDef configDef = null; 212 DisplayGroup dGroup = null; 213 PropertyDef propDef = null; 214 Iterator defs = runtimeConfigDefs.getConfigDefs().iterator(); 215 while(defs.hasNext()) { 216 configDef = (ConfigDef) defs.next(); 217 218 Iterator groups = configDef.getDisplayGroups().iterator(); 219 while(groups.hasNext()) { 220 dGroup = (DisplayGroup) groups.next(); 221 222 Iterator propdefs = dGroup.getPropertyDefs().iterator(); 223 while(propdefs.hasNext()) { 224 propDef = (PropertyDef) propdefs.next(); 225 226 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 |