cocoon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cziege...@apache.org
Subject svn commit: r491430 - /cocoon/trunk/core/cocoon-configuration/cocoon-spring-configurator/src/main/java/org/apache/cocoon/spring/configurator/impl/
Date Sun, 31 Dec 2006 15:20:01 GMT
Author: cziegeler
Date: Sun Dec 31 07:20:00 2006
New Revision: 491430

URL: http://svn.apache.org/viewvc?view=rev&rev=491430
Log:
Final code cleanup

Modified:
    cocoon/trunk/core/cocoon-configuration/cocoon-spring-configurator/src/main/java/org/apache/cocoon/spring/configurator/impl/AbstractElementParser.java
    cocoon/trunk/core/cocoon-configuration/cocoon-spring-configurator/src/main/java/org/apache/cocoon/spring/configurator/impl/AbstractSettingsElementParser.java
    cocoon/trunk/core/cocoon-configuration/cocoon-spring-configurator/src/main/java/org/apache/cocoon/spring/configurator/impl/ChildSettingsElementParser.java
    cocoon/trunk/core/cocoon-configuration/cocoon-spring-configurator/src/main/java/org/apache/cocoon/spring/configurator/impl/SettingsElementParser.java

Modified: cocoon/trunk/core/cocoon-configuration/cocoon-spring-configurator/src/main/java/org/apache/cocoon/spring/configurator/impl/AbstractElementParser.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/core/cocoon-configuration/cocoon-spring-configurator/src/main/java/org/apache/cocoon/spring/configurator/impl/AbstractElementParser.java?view=diff&rev=491430&r1=491429&r2=491430
==============================================================================
--- cocoon/trunk/core/cocoon-configuration/cocoon-spring-configurator/src/main/java/org/apache/cocoon/spring/configurator/impl/AbstractElementParser.java
(original)
+++ cocoon/trunk/core/cocoon-configuration/cocoon-spring-configurator/src/main/java/org/apache/cocoon/spring/configurator/impl/AbstractElementParser.java
Sun Dec 31 07:20:00 2006
@@ -18,13 +18,9 @@
  */
 package org.apache.cocoon.spring.configurator.impl;
 
-import java.io.IOException;
 import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.List;
 
 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.factory.config.BeanDefinition;
@@ -34,10 +30,6 @@
 import org.springframework.beans.factory.support.BeanDefinitionRegistry;
 import org.springframework.beans.factory.support.RootBeanDefinition;
 import org.springframework.beans.factory.xml.BeanDefinitionParser;
-import org.springframework.beans.factory.xml.ParserContext;
-import org.springframework.core.io.Resource;
-import org.springframework.core.io.ResourceLoader;
-import org.springframework.web.context.support.ServletContextResourcePatternResolver;
 import org.w3c.dom.Element;
 import org.w3c.dom.Node;
 import org.w3c.dom.NodeList;
@@ -216,61 +208,5 @@
         final RootBeanDefinition beanDef = this.createBeanDefinition(componentClass, initMethod,
requiresSettings);
 
         this.register(beanDef, beanName, registry);
-    }
-
-    /**
-     * Handle include for spring bean configurations.
-     */
-    protected void handleBeanInclude(final ParserContext parserContext,
-                                     final String        path,
-                                     final boolean       optional)
-    throws Exception {
-        final ResourceLoader resourceLoader = parserContext.getReaderContext().getReader().getResourceLoader();
-        ServletContextResourcePatternResolver resolver = new ServletContextResourcePatternResolver(resourceLoader);
-
-        // check if the directory to read from exists
-        // we only check if optional is set to true
-        boolean load = true;
-        if ( optional
-             && !ResourceUtils.isClasspathUri(path) ) {
-            final Resource rsrc = resolver.getResource(path);
-            if ( !rsrc.exists()) {
-                load = false;
-            }
-        }
-        if ( load ) {
-            try {
-                Resource[] resources = resolver.getResources(path + "/*.xml");
-                Arrays.sort(resources, ResourceUtils.getResourceComparator());
-                for (int i = 0; i < resources.length; i++) {
-                    this.handleImport(parserContext, resources[i].getURL().toExternalForm());
-                }
-            } catch (IOException ioe) {
-                throw new Exception("Unable to read configurations from " + path, ioe);
-            }
-        }
-    }
-
-    protected void handleImport(ParserContext parserContext, String uri) {
-        final ResourceLoader resourceLoader = parserContext.getReaderContext().getReader().getResourceLoader();
-        parserContext.getDelegate().getReaderContext().getReader().loadBeanDefinitions(resourceLoader.getResource(uri));
-    }
-
-    /**
-     * Register a property placeholder configurer. The configurer will read all
-     * *.properties files from the specified locations.
-     * 
-     * @param parserContext
-     * @param locations
-     */
-    protected void registerPropertyOverrideConfigurer(final ParserContext parserContext,
-                                                      final List          locations) {
-        final RootBeanDefinition beanDef = this.createBeanDefinition(ExtendedPropertyOverrideConfigurer.class.getName(),
-                null, true);
-        beanDef.getPropertyValues().addPropertyValue("locations", locations);
-        beanDef.getPropertyValues().addPropertyValue("resourceLoader",
-                parserContext.getReaderContext().getReader().getResourceLoader());
-        beanDef.getPropertyValues().addPropertyValue("beanNameSeparator", "/");
-        this.register(beanDef, ExtendedPropertyOverrideConfigurer.class.getName(), parserContext.getRegistry());
     }
 }

Modified: cocoon/trunk/core/cocoon-configuration/cocoon-spring-configurator/src/main/java/org/apache/cocoon/spring/configurator/impl/AbstractSettingsElementParser.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/core/cocoon-configuration/cocoon-spring-configurator/src/main/java/org/apache/cocoon/spring/configurator/impl/AbstractSettingsElementParser.java?view=diff&rev=491430&r1=491429&r2=491430
==============================================================================
--- cocoon/trunk/core/cocoon-configuration/cocoon-spring-configurator/src/main/java/org/apache/cocoon/spring/configurator/impl/AbstractSettingsElementParser.java
(original)
+++ cocoon/trunk/core/cocoon-configuration/cocoon-spring-configurator/src/main/java/org/apache/cocoon/spring/configurator/impl/AbstractSettingsElementParser.java
Sun Dec 31 07:20:00 2006
@@ -18,10 +18,21 @@
  */
 package org.apache.cocoon.spring.configurator.impl;
 
+import java.io.IOException;
 import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Iterator;
 import java.util.List;
 import java.util.Properties;
 
+import org.apache.cocoon.spring.configurator.ResourceUtils;
+import org.springframework.beans.factory.BeanDefinitionStoreException;
+import org.springframework.beans.factory.config.BeanDefinition;
+import org.springframework.beans.factory.support.RootBeanDefinition;
+import org.springframework.beans.factory.xml.ParserContext;
+import org.springframework.core.io.Resource;
+import org.springframework.core.io.ResourceLoader;
+import org.springframework.web.context.support.ServletContextResourcePatternResolver;
 import org.w3c.dom.Element;
 
 /**
@@ -35,6 +46,18 @@
 public abstract class AbstractSettingsElementParser extends AbstractElementParser {
 
     /**
+     * Get the current running mode
+     */
+    protected abstract String getRunningMode(Element e);
+
+    /**
+     * Create and register the settings bean factory post processor.
+     */
+    protected abstract void createSettingsBeanFactoryPostProcessor(Element       element,
+                                                                   ParserContext parserContext,
+                                                                   String        runningMode);
+
+    /**
      * Get additonal includes of property directories.
      */
     protected List getPropertyIncludes(Element childSettingsElement) {
@@ -84,5 +107,113 @@
             }
         }
         return includes;
+    }
+
+    /**
+     * Return the includes for the property override configuration
+     */
+    protected List getBeanPropertyOverrideIncludes(Element settingsElement) {
+        return this.getBeanIncludes(settingsElement);
+    }
+
+    /**
+     * @see org.springframework.beans.factory.xml.BeanDefinitionParser#parse(org.w3c.dom.Element,
org.springframework.beans.factory.xml.ParserContext)
+     */
+    public BeanDefinition parse(Element element, ParserContext parserContext) {
+        final String runningMode = this.getRunningMode(element);
+
+        // create factory for settings object
+        this.createSettingsBeanFactoryPostProcessor(element, parserContext, runningMode);
+
+        // Get bean includes for property overrides
+        final List overridePropertyIncludes = this.getBeanPropertyOverrideIncludes(element);
+
+        // If there are bean includes for a directory, we register a property placeholder
configurer
+        if ( overridePropertyIncludes.size() > 0 ) {
+            this.registerPropertyOverrideConfigurer(parserContext, overridePropertyIncludes);

+        }
+
+        // register additonal components
+        this.registerComponents(parserContext);
+
+        // Get bean includes
+        final List beanIncludes = this.getBeanIncludes(element);
+        // process bean includes!
+        final Iterator beanIncludeIterator = beanIncludes.iterator();
+        while ( beanIncludeIterator.hasNext() ) {
+            final String dir = (String)beanIncludeIterator.next();
+
+            try {
+                this.handleBeanInclude(parserContext, dir, false);
+                this.handleBeanInclude(parserContext, dir + "/" + runningMode, true);
+            } catch (Exception e) {
+                throw new BeanDefinitionStoreException("Unable to read spring configurations
from " + dir, e);
+            }
+        }
+
+        return null;
+    }
+
+    /**
+     * This method can be used for subclasses to register additional components.
+     */
+    protected void registerComponents(ParserContext parserContext) {
+        // nothing to do here
+    }
+
+    /**
+     * Handle include for spring bean configurations.
+     */
+    protected void handleBeanInclude(final ParserContext parserContext,
+                                     final String        path,
+                                     final boolean       optional)
+    throws Exception {
+        final ResourceLoader resourceLoader = parserContext.getReaderContext().getReader().getResourceLoader();
+        ServletContextResourcePatternResolver resolver = new ServletContextResourcePatternResolver(resourceLoader);
+
+        // check if the directory to read from exists
+        // we only check if optional is set to true
+        boolean load = true;
+        if ( optional
+             && !ResourceUtils.isClasspathUri(path) ) {
+            final Resource rsrc = resolver.getResource(path);
+            if ( !rsrc.exists()) {
+                load = false;
+            }
+        }
+        if ( load ) {
+            try {
+                Resource[] resources = resolver.getResources(path + "/*.xml");
+                Arrays.sort(resources, ResourceUtils.getResourceComparator());
+                for (int i = 0; i < resources.length; i++) {
+                    this.handleImport(parserContext, resources[i].getURL().toExternalForm());
+                }
+            } catch (IOException ioe) {
+                throw new Exception("Unable to read configurations from " + path, ioe);
+            }
+        }
+    }
+
+    protected void handleImport(ParserContext parserContext, String uri) {
+        final ResourceLoader resourceLoader = parserContext.getReaderContext().getReader().getResourceLoader();
+        parserContext.getDelegate().getReaderContext().getReader().loadBeanDefinitions(resourceLoader.getResource(uri));
+    }
+
+    /**
+     * Register a property placeholder configurer. The configurer will read all
+     * *.properties files from the specified locations.
+     * 
+     * @param parserContext
+     * @param locations
+     */
+    protected void registerPropertyOverrideConfigurer(final ParserContext parserContext,
+                                                      final List          locations) {
+        final RootBeanDefinition beanDef = this.createBeanDefinition(ExtendedPropertyOverrideConfigurer.class.getName(),
+                null, true);
+        beanDef.getPropertyValues().addPropertyValue("locations", locations);
+        beanDef.getPropertyValues().addPropertyValue("resourceLoader",
+                parserContext.getReaderContext().getReader().getResourceLoader());
+        beanDef.getPropertyValues().addPropertyValue("beanNameSeparator", "/");
+        this.register(beanDef, ExtendedPropertyOverrideConfigurer.class.getName(), parserContext.getRegistry());
     }
 }

Modified: cocoon/trunk/core/cocoon-configuration/cocoon-spring-configurator/src/main/java/org/apache/cocoon/spring/configurator/impl/ChildSettingsElementParser.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/core/cocoon-configuration/cocoon-spring-configurator/src/main/java/org/apache/cocoon/spring/configurator/impl/ChildSettingsElementParser.java?view=diff&rev=491430&r1=491429&r2=491430
==============================================================================
--- cocoon/trunk/core/cocoon-configuration/cocoon-spring-configurator/src/main/java/org/apache/cocoon/spring/configurator/impl/ChildSettingsElementParser.java
(original)
+++ cocoon/trunk/core/cocoon-configuration/cocoon-spring-configurator/src/main/java/org/apache/cocoon/spring/configurator/impl/ChildSettingsElementParser.java
Sun Dec 31 07:20:00 2006
@@ -18,14 +18,11 @@
  */
 package org.apache.cocoon.spring.configurator.impl;
 
-import java.util.Iterator;
 import java.util.List;
 import java.util.Properties;
 
 import org.apache.cocoon.configuration.Settings;
 import org.apache.cocoon.spring.configurator.WebAppContextUtils;
-import org.springframework.beans.factory.BeanDefinitionStoreException;
-import org.springframework.beans.factory.config.BeanDefinition;
 import org.springframework.beans.factory.support.RootBeanDefinition;
 import org.springframework.beans.factory.xml.ParserContext;
 import org.springframework.web.context.WebApplicationContext;
@@ -35,7 +32,7 @@
  * Add a bean definition for the settings object of a child context to the bean factory
  * and process all includes of spring configurations.
  *
- * @see SitemapNamespaceHandler
+ * @see ConfiguratorNamespaceHandler
  * @see ChildSettingsBeanFactoryPostProcessor
  * @version $Id$
  * @since 1.0
@@ -43,22 +40,21 @@
 public class ChildSettingsElementParser extends AbstractSettingsElementParser {
 
     /**
-     * @see org.springframework.beans.factory.xml.BeanDefinitionParser#parse(org.w3c.dom.Element,
org.springframework.beans.factory.xml.ParserContext)
+     * @see org.apache.cocoon.spring.configurator.impl.AbstractSettingsElementParser#getRunningMode(org.w3c.dom.Element)
      */
-    public BeanDefinition parse(Element element, ParserContext parserContext) {
+    protected String getRunningMode(Element e) {
         // get root application context
         final WebApplicationContext rootAppContext = WebAppContextUtils.getCurrentWebApplicationContext();
         // get running mode from root settings
-        final String runningMode = ((Settings)rootAppContext.getBean(Settings.ROLE)).getRunningMode();
-
-        // Get bean includes
-        final List beanIncludes = this.getBeanIncludes(element);
-
-        // If there are bean includes for a directory, we register a property placeholder
configurer
-        if ( beanIncludes.size() > 0 ) {
-            this.registerPropertyOverrideConfigurer(parserContext, beanIncludes); 
-        }
+        return ((Settings)rootAppContext.getBean(Settings.ROLE)).getRunningMode();
+    }
 
+    /**
+     * Create and register the settings bean factory post processor.
+     */
+    protected void createSettingsBeanFactoryPostProcessor(Element       element,
+                                                          ParserContext parserContext,
+                                                          String        runningMode) {
         // Create definition for child settings
         RootBeanDefinition def =  this.createBeanDefinition(ChildSettingsBeanFactoryPostProcessor.class.getName(),
                 "init",
@@ -75,21 +71,7 @@
             def.getPropertyValues().addPropertyValue("directories", propertiesIncludes);
         }
 
-        // process bean includes!
-        final Iterator beanIncludeIterator = beanIncludes.iterator();
-        while ( beanIncludeIterator.hasNext() ) {
-            final String dir = (String)beanIncludeIterator.next();
-
-            try {
-                this.handleBeanInclude(parserContext, dir, false);
-                this.handleBeanInclude(parserContext, dir + "/" + runningMode, true);
-            } catch (Exception e) {
-                throw new BeanDefinitionStoreException("Unable to read spring configurations
from " + dir, e);
-            }
-        }
-
         // and now we register the child settings
         this.register(def, Settings.ROLE, parserContext.getRegistry());
-        return null;
     }
 }

Modified: cocoon/trunk/core/cocoon-configuration/cocoon-spring-configurator/src/main/java/org/apache/cocoon/spring/configurator/impl/SettingsElementParser.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/core/cocoon-configuration/cocoon-spring-configurator/src/main/java/org/apache/cocoon/spring/configurator/impl/SettingsElementParser.java?view=diff&rev=491430&r1=491429&r2=491430
==============================================================================
--- cocoon/trunk/core/cocoon-configuration/cocoon-spring-configurator/src/main/java/org/apache/cocoon/spring/configurator/impl/SettingsElementParser.java
(original)
+++ cocoon/trunk/core/cocoon-configuration/cocoon-spring-configurator/src/main/java/org/apache/cocoon/spring/configurator/impl/SettingsElementParser.java
Sun Dec 31 07:20:00 2006
@@ -18,8 +18,6 @@
  */
 package org.apache.cocoon.spring.configurator.impl;
 
-import java.util.ArrayList;
-import java.util.Iterator;
 import java.util.List;
 import java.util.Properties;
 
@@ -27,8 +25,6 @@
 
 import org.apache.cocoon.configuration.Settings;
 import org.apache.cocoon.spring.configurator.BlockResourcesHolder;
-import org.springframework.beans.factory.BeanDefinitionStoreException;
-import org.springframework.beans.factory.config.BeanDefinition;
 import org.springframework.beans.factory.support.RootBeanDefinition;
 import org.springframework.beans.factory.xml.ParserContext;
 import org.w3c.dom.Element;
@@ -36,7 +32,7 @@
 /**
  * Add a bean definition for the settings object to the bean factory.
  * 
- * @see SitemapNamespaceHandler
+ * @see ConfiguratorNamespaceHandler
  * @see SettingsBeanFactoryPostProcessor
  * @version $Id$
  * @since 1.0
@@ -89,36 +85,43 @@
     }
 
     /**
-     * @see org.springframework.beans.factory.xml.BeanDefinitionParser#parse(org.w3c.dom.Element,
-     *      org.springframework.beans.factory.xml.ParserContext)
+     * @see org.apache.cocoon.spring.configurator.impl.AbstractSettingsElementParser#getRunningMode(org.w3c.dom.Element)
      */
-    public BeanDefinition parse(Element element, ParserContext parserContext) {
-        final String runningMode = RunningModeHelper.determineRunningMode( this.getAttributeValue(element,
RUNNING_MODE_ATTR, null) );
-        this.createSettingsBeanFactoryPostProcessor(element, parserContext, runningMode);
-
-        // Get bean includes
-        final List beanIncludes = this.getBeanIncludes(element);
-
-        // register a PropertyPlaceholderConfigurer
-        // we create a list with the default locations and add the optional location attribute
-        final List dirs = new ArrayList();
-        // check for boolean settings
-        final boolean readFromClasspath = Boolean.valueOf(this.getAttributeValue(element,
READ_FROM_CLASSPATH_ATTR, "true")).booleanValue();
-        final boolean readFromGlobalLocation = Boolean.valueOf(this.getAttributeValue(element,
READ_FROM_GLOBAL_LOCATION_ATTR, "true")).booleanValue();
+    protected String getRunningMode(Element e) {
+        return RunningModeHelper.determineRunningMode( this.getAttributeValue(e, RUNNING_MODE_ATTR,
null) );        
+    }
+
+    /**
+     * @see org.apache.cocoon.spring.configurator.impl.AbstractSettingsElementParser#getBeanIncludes(org.w3c.dom.Element)
+     */
+    protected List getBeanIncludes(Element settingsElement) {
+        final List includes = super.getBeanIncludes(settingsElement);
+        final boolean readFromClasspath = Boolean.valueOf(this.getAttributeValue(settingsElement,
READ_FROM_CLASSPATH_ATTR, "true")).booleanValue();
         if ( readFromClasspath ) {
-            dirs.add(Constants.CLASSPATH_SPRING_CONFIGURATION_LOCATION);
+            includes.add(0, Constants.CLASSPATH_SPRING_CONFIGURATION_LOCATION);
         }
-        if ( readFromGlobalLocation ) {
-            dirs.add(Constants.GLOBAL_SPRING_CONFIGURATION_LOCATION);
-        }
-        // If there are bean includes for a directory, we register them as well
-        if ( beanIncludes.size() > 0 ) {
-            dirs.addAll(beanIncludes);
-        }
-        if ( dirs.size() > 0 ) {
-            this.registerPropertyOverrideConfigurer(parserContext, dirs);
+        return includes;
+    }
+
+    /**
+     * @see org.apache.cocoon.spring.configurator.impl.AbstractSettingsElementParser#getBeanPropertyOverrideIncludes(org.w3c.dom.Element)
+     */
+    protected List getBeanPropertyOverrideIncludes(Element settingsElement) {
+        final List includes = super.getBeanPropertyOverrideIncludes(settingsElement);
+        final boolean readFromClasspath = Boolean.valueOf(this.getAttributeValue(settingsElement,
READ_FROM_CLASSPATH_ATTR, "true")).booleanValue();
+        final boolean readFromGlobalLocation = Boolean.valueOf(this.getAttributeValue(settingsElement,
READ_FROM_GLOBAL_LOCATION_ATTR, "true")).booleanValue();
+        if (readFromGlobalLocation) {
+            int pos = (readFromClasspath ? 1 : 0);
+            includes.add(pos, Constants.GLOBAL_SPRING_CONFIGURATION_LOCATION);
         }
+        return includes;
+    }
 
+    /**
+     * @see org.apache.cocoon.spring.configurator.impl.AbstractSettingsElementParser#registerComponents(org.springframework.beans.factory.xml.ParserContext)
+     */
+    protected void registerComponents(ParserContext parserContext) {
+        super.registerComponents(parserContext);
         // add the servlet context as a bean
         this.addComponent(ServletContextFactoryBean.class.getName(),
                           ServletContext.class.getName(),
@@ -128,25 +131,5 @@
         this.addComponent(DefaultBlockResourcesHolder.class.getName(), 
                           BlockResourcesHolder.class.getName(),
                           "init", true, parserContext.getRegistry());
-
-        // handle includes - add default location
-        if ( readFromClasspath ) {
-            beanIncludes.add(0, Constants.CLASSPATH_SPRING_CONFIGURATION_LOCATION);
-        }
-
-        // process bean includes!
-        final Iterator beanIncludeIterator = beanIncludes.iterator();
-        while ( beanIncludeIterator.hasNext() ) {
-            final String dir = (String)beanIncludeIterator.next();
-
-            try {
-                this.handleBeanInclude(parserContext, dir, false);
-                this.handleBeanInclude(parserContext, dir + "/" + runningMode, true);
-            } catch (Exception e) {
-                throw new BeanDefinitionStoreException("Unable to read spring configurations
from " + dir, e);
-            }
-        }
-
-        return null;
     }
 }



Mime
View raw message