Coverage Report - de.jollyday.caching.HolidayManagerValueHandler
 
Classes in this File Line Coverage Branch Coverage Complexity
HolidayManagerValueHandler
89%
17/19
N/A
1,75
 
 1  
 package de.jollyday.caching;
 2  
 
 3  
 import de.jollyday.HolidayManager;
 4  
 import de.jollyday.ManagerParameter;
 5  
 import de.jollyday.datasource.ConfigurationDataSource;
 6  
 import de.jollyday.datasource.ConfigurationDataSourceManager;
 7  
 import de.jollyday.util.Cache;
 8  
 import de.jollyday.util.ClassLoadingUtil;
 9  
 
 10  
 /**
 11  
  * Creates the {@link Cache.ValueHandler} which constructs a {@link HolidayManager}.
 12  
  */
 13  76
 public class HolidayManagerValueHandler implements Cache.ValueHandler<HolidayManager> {
 14  
 
 15  
         private ManagerParameter parameter;
 16  
         private String managerImplClassName;
 17  
 
 18  
         /**
 19  
          * Manager for providing configuration data sources which return the holiday
 20  
          * data.
 21  
          */
 22  1260
         private ConfigurationDataSourceManager configurationDataSourceManager = new ConfigurationDataSourceManager();
 23  
 
 24  
         /**
 25  
          * Utility to load classes.
 26  
          */
 27  1261
         private ClassLoadingUtil classLoadingUtil = new ClassLoadingUtil();
 28  
 
 29  1260
         public HolidayManagerValueHandler(final ManagerParameter parameter, final String managerImplClassName) {
 30  1263
                 this.parameter = parameter;
 31  1260
                 this.managerImplClassName = managerImplClassName;
 32  1260
         }
 33  
 
 34  
         @Override
 35  
         public String getKey() {
 36  1259
                 return parameter.createCacheKey();
 37  
         }
 38  
 
 39  
         @Override
 40  
         public HolidayManager createValue() {
 41  76
                 HolidayManager manager = instantiateManagerImpl(managerImplClassName);
 42  76
                 ConfigurationDataSource configurationDataSource = configurationDataSourceManager.getConfigurationDataSource(parameter);
 43  76
                 manager.setConfigurationDataSource(configurationDataSource);
 44  76
                 manager.init(parameter);
 45  74
                 return manager;
 46  
         }
 47  
 
 48  
         /**
 49  
          * Instantiates the manager implementing class.
 50  
          *
 51  
          * @param managerImplClassName
 52  
          *            the managers class name
 53  
          * @return the implementation class instantiated
 54  
          */
 55  
         private HolidayManager instantiateManagerImpl(
 56  
                         String managerImplClassName) {
 57  
                 try {
 58  76
                         Class<?> managerImplClass = classLoadingUtil
 59  76
                                         .loadClass(managerImplClassName);
 60  76
                         Object managerImplObject = managerImplClass.newInstance();
 61  76
                         return HolidayManager.class.cast(managerImplObject);
 62  0
                 } catch (Exception e) {
 63  0
                         throw new IllegalStateException("Cannot create manager class "
 64  
                                         + managerImplClassName, e);
 65  
                 }
 66  
         }
 67  
 
 68  
 
 69  
 }