You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@forrest.apache.org by ni...@apache.org on 2004/04/23 19:54:41 UTC

svn commit: rev 10196 - xml/forrest/branches/copyless/src/java/org/apache/forrest/conf

Author: nicolaken
Date: Fri Apr 23 10:54:40 2004
New Revision: 10196

Modified:
   xml/forrest/branches/copyless/src/java/org/apache/forrest/conf/ForrestConfModule.java
Log:
In Ant, preceding properties take precedence.
Refactor to group in a private method common code.
debug rocket science

Modified: xml/forrest/branches/copyless/src/java/org/apache/forrest/conf/ForrestConfModule.java
==============================================================================
--- xml/forrest/branches/copyless/src/java/org/apache/forrest/conf/ForrestConfModule.java	(original)
+++ xml/forrest/branches/copyless/src/java/org/apache/forrest/conf/ForrestConfModule.java	Fri Apr 23 10:54:40 2004
@@ -31,9 +31,16 @@
 import org.apache.cocoon.components.modules.input.InputModule;
 import org.apache.commons.lang.SystemUtils;
 import org.apache.excalibur.source.Source;
+import org.apache.excalibur.source.SourceNotFoundException;
 import org.apache.excalibur.source.SourceResolver;
 
 /**
+ * @author barozzink 23-apr-2004
+ **/
+/**
+ * @author barozzink 23-apr-2004
+ **/
+/**
  * Input module for accessing the base properties used in Forrest. The main
  * values are the locations of the <b>source </b> directories and of the
  * <b>forrest </b> directories. The values are gotten from the
@@ -65,17 +72,48 @@
                 Initializable, ThreadSafe, Serviceable
 {
 
+    private AntProperties  filteringProperties;
     private String         forrestHome, projectHome;
     private SourceResolver m_resolver;
-    private AntProperties  filteringProperties;
 
-    public void service(ServiceManager manager) throws ServiceException {
-        m_resolver = (SourceResolver) manager.lookup(SourceResolver.ROLE);
+    public Object getAttribute(String name, Configuration modeConf,
+                    Map objectModel) throws ConfigurationException {
+        String original = super.getAttributeValues(name, modeConf, objectModel)[0]
+                        .toString();
+        String attributeValue = this.getAttributeValues(name, modeConf,
+                        objectModel)[0].toString();
+
+        debug(" - Requested:" + name);
+        debug(" - Unfiltered:" + original);
+        debug(" - Given:" + attributeValue);
+
+        return attributeValue;
+    }
+
+    public Object[] getAttributeValues(String name, Configuration modeConf,
+                    Map objectModel) throws ConfigurationException {
+        Object[] attributeValues = super.getAttributeValues(name, modeConf,
+                        objectModel);
+        for (int i = 0; i < attributeValues.length; i++) {
+            attributeValues[i] = filteringProperties.filter(attributeValues[i]
+                            .toString());
+        }
+
+        return attributeValues;
+    }
+
+    private final String getSystemProperty(String propertyName)
+                    throws MalformedURLException, IOException {
+
+        String propertyValue = System.getProperty(propertyName, ".");
+
+        debug("system property " + propertyName + "=" + propertyValue);
+
+        return propertyValue;
     }
 
     public void initialize() throws Exception {
-        // get location of forrest.home
-        //
+
         //NOTE: Don't do this:
         //
         //        forrestHome = System.getenv("FORREST_HOME");
@@ -83,76 +121,58 @@
         //      as it will get FORREST_HOME even when the app
         //      is run as a .war
         forrestHome = getSystemProperty("forrest.home");
-
-        // get location of project.home
         projectHome = getSystemProperty("project.home");
         
+        filteringProperties = new AntProperties();
+        
+        //add forrest.home and project.home to properties
+        filteringProperties.setProperty("forrest.home", forrestHome);
+        filteringProperties.setProperty("project.home", projectHome);
+
+      
+        //NOTE: the first values set get precedence, as in AntProperties
+
+        // get forrest.properties and load the values
+        String forrestPropertiesStringURI = projectHome
+                        + SystemUtils.FILE_SEPARATOR + "forrest.properties";
+
+        filteringProperties = 
+            loadAntPropertiesFromURI(filteringProperties,forrestPropertiesStringURI);
 
+        
         // get default-forrest.properties and load the values
         String defaultRorrestPropertiesStringURI = forrestHome
                         + SystemUtils.FILE_SEPARATOR
                         + "default-forrest.properties";
 
-        if (getLogger().isDebugEnabled()) {
-            getLogger().debug("Searching for default forrest.properties in:"
-                        + defaultRorrestPropertiesStringURI);
-        }
+        filteringProperties = 
+            loadAntPropertiesFromURI(filteringProperties,defaultRorrestPropertiesStringURI);
+
+        debug("Loaded project forrest.properties:" + filteringProperties);
+    }
 
+    /**
+     * @param antPropertiesStringURI
+     * @throws MalformedURLException
+     * @throws IOException
+     * @throws SourceNotFoundException
+     */
+    private AntProperties loadAntPropertiesFromURI(AntProperties precedingProperties, String antPropertiesStringURI) throws MalformedURLException, IOException, SourceNotFoundException {
+        
         Source source = null;
         InputStream in = null;
         try {
-            source = m_resolver.resolveURI(defaultRorrestPropertiesStringURI);
+            source = m_resolver.resolveURI(antPropertiesStringURI);
 
-            if (getLogger().isDebugEnabled()) {
-                getLogger().debug(
-                                "Searching for default forrest.properties in [RESOLVED]:"
+            debug("Searching for forrest.properties in"
                                                 + source.getURI());
-            }
-
             in = source.getInputStream();
-            filteringProperties = new AntProperties();
+            filteringProperties = new AntProperties(precedingProperties);
             filteringProperties.load(in);
 
-            if (getLogger().isDebugEnabled()) {
-                getLogger().debug(
-                                "Loaded default forrest.properties:"
-                                                + filteringProperties);
-            }
-
-        } finally {
-            if (source != null) {
-                m_resolver.release(source);
-            }
-            if (in != null) {
-                try {
-                    in.close();
-                } catch (IOException e) {}
-            }
-        }
-
-        // get forrest.properties and load the values
-        String forrestPropertiesStringURI = projectHome
-                        + SystemUtils.FILE_SEPARATOR + "forrest.properties";
+            debug("Loaded:" + antPropertiesStringURI +
+                                           filteringProperties.toString());
 
-        if (getLogger().isDebugEnabled()) {
-            getLogger()
-                            .debug(
-                                            "Searching for project forrest.properties in:"
-                                                            + defaultRorrestPropertiesStringURI);
-        }
-
-        try {
-            source = m_resolver.resolveURI(forrestPropertiesStringURI);
-
-            if (getLogger().isDebugEnabled()) {
-                getLogger().debug(
-                                "Searching for project forrest.properties in [RESOLVED]:"
-                                                + source.getURI());
-            }
-
-            in = source.getInputStream();
-            filteringProperties = new AntProperties(filteringProperties);
-            filteringProperties.load(in);
         } finally {
             if (source != null) {
                 m_resolver.release(source);
@@ -163,64 +183,26 @@
                 } catch (IOException e) {}
             }
         }
-
-        if (getLogger().isDebugEnabled()) {
-            getLogger().debug(
-                            "Loaded project forrest.properties:"
-                                            + filteringProperties);
-        }
-
-        //add forrest.home and project.home to properties
-        filteringProperties.setProperty("forrest.home", forrestHome);
-        filteringProperties.setProperty("project.home", projectHome);
-
-        if (getLogger().isDebugEnabled()) {
-            getLogger().debug(
-                            "Global forrest.properties:" + filteringProperties);
-        }
-
-    }
-
-    public Object[] getAttributeValues(String name, Configuration modeConf,
-                    Map objectModel) throws ConfigurationException {
-        Object[] attributeValues = super.getAttributeValues(name, modeConf,
-                        objectModel);
-        for (int i = 0; i < attributeValues.length; i++) {
-            attributeValues[i] = filteringProperties.filter(attributeValues[i]
-                            .toString());
-        }
-
-        return attributeValues;
-    }
-
-    public Object getAttribute(String name, Configuration modeConf,
-                    Map objectModel) throws ConfigurationException {
-        String original = super.getAttributeValues(name, modeConf, objectModel)[0]
-                        .toString();
-        String attributeValue = this.getAttributeValues(name, modeConf,
-                        objectModel)[0].toString();
-
-        if (getLogger().isDebugEnabled()) {
-            getLogger().debug(" - Requested:" + name);
-            getLogger().debug(" - Unfiltered:" + original);
-            getLogger().debug(" - Given:" + attributeValue);
-        }
-        return attributeValue;
+        
+        return filteringProperties;
     }
 
-    private final String getSystemProperty(String propertyName)
-                    throws MalformedURLException, IOException {
-
-        String propertyValue = System.getProperty(propertyName, ".");
-
-        if (getLogger().isDebugEnabled()) {
-            getLogger().debug(
-                            "system property " + propertyName + "="
-                                            + propertyValue);
-        }
-
-        return propertyValue;
+    public void service(ServiceManager manager) throws ServiceException {
+        m_resolver = (SourceResolver) manager.lookup(SourceResolver.ROLE);
     }
 
+    
+    /**
+     * Rocked science
+     * @param debugString
+     */
+    private final void debug(String debugString) {
+        if (getLogger().isDebugEnabled()) {
+            getLogger().debug(debugString);
+        }
+   }
+    
+    
+    
 }