Coverage Report - de.jollyday.configuration.impl.URLConfigurationProvider
 
Classes in this File Line Coverage Branch Coverage Complexity
URLConfigurationProvider
93%
27/29
83%
10/12
4
 
 1  
 /**
 2  
  * Copyright 2012 Sven Diedrichsen 
 3  
  * 
 4  
  * Licensed under the Apache License, Version 2.0 (the "License"); 
 5  
  * you may not use this file except in compliance with the License. 
 6  
  * You may obtain a copy of the License at 
 7  
  * 
 8  
  * http://www.apache.org/licenses/LICENSE-2.0 
 9  
  * 
 10  
  * Unless required by applicable law or agreed to in writing, software 
 11  
  * distributed under the License is distributed on an 
 12  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either 
 13  
  * express or implied. See the License for the specific language 
 14  
  * governing permissions and limitations under the License. 
 15  
  */
 16  
 package de.jollyday.configuration.impl;
 17  
 
 18  
 import java.io.InputStream;
 19  
 import java.net.URL;
 20  
 import java.util.Properties;
 21  
 import java.util.logging.Level;
 22  
 import java.util.logging.Logger;
 23  
 
 24  
 import de.jollyday.configuration.ConfigurationProvider;
 25  
 
 26  
 /**
 27  
  * An {@link ConfigurationProvider} implementation which reads a list of URLs
 28  
  * provided by the system property 'de.jollyday.config.urls' in order they are
 29  
  * provided into a {@link Properties} instance.
 30  
  * 
 31  
  * @author Sven Diedrichsen
 32  
  */
 33  10
 public class URLConfigurationProvider implements ConfigurationProvider {
 34  
 
 35  2
         private static final Logger LOG = Logger
 36  1
                         .getLogger(URLConfigurationProvider.class.getName());
 37  
 
 38  
         /**
 39  
          * Returns the properties by reading from the URLs provided by the system
 40  
          * property 'de.jollyday.config.urls'.
 41  
          */
 42  
         @Override
 43  
         public Properties getProperties() {
 44  1257
                 Properties properties = new Properties();
 45  1260
                 Properties systemProps = System.getProperties();
 46  1260
                 String configURLs = systemProps.getProperty(CONFIG_URLS_PROPERTY);
 47  1267
                 if (configURLs != null) {
 48  1235
                         String[] strConfigURLs = configURLs.split(",");
 49  1235
                         if (strConfigURLs != null) {
 50  2467
                                 for (String strURL : strConfigURLs) {
 51  1232
                                         readPropertiesFromURL(properties, strURL);
 52  
                                 }
 53  
                         }
 54  
                 }
 55  1264
                 return properties;
 56  
         }
 57  
 
 58  
         private void readPropertiesFromURL(Properties properties, String strURL) {
 59  1228
                 if (strURL == null || "".equals(strURL))
 60  1
                         return;
 61  1228
                 InputStream inputStream = null;
 62  
                 try {
 63  1227
                         URL configURL = new URL(strURL.trim());
 64  1228
                         inputStream = configURL.openStream();
 65  1225
                         properties.load(inputStream);
 66  1
                 } catch (Exception e) {
 67  1
                         LOG.log(Level.WARNING, "Cannot read configuration from '" + strURL
 68  
                                         + "'.", e);
 69  
                 } finally {
 70  1233
                         closeStreamFromURL(strURL, inputStream);
 71  1231
                 }
 72  1231
         }
 73  
 
 74  
         private void closeStreamFromURL(String strURL, InputStream inputStream) {
 75  1233
                 if (inputStream != null) {
 76  
                         try {
 77  1232
                                 inputStream.close();
 78  0
                         } catch (Exception e) {
 79  0
                                 LOG.warning("Cannot close stream for configuration URL "
 80  
                                                 + strURL + ".");
 81  1230
                         }
 82  
                 }
 83  1231
         }
 84  
 
 85  
 }