KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > forrest > conf > ForrestConfModule


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation or its licensors,
3  * as applicable.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.forrest.conf;
18
19 import java.io.IOException JavaDoc;
20 import java.io.InputStream JavaDoc;
21 import java.net.MalformedURLException JavaDoc;
22 import java.util.Enumeration JavaDoc;
23 import java.util.Map JavaDoc;
24
25 import org.apache.avalon.framework.activity.Initializable;
26 import org.apache.avalon.framework.configuration.Configuration;
27 import org.apache.avalon.framework.configuration.ConfigurationException;
28 import org.apache.avalon.framework.service.ServiceException;
29 import org.apache.avalon.framework.service.ServiceManager;
30 import org.apache.avalon.framework.service.Serviceable;
31 import org.apache.avalon.framework.thread.ThreadSafe;
32 import org.apache.cocoon.components.modules.input.DefaultsModule;
33 import org.apache.cocoon.components.modules.input.InputModule;
34 import org.apache.commons.lang.SystemUtils;
35 import org.apache.excalibur.source.Source;
36 import org.apache.excalibur.source.SourceNotFoundException;
37 import org.apache.excalibur.source.SourceResolver;
38
39 /**
40  * Input module for accessing the base properties used in Forrest. The main
41  * values are the locations of the <b>source </b> directories and of the
42  * <b>forrest </b> directories. The values are gotten using the ForrestConfUtils
43  * class.
44  */

45 public class ForrestConfModule extends DefaultsModule implements InputModule,
46                 Initializable, ThreadSafe, Serviceable
47 {
48     private AntProperties filteringProperties;
49     private String JavaDoc forrestHome, projectHome, contextHome;
50     private SourceResolver m_resolver;
51
52     private final static String JavaDoc defaultHome = "context:/";
53
54     public Object JavaDoc getAttribute(String JavaDoc name, Configuration modeConf,
55                     Map JavaDoc objectModel) throws ConfigurationException {
56         String JavaDoc original = super.getAttributeValues(name, modeConf, objectModel)[0]
57                         .toString();
58         String JavaDoc attributeValue = this.getAttributeValues(name, modeConf,
59                         objectModel)[0].toString();
60
61         if (debugging())
62             debug(" - Requested:" + name);
63         if (debugging())
64             debug(" - Unfiltered:" + original);
65         if (debugging())
66             debug(" - Given:" + attributeValue);
67
68         return attributeValue;
69     }
70
71     public Object JavaDoc[] getAttributeValues(String JavaDoc name, Configuration modeConf,
72                     Map JavaDoc objectModel) throws ConfigurationException {
73         Object JavaDoc[] attributeValues = super.getAttributeValues(name, modeConf,
74                         objectModel);
75         for (int i = 0; i < attributeValues.length; i++) {
76             attributeValues[i] = filteringProperties.filter(attributeValues[i]
77                             .toString());
78         }
79
80         return attributeValues;
81     }
82
83     private final String JavaDoc getSystemProperty(String JavaDoc propertyName) {
84
85         //if the property is not set, default to the webapp context
86
String JavaDoc propertyValue = System.getProperty(propertyName, defaultHome);
87
88         if (debugging())
89             debug("system property " + propertyName + "=" + propertyValue);
90
91         return propertyValue;
92     }
93
94     public void initialize() throws Exception JavaDoc {
95
96         forrestHome = ForrestConfUtils.getForrestHome();
97         projectHome = ForrestConfUtils.getProjectHome();
98         contextHome = ForrestConfUtils.getContextHome();
99
100         filteringProperties = new AntProperties();
101
102         //add forrest.home and project.home to properties
103
filteringProperties.setProperty("forrest.home", forrestHome);
104         filteringProperties.setProperty("project.home", projectHome);
105         filteringProperties.setProperty("context.home", contextHome);
106
107         //NOTE: the first values set get precedence, as in AntProperties
108

109         // get forrest.properties and load the values
110
String JavaDoc forrestPropertiesStringURI = projectHome
111                         + SystemUtils.FILE_SEPARATOR + "forrest.properties";
112
113         filteringProperties = loadAntPropertiesFromURI(filteringProperties,
114                         forrestPropertiesStringURI);
115
116         // get default-forrest.properties and load the values
117
String JavaDoc defaultForrestPropertiesStringURI = contextHome
118                         + SystemUtils.FILE_SEPARATOR
119                         + "default-forrest.properties";
120
121         filteringProperties = loadAntPropertiesFromURI(filteringProperties,
122                         defaultForrestPropertiesStringURI);
123
124         loadSystemProperties(filteringProperties);
125         ForrestConfUtils.aliasSkinProperties(filteringProperties);
126         if (debugging())
127             debug("Loaded project forrest.properties:" + filteringProperties);
128     }
129
130      /**
131      * Override any properties for which a system property exists
132      */

133     private void loadSystemProperties(AntProperties props) {
134         for (Enumeration JavaDoc e = props.propertyNames(); e.hasMoreElements();) {
135             String JavaDoc propName = (String JavaDoc)e.nextElement();
136             String JavaDoc systemPropValue = System.getProperty(propName);
137             if (systemPropValue != null) {
138                 // AntProperties.setProperty doesn't let you override, so we have to remove the property then add it again
139
props.remove(propName);
140                 props.setProperty(propName, systemPropValue);
141             }
142         }
143     }
144
145     /**
146      * @param antPropertiesStringURI
147      * @throws MalformedURLException
148      * @throws IOException
149      * @throws SourceNotFoundException
150      */

151     private AntProperties loadAntPropertiesFromURI(
152                     AntProperties precedingProperties,
153                     String JavaDoc antPropertiesStringURI)
154                     throws MalformedURLException JavaDoc, IOException JavaDoc,
155                     SourceNotFoundException {
156
157         Source source = null;
158         InputStream JavaDoc in = null;
159         try {
160
161             source = m_resolver.resolveURI(antPropertiesStringURI);
162
163             if (debugging())
164                 debug("Searching for forrest.properties in" + source.getURI());
165             in = source.getInputStream();
166             filteringProperties = new AntProperties(precedingProperties);
167             filteringProperties.load(in);
168
169             if (debugging())
170                 debug("Loaded:" + antPropertiesStringURI
171                                 + filteringProperties.toString());
172
173         } finally {
174             if (source != null) {
175                 m_resolver.release(source);
176             }
177             if (in != null) {
178                 try {
179                     in.close();
180                 } catch (IOException JavaDoc e) {}
181             }
182         }
183
184         return filteringProperties;
185     }
186
187     public void service(ServiceManager manager) throws ServiceException {
188         m_resolver = (SourceResolver) manager.lookup(SourceResolver.ROLE);
189     }
190
191     /**
192      * Rocked science
193      */

194     private final boolean debugging() {
195         return getLogger().isDebugEnabled();
196     }
197
198     /**
199      * Rocked science
200      * @param debugString
201      */

202     private final void debug(String JavaDoc debugString) {
203         getLogger().debug(debugString);
204     }
205
206 }
207
Popular Tags