Clover coverage report -
Coverage timestamp: Do Okt 21 2004 12:21:23 CEST
file stats: LOC: 194   Methods: 10
NCLOC: 125   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
URLHandler.java 40% 54,2% 50% 51,9%
coverage coverage
 1    /*
 2    * ConfigurationFileLoader.java
 3    *
 4    * Created on 28. April 2002, 14:45
 5    */
 6   
 7    package org.jconfig.handler;
 8   
 9    import java.io.IOException;
 10    import java.io.InputStream;
 11    import java.io.OutputStreamWriter;
 12    import java.io.PrintWriter;
 13    import java.util.Properties;
 14   
 15    import javax.xml.parsers.DocumentBuilder;
 16    import javax.xml.parsers.DocumentBuilderFactory;
 17    import javax.xml.parsers.ParserConfigurationException;
 18   
 19    import org.jconfig.Configuration;
 20    import org.jconfig.ConfigurationManagerException;
 21    import org.jconfig.URLFileWatcher;
 22    import org.jconfig.error.ErrorReporter;
 23    import org.jconfig.event.FileListener;
 24    import org.jconfig.event.FileListenerEvent;
 25    import org.jconfig.parser.ConfigurationParser;
 26    import org.jconfig.parser.ConfigurationParserFactory;
 27    import org.jconfig.utils.ConfigErrorHandler;
 28    import org.w3c.dom.Document;
 29    import org.xml.sax.SAXException;
 30    import org.jconfig.utils.ConfigurationUtils;
 31    /**
 32    * This class will read the content from an URL and generate the
 33    * properties. If you have the need to use a proxy server, create
 34    * jconfig.properties file and place it inside your system path.
 35    * <BR />
 36    * jconfig.properties example:<BR />
 37    * # jconfig.properties file<BR />
 38    * http.proxyHost=proxy.server.url<BR />
 39    * http.proxyPort=3128
 40    *
 41    * @author Andreas Mecky andreas.mecky@xcom.de
 42    * @author Terry Dye terry.dye@xcom.de
 43    */
 44    public class URLHandler implements ConfigurationHandler, FileListener {
 45   
 46    private boolean validate = false;
 47    private String url;
 48   
 49    //Steve Braun - addition
 50    private URLFileWatcher watcher = null;
 51   
 52  1 public URLHandler() {
 53    }
 54   
 55  1 public void setURL( String url ) {
 56  1 this.url = url;
 57    }
 58   
 59    /**
 60    * This defines if the file will be vaidated using a DTD. The default
 61    * is false.
 62    *
 63    * @param validate defines the validation
 64    */
 65  0 public void setValidation(boolean validate) {
 66  0 this.validate = validate;
 67    }
 68   
 69    /**
 70    * @throws ConfigurationManagerException */
 71  0 public synchronized Configuration load() throws ConfigurationManagerException {
 72  0 Configuration c = load( url );
 73  0 addFileListener(this);
 74  0 return ( c );
 75    }
 76   
 77  0 public void fileChanged(FileListenerEvent event) {
 78  0 try {
 79    // TODO: we need the configname here!
 80  0 load();
 81    } catch (ConfigurationManagerException e) {
 82  0 ErrorReporter.getErrorHandler().reportError("Error while reloading",e);
 83    // technically we shouldn't get this, if we're already registered as a listener
 84    }
 85    }
 86   
 87   
 88    /**
 89    *
 90    *
 91    *@param theURL Description of the Parameter
 92    *@throws ConfigurationManagerException if the file cannot be processed
 93    */
 94  1 public synchronized Configuration load(String configName) throws ConfigurationManagerException {
 95  1 return load(getURL(), configName);
 96    }
 97   
 98  1 protected Configuration load(String theURL,String configName) throws ConfigurationManagerException {
 99  1 java.net.URL jcfURL = null;
 100  1 InputStream is = null;
 101  1 try {
 102    // get a jconfig.properties in classpath, if it exists
 103  1 ClassLoader cl = this.getClass().getClassLoader();
 104  1 InputStream jcf = cl.getResourceAsStream( "jconfig.properties" );
 105    // it is possible that the jconfig.properties does not exist, we get null
 106  1 if ( jcf != null ) {
 107  1 Properties jcfProperties = new Properties();
 108  1 jcfProperties.load( jcf );
 109   
 110    // load what is set in system
 111  1 Properties prop = System.getProperties();
 112    // if we see http.proxyHost and/or http.proxyPort inside
 113    // the jconfig.properties, we can set the System.properties
 114    // for use by the URLConnection object
 115  1 if ( jcfProperties.getProperty( "http.proxyHost" ) != null )
 116  1 prop.put( "http.proxyHost", jcfProperties.getProperty( "http.proxyHost" ) );
 117  1 if ( jcfProperties.getProperty( "http.proxyPort" ) != null )
 118  1 prop.put( "http.proxyPort", jcfProperties.getProperty( "http.proxyPort" ) );
 119    }
 120    // prepare URL, open the connection and grab stream for parsing
 121  1 jcfURL = new java.net.URL( theURL );
 122   
 123  1 java.net.URLConnection con = jcfURL.openConnection();
 124  1 is = con.getInputStream();
 125    }
 126    catch ( Exception e ) {
 127  0 ErrorReporter.getErrorHandler().reportError("Problem with URL handling/connection/validating",e);
 128  0 throw new ConfigurationManagerException( "Problem with URL handling/connection/validating: "
 129    + e.getMessage() );
 130    }
 131   
 132  1 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 133  1 dbf.setValidating(validate);
 134  1 DocumentBuilder db = null;
 135  1 try {
 136  1 db = dbf.newDocumentBuilder();
 137    } catch (ParserConfigurationException pce) {
 138  0 ErrorReporter.getErrorHandler().reportError("The parser cannot create a new document builder",pce);
 139  0 throw new ConfigurationManagerException("The parser cannot create a new document builder: " + pce.getMessage());
 140    }
 141    // if we have to validate then we need to define an error handler here
 142  1 if (validate) {
 143  0 try {
 144    // Set an ErrorHandler before parsing
 145  0 OutputStreamWriter errorWriter =
 146    new OutputStreamWriter(System.err, "UTF-8");
 147  0 db.setErrorHandler(
 148    new ConfigErrorHandler(new PrintWriter(errorWriter, true)));
 149    } catch (Exception e) {
 150  0 ErrorReporter.getErrorHandler().reportError("The parser cannot set up the error handler",e);
 151  0 throw new ConfigurationManagerException("The parser cannot set up the error handler");
 152    }
 153    }
 154  1 Document doc = null;
 155  1 try {
 156  1 doc = db.parse(is);
 157    } catch (SAXException se) {
 158  0 ErrorReporter.getErrorHandler().reportError("The parser cannot parse the XML",se);
 159  0 throw new ConfigurationManagerException("The parser cannot parse the XML: " + se.getMessage());
 160    } catch (IOException ioe) {
 161  0 ErrorReporter.getErrorHandler().reportError("The parser cannot open the file",ioe);
 162  0 throw new ConfigurationManagerException("The parser cannot open the file: " + ioe.getMessage());
 163    }
 164  1 ConfigurationParser parser = ConfigurationParserFactory.getParser(configName);
 165  1 Configuration config = parser.parse(doc, configName);
 166  1 config.resetCreated();
 167  1 return config;
 168    }
 169   
 170    /** This method should store all categories and properties.
 171    * @throws ConfigurationManagerException
 172    */
 173  0 public void store(Configuration configuration) throws ConfigurationManagerException {
 174    }
 175   
 176    //Steve Braun - addition
 177  0 public void addFileListener( FileListener fileListener ) {
 178  0 try {
 179  0 String val = ConfigurationUtils.getConfigProperty("jconfig.filewatcher","true");
 180  0 if ( val.equalsIgnoreCase("true") ) {
 181  0 watcher = new URLFileWatcher( getURL() );
 182  0 watcher.addFileListener( fileListener );
 183  0 watcher.start();
 184    }
 185    }
 186    catch ( Exception e ) {
 187  0 ErrorReporter.getErrorHandler().reportError("Problem while setting up the watcher",e);
 188    }
 189    }
 190   
 191  1 public String getURL() {
 192  1 return ( url );
 193    }
 194    }