You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by cz...@apache.org on 2006/12/25 21:05:25 UTC

svn commit: r490164 - in /cocoon/trunk/core: cocoon-configuration/cocoon-configuration-api/src/main/java/org/apache/cocoon/configuration/ cocoon-spring/src/main/java/org/apache/cocoon/core/container/spring/

Author: cziegeler
Date: Mon Dec 25 12:05:24 2006
New Revision: 490164

URL: http://svn.apache.org/viewvc?view=rev&rev=490164
Log:
Load properties from WEB-INF/classes after properties from jars
If additional property specifies a directory, load all properties from this directory

Modified:
    cocoon/trunk/core/cocoon-configuration/cocoon-configuration-api/src/main/java/org/apache/cocoon/configuration/PropertyProvider.java
    cocoon/trunk/core/cocoon-spring/src/main/java/org/apache/cocoon/core/container/spring/ResourceUtils.java
    cocoon/trunk/core/cocoon-spring/src/main/java/org/apache/cocoon/core/container/spring/SettingsBeanFactoryPostProcessor.java

Modified: cocoon/trunk/core/cocoon-configuration/cocoon-configuration-api/src/main/java/org/apache/cocoon/configuration/PropertyProvider.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/core/cocoon-configuration/cocoon-configuration-api/src/main/java/org/apache/cocoon/configuration/PropertyProvider.java?view=diff&rev=490164&r1=490163&r2=490164
==============================================================================
--- cocoon/trunk/core/cocoon-configuration/cocoon-configuration-api/src/main/java/org/apache/cocoon/configuration/PropertyProvider.java (original)
+++ cocoon/trunk/core/cocoon-configuration/cocoon-configuration-api/src/main/java/org/apache/cocoon/configuration/PropertyProvider.java Mon Dec 25 12:05:24 2006
@@ -31,7 +31,19 @@
  */
 public interface PropertyProvider {
 
+    /**
+     * The bean must be registered with this name.
+     */
     String ROLE = PropertyProvider.class.getName();
 
+    /**
+     * Provide the properties.
+     *
+     * @param settings    The already loaded settings.
+     * @param runningMode The current running mode.
+     * @param path        A path specifying the application context the settings are applied to.
+     *                    A path of null indicates the root application context.
+     * @return            The additional properties.
+     */
     Properties getProperties(Settings settings, String runningMode, String path);
 }

Modified: cocoon/trunk/core/cocoon-spring/src/main/java/org/apache/cocoon/core/container/spring/ResourceUtils.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/core/cocoon-spring/src/main/java/org/apache/cocoon/core/container/spring/ResourceUtils.java?view=diff&rev=490164&r1=490163&r2=490164
==============================================================================
--- cocoon/trunk/core/cocoon-spring/src/main/java/org/apache/cocoon/core/container/spring/ResourceUtils.java (original)
+++ cocoon/trunk/core/cocoon-spring/src/main/java/org/apache/cocoon/core/container/spring/ResourceUtils.java Mon Dec 25 12:05:24 2006
@@ -33,12 +33,12 @@
 import org.springframework.web.context.support.ServletContextResourcePatternResolver;
 
 /**
- * Utility class for Spring resource handling
+ * Utility class for Spring resource handling.
  * 
  * @version $Id$
  * @since 2.2
  */
-public class ResourceUtils {
+public abstract class ResourceUtils {
 
     /**
      * Get the uri of a resource. This method corrects the uri in the case of
@@ -148,14 +148,24 @@
     }
 
     /**
-     * Return a resource comparator
+     * Return a resource comparator.
+     * This comparator compares the file name of two resources.
+     * In addition all resources contained in a directory named
+     * WEB-INF/classes/cocoon are sorted (in alphabetical) order
+     * after all other files.
      */
     public static Comparator getResourceComparator() {
         return new ResourceComparator();
     }
 
+    /**
+     * Class implementing a simple resource comparator as described
+     * here: {@link ResourceUtils#getResourceComparator}.
+     */
     protected final static class ResourceComparator implements Comparator {
 
+        protected static final String WEB_INF_CLASSES_META_INF_COCOON = "/WEB-INF/classes/META-INF/cocoon/";
+
         /**
          * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
          */
@@ -163,6 +173,24 @@
             if (!(o1 instanceof Resource) || !(o2 instanceof Resource)) {
                 return 0;
             }
+            try {
+                String name1 = ((Resource)o1).getURL().toExternalForm();
+                String name2 = ((Resource)o2).getURL().toExternalForm();
+                // replace '\' with '/'
+                name1.replace('\\', '/');
+                name2.replace('\\', '/');
+                boolean webInfClasses1 = name1.indexOf(ResourceComparator.WEB_INF_CLASSES_META_INF_COCOON) != -1;
+                boolean webInfClasses2 = name2.indexOf(ResourceComparator.WEB_INF_CLASSES_META_INF_COCOON) != -1;
+                if ( !webInfClasses1 && webInfClasses2 ) {
+                    return -1;
+                }
+                if ( webInfClasses1 && !webInfClasses2 ) {
+                    return +1;
+                }
+            } catch (IOException io) {
+                // ignore
+            }
+            // default behaviour:
             return ((Resource) o1).getFilename().compareTo(((Resource) o2).getFilename());
         }
     }

Modified: cocoon/trunk/core/cocoon-spring/src/main/java/org/apache/cocoon/core/container/spring/SettingsBeanFactoryPostProcessor.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/core/cocoon-spring/src/main/java/org/apache/cocoon/core/container/spring/SettingsBeanFactoryPostProcessor.java?view=diff&rev=490164&r1=490163&r2=490164
==============================================================================
--- cocoon/trunk/core/cocoon-spring/src/main/java/org/apache/cocoon/core/container/spring/SettingsBeanFactoryPostProcessor.java (original)
+++ cocoon/trunk/core/cocoon-spring/src/main/java/org/apache/cocoon/core/container/spring/SettingsBeanFactoryPostProcessor.java Mon Dec 25 12:05:24 2006
@@ -128,13 +128,20 @@
      * 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.
+     * 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
      *
@@ -146,7 +153,7 @@
     protected MutableSettings createSettings() {
         // get the running mode
         final String mode = RunningModeHelper.determineRunningMode( this.runningMode );
-        
+
         /*
         if ( !Arrays.asList(SettingsDefaults.RUNNING_MODES).contains(mode) ) {
             final String msg =
@@ -199,12 +206,27 @@
             if ( this.logger.isDebugEnabled() ) {
                 this.logger.debug("Reading user settings from '" + additionalPropertyFile + "'");
             }
-            try {
-                final FileInputStream fis = new FileInputStream(additionalPropertyFile);
-                properties.load(fis);
-                fis.close();
-            } catch (IOException ignore) {
-                this.logger.info("Unable to read '" + additionalPropertyFile + "' - continuing with initialization.", ignore);
+            final File additionalFile = new File(additionalPropertyFile);
+            if ( additionalFile.exists() ) {
+                if ( additionalFile.isDirectory() ) {
+                    // read from directory
+                    ResourceUtils.readProperties(additionalFile.getAbsolutePath(),
+                            properties, this.getResourceLoader(), this.logger);
+                    // read all properties from the mode dependent directory
+                    ResourceUtils.readProperties(additionalFile.getAbsolutePath() + File.separatorChar + mode,
+                                                 properties, this.getResourceLoader(), this.logger);
+                } else {
+                    // read the file
+                    try {
+                        final FileInputStream fis = new FileInputStream(additionalFile);
+                        properties.load(fis);
+                        fis.close();
+                    } catch (IOException ignore) {
+                        this.logger.info("Unable to read '" + additionalPropertyFile + "' - continuing with initialization.", ignore);
+                    }
+                }
+            } else {
+                this.logger.info("Additional settings file '" + additionalPropertyFile + "' does not exist - continuing with initialization.");
             }
         }
         // check for property providers