cocoon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cziege...@apache.org
Subject svn commit: r453198 - in /cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/container/spring: AbstractElementParser.java CocoonPropertyOverrideConfigurer.java Constants.java SettingsElementParser.java SitemapElementParser.java
Date Thu, 05 Oct 2006 12:15:14 GMT
Author: cziegeler
Date: Thu Oct  5 05:15:13 2006
New Revision: 453198

URL: http://svn.apache.org/viewvc?view=rev&rev=453198
Log:
Read properties from the spring directory

Added:
    cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/container/spring/Constants.java
  (with props)
Modified:
    cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/container/spring/AbstractElementParser.java
    cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/container/spring/CocoonPropertyOverrideConfigurer.java
    cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/container/spring/SettingsElementParser.java
    cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/container/spring/SitemapElementParser.java

Modified: cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/container/spring/AbstractElementParser.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/container/spring/AbstractElementParser.java?view=diff&rev=453198&r1=453197&r2=453198
==============================================================================
--- cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/container/spring/AbstractElementParser.java
(original)
+++ cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/container/spring/AbstractElementParser.java
Thu Oct  5 05:15:13 2006
@@ -188,10 +188,10 @@
      * @throws ConfigurationException
      */
     protected void handleBeanInclude(ParserContext parserContext,
-                                     String         src,
-                                     String         dir,
-                                     String         pattern,
-                                     boolean optional)
+                                     String        src,
+                                     String        dir,
+                                     String        pattern,
+                                     boolean       optional)
     throws Exception {
         final ResourceLoader resourceLoader = parserContext.getReaderContext().getReader().getResourceLoader();
         PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(resourceLoader);
@@ -242,4 +242,20 @@
         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 location.
+     * @param parserContext
+     * @param springConfigLocation
+     */
+    protected void registerPropertyPlaceholderConfigurer(final ParserContext parserContext,
final String location) {
+        final RootBeanDefinition beanDef = this.createBeanDefinition(CocoonPropertyOverrideConfigurer.class.getName(),
+                null,
+                false);
+        beanDef.getPropertyValues().addPropertyValue("location", location);
+        beanDef.getPropertyValues().addPropertyValue("resourceLoader",  parserContext.getReaderContext().getReader().getResourceLoader());
+        this.register(beanDef, CocoonPropertyOverrideConfigurer.class.getName(), parserContext.getRegistry());
+    }
+
 }

Modified: cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/container/spring/CocoonPropertyOverrideConfigurer.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/container/spring/CocoonPropertyOverrideConfigurer.java?view=diff&rev=453198&r1=453197&r2=453198
==============================================================================
--- cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/container/spring/CocoonPropertyOverrideConfigurer.java
(original)
+++ cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/container/spring/CocoonPropertyOverrideConfigurer.java
Thu Oct  5 05:15:13 2006
@@ -18,36 +18,69 @@
  */
 package org.apache.cocoon.core.container.spring;
 
+import java.io.IOException;
+import java.util.Arrays;
 import java.util.Properties;
 
-import org.apache.cocoon.configuration.Settings;
 import org.springframework.beans.BeansException;
+import org.springframework.beans.factory.BeanDefinitionStoreException;
 import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
 import org.springframework.beans.factory.config.PropertyOverrideConfigurer;
+import org.springframework.core.io.DefaultResourceLoader;
+import org.springframework.core.io.Resource;
+import org.springframework.core.io.ResourceLoader;
+import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
 
 /**
  * Our version of the property override configurer which uses the settings
  * object to get the properties.
+ *
  * @version $Id$
  */
 public class CocoonPropertyOverrideConfigurer extends PropertyOverrideConfigurer {
 
-    protected Settings settings;
+    protected String location = Constants.DEFAULT_SPRING_CONFIGURATION_LOCATION;
+    protected ResourceLoader resourceLoader = new DefaultResourceLoader();
 
-    public void setSettings(Settings object) {
-        this.settings = object;
+    public void setLocation(final String object) {
+        this.location = object;
+    }
+
+    public void setResourceLoader(final ResourceLoader loader) {
+        this.resourceLoader = loader;
     }
 
     /**
      * @see org.springframework.beans.factory.config.PropertyResourceConfigurer#postProcessBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory)
      */
-    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws
BeansException {
-        Properties mergedProps = new SettingsProperties(this.settings);
-
-        // Convert the merged properties, if necessary.
-        convertProperties(mergedProps);
-
-        // Let the subclass process the properties.
-        processProperties(beanFactory, mergedProps);
+    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)
+    throws BeansException {
+        final Properties mergedProps = new Properties();
+        final PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(resourceLoader);
+        final Resource dirResource = resourceLoader.getResource(this.location);
+
+        if ( dirResource.exists() ) {
+            try {
+                Resource[] resources = resolver.getResources(this.location + "/*.properties");
+                if ( resources != null ) {
+                    Arrays.sort(resources, AbstractSettingsBeanFactoryPostProcessor.getResourceComparator());
+                    for(int i=0; i < resources.length; i++) {
+                        final Properties p = new Properties();
+                        p.load(resources[i].getInputStream());
+                        mergedProps.putAll(p);
+                    }
+                }
+            } catch (IOException ioe) {
+                throw new BeanDefinitionStoreException("Unable to read property configurations
from " + this.location, ioe);
+            }
+        }
+
+        if ( mergedProps.size() > 0 ) {
+            // Convert the merged properties, if necessary.
+            convertProperties(mergedProps);
+    
+            // Let the subclass process the properties.
+            processProperties(beanFactory, mergedProps);
+        }
     }
 }

Added: cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/container/spring/Constants.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/container/spring/Constants.java?view=auto&rev=453198
==============================================================================
--- cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/container/spring/Constants.java
(added)
+++ cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/container/spring/Constants.java
Thu Oct  5 05:15:13 2006
@@ -0,0 +1,32 @@
+/* 
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed  under the  License is distributed on an "AS IS" BASIS,
+ * WITHOUT  WARRANTIES OR CONDITIONS  OF ANY KIND, either  express  or
+ * implied.
+ *
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.cocoon.core.container.spring;
+
+/**
+ * Some constants for the spring integration.
+ * @version $Id$
+ */
+public class Constants {
+
+    /** The default location of spring related configuration files. */
+    public static final String DEFAULT_SPRING_CONFIGURATION_LOCATION = "/WEB-INF/cocoon/spring";
+
+    /** The default location of spring related per sitemap configuration files. */
+    public static final String DEFAULT_SPRING_SITEMAP_CONFIGURATION_FILES = "config/spring";
+}

Propchange: cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/container/spring/Constants.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/container/spring/Constants.java
------------------------------------------------------------------------------
    svn:keywords = Id

Modified: cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/container/spring/SettingsElementParser.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/container/spring/SettingsElementParser.java?view=diff&rev=453198&r1=453197&r2=453198
==============================================================================
--- cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/container/spring/SettingsElementParser.java
(original)
+++ cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/container/spring/SettingsElementParser.java
Thu Oct  5 05:15:13 2006
@@ -47,11 +47,8 @@
      * @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) {
-        try {
-            this.handleBeanInclude(parserContext, null, "/WEB-INF/cocoon/spring", "*.xml",
true);
-        } catch (Exception e) {
-            throw new BeanDefinitionStoreException("Unable to read spring configurations.",e);
-        }
+        final String springConfigLocation = this.getAttributeValue(element, "location", Constants.DEFAULT_SPRING_CONFIGURATION_LOCATION);
+
         // create bean definition for settings object
         final String componentClassName = this.getAttributeValue(element, PROCESSOR_CLASS_NAME_ATTR,
SettingsBeanFactoryPostProcessor.class.getName());
         final RootBeanDefinition beanDef = this.createBeanDefinition(componentClassName,
"init", false);
@@ -64,10 +61,17 @@
         this.register(beanDef, Settings.ROLE, parserContext.getRegistry());
 
         // register a PropertyPlaceholderConfigurer
-        this.addComponent(CocoonPropertyOverrideConfigurer.class.getName(), CocoonPropertyOverrideConfigurer.class.getName(),
null, true, parserContext.getRegistry());
+        this.registerPropertyPlaceholderConfigurer(parserContext, springConfigLocation);
 
         // add the servlet context as a bean
         this.addComponent(ServletContextFactoryBean.class.getName(), ServletContext.class.getName(),
null, false, parserContext.getRegistry());
+
+        // handle includes
+        try {
+            this.handleBeanInclude(parserContext, null, springConfigLocation, "*.xml", true);
+        } catch (Exception e) {
+            throw new BeanDefinitionStoreException("Unable to read spring configurations
from " + springConfigLocation, e);
+        }
         return null;
     }
 }

Modified: cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/container/spring/SitemapElementParser.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/container/spring/SitemapElementParser.java?view=diff&rev=453198&r1=453197&r2=453198
==============================================================================
--- cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/container/spring/SitemapElementParser.java
(original)
+++ cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/container/spring/SitemapElementParser.java
Thu Oct  5 05:15:13 2006
@@ -46,8 +46,6 @@
  */
 public class SitemapElementParser extends AbstractElementParser {
 
-    private static final String DEFAULT_CONFIG_SPRING = "config/spring";
-
     protected Element readSitemap(String location, ResourceLoader resourceLoader)
     throws Exception {
         // read the sitemap
@@ -122,14 +120,20 @@
     public BeanDefinition parse(Element element, ParserContext parserContext) {
         final String location = element.getAttribute("location");
         final ResourceLoader resourceLoader = parserContext.getReaderContext().getReader().getResourceLoader();
-        RootBeanDefinition def =  this.createBeanDefinition(SubSettingsBeanFactoryPostProcessor.class.getName(),
-                "init",
-                false);
-        def.getPropertyValues().addPropertyValue("location", location);
         try {
             final Element rootElement = this.readSitemap(location, resourceLoader);
             final Element componentsElement = this.getComponentsElement(rootElement);
             final boolean useDefaultIncludes = this.isUseDefaultIncludes(componentsElement);
+
+            // register a PropertyPlaceholderConfigurer
+            if ( useDefaultIncludes ) {
+                this.registerPropertyPlaceholderConfigurer(parserContext, Constants.DEFAULT_SPRING_SITEMAP_CONFIGURATION_FILES);
+            }
+            
+            RootBeanDefinition def =  this.createBeanDefinition(SubSettingsBeanFactoryPostProcessor.class.getName(),
+                    "init",
+                    false);
+            def.getPropertyValues().addPropertyValue("location", location);
             def.getPropertyValues().addPropertyValue("useDefaultIncludes", Boolean.valueOf(useDefaultIncludes));
 
             final Properties globalSitemapVariables = this.getGlobalSitemapVariables(rootElement);
@@ -143,7 +147,7 @@
             }
 
             if ( useDefaultIncludes ) {
-                this.handleBeanInclude(parserContext, null, DEFAULT_CONFIG_SPRING, "*.xml",
true);
+                this.handleBeanInclude(parserContext, null, Constants.DEFAULT_SPRING_SITEMAP_CONFIGURATION_FILES,
"*.xml", true);
             }
             // search for includes
             if ( componentsElement != null ) {
@@ -158,10 +162,10 @@
                     }
                 }
             }
+            this.register(def, Settings.ROLE, parserContext.getRegistry());
         } catch (Exception e) {
             throw new BeanDefinitionStoreException("Unable to process sitemap at '" + location
+ "'.",e);
         }
-        this.register(def, Settings.ROLE, parserContext.getRegistry());
         return null;
     }
 }



Mime
View raw message