You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lenya.apache.org by th...@apache.org on 2007/08/23 09:13:51 UTC

svn commit: r568857 - /lenya/trunk/src/modules-core/properties/java/src/org/apache/lenya/cms/cocoon/components/modules/input/PropertiesModule.java

Author: thorsten
Date: Thu Aug 23 00:13:50 2007
New Revision: 568857

URL: http://svn.apache.org/viewvc?rev=568857&view=rev
Log:
Bug 42558
lenya.properties.xml per publication. Patch submitted by Markus Angst. Thank you very much Markus.
Additional added logging in an empty catch.

Modified:
    lenya/trunk/src/modules-core/properties/java/src/org/apache/lenya/cms/cocoon/components/modules/input/PropertiesModule.java

Modified: lenya/trunk/src/modules-core/properties/java/src/org/apache/lenya/cms/cocoon/components/modules/input/PropertiesModule.java
URL: http://svn.apache.org/viewvc/lenya/trunk/src/modules-core/properties/java/src/org/apache/lenya/cms/cocoon/components/modules/input/PropertiesModule.java?rev=568857&r1=568856&r2=568857&view=diff
==============================================================================
--- lenya/trunk/src/modules-core/properties/java/src/org/apache/lenya/cms/cocoon/components/modules/input/PropertiesModule.java (original)
+++ lenya/trunk/src/modules-core/properties/java/src/org/apache/lenya/cms/cocoon/components/modules/input/PropertiesModule.java Thu Aug 23 00:13:50 2007
@@ -19,6 +19,7 @@
 import java.io.IOException;
 import java.net.MalformedURLException;
 import java.util.Enumeration;
+import java.util.HashSet;
 import java.util.Iterator;
 import java.util.Map;
 import java.util.SortedSet;
@@ -39,9 +40,10 @@
 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;
 import org.apache.forrest.conf.AntProperties;
+import org.apache.lenya.cms.publication.Publication;	
+import org.apache.lenya.cms.publication.PublicationUtil;
 import org.apache.lenya.cms.module.ModuleManager;
 import org.w3c.dom.Document;
 import org.w3c.dom.Element;
@@ -55,11 +57,16 @@
  */
 public class PropertiesModule extends DefaultsModule implements InputModule,
         Initializable, ThreadSafe, Serviceable {
+    
+    private HashSet pubInit;
+    
     private AntProperties filteringProperties;
 
     private SourceResolver m_resolver;
 
     private ModuleManager moduleManager;
+    
+    private ServiceManager serviceManager;
 
     private final static String lenyaHome = "context:/";
 
@@ -73,17 +80,19 @@
             throws ConfigurationException {
         String attributeValue;
 
+        loadPublicationPropertiesIfNotDone(objectModel);
         attributeValue = filteringProperties.getProperty(name);
         if (attributeValue == null) {
             String error = "Unable to get attribute value for "
-                    + name
-                    + "\n"
-                    + "Please make sure you defined "
-                    + name
-                    + " in lenya.properties.xml either in $LENYA_HOME or in the module that is requesting this property"
-                    + "\n"
-                    + "If you see this message, most of the time you spotted a module bug "
-                    + "(forget to define the default property). Please report it to our mailing list.";
+                + name
+                + ".\n"
+                + "Please make sure you defined "
+                + name
+                + " in lenya.properties.xml either in $LENYA_HOME, $PUB_HOME or "
+                + "in the module that is requesting this property.\n"
+                + "If you see this message, most of the time you spotted a module bug "
+                + "(forget to define the default property). Please report it to "
+                + "our mailing list.";           
             throw new ConfigurationException(error);
         }
 
@@ -97,6 +106,7 @@
 
     public Object[] getAttributeValues(String name, Configuration modeConf,
             Map objectModel) throws ConfigurationException {
+        loadPublicationPropertiesIfNotDone(objectModel);
         Object[] attributeValues = super.getAttributeValues(name, modeConf,
                 objectModel);
         for (int i = 0; i < attributeValues.length; i++) {
@@ -109,7 +119,7 @@
 
     public Iterator getAttributeNames(Configuration modeConf, Map objectModel)
             throws ConfigurationException {
-
+        loadPublicationPropertiesIfNotDone(objectModel);
         SortedSet matchset = new TreeSet();
         Enumeration enumeration = filteringProperties.keys();
         while (enumeration.hasMoreElements()) {
@@ -124,22 +134,31 @@
 
     public void initialize() throws Exception {
 
+        pubInit = new HashSet();
+        
         // add all homes important to Lenya to the properties
         setHomes();
 
         loadSystemProperties(filteringProperties);
 
         // NOTE: the first values set get precedence, as in AntProperties
+        // 
+        // Order of precedence:        
+        // 1. Publication (lazy loaded in loadPublicationPropertiesIfNotDone())
+        // 2. Lenya local
+        // 3. Modules (all modules, not only the ones referenced in the publication)
+        // 4. Lenya
+        //
         String lenyaPropertiesStringURI = "";
 
-        // get the values from local.lenya.properties.xml
         try {
+            // get the values from local.lenya.properties.xml
             lenyaPropertiesStringURI = lenyaHome + SystemUtils.FILE_SEPARATOR
                     + PROPERTY_NAME_LOCAL;
-
             filteringProperties = loadXMLPropertiesFromURI(filteringProperties,
-                    lenyaPropertiesStringURI);
+                    lenyaPropertiesStringURI, false);
 
+            // get the values from all modules
             String[] module2src = moduleManager.getModuleIds();
             for (int i = 0; i < module2src.length; i++) {
                 String id = module2src[i];
@@ -148,17 +167,15 @@
                     lenyaPropertiesStringURI = value + SystemUtils.FILE_SEPARATOR
                             + PROPERTY_NAME;
                     filteringProperties = loadXMLPropertiesFromURI(
-                            filteringProperties, lenyaPropertiesStringURI);
+                            filteringProperties, lenyaPropertiesStringURI, false);
                 }
             }
             // get the values from lenya.properties.xml this are the default
-            // lenya
-            // values
+            // lenya values
             lenyaPropertiesStringURI = lenyaHome + SystemUtils.FILE_SEPARATOR
                     + PROPERTY_NAME;
-
             filteringProperties = loadXMLPropertiesFromURI(filteringProperties,
-                    lenyaPropertiesStringURI);
+                    lenyaPropertiesStringURI, false);
         } finally {
             if (debugging())
                 debug("Loaded project lenya.properties.xml:" + filteringProperties);
@@ -186,26 +203,30 @@
             String propName = (String) e.nextElement();
             String systemPropValue = System.getProperty(propName);
             if (systemPropValue != null) {
-                // AntProperties.setProperty doesn't let you override, so we
-                // have to remove the property then add it again
-                props.remove(propName);
-                props.setProperty(propName, systemPropValue);
+                overwriteProperty(props, propName, systemPropValue);
             }
         }
     }
 
+    private void overwriteProperty(AntProperties props, String propName, String propValue) {
+        //
+        // AntProperties.setProperty doesn't let you override, so we
+        // have to remove the property then add it again
+        props.remove(propName);
+        props.setProperty(propName, propValue);
+    }
+
     /**
+     * @param precedingProperties
      * @param propertiesStringURI
+     * @param overwrite
      * @throws IOException
      * @throws MalformedURLException
-     * @throws MalformedURLException
-     * @throws IOException
      * @throws ParserConfigurationException
      * @throws SAXException
-     * @throws SourceNotFoundException
      */
-    private AntProperties loadXMLPropertiesFromURI(
-            AntProperties precedingProperties, String propertiesStringURI)
+    private AntProperties loadXMLPropertiesFromURI(AntProperties precedingProperties, 
+            String propertiesStringURI, boolean overwrite)
             throws MalformedURLException, IOException,
             ParserConfigurationException, SAXException {
 
@@ -225,8 +246,13 @@
                 if (nl != null && nl.getLength() > 0) {
                     for (int i = 0; i < nl.getLength(); i++) {
                         Element el = (Element) nl.item(i);
-                        filteringProperties.setProperty(el.getAttribute("name"), el
-                                .getAttribute("value"));
+                        if (overwrite == true) {
+                            overwriteProperty(filteringProperties, el.getAttribute("name"), 
+                                              el.getAttribute("value"));
+                        } else {
+                            filteringProperties.setProperty(el.getAttribute("name"), 
+                                                            el.getAttribute("value"));
+                        }
                     }
                 }
 
@@ -245,7 +271,36 @@
         return filteringProperties;
     }
 
-    public void service(ServiceManager manager) throws ServiceException {
+    /**
+     * Get the properties from the requested publication
+     */
+    private void loadPublicationPropertiesIfNotDone(Map objectModel) 
+            throws ConfigurationException {
+        Publication pub;
+        String pubId;
+
+        try {
+            pub = PublicationUtil.getPublication(serviceManager, objectModel);
+        } catch (Exception e) {
+            throw new ConfigurationException(e.getMessage());
+        }
+        pubId = pub.getId();
+        if (pubInit.contains(pubId)) {
+            return;
+        }
+        try {
+            filteringProperties = loadXMLPropertiesFromURI(filteringProperties,
+                                  PROPERTY_NAME, true);
+        } catch (IOException e) {       
+          getLogger().warn("Could not load properties from pub \""+pubId+"\".\n"+e);
+        } catch (Exception e) {
+            throw new ConfigurationException(e.getMessage());
+        }
+        pubInit.add(pubId);
+    }
+
+    public void service(ServiceManager manager) throws ServiceException {        
+        this.serviceManager = manager;
         m_resolver = (SourceResolver) manager.lookup(SourceResolver.ROLE);
         moduleManager = (ModuleManager) manager.lookup(ModuleManager.ROLE);
     }



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@lenya.apache.org
For additional commands, e-mail: commits-help@lenya.apache.org