Return-Path: Delivered-To: apmail-xml-forrest-dev-archive@www.apache.org Received: (qmail 52561 invoked from network); 19 Apr 2004 15:14:02 -0000 Received: from daedalus.apache.org (HELO mail.apache.org) (208.185.179.12) by minotaur-2.apache.org with SMTP; 19 Apr 2004 15:14:02 -0000 Received: (qmail 34240 invoked by uid 500); 19 Apr 2004 15:13:40 -0000 Delivered-To: apmail-xml-forrest-dev-archive@xml.apache.org Received: (qmail 33983 invoked by uid 500); 19 Apr 2004 15:13:38 -0000 Mailing-List: contact forrest-cvs-help@xml.apache.org; run by ezmlm Precedence: bulk list-help: list-unsubscribe: list-post: Reply-To: forrest-dev@xml.apache.org Delivered-To: mailing list forrest-cvs@xml.apache.org Received: (qmail 33905 invoked from network); 19 Apr 2004 15:13:37 -0000 Received: from unknown (HELO minotaur.apache.org) (209.237.227.194) by daedalus.apache.org with SMTP; 19 Apr 2004 15:13:37 -0000 Received: (qmail 52369 invoked by uid 65534); 19 Apr 2004 15:13:42 -0000 Date: 19 Apr 2004 15:13:42 -0000 Message-ID: <20040419151342.52363.qmail@minotaur.apache.org> From: nicolaken@apache.org To: forrest-cvs@xml.apache.org Subject: svn commit: rev 10101 - xml/forrest/trunk/src/java/org/apache/forrest/conf X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N X-Spam-Rating: minotaur-2.apache.org 1.6.2 0/1000/N Author: nicolaken Date: Mon Apr 19 08:13:41 2004 New Revision: 10101 Modified: xml/forrest/trunk/src/java/org/apache/forrest/conf/AntProperties.java xml/forrest/trunk/src/java/org/apache/forrest/conf/ForrestConfModule.java Log: Almost there. Modified: xml/forrest/trunk/src/java/org/apache/forrest/conf/AntProperties.java ============================================================================== --- xml/forrest/trunk/src/java/org/apache/forrest/conf/AntProperties.java (original) +++ xml/forrest/trunk/src/java/org/apache/forrest/conf/AntProperties.java Mon Apr 19 08:13:41 2004 @@ -24,89 +24,160 @@ import java.util.Map; import java.util.Properties; import java.util.Set; +import java.util.StringTokenizer; import org.apache.commons.lang.StringUtils; /** - * Class for accessing properties in a properties file - * roughly compatible with Ant property files, where ${name} - * is replaced with the value of the property 'name' if - * declared beforehand. - **/ -public class AntProperties extends Properties { - - public AntProperties() { - super(); - } - - public AntProperties(Properties arg0) { - super(arg0); - } - - - public synchronized void load(InputStream arg0) throws IOException { - // TODO Auto-generated method stub - super.load(arg0); - - BufferedReader in = null; - try { - - in = new BufferedReader(new InputStreamReader(arg0)); - - String currentLine, name, value; - int splitIndex; - - while((currentLine = in.readLine()) != null) { - // # == comment - if(!currentLine.startsWith("#")&&!(currentLine.trim().length()==0)){ - splitIndex = currentLine.indexOf('='); - name = currentLine.substring(0, splitIndex).trim(); - value = currentLine.substring(splitIndex+1).trim(); - this.put(name,value); - } - } - } - finally { - if (in != null) { - try { - in.close(); - } - catch (IOException e) { - } - } - } - } - - public synchronized Object put(Object name, Object value) { - //if the property is already there don't overwrite, as in Ant - //properties defined first take precedence - if(!super.containsKey(name)){ - Enumeration names = super.propertyNames(); - while( names.hasMoreElements() ) { - String currentName = (String) names.nextElement(); - String valueToSearchFor = "${"+currentName+"}"; - String valueToReplaceWith = (String) super.get(currentName); - value = StringUtils.replace(value.toString(), valueToSearchFor, valueToReplaceWith); - } - return super.put(name,value); - } - - return null; - } - - public synchronized void putAll(Map arg0) { - Set keys = arg0.keySet(); - Iterator i = keys.iterator(); - while(i.hasNext()) { - String currentKey = i.next().toString(); - this.put(currentKey,arg0.get(currentKey)); - } - - } - - public synchronized Object setProperty(String name, String value) { - // TODO Auto-generated method stub - return this.put(name, value); - } + * Class for accessing properties in a properties file roughly compatible with + * Ant property files, where ${name} is replaced with the value of the property + * if declared beforehand. + */ +public class AntProperties extends Properties +{ -} + public AntProperties() { + super(); + } + + public AntProperties(Properties arg0) { + super(arg0); + putAll(arg0); + } + + public synchronized void load(InputStream arg0) throws IOException { + super.load(arg0); + + BufferedReader in = null; + try { + + in = new BufferedReader(new InputStreamReader(arg0)); + + String currentLine, name, value; + int splitIndex; + + while ((currentLine = in.readLine()) != null) { + // # == comment + if (!currentLine.startsWith("#") + && !(currentLine.trim().length() == 0)) { + splitIndex = currentLine.indexOf('='); + name = currentLine.substring(0, splitIndex).trim(); + value = currentLine.substring(splitIndex + 1).trim(); + this.put(name, value); + } + } + } finally { + if (in != null) { + try { + in.close(); + } catch (IOException e) {} + } + } + } + + public synchronized Object put(Object name, Object value) { + //if the property is already there don't overwrite, as in Ant + //properties defined first take precedence + if (!super.containsKey(name)) { + Enumeration names = super.propertyNames(); + while (names.hasMoreElements()) { + String currentName = (String) names.nextElement(); + String valueToSearchFor = "${" + currentName + "}"; + String valueToReplaceWith = (String) super.get(currentName); + value = StringUtils.replace(value.toString(), valueToSearchFor, + valueToReplaceWith); + } + return super.put(name, value); + } + + return null; + } + + public synchronized void putAll(Map arg0) { + Set keys = arg0.keySet(); + Iterator i = keys.iterator(); + while (i.hasNext()) { + String currentKey = i.next().toString(); + this.put(currentKey, arg0.get(currentKey)); + } + + } + + public synchronized Object setProperty(String name, String value) { + return this.put(name, value); + } + + public String filter(String stringToFilter) { + + StringBuffer resultStringBuffer = new StringBuffer(); + + StringTokenizer st = new StringTokenizer(stringToFilter, "@", true); + + String currentToken = st.nextToken(); + STATE state; + + if (currentToken.equals("@")) { + state = new STATE(STATE.START_TOKEN); + } else { + state = new STATE(STATE.STRING_NOT_TO_FILTER); + } + + while (st.hasMoreTokens()) { + + if (state.isStringToFilter()) { + resultStringBuffer.append(getProperty(currentToken,"@"+currentToken+"@")); + System.out.println("currentToken to Filter:" + currentToken); + } else if (state.isStringNOTToFilter()) { + resultStringBuffer.append(currentToken); + } + + System.out.println("resultStringBuffer;" + resultStringBuffer); + + { + currentToken = st.nextToken(); + state.increment(); + } + } + + return resultStringBuffer.toString(); + } + + private class STATE + { + + final static int STRING_NOT_TO_FILTER = 0; + final static int START_TOKEN = 1; + final static int STRING_TO_FILTER = 2; + final static int END_TOKEN = 3; + + private int currentState; + + STATE(int startState) { + this.currentState = startState; + } + + void increment() { + currentState++; + if (currentState > 3) + currentState = 0; + + } + + boolean isStringToFilter() { + if (currentState == STRING_TO_FILTER) { + return true; + } else { + return false; + } + } + + boolean isStringNOTToFilter() { + if (currentState == STRING_NOT_TO_FILTER) { + return true; + } else { + return false; + } + } + } + +} \ No newline at end of file Modified: xml/forrest/trunk/src/java/org/apache/forrest/conf/ForrestConfModule.java ============================================================================== --- xml/forrest/trunk/src/java/org/apache/forrest/conf/ForrestConfModule.java (original) +++ xml/forrest/trunk/src/java/org/apache/forrest/conf/ForrestConfModule.java Mon Apr 19 08:13:41 2004 @@ -17,8 +17,8 @@ import java.io.IOException; import java.io.InputStream; +import java.net.MalformedURLException; import java.util.Map; -import java.util.Properties; import org.apache.avalon.framework.activity.Initializable; import org.apache.avalon.framework.configuration.Configuration; @@ -27,91 +27,182 @@ import org.apache.avalon.framework.service.ServiceManager; import org.apache.avalon.framework.service.Serviceable; import org.apache.avalon.framework.thread.ThreadSafe; -import org.apache.cocoon.components.modules.input.AbstractJXPathModule; +import org.apache.cocoon.components.modules.input.DefaultsModule; 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.SourceResolver; /** - * @date 15-apr-2004 - **/ -public class ForrestConfModule { - /** - * Input module for accessing properties in a properties file - * roughly compatible with Ant property files, where ${name} - * is replaced with the value of the property 'name' if - * declared beforehand. - * - */ - public class AntPropertiesModule extends AbstractJXPathModule - implements InputModule, Serviceable, Initializable, ThreadSafe { - - private SourceResolver m_resolver; private Properties forrestProperties;; - - public void service(ServiceManager manager) throws ServiceException { - m_resolver = (SourceResolver) manager.lookup(SourceResolver.ROLE); - } - - public void initialize() throws Exception{ - // get forrest properties - - Source source = null; - InputStream in = null; - try { - source = m_resolver.resolveURI("forrest.properties"); - - if(source.exists()) { - - in = source.getInputStream(); - - forrestProperties = new AntProperties(); - forrestProperties.load(in); - } - else{ - forrestProperties = new Properties(); - } - } - catch (IOException e) { - throw new Exception("Cannot load forrest.properties file."); - } - finally { - if (source != null) { - m_resolver.release(source); - } - if (in != null) { - try { - in.close(); - } - catch (IOException e) { - } - } - } - - // get location of forrest home - - String forrestHome; - - try{ - forrestHome = System.getenv("FORREST_HOME"); - getLogger().debug("gotten forrestHome from FORREST_HOME, value:"+forrestHome); - } - catch(Exception e){ - //probably using a JDK with deprecated getEnv - forrestHome = System.getProperty("forrest.home","."); - getLogger().debug("gotten forrestHome from forrest.home, value:"+forrestHome); - } - - forrestProperties.put("rawforresthome",forrestHome); - forrestProperties.put("forresthome",m_resolver.resolveURI(forrestHome).getURI()); - } - - - protected Object getContextObject(Configuration modeConf, Map objectModel) - throws ConfigurationException { - - return forrestProperties; - } + * Input module for accessing the base properties used in Forrest. The main + * values are the locations of the source directories and of the + * forrest directories. The values are gotten from the + * + *
+ * forrest.properties
+ * 
+ * + * ant property file and resolved relative to the java system properties named + * + *
+ * forrest.home
+ * 
+ * + * and + * + *
+ * project.home
+ * 
, that the forrest scripts set using the calling directory and the + * environment variable + * + *
+ * FORREST_HOME
+ * 
. If Forrest is run from a war, it won't have these properties set, so + * the directories are resolved relative to the current directory, that in this + * case is the forrest webapp root. + */ +public class ForrestConfModule extends DefaultsModule implements InputModule, + Initializable, ThreadSafe, Serviceable +{ - } + private String forrestHome, projectHome; + private SourceResolver m_resolver; + private AntProperties filteringProperties; + + public void service(ServiceManager manager) throws ServiceException { + System.out.println("getting resolver..."); + m_resolver = (SourceResolver) manager.lookup(SourceResolver.ROLE); + System.out.println("gotten resolver:" + m_resolver); + } + + public void initialize() throws Exception { + // get location of forrest.home + // + //NOTE: Don't do this: + // + // forrestHome = System.getenv("FORREST_HOME"); + // + // as it will get FORREST_HOME even when the app + // is run as a .war + forrestHome = getAndResolveSystemProperty("forrest.home"); + + // get location of project.home + projectHome = getAndResolveSystemProperty("project.home"); + + // get default-forrest.properties and load the values + String defaultRorrestPropertiesStringURI = forrestHome + + SystemUtils.FILE_SEPARATOR + + "default-forrest.properties"; + System.out.println("defaultRorrestPropertiesStringURI:" + + defaultRorrestPropertiesStringURI); + + Source source = null; + InputStream in = null; + try { + System.out.println("using resolver:" + m_resolver); + source = m_resolver.resolveURI(defaultRorrestPropertiesStringURI); + + System.out.println("Resolved URI:" + source); + + in = source.getInputStream(); + filteringProperties = new AntProperties(); + filteringProperties.load(in); + + System.out.println("Loaded defaults:" + 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"; + System.out.println("forrestPropertiesStringURI:" + + forrestPropertiesStringURI); + + try { + System.out.println("using resolver:" + m_resolver); + source = m_resolver.resolveURI(forrestPropertiesStringURI); + + System.out.println("Resolved URI:" + source); + + in = source.getInputStream(); + filteringProperties = new AntProperties(filteringProperties); + filteringProperties.load(in); + } finally { + if (source != null) { + m_resolver.release(source); + } + if (in != null) { + try { + in.close(); + } catch (IOException e) {} + } + } + + System.out.println("Loaded project stuff:" + filteringProperties); + + //add forrest.home and project.home to properties + filteringProperties.setProperty("forrest.home", forrestHome); + filteringProperties.setProperty("project.home", projectHome); + + System.out.println("Loaded all:" + 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(); + + System.out.println(" - Requested:" + name); + System.out.println(" - Original:" + original); + System.out.println(" - Given:" + attributeValue); + return attributeValue; + } + + private final String getAndResolveSystemProperty(String propertyName) + throws MalformedURLException, IOException { + String raw = System.getProperty(propertyName, "."); + + if (getLogger().isDebugEnabled()) { + getLogger().debug("raw " + propertyName + "=" + raw); + + } + + System.out.println("raw " + propertyName + "=" + raw); + System.out.println("resolver: " + m_resolver); + + String value = m_resolver.resolveURI(raw).getURI(); + + if (getLogger().isDebugEnabled()) { + getLogger().debug("resolved " + propertyName + "=" + value); + + } + + return raw; + } } +