View | Details | Raw Unified | Return to bug 42558
Collapse All | Expand All

(-)C:/lenya/lenya-2.0-dev/src/modules-core/properties/java/src/org/apache/lenya/cms/cocoon/components/modules/input/PropertiesModule.java (-54 / +68 lines)
Lines 19-25 Link Here
19
import java.io.IOException;
19
import java.io.IOException;
20
import java.net.MalformedURLException;
20
import java.net.MalformedURLException;
21
import java.util.Enumeration;
21
import java.util.Enumeration;
22
import java.util.HashSet;
22
import java.util.HashMap;
23
import java.util.Iterator;
23
import java.util.Iterator;
24
import java.util.Map;
24
import java.util.Map;
25
import java.util.SortedSet;
25
import java.util.SortedSet;
Lines 42-48 Link Here
42
import org.apache.excalibur.source.Source;
42
import org.apache.excalibur.source.Source;
43
import org.apache.excalibur.source.SourceResolver;
43
import org.apache.excalibur.source.SourceResolver;
44
import org.apache.forrest.conf.AntProperties;
44
import org.apache.forrest.conf.AntProperties;
45
import org.apache.lenya.cms.publication.Publication;	
45
import org.apache.lenya.cms.cocoon.source.AggregatingSource;
46
import org.apache.lenya.cms.publication.Publication;
46
import org.apache.lenya.cms.publication.PublicationUtil;
47
import org.apache.lenya.cms.publication.PublicationUtil;
47
import org.apache.lenya.cms.module.ModuleManager;
48
import org.apache.lenya.cms.module.ModuleManager;
48
import org.w3c.dom.Document;
49
import org.w3c.dom.Document;
Lines 58-64 Link Here
58
public class PropertiesModule extends DefaultsModule implements InputModule,
59
public class PropertiesModule extends DefaultsModule implements InputModule,
59
        Initializable, ThreadSafe, Serviceable {
60
        Initializable, ThreadSafe, Serviceable {
60
    
61
    
61
    private HashSet pubInit;
62
    private HashMap pubProperties;
62
    
63
    
63
    private AntProperties filteringProperties;
64
    private AntProperties filteringProperties;
64
65
Lines 80-87 Link Here
80
            throws ConfigurationException {
81
            throws ConfigurationException {
81
        String attributeValue;
82
        String attributeValue;
82
83
83
        loadPublicationPropertiesIfNotDone(objectModel);
84
        AntProperties pubProp = loadPublicationPropertiesIfNotDone(objectModel);
84
        attributeValue = filteringProperties.getProperty(name);
85
        attributeValue = pubProp.getProperty(name);
86
        if (attributeValue == null) {
87
            attributeValue = filteringProperties.getProperty(name);
88
        }
85
        if (attributeValue == null) {
89
        if (attributeValue == null) {
86
            String error = "Unable to get attribute value for "
90
            String error = "Unable to get attribute value for "
87
                + name
91
                + name
Lines 95-101 Link Here
95
                + "our mailing list.";           
99
                + "our mailing list.";           
96
            throw new ConfigurationException(error);
100
            throw new ConfigurationException(error);
97
        }
101
        }
98
99
        if (debugging()) {
102
        if (debugging()) {
100
            debug(" - Requested:" + name);
103
            debug(" - Requested:" + name);
101
            debug(" - Given:" + attributeValue);
104
            debug(" - Given:" + attributeValue);
Lines 100-106 Link Here
100
            debug(" - Requested:" + name);
103
            debug(" - Requested:" + name);
101
            debug(" - Given:" + attributeValue);
104
            debug(" - Given:" + attributeValue);
102
        }
105
        }
103
104
        return attributeValue;
106
        return attributeValue;
105
    }
107
    }
106
108
Lines 106-114 Link Here
106
108
107
    public Object[] getAttributeValues(String name, Configuration modeConf,
109
    public Object[] getAttributeValues(String name, Configuration modeConf,
108
            Map objectModel) throws ConfigurationException {
110
            Map objectModel) throws ConfigurationException {
109
        loadPublicationPropertiesIfNotDone(objectModel);
111
        
112
        AntProperties pubProp = loadPublicationPropertiesIfNotDone(objectModel);
110
        Object[] attributeValues = super.getAttributeValues(name, modeConf,
113
        Object[] attributeValues = super.getAttributeValues(name, modeConf,
111
                objectModel);
114
                objectModel);
115
        // FIXME
116
        // what exactly are we doing here?
112
        for (int i = 0; i < attributeValues.length; i++) {
117
        for (int i = 0; i < attributeValues.length; i++) {
113
            attributeValues[i] = filteringProperties.filter(attributeValues[i]
118
            attributeValues[i] = filteringProperties.filter(attributeValues[i]
114
                    .toString());
119
                    .toString());
Lines 113-119 Link Here
113
            attributeValues[i] = filteringProperties.filter(attributeValues[i]
118
            attributeValues[i] = filteringProperties.filter(attributeValues[i]
114
                    .toString());
119
                    .toString());
115
        }
120
        }
116
121
        // depending on the answer to the question above, this has to be
122
        // done also...
123
        // for (int i = 0; i < attributeValues.length; i++) {
124
        //     attributeValues[i] = pubProp.filter(attributeValues[i]
125
        //             .toString());
126
        // }
117
        return attributeValues;
127
        return attributeValues;
118
    }
128
    }
119
129
Lines 119-127 Link Here
119
129
120
    public Iterator getAttributeNames(Configuration modeConf, Map objectModel)
130
    public Iterator getAttributeNames(Configuration modeConf, Map objectModel)
121
            throws ConfigurationException {
131
            throws ConfigurationException {
122
        loadPublicationPropertiesIfNotDone(objectModel);
123
        SortedSet matchset = new TreeSet();
132
        SortedSet matchset = new TreeSet();
124
        Enumeration enumeration = filteringProperties.keys();
133
        
134
        AntProperties pubProp = loadPublicationPropertiesIfNotDone(objectModel);
135
        Enumeration enumeration = pubProp.keys();
136
        while (enumeration.hasMoreElements()) {
137
            String key = (String) enumeration.nextElement();
138
            matchset.add(key);
139
        }
140
        enumeration = filteringProperties.keys();
125
        while (enumeration.hasMoreElements()) {
141
        while (enumeration.hasMoreElements()) {
126
            String key = (String) enumeration.nextElement();
142
            String key = (String) enumeration.nextElement();
127
            matchset.add(key);
143
            matchset.add(key);
Lines 133-140 Link Here
133
    }
149
    }
134
150
135
    public void initialize() throws Exception {
151
    public void initialize() throws Exception {
136
152
        
137
        pubInit = new HashSet();
153
        pubProperties = new HashMap();
138
        
154
        
139
        // add all homes important to Lenya to the properties
155
        // add all homes important to Lenya to the properties
140
        setHomes();
156
        setHomes();
Lines 144-150 Link Here
144
        // NOTE: the first values set get precedence, as in AntProperties
160
        // NOTE: the first values set get precedence, as in AntProperties
145
        // 
161
        // 
146
        // Order of precedence:        
162
        // Order of precedence:        
147
        // 1. Publication (lazy loaded in loadPublicationPropertiesIfNotDone())
163
        // 1. Publication (lazy loaded in loadPublicationPropertiesIfNotDone();
164
        //    aggregate-fallback enabled)
148
        // 2. Lenya local
165
        // 2. Lenya local
149
        // 3. Modules (all modules, not only the ones referenced in the publication)
166
        // 3. Modules (all modules, not only the ones referenced in the publication)
150
        // 4. Lenya
167
        // 4. Lenya
Lines 156-162 Link Here
156
            lenyaPropertiesStringURI = lenyaHome + SystemUtils.FILE_SEPARATOR
173
            lenyaPropertiesStringURI = lenyaHome + SystemUtils.FILE_SEPARATOR
157
                    + PROPERTY_NAME_LOCAL;
174
                    + PROPERTY_NAME_LOCAL;
158
            filteringProperties = loadXMLPropertiesFromURI(filteringProperties,
175
            filteringProperties = loadXMLPropertiesFromURI(filteringProperties,
159
                    lenyaPropertiesStringURI, false);
176
                    lenyaPropertiesStringURI);
160
177
161
            // get the values from all modules
178
            // get the values from all modules
162
            String[] module2src = moduleManager.getModuleIds();
179
            String[] module2src = moduleManager.getModuleIds();
Lines 167-173 Link Here
167
                    lenyaPropertiesStringURI = value + SystemUtils.FILE_SEPARATOR
184
                    lenyaPropertiesStringURI = value + SystemUtils.FILE_SEPARATOR
168
                            + PROPERTY_NAME;
185
                            + PROPERTY_NAME;
169
                    filteringProperties = loadXMLPropertiesFromURI(
186
                    filteringProperties = loadXMLPropertiesFromURI(
170
                            filteringProperties, lenyaPropertiesStringURI, false);
187
                            filteringProperties, lenyaPropertiesStringURI);
171
                }
188
                }
172
            }
189
            }
173
            // get the values from lenya.properties.xml this are the default
190
            // get the values from lenya.properties.xml this are the default
Lines 175-181 Link Here
175
            lenyaPropertiesStringURI = lenyaHome + SystemUtils.FILE_SEPARATOR
192
            lenyaPropertiesStringURI = lenyaHome + SystemUtils.FILE_SEPARATOR
176
                    + PROPERTY_NAME;
193
                    + PROPERTY_NAME;
177
            filteringProperties = loadXMLPropertiesFromURI(filteringProperties,
194
            filteringProperties = loadXMLPropertiesFromURI(filteringProperties,
178
                    lenyaPropertiesStringURI, false);
195
                    lenyaPropertiesStringURI);
179
        } finally {
196
        } finally {
180
            if (debugging())
197
            if (debugging())
181
                debug("Loaded project lenya.properties.xml:" + filteringProperties);
198
                debug("Loaded project lenya.properties.xml:" + filteringProperties);
Lines 225-247 Link Here
225
     * @throws ParserConfigurationException
242
     * @throws ParserConfigurationException
226
     * @throws SAXException
243
     * @throws SAXException
227
     */
244
     */
228
    private AntProperties loadXMLPropertiesFromURI(AntProperties precedingProperties, 
245
    private AntProperties loadXMLPropertiesFromURI(AntProperties properties, 
229
            String propertiesStringURI, boolean overwrite)
246
            String propertiesStringURI)
230
            throws MalformedURLException, IOException,
247
            throws MalformedURLException, IOException,
231
            ParserConfigurationException, SAXException {
248
            ParserConfigurationException, SAXException {
232
249
        
233
        Source source = null;
250
        Source source = null;
234
        try {
251
        try {
235
236
            source = m_resolver.resolveURI(propertiesStringURI);
252
            source = m_resolver.resolveURI(propertiesStringURI);
237
238
            if (source.exists()) {
253
            if (source.exists()) {
239
240
                DocumentBuilderFactory factory = DocumentBuilderFactory
254
                DocumentBuilderFactory factory = DocumentBuilderFactory
241
                        .newInstance();
255
                        .newInstance();
242
                DocumentBuilder builder = factory.newDocumentBuilder();
256
                DocumentBuilder builder = factory.newDocumentBuilder();
243
                Document document = builder.parse(source.getURI());
257
                Document document = null;
244
258
                if (source instanceof AggregatingSource) {
259
                    document = builder.parse(source.getInputStream());
260
                } else {
261
                    document = builder.parse(source.getURI());
262
                }
245
                NodeList nl = document.getElementsByTagName("property");
263
                NodeList nl = document.getElementsByTagName("property");
246
                if (nl != null && nl.getLength() > 0) {
264
                if (nl != null && nl.getLength() > 0) {
247
                    for (int i = 0; i < nl.getLength(); i++) {
265
                    for (int i = 0; i < nl.getLength(); i++) {
Lines 246-267 Link Here
246
                if (nl != null && nl.getLength() > 0) {
264
                if (nl != null && nl.getLength() > 0) {
247
                    for (int i = 0; i < nl.getLength(); i++) {
265
                    for (int i = 0; i < nl.getLength(); i++) {
248
                        Element el = (Element) nl.item(i);
266
                        Element el = (Element) nl.item(i);
249
                        if (overwrite == true) {
267
                        properties.setProperty(el.getAttribute("name"), 
250
                            overwriteProperty(filteringProperties, el.getAttribute("name"), 
268
                                               el.getAttribute("value"));
251
                                              el.getAttribute("value"));
252
                        } else {
253
                            filteringProperties.setProperty(el.getAttribute("name"), 
254
                                                            el.getAttribute("value"));
255
                        }
256
                    }
269
                    }
257
                }
270
                }
258
259
                if (debugging())
271
                if (debugging())
260
                    debug("Loaded:" + propertiesStringURI
272
                    debug("Loaded: " + propertiesStringURI + properties.toString());
261
                            + filteringProperties.toString());
262
263
            }
273
            }
264
265
        } finally {
274
        } finally {
266
            if (source != null) {
275
            if (source != null) {
267
                m_resolver.release(source);
276
                m_resolver.release(source);
Lines 267-274 Link Here
267
                m_resolver.release(source);
276
                m_resolver.release(source);
268
            }
277
            }
269
        }
278
        }
270
279
        return properties;
271
        return filteringProperties;
272
    }
280
    }
273
281
274
    /**
282
    /**
Lines 274-304 Link Here
274
    /**
282
    /**
275
     * Get the properties from the requested publication
283
     * Get the properties from the requested publication
276
     */
284
     */
277
    private void loadPublicationPropertiesIfNotDone(Map objectModel) 
285
    private AntProperties loadPublicationPropertiesIfNotDone(Map objectModel) 
278
            throws ConfigurationException {
286
            throws ConfigurationException {
279
        Publication pub;
287
        String pubId = getPubId(objectModel);
280
        String pubId;
288
        AntProperties pubProp = new AntProperties();
281
289
        
290
        if (pubProperties.containsKey(pubId)) {            
291
            return (AntProperties)pubProperties.get(pubId);
292
        }
282
        try {
293
        try {
283
            pub = PublicationUtil.getPublication(serviceManager, objectModel);
294
            pubProp = loadXMLPropertiesFromURI(pubProp,
295
                                  "aggregate-fallback://" + PROPERTY_NAME);
296
            pubProperties.put(pubId, pubProp);
297
        } catch (IOException e) {       
298
            getLogger().warn("Could not load properties from pub \"" + pubId + "\".\n" + e);
284
        } catch (Exception e) {
299
        } catch (Exception e) {
285
            throw new ConfigurationException(e.getMessage());
300
            throw new ConfigurationException(e.getMessage());
286
        }
301
        }
287
        pubId = pub.getId();
302
        return pubProp;
288
        if (pubInit.contains(pubId)) {
303
    }
289
            return;
304
290
        }
305
    private String getPubId(Map objectModel) throws ConfigurationException {
306
        Publication pub;
307
291
        try {
308
        try {
292
            filteringProperties = loadXMLPropertiesFromURI(filteringProperties,
309
            pub = PublicationUtil.getPublication(serviceManager, objectModel);
293
                                  PROPERTY_NAME, true);
310
            return pub.getId();
294
        } catch (IOException e) {       
295
          getLogger().warn("Could not load properties from pub \""+pubId+"\".\n"+e);
296
        } catch (Exception e) {
311
        } catch (Exception e) {
297
            throw new ConfigurationException(e.getMessage());
312
            throw new ConfigurationException(e.getMessage());
298
        }
313
        }
299
        pubInit.add(pubId);
300
    }
314
    }
301
315
    
302
    public void service(ServiceManager manager) throws ServiceException {        
316
    public void service(ServiceManager manager) throws ServiceException {        
303
        this.serviceManager = manager;
317
        this.serviceManager = manager;
304
        m_resolver = (SourceResolver) manager.lookup(SourceResolver.ROLE);
318
        m_resolver = (SourceResolver) manager.lookup(SourceResolver.ROLE);

Return to bug 42558