    View Javadoc
    1   /*
    2    * Copyright (c) 2003
    3    * Information Desire GmbH
    4    * All rights reserved.
    5    */
    6   package com.infodesire.infobit.config;
    7
    8   import org.apache.commons.logging.Log;
    9   import org.apache.commons.logging.LogFactory;
    10
    11  import java.net.URL;
    12  import java.io.InputStreamReader;
    13  import java.io.InputStream;
    14  import java.io.IOException;
    15
    16  import java.util.Properties;
    17
    18  import javax.xml.parsers.SAXParserFactory;
    19  import javax.xml.parsers.SAXParser;
    20  import javax.xml.parsers.ParserConfigurationException;
    21
    22  import org.xml.sax.helpers.DefaultHandler;
    23  import org.xml.sax.SAXException;
    24  import org.xml.sax.Attributes;
    25
    26  import com.infodesire.infobit.InfobitConfiguration;
    27  import com.infodesire.infobit.InfobitPool;
    28  import com.infodesire.infobit.InfobitException;
    29
    30  /***
    31   * infobit config loader. configures given infobit manager from specified file
    32   * on classpath.
    33   *
    34   * @author    konstantin
    35   * @created   July 29, 2003
    36   * @version   $Revision: 1.4 $
    37   */
    38  public class InfobitConfig {
    39      private static Log _log = LogFactory.getLog(InfobitConfig.class);
    40
    41      private InfobitPool _pool;
    42      private InfobitConfiguration _configuration;
    43      private Properties _properties = new Properties();
    44
    45
    46      /***
    47       * configure manager instance from file
    48       *
    49       * @param file           Description of Parameter
    50       * @param configuration  Description of Parameter
    51       */
    52      public void configure(InfobitConfiguration configuration, String file) {
    53          if (_log.isDebugEnabled()) {
    54              _log.debug("loading from file: " + file);
    55          }
    56          _configuration = configuration;
    57          InputStream input = Thread.currentThread().getContextClassLoader().getResourceAsStream(file);
    58          if (input == null && !file.startsWith("/")) {
    59              input = Thread.currentThread().getContextClassLoader().getResourceAsStream("/" + file);
    60          }
    61          try {
    62              SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
    63              parser.parse(input, new ConfigHandler());
    64          } catch (Throwable t) {
    65              _log.error("unable to configure infobit manager", t);
    66              throw new RuntimeException(t);
    67          }
    68      }
    69
    70
    71      /***
    72       * SAX Handler implementation for handling tags in config file and building
    73       * config objects. ( server managers )
    74       *
    75       * @author    konstantin
    76       * @created   July 4, 2003
    77       * @version   $Revision: 1.4 $
    78       */
    79      private class ConfigHandler extends DefaultHandler {
    80          final static String POOL = "pool";
    81          final static String PROPERTY = "property";
    82          final static String NAME = "name";
    83          private String _currentPropertyName;
    84          private StringBuffer _currentPropertyValue;
    85          private String _className;
    86
    87
    88          /***
    89           * start element processing
    90           *
    91           * @param uri               Description of Parameter
    92           * @param localName         Description of Parameter
    93           * @param qName             Description of Parameter
    94           * @param attributes        Description of Parameter
    95           * @exception SAXException  Description of Exception
    96           */
    97          public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
    98              if (POOL.equals(qName)) {
    99                  _pool = new InfobitPool();
    100                 _properties.clear();
    101             }
    102             else if (PROPERTY.equals(qName)) {
    103                 _currentPropertyName = attributes.getValue("name");
    104                 _currentPropertyValue = new StringBuffer();
    105             }
    106         }
    107
    108
    109         /***
    110          * end element processing
    111          *
    112          * @param uri               Description of Parameter
    113          * @param localName         Description of Parameter
    114          * @param qName             Description of Parameter
    115          * @exception SAXException  Description of Exception
    116          */
    117         public void endElement(String uri, String localName, String qName) throws SAXException {
    118             if (POOL.equals(qName)) {
    119                 try {
    120                     _pool.init(_properties);
    121                 } catch (InfobitException ie) {
    122                     _log.error("could not create pool" + _pool.getName(), ie);
    123                     return;
    124                 }
    125                 _configuration.addPool(_pool);
    126
    127             }
    128             else if (PROPERTY.equals(qName)) {
    129                 _properties.put(_currentPropertyName, _currentPropertyValue.toString());
    130                 _currentPropertyName = null;
    131                 _currentPropertyValue = null;
    132             }
    133         }
    134
    135
    136         /***
    137          * process element contents
    138          *
    139          * @param chars             Description of Parameter
    140          * @param offset            Description of Parameter
    141          * @param len               Description of Parameter
    142          * @exception SAXException  Description of Exception
    143          */
    144         public void characters(char[] chars, int offset, int len) throws SAXException {
    145             if (_currentPropertyValue != null) {
    146                 _currentPropertyValue.append(chars, offset, len);
    147             }
    148         }
    149
    150     }
    151
    152 }

This page was automatically generated by Maven