cocoon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cziege...@apache.org
Subject svn commit: r491401 - /cocoon/trunk/core/cocoon-configuration/cocoon-spring-configurator/src/main/java/org/apache/cocoon/spring/configurator/impl/
Date Sun, 31 Dec 2006 11:08:16 GMT
Author: cziegeler
Date: Sun Dec 31 03:08:14 2006
New Revision: 491401

URL: http://svn.apache.org/viewvc?view=rev&rev=491401
Log:
Unify global and child settings handling

Modified:
    cocoon/trunk/core/cocoon-configuration/cocoon-spring-configurator/src/main/java/org/apache/cocoon/spring/configurator/impl/AbstractSettingsBeanFactoryPostProcessor.java
    cocoon/trunk/core/cocoon-configuration/cocoon-spring-configurator/src/main/java/org/apache/cocoon/spring/configurator/impl/ChildSettingsBeanFactoryPostProcessor.java
    cocoon/trunk/core/cocoon-configuration/cocoon-spring-configurator/src/main/java/org/apache/cocoon/spring/configurator/impl/RunningModeHelper.java
    cocoon/trunk/core/cocoon-configuration/cocoon-spring-configurator/src/main/java/org/apache/cocoon/spring/configurator/impl/SettingsBeanFactoryPostProcessor.java

Modified: cocoon/trunk/core/cocoon-configuration/cocoon-spring-configurator/src/main/java/org/apache/cocoon/spring/configurator/impl/AbstractSettingsBeanFactoryPostProcessor.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/core/cocoon-configuration/cocoon-spring-configurator/src/main/java/org/apache/cocoon/spring/configurator/impl/AbstractSettingsBeanFactoryPostProcessor.java?view=diff&rev=491401&r1=491400&r2=491401
==============================================================================
--- cocoon/trunk/core/cocoon-configuration/cocoon-spring-configurator/src/main/java/org/apache/cocoon/spring/configurator/impl/AbstractSettingsBeanFactoryPostProcessor.java
(original)
+++ cocoon/trunk/core/cocoon-configuration/cocoon-spring-configurator/src/main/java/org/apache/cocoon/spring/configurator/impl/AbstractSettingsBeanFactoryPostProcessor.java
Sun Dec 31 03:08:14 2006
@@ -27,13 +27,17 @@
 import javax.servlet.ServletContext;
 
 import org.apache.cocoon.configuration.MutableSettings;
+import org.apache.cocoon.configuration.PropertyHelper;
+import org.apache.cocoon.configuration.PropertyProvider;
 import org.apache.cocoon.configuration.Settings;
+import org.apache.cocoon.spring.configurator.ResourceUtils;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.springframework.beans.BeansException;
 import org.springframework.beans.factory.BeanDefinitionStoreException;
 import org.springframework.beans.factory.BeanFactory;
 import org.springframework.beans.factory.FactoryBean;
+import org.springframework.beans.factory.HierarchicalBeanFactory;
 import org.springframework.beans.factory.config.BeanDefinition;
 import org.springframework.beans.factory.config.BeanDefinitionVisitor;
 import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
@@ -70,9 +74,17 @@
 
     protected ResourceLoader resourceLoader;
 
+    /**
+     * Additional properties.
+     */
     protected Properties additionalProperties;
 
     /**
+     * List of additional property directories.
+     */
+    protected List directories;
+
+    /**
      * @see org.springframework.beans.factory.config.PropertyPlaceholderConfigurer#setBeanFactory(org.springframework.beans.factory.BeanFactory)
      */
     public void setBeanFactory(BeanFactory factory) {
@@ -94,11 +106,148 @@
         this.resourceLoader = loader;
     }
 
+    public void setDirectories(List directories) {
+        this.directories = directories;
+    }
+
+    public void setAdditionalProperties(Properties props) {
+        this.additionalProperties = props;
+    }
+
+    /**
+     * Initialize this processor.
+     * Setup the settings object.
+     * @throws Exception
+     */
+    public void init()
+    throws Exception {
+        this.settings = this.createSettings();
+
+        this.dumpSettings();
+    }
+
+    /**
+     * Get the running mode.
+     * This method should be implemented by subclasses.
+     */
+    protected abstract String getRunningMode();
+
+    /**
+     * Return a parent settings object if available.
+     */
+    protected Settings getParentSettings() {
+        final BeanFactory parentBeanFactory = ((HierarchicalBeanFactory)this.beanFactory).getParentBeanFactory();
+        if ( parentBeanFactory != null ) {
+            return (Settings)parentBeanFactory.getBean(Settings.ROLE);
+        }
+        return null;
+    }
+
+    /**
+     * Create a new settings object.
+     * If a parent settings object is available a new child settings object is created.
+     * Otherwise a new root settings object with the running mode is instantiated.
+     */
+    protected MutableSettings createMutableSettingsInstance() {
+        final Settings parentSettings = this.getParentSettings();
+        if ( parentSettings == null ) {
+            return new MutableSettings(this.getRunningMode());              
+        }
+        return new MutableSettings(parentSettings);
+    }
+
+    /**
+     * This method can be used by subclasses to initialize the settings and/or
+     * the properties before {@link #createSettings()} does it's work.
+     */
+    protected void preInit(final MutableSettings s, final Properties properties) {
+        // default implementation does nothing
+    }
+
     /**
-     * This method can be overwritten by subclasses to further initialize the settings
+     * This method can be used by subclasses to initialize the settings and/or
+     * the properties after {@link #createSettings()} did it's work.
      */
-    protected void doInit() {
-        // nothing to do here
+    protected void postInit(final MutableSettings s, final Properties properties) {
+        // default implementation does nothing
+    }
+
+    protected String getNameForPropertyProvider() {
+        return null;
+    }
+
+    /**
+     * Create a settings object.
+     * This method creates the settings by executing the following task:
+     * 1) Create a new mutable settings object invoking {@link #createMutableSettingsInstance()}.
+     * 2) Configure the properties and settings object by calling {@link #preInit(MutableSettings,
Properties)}.
+     * 3) Invoke a {@link PropertyProvider} if configured in the same application context
(or its parent)
+     * 4) Add properties from configured directories {@link #directories}.
+     * 5) Add additional properties configured at {@link #additionalProperties}
+     * 6) Apply system properties
+     * 7) Configure the properties and settings object by calling {@link #postInit(MutableSettings,
Properties)}.
+     * 8) Replace references in properties
+     * 9) Configure the settings object with the properties
+     * 10) Make the settings object read-only.
+     *
+     * @return A new Settings object
+     */
+    protected MutableSettings createSettings() {
+        final String mode = this.getRunningMode();
+        // create an empty settings objects
+        final MutableSettings s = this.createMutableSettingsInstance();
+        // create the initial properties
+        final Properties properties = new Properties();
+        // invoke pre initialization hook
+        this.preInit(s, properties);
+
+        // check for property providers
+        if (this.beanFactory != null && this.beanFactory.containsBean(PropertyProvider.ROLE)
) {
+            try {
+                final PropertyProvider provider = (PropertyProvider)this.beanFactory.getBean(PropertyProvider.ROLE);
+                final Properties providedProperties = provider.getProperties(s, mode, this.getNameForPropertyProvider());
+                if ( providedProperties != null ) {
+                    properties.putAll(providedProperties);
+                }
+            } catch (Exception ignore) {
+                this.logger.warn("Unable to get properties from provider.", ignore);
+                this.logger.warn("Continuing initialization.");            
+            }
+        }
+
+        // add aditional directories
+        if ( this.directories != null ) {
+            final Iterator i = directories.iterator();
+            while ( i.hasNext() ) {
+                final String directory = (String)i.next();
+                // now read all properties from the properties directory
+                ResourceUtils.readProperties(directory, properties, this.getResourceLoader(),
this.logger);
+                // read all properties from the mode dependent directory
+                ResourceUtils.readProperties(directory + '/' + mode, properties, this.getResourceLoader(),
this.logger);
+            }
+        }
+
+        // add additional properties
+        if ( this.additionalProperties != null ) {
+            PropertyHelper.replaceAll(this.additionalProperties, s);
+            properties.putAll(this.additionalProperties);
+        }
+
+        // now overwrite with system properties
+        try {
+            properties.putAll(System.getProperties());
+        } catch (SecurityException se) {
+            // we ignore this
+        }
+        // invoke pre initialization hook
+        this.postInit(s, properties);
+
+        PropertyHelper.replaceAll(properties, this.getParentSettings());
+        // configure settings
+        s.configure(properties);
+        s.makeReadOnly();
+
+        return s;
     }
 
     protected ResourceLoader getResourceLoader() {

Modified: cocoon/trunk/core/cocoon-configuration/cocoon-spring-configurator/src/main/java/org/apache/cocoon/spring/configurator/impl/ChildSettingsBeanFactoryPostProcessor.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/core/cocoon-configuration/cocoon-spring-configurator/src/main/java/org/apache/cocoon/spring/configurator/impl/ChildSettingsBeanFactoryPostProcessor.java?view=diff&rev=491401&r1=491400&r2=491401
==============================================================================
--- cocoon/trunk/core/cocoon-configuration/cocoon-spring-configurator/src/main/java/org/apache/cocoon/spring/configurator/impl/ChildSettingsBeanFactoryPostProcessor.java
(original)
+++ cocoon/trunk/core/cocoon-configuration/cocoon-spring-configurator/src/main/java/org/apache/cocoon/spring/configurator/impl/ChildSettingsBeanFactoryPostProcessor.java
Sun Dec 31 03:08:14 2006
@@ -18,21 +18,18 @@
  */
 package org.apache.cocoon.spring.configurator.impl;
 
-import java.util.Iterator;
-import java.util.List;
-import java.util.Properties;
-
-import org.apache.cocoon.configuration.MutableSettings;
-import org.apache.cocoon.configuration.PropertyHelper;
-import org.apache.cocoon.configuration.PropertyProvider;
-import org.apache.cocoon.configuration.Settings;
-import org.apache.cocoon.spring.configurator.ResourceUtils;
-import org.springframework.beans.factory.BeanFactory;
-import org.springframework.beans.factory.HierarchicalBeanFactory;
 
 /**
  * This is a bean factory post processor which sets up a child settings object.
  *
+ * The settings object is created by reading several property files and merging of
+ * the values. If there is more than one definition for a property, the last one wins.
+ * The property files are read in the following order:
+ * 1) Property provider (if configured in the bean factory)
+ * 2) Add properties from configured directories {@link #directories}.
+ * 3) Add additional properties configured at {@link #additionalProperties}
+ * 4) System properties
+ *
  * @since 1.0
  * @version $Id$
  */
@@ -42,92 +39,24 @@
     /** Unique name for this child settings context. */
     protected String name;
 
-    /** List of property directories. */
-    protected List directories;
-
     /**
-     * Initialize this settings.
-     * Setup the settings object.
-     * @throws Exception
+     * Set the unique name for this settings context.
      */
-    public void init()
-    throws Exception {
-        this.settings = this.createSettings();
-
-        this.doInit();
-
-        // settings can't be changed anymore
-        this.settings.makeReadOnly();
-    }
-
     public void setName(String newName) {
         this.name = newName;
     }
 
-    public void setDirectories(List directories) {
-        this.directories = directories;
-    }
-
-    public void setAdditionalProperties(Properties props) {
-        this.additionalProperties = props;
+    /**
+     * @see org.apache.cocoon.spring.configurator.impl.AbstractSettingsBeanFactoryPostProcessor#getRunningMode()
+     */
+    protected String getRunningMode() {
+        return this.getParentSettings().getRunningMode();
     }
 
     /**
-     * Get the settings for Cocoon.
-     * This method reads several property files and merges the result. If there
-     * is more than one definition for a property, the last one wins.
-     * The property files are read in the following order:
-     * 1) PROPERTYDIR/*.properties
-     *    Default values for the core and each block - the order in which the files are read
is not guaranteed.
-     * 2) PROPERTYDIR/[RUNNING_MODE]/*.properties
-     *    Default values for the running mode - the order in which the files are read is
not guaranteed.
-     * 3) Property providers (ToBeDocumented)
-     *
-     * @return A new Settings object
+     * @see org.apache.cocoon.spring.configurator.impl.AbstractSettingsBeanFactoryPostProcessor#getNameForPropertyProvider()
      */
-    protected MutableSettings createSettings() {
-        final BeanFactory parentBeanFactory = ((HierarchicalBeanFactory)this.beanFactory).getParentBeanFactory();
-        final Settings parent = (Settings)parentBeanFactory.getBean(Settings.ROLE);
-        // get the running mode
-        final String mode = parent.getRunningMode();
-        // get properties
-        final Properties properties = new Properties();
-
-        // create an empty settings objects
-        final MutableSettings s = new MutableSettings(parent);
-
-        if ( this.directories != null ) {
-            final Iterator i = directories.iterator();
-            while ( i.hasNext() ) {
-                final String directory = (String)i.next();
-                // now read all properties from the properties directory
-                ResourceUtils.readProperties(directory, properties, this.getResourceLoader(),
this.logger);
-                // read all properties from the mode dependent directory
-                ResourceUtils.readProperties(directory + '/' + mode, properties, this.getResourceLoader(),
this.logger);
-            }
-        }
-
-        // Next look for a custom property provider in the parent bean factory
-        if (parentBeanFactory.containsBean(PropertyProvider.ROLE) ) {
-            try {
-                final PropertyProvider provider = (PropertyProvider)parentBeanFactory.getBean(PropertyProvider.ROLE);
-                final Properties providedProperties = provider.getProperties(s, mode, this.name);
-                if ( providedProperties != null ) {
-                    properties.putAll(providedProperties);
-                }
-            } catch (Exception ignore) {
-                this.logger.warn("Unable to get properties from provider.", ignore);
-                this.logger.warn("Continuing initialization.");            
-            }
-        }
-
-        if ( this.additionalProperties != null ) {
-            PropertyHelper.replaceAll(this.additionalProperties, s);
-            properties.putAll(this.additionalProperties);
-        }
-        PropertyHelper.replaceAll(properties, parent);
-        s.configure(properties);
-
-        return s;
+    protected String getNameForPropertyProvider() {
+        return this.name;
     }
 }

Modified: cocoon/trunk/core/cocoon-configuration/cocoon-spring-configurator/src/main/java/org/apache/cocoon/spring/configurator/impl/RunningModeHelper.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/core/cocoon-configuration/cocoon-spring-configurator/src/main/java/org/apache/cocoon/spring/configurator/impl/RunningModeHelper.java?view=diff&rev=491401&r1=491400&r2=491401
==============================================================================
--- cocoon/trunk/core/cocoon-configuration/cocoon-spring-configurator/src/main/java/org/apache/cocoon/spring/configurator/impl/RunningModeHelper.java
(original)
+++ cocoon/trunk/core/cocoon-configuration/cocoon-spring-configurator/src/main/java/org/apache/cocoon/spring/configurator/impl/RunningModeHelper.java
Sun Dec 31 03:08:14 2006
@@ -26,8 +26,11 @@
  * @since 1.0
  * @version $Id$
  */
-public class RunningModeHelper {
+public abstract class RunningModeHelper {
 
+    /**
+     * Name of the system property specifying the running mode.
+     */
     public final static String PROPERTY_RUNNING_MODE = "org.apache.cocoon.mode";
     
     // determine an ev. set running mode from the system properties
@@ -42,15 +45,12 @@
         SYSTEM_RUNNING_MODE = mode;
     }
 
-    /** Name of the property specifying the running mode. */
-    private RunningModeHelper() {
-        // never initiate
-    }
-
     /** 
-     * Determine the runningmode. 
+     * Determine the running mode. 
      * A non-null system property will have precedence over everything else.
      * The system default running mode will be used if the passed parameter mode is null.
+     * @see #PROPERTY_RUNNING_MODE
+     * @see SettingsDefaults#DEFAULT_RUNNING_MODE
      */
     public static String determineRunningMode(String mode) {
         if (SYSTEM_RUNNING_MODE != null) {
@@ -60,5 +60,20 @@
             return SettingsDefaults.DEFAULT_RUNNING_MODE;
         }
         return mode;
+    }
+
+    /**
+     * Check if the value for the running mode is valid.
+     * @throws IllegalArgumentException if the mode is invalid
+     */
+    public static void checkRunningMode(String mode)
+    throws IllegalArgumentException {
+        /*
+        if ( !Arrays.asList(SettingsDefaults.RUNNING_MODES).contains(mode) ) {
+            final String msg =
+                "Invalid running mode: " + mode + " - Use one of: " + Arrays.asList(SettingsDefaults.RUNNING_MODES);
+            throw new IllegalArgumentException(msg);
+        }
+        */        
     }
 }

Modified: cocoon/trunk/core/cocoon-configuration/cocoon-spring-configurator/src/main/java/org/apache/cocoon/spring/configurator/impl/SettingsBeanFactoryPostProcessor.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/core/cocoon-configuration/cocoon-spring-configurator/src/main/java/org/apache/cocoon/spring/configurator/impl/SettingsBeanFactoryPostProcessor.java?view=diff&rev=491401&r1=491400&r2=491401
==============================================================================
--- cocoon/trunk/core/cocoon-configuration/cocoon-spring-configurator/src/main/java/org/apache/cocoon/spring/configurator/impl/SettingsBeanFactoryPostProcessor.java
(original)
+++ cocoon/trunk/core/cocoon-configuration/cocoon-spring-configurator/src/main/java/org/apache/cocoon/spring/configurator/impl/SettingsBeanFactoryPostProcessor.java
Sun Dec 31 03:08:14 2006
@@ -26,8 +26,6 @@
 import java.util.Properties;
 
 import org.apache.cocoon.configuration.MutableSettings;
-import org.apache.cocoon.configuration.PropertyHelper;
-import org.apache.cocoon.configuration.PropertyProvider;
 import org.apache.cocoon.configuration.Settings;
 import org.apache.cocoon.configuration.SettingsDefaults;
 import org.apache.cocoon.spring.configurator.ResourceUtils;
@@ -38,125 +36,99 @@
  * them in the spring configuration files.
  * In addition this bean acts as a factory bean providing the settings object.
  *
+ * The settings object is created by reading several property files and merging of
+ * the values. If there is more than one definition for a property, the last one wins.
+ * The property files are read in the following order:
+ * 1) If {@link #readFromClasspath} is true: classpath*:/META-INF/cocoon/properties/*.properties
+ *    Default values for the core and each block - the files are read in alphabetical order.
+ *    Actually the files are read in two chunks, the first one containing all property files
+ *    from jar files, and the second one containing all property files from WEB-INF/classes.
+ * 2) If {@link #readFromClasspath} is true: classpath*:/META-INF/cocoon/properties/[RUNNING_MODE]/*.properties
+ *    Default values for the core and each block for a specific running mode - the files
are
+ *    read in alphabetical order.
+ *    Actually the files are read in two chunks, the first one containing all property files
+ *    from jar files, and the second one containing all property files from WEB-INF/classes.
+ * 3) If {@link #readFromGlobalLocation} is true: /WEB-INF/cocoon/properties/*.properties
+ *    Default values for the core and each block - the files are read in alphabetical order.
+ *    Actually the files are read in two chunks, the first one containing all property files
+ *    from jar files, and the second one containing all property files from WEB-INF/classes.
+ * 4) If {@link #readFromGlobalLocation} is true: /WEB-INF/cocoon/properties/[RUNNING_MODE]/*.properties
+ *    Default values for the core and each block for a specific running mode - the files
are
+ *    read in alphabetical order.
+ *    Actually the files are read in two chunks, the first one containing all property files
+ *    from jar files, and the second one containing all property files from WEB-INF/classes.
+ * 5) Working directory from servlet context (if not already set)
+ * 6) Optional property file which is stored under ".cocoon/settings.properties" in the user
+ *    directory.
+ * 7) Additional property file specified by the "org.apache.cocoon.settings" property. If
the
+ *    property defines a directory, all property files from this directory are read in alphabetical
+ *    order and all files from a sub directory with the name of the current running mode
+ *    are read in alphabetical order as well.
+ * 8) Property provider (if configured in the bean factory)
+ * 9) Add properties from configured directories {@link #directories}.
+ * 10) Add additional properties configured at {@link #additionalProperties}
+ * 11) System properties
+ *
+ * This means that system properties (provided on startup of the web application) override
all
+ * others etc.
+ *
  * @since 1.0
  * @version $Id$
  */
 public class SettingsBeanFactoryPostProcessor
     extends AbstractSettingsBeanFactoryPostProcessor {
 
+    /**
+     * The running mode for the web application.
+     */
     protected String runningMode = SettingsDefaults.DEFAULT_RUNNING_MODE;
 
+    /**
+     * Should we read the properties from the classpath?
+     * @see Constants#CLASSPATH_PROPERTIES_LOCATION
+     */
+    protected boolean readFromClasspath = true;
+
+    /**
+     * Should we read the properties from the global location?
+     * @see Constants#GLOBAL_PROPERTIES_LOCATION
+     */
+    protected boolean readFromGlobalLocation = true;
+
+    /**
+     * Set the running mode.
+     */
     public void setRunningMode(String runningMode) {
         this.runningMode = runningMode;
     }
 
     /**
-     * Initialize this processor.
-     * Setup the settings object.
-     * @throws Exception
+     * Set if we read property configurations from the classpath.
+     * @param readFromClasspath
      */
-    public void init()
-    throws Exception {
-        this.settings = this.createSettings();
-
-        this.doInit();
-
-        this.initSettingsFiles();
-        // settings can't be changed anymore
-        this.settings.makeReadOnly();
-
-        this.dumpSystemProperties();
-        this.dumpSettings();
-        this.forceLoad();
+    public void setReadFromClasspath(boolean readFromClasspath) {
+        this.readFromClasspath = readFromClasspath;
     }
 
     /**
-     * Init work, upload and cache directory
+     * Set if we read property configurations from the global location.
      */
-    protected void initSettingsFiles() {
-        // first init the work-directory for the logger.
-        // this is required if we are running inside a war file!
-        final String workDirParam = settings.getWorkDirectory();
-        File workDir;
-        if (workDirParam != null) {
-            // No context path : consider work-directory as absolute
-            workDir = new File(workDirParam);
-        } else {
-            workDir = new File("cocoon-files");
-        }
-        workDir.mkdirs();
-        settings.setWorkDirectory(workDir.getAbsolutePath());
-
-        // Output some debug info
-        if (this.logger.isDebugEnabled()) {
-            if (workDirParam != null) {
-                this.logger.debug("Using work-directory " + workDir);
-            } else {
-                this.logger.debug("Using default work-directory " + workDir);
-            }
-        }
-
-        String cacheDirParam = settings.getCacheDirectory();
-        File cacheDir;
-        if (cacheDirParam != null) {
-            cacheDir = new File(cacheDirParam);
-            if (this.logger.isDebugEnabled()) {
-                this.logger.debug("Using cache-directory " + cacheDir);
-            }
-        } else {
-            cacheDir = new File(workDir, "cache-dir" + File.separator);
-            File parent = cacheDir.getParentFile();
-            if (parent != null) {
-                parent.mkdirs();
-            }
-            if (this.logger.isDebugEnabled()) {
-                this.logger.debug("cache-directory was not set - defaulting to " + cacheDir);
-            }
-        }
-        cacheDir.mkdirs();
-        settings.setCacheDirectory(cacheDir.getAbsolutePath());
+    public void setReadFromGlobalLocation(boolean readFromGlobalLocation) {
+        this.readFromGlobalLocation = readFromGlobalLocation;
     }
 
     /**
-     * Get the settings for Cocoon.
-     * This method reads several property files and merges the result. If there
-     * is more than one definition for a property, the last one wins.
-     * The property files are read in the following order:
-     * 1) classpath*:/META-INF/cocoon/properties/*.properties
-     *    Default values for the core and each block - the files are read in alphabetical
order.
-     *    Actually the files are read in two chunks, the first one containing all property
files
-     *    from jar files, and the second one containing all property files from WEB-INF/classes.
-     * 2) classpath*:/META-INF/cocoon/properties/[RUNNING_MODE]/*.properties
-     *    Default values for the core and each block for a specific running mode - the files
are
-     *    read in alphabetical order.
-     *    Actually the files are read in two chunks, the first one containing all property
files
-     *    from jar files, and the second one containing all property files from WEB-INF/classes.
-     * 3) Working directory from servlet context (if not already set)
-     * 4) Optional property file which is stored under ".cocoon/settings.properties" in the
user
-     *    directory.
-     * 5) Additional property file specified by the "org.apache.cocoon.settings" property.
If the
-     *    property defines a directory, all property files from this directory are read in
alphabetical
-     *    order and all files from a sub directory with the name of the current running mode
-     *    are read in alphabetical order as well.
-     * 6) Property provider (if configured in the bean factory)
-     * 7) System properties
-     *
-     * This means that system properties (provided on startup of the web application) override
all
-     * others etc.
-     *
-     * @return A new Settings object
+     * Initialize this processor.
+     * Setup the settings object.
+     * @throws Exception
      */
-    protected MutableSettings createSettings() {
+    public void init()
+    throws Exception {
         // get the running mode
-        final String mode = RunningModeHelper.determineRunningMode( this.runningMode );
+        final String mode = this.getRunningMode();
+        RunningModeHelper.checkRunningMode(mode);
 
-        /*
-        if ( !Arrays.asList(SettingsDefaults.RUNNING_MODES).contains(mode) ) {
-            final String msg =
-                "Invalid running mode: " + mode + " - Use one of: " + Arrays.asList(SettingsDefaults.RUNNING_MODES);
-            throw new IllegalArgumentException(msg);
-        }
-        */
+        // print out version information
         final Properties pomProps = ResourceUtils.getPOMProperties("org.apache.cocoon", "cocoon-spring-configurator");
         final String version;
         if ( pomProps != null ) {
@@ -164,29 +136,45 @@
         } else {
             version = null;
         }
-
         this.servletContext.log("Apache Cocoon Spring Configurator " +
                                 (version != null ? "v" + version + " " : "") +
                                 "is running in mode '" + mode + "'.");
 
-        // create an empty settings objects
-        final MutableSettings s = new MutableSettings(mode);
-        // create an empty properties object
-        final Properties properties = new Properties();
-
-        // now read all properties from classpath directory
-        ResourceUtils.readProperties(org.apache.cocoon.spring.configurator.impl.Constants.CLASSPATH_PROPERTIES_LOCATION,
-                properties, this.getResourceLoader(), this.logger);
-        // read all properties from the mode dependent directory
-        ResourceUtils.readProperties(org.apache.cocoon.spring.configurator.impl.Constants.CLASSPATH_PROPERTIES_LOCATION
-                + "/" + mode, properties, this.getResourceLoader(), this.logger);
-
-        // now read all properties from the properties directory
-        ResourceUtils.readProperties(org.apache.cocoon.spring.configurator.impl.Constants.GLOBAL_PROPERTIES_LOCATION,
-                properties, this.getResourceLoader(), this.logger);
-        // read all properties from the mode dependent directory
-        ResourceUtils.readProperties(org.apache.cocoon.spring.configurator.impl.Constants.GLOBAL_PROPERTIES_LOCATION
-                + "/" + mode, properties, this.getResourceLoader(), this.logger);
+        // first we dump the system properties
+        this.dumpSystemProperties();
+        // now create the settings object
+        super.init();
+
+        // finally pre load classes
+        this.forceLoad();
+    }
+
+    /**
+     * @see org.apache.cocoon.spring.configurator.impl.AbstractSettingsBeanFactoryPostProcessor#getRunningMode()
+     */
+    protected String getRunningMode() {
+        return RunningModeHelper.determineRunningMode( this.runningMode );
+    }
+
+    protected void preInit(final MutableSettings s, final Properties properties) {
+        final String mode = this.getRunningMode();
+        if ( this.readFromClasspath ) {
+            // now read all properties from classpath directory
+            ResourceUtils.readProperties(Constants.CLASSPATH_PROPERTIES_LOCATION,
+                    properties, this.getResourceLoader(), this.logger);
+            // read all properties from the mode dependent directory
+            ResourceUtils.readProperties(Constants.CLASSPATH_PROPERTIES_LOCATION
+                    + "/" + mode, properties, this.getResourceLoader(), this.logger);
+        }
+
+        if ( this.readFromGlobalLocation ) {
+            // now read all properties from the properties directory
+            ResourceUtils.readProperties(Constants.GLOBAL_PROPERTIES_LOCATION,
+                    properties, this.getResourceLoader(), this.logger);
+            // read all properties from the mode dependent directory
+            ResourceUtils.readProperties(Constants.GLOBAL_PROPERTIES_LOCATION
+                    + "/" + mode, properties, this.getResourceLoader(), this.logger);
+        }
 
         // fill from the servlet context
         if ( s.getWorkDirectory() == null ) {
@@ -242,35 +230,53 @@
                 this.logger.info("Additional settings file '" + additionalPropertyFile +
"' does not exist - continuing with initialization.");
             }
         }
-        // check for property providers
-        if (this.beanFactory != null && this.beanFactory.containsBean(PropertyProvider.ROLE)
) {
-            try {
-                final PropertyProvider provider = (PropertyProvider)this.beanFactory.getBean(PropertyProvider.ROLE);
-                final Properties providedProperties = provider.getProperties(s, mode, null);
-                if ( providedProperties != null ) {
-                    properties.putAll(providedProperties);
-                }
-            } catch (Exception ignore) {
-                this.logger.warn("Unable to get properties from provider.", ignore);
-                this.logger.warn("Continuing initialization.");            
-            }
-        }
-        
-        if ( this.additionalProperties != null ) {
-            PropertyHelper.replaceAll(this.additionalProperties, s);
-            properties.putAll(this.additionalProperties);
+    }
+
+    /**
+     * Init work, upload and cache directory
+     */
+    protected void postInit(final MutableSettings s, final Properties properties) {
+        // first init the work-directory for the logger.
+        // this is required if we are running inside a war file!
+        final String workDirParam = s.getWorkDirectory();
+        File workDir;
+        if (workDirParam != null) {
+            // No context path : consider work-directory as absolute
+            workDir = new File(workDirParam);
+        } else {
+            workDir = new File("cocoon-files");
         }
+        workDir.mkdirs();
+        s.setWorkDirectory(workDir.getAbsolutePath());
 
-        // now overwrite with system properties
-        try {
-            properties.putAll(System.getProperties());
-        } catch (SecurityException se) {
-            // we ignore this
+        // Output some debug info
+        if (this.logger.isDebugEnabled()) {
+            if (workDirParam != null) {
+                this.logger.debug("Using work-directory " + workDir);
+            } else {
+                this.logger.debug("Using default work-directory " + workDir);
+            }
         }
-        PropertyHelper.replaceAll(properties, null);
-        s.configure(properties);
 
-        return s;
+        String cacheDirParam = s.getCacheDirectory();
+        File cacheDir;
+        if (cacheDirParam != null) {
+            cacheDir = new File(cacheDirParam);
+            if (this.logger.isDebugEnabled()) {
+                this.logger.debug("Using cache-directory " + cacheDir);
+            }
+        } else {
+            cacheDir = new File(workDir, "cache-dir" + File.separator);
+            File parent = cacheDir.getParentFile();
+            if (parent != null) {
+                parent.mkdirs();
+            }
+            if (this.logger.isDebugEnabled()) {
+                this.logger.debug("cache-directory was not set - defaulting to " + cacheDir);
+            }
+        }
+        cacheDir.mkdirs();
+        s.setCacheDirectory(cacheDir.getAbsolutePath());
     }
 
     /**



Mime
View raw message