cocoon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cziege...@apache.org
Subject svn commit: r424941 - in /cocoon/trunk/core/cocoon-core/src: main/java/org/apache/cocoon/components/treeprocessor/sitemap/ main/java/org/apache/cocoon/configuration/impl/ main/java/org/apache/cocoon/core/osgi/ test/java/org/apache/cocoon/core/container/
Date Mon, 24 Jul 2006 07:15:42 GMT
Author: cziegeler
Date: Mon Jul 24 00:15:41 2006
New Revision: 424941

URL: http://svn.apache.org/viewvc?rev=424941&view=rev
Log:
Refactoring property handling

Modified:
    cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/components/treeprocessor/sitemap/SitemapLanguage.java
    cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/configuration/impl/MutableSettings.java
    cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/configuration/impl/SettingsHelper.java
    cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/osgi/OSGiSettings.java
    cocoon/trunk/core/cocoon-core/src/test/java/org/apache/cocoon/core/container/ContainerTestCase.java

Modified: cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/components/treeprocessor/sitemap/SitemapLanguage.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/components/treeprocessor/sitemap/SitemapLanguage.java?rev=424941&r1=424940&r2=424941&view=diff
==============================================================================
--- cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/components/treeprocessor/sitemap/SitemapLanguage.java
(original)
+++ cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/components/treeprocessor/sitemap/SitemapLanguage.java
Mon Jul 24 00:15:41 2006
@@ -397,37 +397,35 @@
         final BeanFactoryFactoryImpl factory = new BeanFactoryFactoryImpl();
         factory.setBeanFactory(this.beanFactory);
 
-        // check for sitemap local properties
-        Settings settings = (Settings)factory.getCurrentBeanFactory(itsContext).getBean(Settings.ROLE);
-        if ( componentConfig != null && componentConfig.getAttribute("property-dir",
null) != null ) {
-            final String propertyDir = componentConfig.getAttribute("property-dir");
-            settings = this.createSettings(settings, propertyDir, useDefaultIncludes, factory.getCurrentBeanFactory(itsContext));
-        }
         // compatibility with 2.1.x - check for global variables in sitemap
         // TODO - This will be removed in later versions!
+        Properties globalSitemapVariables = null;
         if ( tree.getChild("pipelines").getChild("component-configurations", false) != null
) {
             Deprecation.logger.warn("The 'component-configurations' section in the sitemap
is deprecated. Please check for alternatives.");
+            globalSitemapVariables = new Properties();
             // now check for global variables - if any other element occurs: throw exception
             Configuration[] children = tree.getChild("pipelines").getChild("component-configurations").getChildren();
             for(int i=0; i<children.length; i++) {
                 if ( "global-variables".equals(children[i].getName()) ) {
-                    final Properties p = new Properties();
-                    final MutableSettings mutableSettings;
-                    if ( settings instanceof MutableSettings ) {
-                        mutableSettings = (MutableSettings)settings;
-                    } else {
-                        mutableSettings = new MutableSettings(settings);
-                    }
                     Configuration[] variables = children[i].getChildren();
                     for(int v=0; v<variables.length; v++) {
-                        p.setProperty(variables[v].getName(), variables[v].getValue());
+                        globalSitemapVariables.setProperty(variables[v].getName(), variables[v].getValue());
                     }
-                    mutableSettings.fill(p);
-                } else {
+               } else {
                     throw new ConfigurationException("Component configurations in the sitemap
are not allowed for component: " + children[i].getName());
                 }
             }
         }
+        // check for sitemap local properties
+        Settings settings = (Settings)factory.getCurrentBeanFactory(itsContext).getBean(Settings.ROLE);
+        if ( componentConfig != null && componentConfig.getAttribute("property-dir",
null) != null ) {
+            final String propertyDir = componentConfig.getAttribute("property-dir");
+            settings = this.createSettings(settings, propertyDir, useDefaultIncludes, factory.getCurrentBeanFactory(itsContext),
globalSitemapVariables);
+        } else if ( globalSitemapVariables != null ) {
+            MutableSettings s = new MutableSettings(settings);
+            s.configure(globalSitemapVariables);
+            settings = s;
+        }
         // replace properties?
         if ( componentConfig == null || componentConfig.getAttributeAsBoolean("replace-properties",
true) ) {
             tree = this.replaceProperties(tree, settings);
@@ -498,7 +496,12 @@
 
         // FIXME: Internal configurations doesn't work in a non bean factory
         // environment
-        this.itsBeanFactory = factory.createBeanFactory(this.itsClassLoader, this.getLogger(),
componentConfig, itsContext, this.processor.getSourceResolver(), settings);
+        this.itsBeanFactory = factory.createBeanFactory(this.itsClassLoader,
+                                                        this.getLogger(),
+                                                        componentConfig,
+                                                        itsContext,
+                                                        this.processor.getSourceResolver(),
+                                                        settings);
         this.itsManager = (ServiceManager) this.itsBeanFactory.getBean(ProcessingUtil.SERVICE_MANAGER_ROLE);
         if (componentConfig != null) {
             // only register listeners if a new bean factory is created
@@ -1149,40 +1152,50 @@
      *
      * @return A new Settings object
      */
-    protected MutableSettings createSettings(Settings parent,
-                                             String   directory,
-                                             boolean  useDefaultIncludes,
-                                             BeanFactory parentBeanFactory) {
+    protected MutableSettings createSettings(Settings    parent,
+                                             String      directory,
+                                             boolean     useDefaultIncludes,
+                                             BeanFactory parentBeanFactory,
+                                             Properties  globalSitemapVariables) {
         // 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);
 
         // read properties from default includes
         if ( useDefaultIncludes ) {
-            this.readProperties(SitemapLanguage.DEFAULT_CONFIG_PROPERTIES, s);
+            this.readProperties(SitemapLanguage.DEFAULT_CONFIG_PROPERTIES, s, properties);
             // read all properties from the mode dependent directory
-            this.readProperties(SitemapLanguage.DEFAULT_CONFIG_PROPERTIES + '/' + mode, s);
   
+            this.readProperties(SitemapLanguage.DEFAULT_CONFIG_PROPERTIES + '/' + mode, s,
properties);    
         }
 
         // now read all properties from the properties directory
-        this.readProperties(directory, s);
+        this.readProperties(directory, s, properties);
         // read all properties from the mode dependent directory
-        this.readProperties(directory + '/' + mode, s);
+        this.readProperties(directory + '/' + mode, s, properties);
 
         // Next look for a custom property provider in the parent bean factory
         if (parentBeanFactory != null && parentBeanFactory.containsBean(PropertyProvider.ROLE)
) {
             try {
                 final Environment env = EnvironmentHelper.getCurrentEnvironment();
-                PropertyProvider provider = (PropertyProvider)parentBeanFactory.getBean(PropertyProvider.ROLE);
+                final PropertyProvider provider = (PropertyProvider)parentBeanFactory.getBean(PropertyProvider.ROLE);
                 // TODO - add the name of the sitemap file to the path
-                s.fill(provider.getProperties(s, mode, env.getURIPrefix()));
+                final Properties providedProperties = provider.getProperties(s, mode, env.getURIPrefix());
+                if ( providedProperties != null ) {
+                    properties.putAll(providedProperties);
+                }
             } catch (Exception ignore) {
                 this.getLogger().warn("Unable to get properties from provider.", ignore);
                 this.getLogger().warn("Continuing initialization.");            
             }
         }
+        if ( globalSitemapVariables != null ) {
+            properties.putAll(globalSitemapVariables);
+        }
+        s.configure(properties);
 
         return s;
     }
@@ -1193,8 +1206,9 @@
     /**
      * Read all property files from the given directory and apply them to the settings.
      */
-    protected void readProperties(String          directoryName,
-                                  MutableSettings s) {
+    protected void readProperties(String     directoryName,
+                                  Settings   s,
+                                  Properties properties) {
         final SourceResolver resolver = this.processor.getSourceResolver();
         Source directory = null;
         try {
@@ -1218,10 +1232,8 @@
                     if ( this.getLogger().isDebugEnabled() ) {
                         this.getLogger().debug("Reading settings from '" + src.getURI() +
"'.");
                     }
-                    final Properties p = new Properties();
-                    p.load(propsIS);
+                    properties.load(propsIS);
                     propsIS.close();
-                    s.fill(p);
                 }
             }
         } catch (IOException ignore) {

Modified: cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/configuration/impl/MutableSettings.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/configuration/impl/MutableSettings.java?rev=424941&r1=424940&r2=424941&view=diff
==============================================================================
--- cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/configuration/impl/MutableSettings.java
(original)
+++ cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/configuration/impl/MutableSettings.java
Mon Jul 24 00:15:41 2006
@@ -40,8 +40,8 @@
     /** Prefix for properties. */
     protected static final String KEYPREFIX = "org.apache.cocoon.";
 
-    /** The list of properties used to configure Cocoon. */
-    protected final List properties = new ArrayList();
+    /** The properties used to configure Cocoon. */
+    protected final Properties properties = new Properties();
 
     /**
      * This parameter points to the main configuration file for Cocoon.
@@ -190,10 +190,13 @@
     /** The optional parent settings object. */
     protected Settings parent;
 
+    /** Running mode. */
+    protected final String runningMode;
+
     /**
      * Create a new settings object.
      */
-    public MutableSettings() {
+    public MutableSettings(String mode) {
         // set default values
         this.reloadingEnabled = SettingsDefaults.RELOADING_ENABLED_DEFAULT;
         this.enableUploads = SettingsDefaults.ENABLE_UPLOADS;
@@ -207,16 +210,18 @@
         this.containerEncoding = SettingsDefaults.DEFAULT_CONTAINER_ENCODING;
         this.loggingConfiguration = SettingsDefaults.DEFAULT_LOGGING_CONFIGURATION;
         this.configuration = SettingsDefaults.DEFAULT_CONFIGURATION;
+        this.runningMode = mode;
     }
 
     public MutableSettings(Settings parent) {
         this.parent = parent;
+        this.runningMode = parent.getRunningMode();
     }
 
     /**
      * Fill from a properties object
      */
-    public void fill(Properties props) {
+    public void configure(Properties props) {
         this.checkWriteable();
         if ( props != null ) {
             final Iterator i = props.entrySet().iterator();
@@ -273,7 +278,7 @@
                     }
                 }
             }
-            this.properties.add(props);
+            this.properties.putAll(props);
         }
     }
 
@@ -616,11 +621,7 @@
             }
         }
 
-        // Iterate in reverse order, as most specific property sources are added last
-        for (int i = this.properties.size() - 1; i >= 0 && value == null; i--)
{
-            final Properties p = (Properties)this.properties.get(i);
-            value = p.getProperty(key);
-        }
+        value = this.properties.getProperty(key);
 
         if ( value == null ) {
             if ( this.parent != null ) {
@@ -927,14 +928,11 @@
      */
     public List getPropertyNames(String keyPrefix) {
         final List props = new ArrayList();
-        for(int i=0; i < this.properties.size(); i++) {
-            final Properties p = (Properties)this.properties.get(i);
-            final Iterator kI = p.keySet().iterator();
-            while ( kI.hasNext() ) {
-                final String name = (String)kI.next();
-                if ( name.startsWith(keyPrefix) && !props.contains(name) ) {
-                    props.add(name);
-                }
+        final Iterator kI = this.properties.keySet().iterator();
+        while ( kI.hasNext() ) {
+            final String name = (String)kI.next();
+            if ( name.startsWith(keyPrefix) && !props.contains(name) ) {
+                props.add(name);
             }
         }
         if ( this.parent != null ) {
@@ -955,14 +953,11 @@
      */
     public List getPropertyNames() {
         final List props = new ArrayList();
-        for(int i=0; i < this.properties.size(); i++) {
-            final Properties p = (Properties)this.properties.get(i);
-            final Iterator kI = p.keySet().iterator();
-            while ( kI.hasNext() ) {
-                final String name = (String)kI.next();
-                if (!props.contains(name) ) {
-                    props.add(name);
-                }
+        final Iterator kI = this.properties.keySet().iterator();
+        while ( kI.hasNext() ) {
+            final String name = (String)kI.next();
+            if (!props.contains(name) ) {
+                props.add(name);
             }
         }
         if ( this.parent != null ) {
@@ -982,6 +977,6 @@
      * @see org.apache.cocoon.configuration.Settings#getRunningMode()
      */
     public String getRunningMode() {
-        return null;
+        return this.runningMode;
     }
 }

Modified: cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/configuration/impl/SettingsHelper.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/configuration/impl/SettingsHelper.java?rev=424941&r1=424940&r2=424941&view=diff
==============================================================================
--- cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/configuration/impl/SettingsHelper.java
(original)
+++ cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/configuration/impl/SettingsHelper.java
Mon Jul 24 00:15:41 2006
@@ -88,19 +88,24 @@
         logger.info("Running in mode: " + mode);
 
         // create an empty settings objects
-        final MutableSettings s = new MutableSettings();
+        final MutableSettings s = new MutableSettings(mode);
+        // create an empty properties object
+        final Properties properties = new Properties();
 
         // now read all properties from the properties directory
-        readProperties("context://WEB-INF/cocoon/properties", s, resolver, logger);
+        readProperties("context://WEB-INF/cocoon/properties", s, properties, resolver, logger);
         // read all properties from the mode dependent directory
-        readProperties("context://WEB-INF/cocoon/properties/" + mode, s, resolver, logger);
+        readProperties("context://WEB-INF/cocoon/properties/" + mode, s, properties, resolver,
logger);
 
         // Next look for a custom property provider in the spring root context
         BeanFactory rootContext = BeanFactoryUtil.getWebApplicationContext(servletContext);
         if (rootContext != null && rootContext.containsBean(PropertyProvider.ROLE)
) {
             try {
-                PropertyProvider provider = (PropertyProvider)rootContext.getBean(PropertyProvider.ROLE);
-                s.fill(provider.getProperties(s, mode, null));
+                final PropertyProvider provider = (PropertyProvider)rootContext.getBean(PropertyProvider.ROLE);
+                final Properties providedProperties = provider.getProperties(s, mode, null);
+                if ( providedProperties != null ) {
+                    properties.putAll(properties);
+                }
             } catch (Exception ignore) {
                 logger.info("Unable to get properties from configured property provider -
continuing with initialization.");
                 logger.debug("Unable to get properties from provider.", ignore);
@@ -108,7 +113,10 @@
         }
         // fill from the environment configuration, like web.xml etc.
         if ( externalPropertyProvider != null ) {
-            s.fill(externalPropertyProvider.getProperties(s, mode, null));
+            final Properties providedProperties = externalPropertyProvider.getProperties(s,
mode, null);
+            if ( providedProperties != null ) {
+                properties.putAll(properties);
+            }
         }
 
         // read additional properties file
@@ -120,11 +128,8 @@
             if ( testFile.exists() ) {
                 logger.info("Reading user settings from '" + fileName + "'");
                 try {
-                    final Properties p = new Properties();
-                    FileInputStream fis = new FileInputStream(fileName);
-                    p.load(fis);
-                    fis.close();
-                    s.fill(p);
+                    final FileInputStream fis = new FileInputStream(fileName);
+                    properties.load(fis);
                 } catch (IOException ignore) {
                     logger.info("Unable to read '" + fileName + "' - continuing with initialization.");
                     logger.debug("Unable to read '" + fileName + "'.", ignore);
@@ -137,11 +142,9 @@
         if ( additionalPropertyFile != null ) {
             logger.info("Reading user settings from '" + additionalPropertyFile + "'");
             try {
-                final Properties p = new Properties();
-                FileInputStream fis = new FileInputStream(additionalPropertyFile);
-                p.load(fis);
+                final FileInputStream fis = new FileInputStream(additionalPropertyFile);
+                properties.load(fis);
                 fis.close();
-                s.fill(p);
             } catch (IOException ignore) {
                 logger.info("Unable to read '" + additionalPropertyFile + "' - continuing
with initialization.");
                 logger.debug("Unable to read '" + additionalPropertyFile + "'.", ignore);
@@ -149,10 +152,11 @@
         }
         // now overwrite with system properties
         try {
-            s.fill(System.getProperties());
+            properties.putAll(System.getProperties());
         } catch (SecurityException se) {
             // we ignore this
         }
+        s.configure(properties);
 
         return s;
     }
@@ -161,7 +165,8 @@
      * Read all property files from the given directory and apply them to the settings.
      */
     protected static void readProperties(String          directoryName,
-                                         MutableSettings s,
+                                         Settings        s,
+                                         Properties      properties,
                                          SourceResolver  resolver,
                                          Logger  logger) {
         Source directory = null;
@@ -184,10 +189,8 @@
                     final Source src = (Source)i.next();
                     final InputStream propsIS = src.getInputStream();
                     logger.info("Reading settings from '" + src.getURI() + "'.");
-                    final Properties p = new Properties();
-                    p.load(propsIS);
+                    properties.load(propsIS);
                     propsIS.close();
-                    s.fill(p);
                 }
             }
         } catch (IOException ignore) {

Modified: cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/osgi/OSGiSettings.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/osgi/OSGiSettings.java?rev=424941&r1=424940&r2=424941&view=diff
==============================================================================
--- cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/osgi/OSGiSettings.java
(original)
+++ cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/osgi/OSGiSettings.java
Mon Jul 24 00:15:41 2006
@@ -27,6 +27,10 @@
 	
     private Logger logger;
 
+    public OSGiSettings(String mode) {
+        super(mode);
+    }
+
 	protected void activate(ComponentContext componentContext) {
     	CoreUtil.initSettingsFiles(this, this.logger);
         // componentContext.getBundleContext().getDataFile("cocoon-files");

Modified: cocoon/trunk/core/cocoon-core/src/test/java/org/apache/cocoon/core/container/ContainerTestCase.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/core/cocoon-core/src/test/java/org/apache/cocoon/core/container/ContainerTestCase.java?rev=424941&r1=424940&r2=424941&view=diff
==============================================================================
--- cocoon/trunk/core/cocoon-core/src/test/java/org/apache/cocoon/core/container/ContainerTestCase.java
(original)
+++ cocoon/trunk/core/cocoon-core/src/test/java/org/apache/cocoon/core/container/ContainerTestCase.java
Mon Jul 24 00:15:41 2006
@@ -292,7 +292,7 @@
         final AvalonEnvironment avalonEnv = new AvalonEnvironment();
         avalonEnv.logger = this.logger;
         avalonEnv.context = this.context;
-        avalonEnv.settings = new MutableSettings();
+        avalonEnv.settings = new MutableSettings("test");
 
         // read roles and components
         ConfigurationInfo rolesInfo = ConfigReader.readConfiguration(confRM, confCM, null,
avalonEnv, null);



Mime
View raw message