1 13 package info.magnolia.cms.servlets; 14 15 import info.magnolia.cms.beans.config.ConfigLoader; 16 import info.magnolia.cms.beans.config.ModuleRegistration; 17 import info.magnolia.cms.core.SystemProperty; 18 import info.magnolia.cms.module.ModuleDefinition; 19 import info.magnolia.cms.module.PropertyDefinition; 20 import info.magnolia.logging.Log4jConfigurer; 21 22 import java.io.File ; 23 import java.io.FileInputStream ; 24 import java.io.FileNotFoundException ; 25 import java.io.IOException ; 26 import java.io.InputStream ; 27 import java.net.InetAddress ; 28 import java.net.UnknownHostException ; 29 import java.text.MessageFormat ; 30 import java.util.Iterator ; 31 import java.util.Map ; 32 import java.util.Properties ; 33 34 import javax.servlet.ServletContext ; 35 import javax.servlet.ServletContextEvent ; 36 import javax.servlet.ServletContextListener ; 37 38 import org.apache.commons.collections.OrderedMap; 39 import org.apache.commons.collections.OrderedMapIterator; 40 import org.apache.commons.io.IOUtils; 41 import org.apache.commons.lang.ArrayUtils; 42 import org.apache.commons.lang.StringUtils; 43 import org.slf4j.Logger; 44 import org.slf4j.LoggerFactory; 45 46 47 117 public class PropertyInitializer implements ServletContextListener { 118 119 122 private static final long serialVersionUID = 222L; 123 124 127 private static Logger log = LoggerFactory.getLogger(PropertyInitializer.class); 128 129 132 public static final String MAGNOLIA_INITIALIZATION_FILE = "magnolia.initialization.file"; 134 137 public static final String DEFAULT_INITIALIZATION_PARAMETER = "WEB-INF/config/${servername}/${webapp}/magnolia.properties," + "WEB-INF/config/${servername}/magnolia.properties," + "WEB-INF/config/${webapp}/magnolia.properties," + "WEB-INF/config/default/magnolia.properties," + "WEB-INF/config/magnolia.properties"; 144 147 private static final String MGNL_BEANS_PROPERTIES = "/mgnl-beans.properties"; 148 149 152 protected Properties envProperties = new Properties (); 153 154 157 public void contextDestroyed(ServletContextEvent sce) { 158 Log4jConfigurer.shutdownLogging(envProperties); 159 } 160 161 164 public void contextInitialized(ServletContextEvent sce) { 165 final ServletContext context = sce.getServletContext(); 166 167 loadBeanProperties(); 168 169 loadModuleProperties(); 170 171 String propertiesLocationString = context.getInitParameter(MAGNOLIA_INITIALIZATION_FILE); 172 173 if (log.isDebugEnabled()) { 174 log.debug("{} value in web.xml is :[{}]", MAGNOLIA_INITIALIZATION_FILE, propertiesLocationString); } 176 if (StringUtils.isEmpty(propertiesLocationString)) { 177 log.debug("{} value in web.xml is undefined, falling back to default: {}", MAGNOLIA_INITIALIZATION_FILE, DEFAULT_INITIALIZATION_PARAMETER); 178 propertiesLocationString = DEFAULT_INITIALIZATION_PARAMETER; 179 } 180 181 String [] propertiesLocation = StringUtils.split(propertiesLocationString, ','); 182 183 String servername = initServername(); 184 185 String rootPath = initRootPath(context); 186 187 String webapp = initWebappName(rootPath); 188 189 if (log.isDebugEnabled()) { 190 log.debug("rootPath is {}, webapp is {}", rootPath, webapp); } 192 193 createApplicationDirectories(webapp); 194 195 boolean found = false; 196 197 found = loadPropertiesFiles(propertiesLocation, servername, rootPath, webapp, found); 198 199 overloadWithSystemProperties(); 200 201 if(!found){ 202 log 203 .error(MessageFormat 204 .format( 205 "No configuration found using location list {0}. [servername] is [{1}], [webapp] is [{2}] and base path is [{3}]", new Object []{ArrayUtils.toString(propertiesLocation), servername, webapp, rootPath})); 207 return; 208 } 209 210 Log4jConfigurer.initLogging(context); 211 212 new ConfigLoader(context); 213 214 } 215 216 219 protected void loadModuleProperties() { 220 OrderedMap defs = ModuleRegistration.getInstance().getModuleDefinitions(); 221 for (OrderedMapIterator iter = defs.orderedMapIterator(); iter.hasNext();) { 222 iter.next(); 223 ModuleDefinition def = (ModuleDefinition) iter.getValue(); 224 for (Iterator iter2 = def.getProperties().iterator(); iter2.hasNext();) { 225 PropertyDefinition property = (PropertyDefinition) iter2.next(); 226 SystemProperty.setProperty(property.getName(), property.getValue()); 227 } 228 } 229 } 230 231 protected boolean loadPropertiesFiles(String [] propertiesLocation, String servername, String rootPath, String webapp, boolean found) { 232 for (int j = propertiesLocation.length-1; j >= 0; j--) { 233 String location = StringUtils.trim(propertiesLocation[j]); 234 location = StringUtils.replace(location, "${servername}", servername); location = StringUtils.replace(location, "${webapp}", webapp); 237 if(loadPropertiesFile(rootPath, location)){ 238 found = true; 239 } 240 } 241 return found; 242 } 243 244 protected String initWebappName(String rootPath) { 245 String webapp = StringUtils.substringAfterLast(rootPath, "/"); envProperties.put(SystemProperty.MAGNOLIA_WEBAPP, webapp); 247 return webapp; 248 } 249 250 protected String initRootPath(final ServletContext context) { 251 String rootPath = StringUtils.replace(context.getRealPath(StringUtils.EMPTY), "\\", "/"); rootPath = StringUtils.removeEnd(rootPath, "/"); 253 envProperties.put(SystemProperty.MAGNOLIA_APP_ROOTDIR, rootPath); 254 255 String magnoliaRootSysproperty = (String ) envProperties.get(SystemProperty.MAGNOLIA_ROOT_SYSPROPERTY); 257 if (StringUtils.isNotEmpty(magnoliaRootSysproperty)) { 258 System.setProperty(magnoliaRootSysproperty, rootPath); 259 log.info("Setting the magnolia root system property: [" + magnoliaRootSysproperty + "] to [" + rootPath + "]"); } 261 return rootPath; 262 } 263 264 protected String initServername() { 265 String servername = null; 266 267 try { 268 servername = StringUtils.lowerCase(InetAddress.getLocalHost().getHostName()); 269 envProperties.put(SystemProperty.MAGNOLIA_SERVERNAME, servername); 270 } 271 catch (UnknownHostException e) { 272 log.error(e.getMessage()); 273 } 274 return servername; 275 } 276 277 protected void loadBeanProperties() { 278 InputStream mgnlbeansStream = getClass().getResourceAsStream(MGNL_BEANS_PROPERTIES); 280 281 if (mgnlbeansStream != null) { 282 Properties mgnlbeans = new Properties (); 283 try { 284 mgnlbeans.load(mgnlbeansStream); 285 } 286 catch (IOException e) { 287 log.error("Unable to load {} due to an IOException: {}", MGNL_BEANS_PROPERTIES, e.getMessage()); 288 } 289 finally { 290 IOUtils.closeQuietly(mgnlbeansStream); 291 } 292 293 for (Iterator iter = mgnlbeans.keySet().iterator(); iter.hasNext();) { 294 String key = (String ) iter.next(); 295 SystemProperty.setProperty(key, mgnlbeans.getProperty(key)); 296 } 297 298 } 299 else { 300 log 301 .warn( 302 "{} not found in the classpath. Check that all the needed implementation classes are defined in your custom magnolia.properties file.", 303 MGNL_BEANS_PROPERTIES); 304 } 305 } 306 307 310 private void createApplicationDirectories(String webapp) { 311 File logs = new File (webapp + File.separator + "logs"); 312 File tmp = new File (webapp + File.separator + "tmp"); 313 if (!logs.exists()) { 314 logs.mkdir(); 315 log.debug("Creating " + logs.getAbsoluteFile() + " folder"); 316 } 317 318 if (!tmp.exists()) { 319 tmp.mkdir(); 320 log.debug("Creating " + tmp.getAbsoluteFile() + " folder"); 321 } 322 } 323 324 330 protected boolean loadPropertiesFile(String rootPath, String location) { 331 File initFile = new File (rootPath, location); 332 333 if (!initFile.exists() || initFile.isDirectory()) { 334 if (log.isDebugEnabled()) { 335 log.debug("Configuration file not found with path [{}]", initFile.getAbsolutePath()); 337 } 338 return false; 339 } 340 341 InputStream fileStream=null; 342 try { 343 fileStream = new FileInputStream (initFile); 344 } 345 catch (FileNotFoundException e1) { 346 log.debug("Configuration file not found with path [{}]", initFile.getAbsolutePath()); 348 return false; 349 } 350 351 try { 352 envProperties.load(fileStream); 353 log.info("Loading configuration at {}", initFile.getAbsolutePath()); } 355 catch (Exception e) { 356 log.error(e.getMessage(), e); 357 return false; 358 } 359 finally { 360 IOUtils.closeQuietly(fileStream); 361 } 362 return true; 363 } 364 365 368 protected void overloadWithSystemProperties() { 369 Iterator it = envProperties.entrySet().iterator(); 370 while (it.hasNext()) { 371 Map.Entry param = (Map.Entry ) it.next(); 372 String value = (String )param.getValue(); 373 if(System.getProperties().containsKey(param.getKey())){ 374 log.info("system property found: {}", param.getKey()); 375 value = (String ) System.getProperty((String )param.getKey()); 376 } 377 SystemProperty.setProperty((String ) param.getKey(), value); 378 } 379 } 380 } | Popular Tags |