incubator-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ran...@apache.org
Subject cvs commit: incubator-ftpserver/src/java/org/apache/ftpserver/config PropertiesConfiguration.java XmlConfiguration.java XmlConfigurationHandler.java
Date Wed, 07 Sep 2005 05:05:58 GMT
rana_b      2005/09/06 22:05:58

  Added:       src/java/org/apache/ftpserver/config
                        PropertiesConfiguration.java XmlConfiguration.java
                        XmlConfigurationHandler.java
  Log:
  Refactored - removed avalon entirely
  
  Revision  Changes    Path
  1.1                  incubator-ftpserver/src/java/org/apache/ftpserver/config/PropertiesConfiguration.java
  
  Index: PropertiesConfiguration.java
  ===================================================================
  // $Id: PropertiesConfiguration.java,v 1.1 2005/09/07 05:05:57 rana_b Exp $
  /*
   * Copyright 2004 The Apache Software Foundation
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  package org.apache.ftpserver.config;
  
  import java.io.IOException;
  import java.io.InputStream;
  import java.util.Enumeration;
  import java.util.Properties;
  
  import org.apache.ftpserver.ftplet.Configuration;
  import org.apache.ftpserver.ftplet.FtpException;
  
  /**
   * Properties based configuration.
   * 
   * @author <a href="mailto:rana_b@yahoo.com">Rana Bhattacharyya</a>
   */
  public 
  class PropertiesConfiguration implements Configuration {
      
      private final static String PREFIX = "config.";
      
      private Properties m_prop;
      
      
      /**
       * Constructor - set the properties input stream.
       */
      public PropertiesConfiguration(InputStream in) throws IOException {
          
          // load properties
          Properties prop = new Properties();
          prop.load(in);
          
          // strip prefix
          m_prop = new Properties();
          int prefixLen = PREFIX.length();
          Enumeration keys = prop.propertyNames();
          while(keys.hasMoreElements()) {
              String key = (String)keys.nextElement();
              String val = prop.getProperty(key);
              if(key.startsWith(PREFIX)) {
                  key = key.substring(prefixLen);
              }
              m_prop.setProperty(key, val);
          }
          prop.clear();
      }    
      
      /**
       * Constructor - set the properties.
       */
      private PropertiesConfiguration(Properties prop) {
          m_prop = prop;
      }    
      
      /**
       * Get string - if not found throws FtpException.
       */
      public String getString(String param) throws FtpException {
          String val = m_prop.getProperty(param);
          if(val == null) {
              throw new FtpException("Not found : " + param);
          }
          return val;
      }
      
      /**
       * Get string - if not found returns the default value.
       */
      public String getString(String param, String defaultVal) {
          return m_prop.getProperty(param, defaultVal);
      }
      
      /**
       * Get integer - if not found throws FtpException.
       */
      public int getInt(String param) throws FtpException {
          String val = m_prop.getProperty(param);
          if(val == null) {
              throw new FtpException("Not found : " + param);
          }
          
          try {
              return Integer.parseInt(val);
          }
          catch(Exception ex) {
              throw new FtpException("PropertiesConfiguration.getInt()", ex);
          }
      }
      
      /**
       * Get int - if not found returns the default value.
       */
      public int getInt(String param, int defaultVal) {
          int retVal = defaultVal;
          try {
              retVal = getInt(param);
          }
          catch(Exception ex) {
          }
          return retVal;
      }
      
      /**
       * Get long - if not found throws FtpException.
       */
      public long getLong(String param) throws FtpException {
          String val = m_prop.getProperty(param);
          if(val == null) {
              throw new FtpException("Not found : " + param);
          }
          
          try {
              return Long.parseLong(val);
          }
          catch(Exception ex) {
              throw new FtpException("PropertiesConfiguration.getLong()", ex);
          }
      }
      
      /**
       * Get long - if not found returns the default value.
       */
      public long getLong(String param, long defaultVal) {
          long retVal = defaultVal;
          try {
              retVal = getLong(param);
          }
          catch(Exception ex) {
          }
          return retVal;
      }
      
      /**
       * Get boolean - if not found throws FtpException.
       */
      public boolean getBoolean(String param) throws FtpException {
          String val = m_prop.getProperty(param);
          if(val == null) {
              throw new FtpException("Not found : " + param);
          }
          return val.equalsIgnoreCase("true");
      }
      
      /**
       * Get boolean - if not found returns the default value.
       */
      public boolean getBoolean(String param, boolean defaultVal) {
          boolean retVal = defaultVal;
          try {
              retVal = getBoolean(param);
          }
          catch(Exception ex) {
          }
          return retVal;
      }
      
      /**
       * Get double - if not found throws FtpException.
       */
      public double getDouble(String param) throws FtpException {
          String val = m_prop.getProperty(param);
          if(val == null) {
              throw new FtpException("Not found : " + param);
          }
          
          try {
              return Double.parseDouble(val);
          }
          catch(Exception ex) {
              throw new FtpException("PropertiesConfiguration.getDouble()", ex);
          }
      }
      
      /**
       * Get double - if not found returns the default value.
       */
      public double getDouble(String param, double defaultVal) {
          double retVal = defaultVal;
          try {
              retVal = getDouble(param);
          }
          catch(Exception ex) {
          }
          return retVal;
      }
      
      /**
       * Get sub configuration - if not found throws FtpException.
       */
      public Configuration getConfiguration(String param) throws FtpException {
          Properties prop = new Properties();
          Enumeration propNames = m_prop.propertyNames();
          String prefix = param + '.';
          int prefixLen = prefix.length();
          while(propNames.hasMoreElements()) {
              String key = (String)propNames.nextElement();
              if(!key.startsWith(prefix)) {
                  continue;
              }
              String val = m_prop.getProperty(key);
              key = key.substring(prefixLen);
              prop.setProperty(key, val);
          }
          
          if(prop.isEmpty()) {
              throw new FtpException("Not found : " + param);
          }
          return new PropertiesConfiguration(prop);
      }
      
      /**
       * Get sub configuration - if not found returns the default value.
       */
      public Configuration getConfiguration(String param, Configuration defaultVal) {
          Configuration conf = defaultVal;
          try {
              conf = getConfiguration(param);
          }
          catch(Exception ex) {
          }
          return conf;
      }
  }
  
  
  
  1.1                  incubator-ftpserver/src/java/org/apache/ftpserver/config/XmlConfiguration.java
  
  Index: XmlConfiguration.java
  ===================================================================
  // $Id: XmlConfiguration.java,v 1.1 2005/09/07 05:05:57 rana_b Exp $
  /*
   * Copyright 2004 The Apache Software Foundation
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  package org.apache.ftpserver.config;
  
  import org.apache.ftpserver.ftplet.Configuration;
  import org.apache.ftpserver.ftplet.FtpException;
  
  import java.util.ArrayList;
  import java.util.Iterator;
  
  /**
   * XML based configuration element.
   * 
   * @author <a href="mailto:rana_b@yahoo.com">Rana Bhattacharyya</a>
   */
  public
  class XmlConfiguration implements Configuration {
      
      private String m_name;
      private String m_value;
      private ArrayList m_children;
      
      /**
       * Create a new <code>XmlConfiguration</code> instance.
       */
      public XmlConfiguration(String name) {
          m_name = name;
      }
      
      /**
       * Returns the name of this element.
       */
      String getName() {
          return m_name;
      }
      
      /**
       * Return count of children.
       */
      int getChildCount() {
          if( null == m_children ) {
              return 0;
          }
          return m_children.size();
      }
      
      /**
       * Set data to the value of this element.
       */
      void setValue(String value) {
          m_value = value;
      }
      
      /**
       * Add a child <code>XmlConfiguration</code> to this element.
       */
      void addChild(XmlConfiguration elem) {
          if(m_children == null) {
              m_children = new ArrayList();
          }
  
          m_children.add( elem );
      }
      
      /**
       * Get string - if not found throws FtpException.
       */
      public String getString(String param) throws FtpException {
          XmlConfiguration child = (XmlConfiguration)getConfiguration(param);
          String val = child.m_value;
          if(val == null) {
              throw new FtpException("Not found : " + param);
          }
          return val;
      }
      
      /**
       * Get string - if not found returns the default value.
       */
      public String getString(String param, String defaultVal) {
          try {
              return getString(param);
          }
          catch(Exception ex) {
              return defaultVal;
          }
      }
      
      
      /**
       * Get integer - if not found throws FtpException.
       */
      public int getInt(String param) throws FtpException {
          String val = getString(param);
          try {
              return Integer.parseInt(val);
          }
          catch(Exception ex) {
              throw new FtpException("XmlConfiguration.getInt()", ex);
          }
      }
      
      /**
       * Get integer - if not found returns the default value.
       */
      public int getInt(String param, int defaultVal) {
          int retVal = defaultVal;
          try {
              retVal = getInt(param);
          }
          catch(Exception ex) {
          }
          return retVal;
      }
      
      /**
       * Get long - if not found throws FtpException.
       */
      public long getLong(String param) throws FtpException {
          String val = getString(param);
          try {
              return Long.parseLong(val);
          }
          catch(Exception ex) {
              throw new FtpException("XmlConfiguration.getLong()", ex);
          }
      }
      
      /**
       * Get long - if not found returns the default value.
       */
      public long getLong(String param, long defaultVal) {
          long retVal = defaultVal;
          try {
              retVal = getLong(param);
          }
          catch(Exception ex) {
          }
          return retVal;
      }
      
      /**
       * Get boolean - if not found throws FtpException.
       */
      public boolean getBoolean(String param) throws FtpException {
          String val = getString(param);
          return val.equalsIgnoreCase("true");
      }
      
      /**
       * Get boolean - if not found returns the default value.
       */
      public boolean getBoolean(String param, boolean defaultVal) {
          boolean retVal = defaultVal;
          try {
              retVal = getBoolean(param);
          }
          catch(Exception ex) {
          }
          return retVal;
      }
      
      /**
       * Get double - if not found throws FtpException.
       */
      public double getDouble(String param) throws FtpException {
          String val = getString(param);
          try {
              return Double.parseDouble(val);
          }
          catch(Exception ex) {
              throw new FtpException("XmlConfiguration.getDouble()", ex);
          }
      }
      
      /**
       * Get double - if not found returns the default value.
       */
      public double getDouble(String param, double defaultVal) {
          double retVal = defaultVal;
          try {
              retVal = getDouble(param);
          }
          catch(Exception ex) {
          }
          return retVal;
      }
      
      /**
       * Return the first <code>XmlConfiguration</code> object child of this
       * associated with the given name. 
       */
      public Configuration getConfiguration(String param) throws FtpException {
          XmlConfiguration child = null;
          
          if(m_children != null) {
              for(Iterator it=m_children.iterator(); it.hasNext(); ) {
                  XmlConfiguration thisChild = (XmlConfiguration)it.next();
                  if( thisChild.m_name.equals(param) ) {
                      child = thisChild;
                      break;
                  }
              }
          } 
          
          if(child == null) {
              throw new FtpException("Not found : " + param);
          }
          return child;
      }
  
      /**
       * Return the first <code>XmlConfiguration</code> object child of this
       * associated with the given name. 
       */
      public Configuration getConfiguration(String param, Configuration defVal) {
          Configuration config = null;
          try {
              config = getConfiguration(param);
          }
          catch(Exception ex) {
              config = defVal;
          }
          if(config == null) {
              config = defVal;
          }
          return config;
      }
  }
  
  
  
  1.1                  incubator-ftpserver/src/java/org/apache/ftpserver/config/XmlConfigurationHandler.java
  
  Index: XmlConfigurationHandler.java
  ===================================================================
  // $Id: XmlConfigurationHandler.java,v 1.1 2005/09/07 05:05:57 rana_b Exp $
  /*
   * Copyright 2004 The Apache Software Foundation
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  package org.apache.ftpserver.config;
  
  import java.io.InputStream;
  import java.util.ArrayList;
  
  import javax.xml.parsers.SAXParser;
  import javax.xml.parsers.SAXParserFactory;
  
  import org.xml.sax.Attributes;
  import org.xml.sax.InputSource;
  import org.xml.sax.SAXException;
  import org.xml.sax.SAXParseException;
  import org.xml.sax.XMLReader;
  import org.xml.sax.helpers.DefaultHandler;
  
  /**
   * A SAXHandler helps to build XmlConfiguration out of sax events.
   * 
   * @author <a href="mailto:rana_b@yahoo.com">Rana Bhattacharyya</a>
   */
  public 
  class XmlConfigurationHandler extends DefaultHandler {
  
      private ArrayList m_elements = new ArrayList();
      private XmlConfiguration m_root = null;
      private InputSource m_source = null;
      private StringBuffer m_elemVal = new StringBuffer(128);
      
      
      /**
       * Constructor.
       * @param is xml input stream
       */
      public XmlConfigurationHandler(InputStream is) {
          m_source = new InputSource(is);
      }
      
      /**
       * Get parser instance
       */
      protected XMLReader getParser() throws Exception {                                 
         
          SAXParserFactory factory = SAXParserFactory.newInstance();
          SAXParser parser = factory.newSAXParser();
          return parser.getXMLReader();
      }
      
      /**
       * Parse the input to create xml configuration
       */
      public XmlConfiguration parse() throws Exception {                               
          if(m_root != null) {
              return m_root;
          }
          
          XMLReader xmlreader = getParser();
          xmlreader.setContentHandler(this);
          xmlreader.setErrorHandler(this);
          xmlreader.parse(m_source); 
          
          return m_root;
      }
  
      /**
       * Update last element value.
       */
      public void characters(char[] ch, int start, int length) throws SAXException {
          m_elemVal.append(ch, start, length);
      }
  
      /**
       * Remove the last element from stack.
       */
      public void endElement(String uri,
                             String lname,
                             String qname) throws SAXException {
          
          int location = m_elements.size() - 1;
          XmlConfiguration lastElem = (XmlConfiguration)m_elements.remove(location);
  
          if(lastElem.getChildCount() == 0) {
              String elemVal = m_elemVal.toString().trim();
              
              if(elemVal.equals("")) {
                  lastElem.setValue(null);
              }
              else {
                  lastElem.setValue(elemVal);
              }
          }
          else {
              lastElem.setValue(null);
          }
          
          if(location == 0) {
              m_root = lastElem;
          }
      }
  
      /**
       * New element. Set element attributes and push into stack.
       */
      public void startElement(String uri,
                               String lname,
                               String qname,
                               Attributes attrs ) throws SAXException {
          
          XmlConfiguration element = new XmlConfiguration(qname); 
          int lastIdx = m_elements.size() - 1;
          m_elemVal.setLength(0);
  
          if(lastIdx > -1) {
              XmlConfiguration parent = (XmlConfiguration)m_elements.get(lastIdx);
              parent.setValue(null);
              parent.addChild(element);
          }
          m_elements.add(element);
      }
  
      /**
       * Handle parsing error.
       */
      public void error(SAXParseException exception) throws SAXParseException {
          throw exception;
      }
          
      /**
       * Handle parsing error.
       */
      public void fatalError(SAXParseException exception) throws SAXParseException {
          throw exception;
      }
      
      /**
       * Handle parsing warning.
       */
      public void warning(SAXParseException exception) throws SAXParseException {
          throw exception;
      }
  }
  
  
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: cvs-unsubscribe@incubator.apache.org
For additional commands, e-mail: cvs-help@incubator.apache.org


Mime
View raw message