1 17 package org.apache.forrest.conf; 18 19 import java.io.IOException ; 20 import java.io.InputStream ; 21 import java.net.MalformedURLException ; 22 import java.util.Enumeration ; 23 import java.util.Map ; 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 45 public class ForrestConfModule extends DefaultsModule implements InputModule, 46 Initializable, ThreadSafe, Serviceable 47 { 48 private AntProperties filteringProperties; 49 private String forrestHome, projectHome, contextHome; 50 private SourceResolver m_resolver; 51 52 private final static String defaultHome = "context:/"; 53 54 public Object getAttribute(String name, Configuration modeConf, 55 Map objectModel) throws ConfigurationException { 56 String original = super.getAttributeValues(name, modeConf, objectModel)[0] 57 .toString(); 58 String 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 [] getAttributeValues(String name, Configuration modeConf, 72 Map objectModel) throws ConfigurationException { 73 Object [] 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 getSystemProperty(String propertyName) { 84 85 String 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 { 95 96 forrestHome = ForrestConfUtils.getForrestHome(); 97 projectHome = ForrestConfUtils.getProjectHome(); 98 contextHome = ForrestConfUtils.getContextHome(); 99 100 filteringProperties = new AntProperties(); 101 102 filteringProperties.setProperty("forrest.home", forrestHome); 104 filteringProperties.setProperty("project.home", projectHome); 105 filteringProperties.setProperty("context.home", contextHome); 106 107 109 String forrestPropertiesStringURI = projectHome 111 + SystemUtils.FILE_SEPARATOR + "forrest.properties"; 112 113 filteringProperties = loadAntPropertiesFromURI(filteringProperties, 114 forrestPropertiesStringURI); 115 116 String 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 133 private void loadSystemProperties(AntProperties props) { 134 for (Enumeration e = props.propertyNames(); e.hasMoreElements();) { 135 String propName = (String )e.nextElement(); 136 String systemPropValue = System.getProperty(propName); 137 if (systemPropValue != null) { 138 props.remove(propName); 140 props.setProperty(propName, systemPropValue); 141 } 142 } 143 } 144 145 151 private AntProperties loadAntPropertiesFromURI( 152 AntProperties precedingProperties, 153 String antPropertiesStringURI) 154 throws MalformedURLException , IOException , 155 SourceNotFoundException { 156 157 Source source = null; 158 InputStream 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 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 194 private final boolean debugging() { 195 return getLogger().isDebugEnabled(); 196 } 197 198 202 private final void debug(String debugString) { 203 getLogger().debug(debugString); 204 } 205 206 } 207 | Popular Tags |