Coverage Report - de.jollyday.configuration.ConfigurationProviderManager
 
Classes in this File Line Coverage Branch Coverage Complexity
ConfigurationProviderManager
100%
30/30
80%
8/10
2,5
 
 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;
 17  
 
 18  
 import java.util.Properties;
 19  
 import java.util.logging.Logger;
 20  
 
 21  
 import de.jollyday.ManagerParameter;
 22  
 import de.jollyday.configuration.impl.DefaultConfigurationProvider;
 23  
 import de.jollyday.configuration.impl.URLConfigurationProvider;
 24  
 import de.jollyday.util.ClassLoadingUtil;
 25  
 
 26  
 /**
 27  
  * Manages the configuration provider implementations and thus delivering the
 28  
  * jollyday configuration.
 29  
  * 
 30  
  * @author sven
 31  
  */
 32  
 public class ConfigurationProviderManager {
 33  
 
 34  1
         private static final Logger LOG = Logger.getLogger(ConfigurationProviderManager.class.getName());
 35  
 
 36  6
         private ConfigurationProvider defaultConfigurationProvider = new DefaultConfigurationProvider();
 37  6
         private ConfigurationProvider urlConfigurationProvider = new URLConfigurationProvider();
 38  6
         private transient ClassLoadingUtil classLoadingUtil = new ClassLoadingUtil();
 39  
 
 40  
         /**
 41  
          * Constructs an instance
 42  
          */
 43  6
         public ConfigurationProviderManager() {
 44  6
         }
 45  
 
 46  
         /**
 47  
          * Reads the jollyday configuration from the
 48  
          * {@link DefaultConfigurationProvider}, the
 49  
          * {@link URLConfigurationProvider} and any configuration provider specified
 50  
          * by the system property 'de.jollyday.config.provider'.
 51  
          * 
 52  
          * @param parameter
 53  
          *            the configuration {@link ManagerParameter} to use
 54  
          */
 55  
         public void mergeConfigurationProperties(ManagerParameter parameter) {
 56  1259
                 addInternalConfigurationProviderProperies(parameter);
 57  1266
                 addCustomConfigurationProviderProperties(parameter);
 58  1267
         }
 59  
 
 60  
         private void addInternalConfigurationProviderProperies(ManagerParameter parameter) {
 61  1258
                 parameter.mergeProperties(urlConfigurationProvider.getProperties());
 62  1265
                 parameter.mergeProperties(defaultConfigurationProvider.getProperties());
 63  1267
         }
 64  
 
 65  
         private void addCustomConfigurationProviderProperties(ManagerParameter parameter) {
 66  1267
                 Properties systemProps = System.getProperties();
 67  1267
                 String providersStrList = systemProps.getProperty(ConfigurationProvider.CONFIG_PROVIDERS_PROPERTY);
 68  1268
                 if (providersStrList != null) {
 69  4
                         String[] providersClassNames = providersStrList.split(",");
 70  4
                         if (providersClassNames != null) {
 71  9
                                 for (String providerClassName : providersClassNames) {
 72  5
                                         if (providerClassName == null || "".equals(providerClassName))
 73  1
                                                 continue;
 74  
                                         try {
 75  8
                                                 Class<?> providerClass = Class.forName(providerClassName.trim(), true,
 76  4
                                                                 classLoadingUtil.getClassloader());
 77  8
                                                 ConfigurationProvider configurationProvider = ConfigurationProvider.class.cast(providerClass
 78  4
                                                                 .newInstance());
 79  2
                                                 parameter.mergeProperties(configurationProvider.getProperties());
 80  2
                                         } catch (Exception e) {
 81  4
                                                 LOG.warning("Cannot load configuration from provider class '" + providerClassName + "'. "
 82  2
                                                                 + e.getClass().getSimpleName() + " (" + e.getMessage() + ").");
 83  2
                                         }
 84  
                                 }
 85  
                         }
 86  
                 }
 87  1267
         }
 88  
 
 89  
 }