1 17 18 package org.sape.carbon.core.config.cache; 19 20 import java.lang.reflect.Constructor ; 21 import java.lang.reflect.InvocationTargetException ; 22 23 import org.sape.carbon.core.bootstrap.BootStrapper; 24 import org.sape.carbon.core.config.ConfigurationService; 25 import org.sape.carbon.core.exception.ExceptionUtility; 26 27 import org.apache.commons.logging.Log; 28 import org.apache.commons.logging.LogFactory; 29 30 42 public class ConfigurationCacheFactory { 43 44 45 private static Log log = 46 LogFactory.getLog(ConfigurationCacheFactory.class); 47 48 49 public static final String CACHE_TYPE_PROPERTY_NAME = 50 "carbon.config.CacheType"; 51 52 58 public static ConfigurationCache getInstance( 59 ConfigurationService configService) { 60 61 ConfigurationCache cache = 62 new SynchronizedConfigurationCache(configService); 63 64 String cacheTypeName = 65 BootStrapper.getInstance().getDeploymentProperty( 66 ConfigurationCacheFactory.CACHE_TYPE_PROPERTY_NAME); 67 68 if (cacheTypeName != null) { 69 try { 71 72 Class cacheType = Class.forName(cacheTypeName); 73 Constructor cacheConstructor = cacheType.getConstructor( 74 new Class [] {ConfigurationService.class}); 75 cache = (ConfigurationCache) cacheConstructor.newInstance( 76 new Object [] {configService}); 77 78 } catch (ClassCastException cce) { 79 if (log.isWarnEnabled()) { 80 log.warn("Specifed Configuration cache [" 81 + cacheTypeName 82 + "], was not of type " 83 + ConfigurationCache.class.getName() 84 + ", using default"); 85 } 86 } catch (NoSuchMethodException nsme) { 87 if (log.isWarnEnabled()) { 88 log.warn("Specifed Configuration cache [" 89 + cacheTypeName 90 + "], does not have a single parameter " 91 + "constructor of type " 92 + ConfigurationService.class.getName() 93 + ", using default"); 94 } 95 } catch (ClassNotFoundException cnfe) { 96 if (log.isWarnEnabled()) { 97 log.warn("Specifed Configuration cache [" 98 + cacheTypeName 99 + "], was not found, using default"); 100 } 101 } catch (InstantiationException ie) { 102 if (log.isWarnEnabled()) { 103 log.warn("Caught InstantiationException " 104 + "instantiating class [" 105 + cacheTypeName 106 + "], using default"); 107 } 108 } catch (IllegalAccessException iae) { 109 if (log.isWarnEnabled()) { 110 log.warn("Caught IllegalAccessException " 111 + "instantiating class [" 112 + cacheTypeName 113 + "], using default"); 114 } 115 } catch (InvocationTargetException ite) { 116 if (log.isWarnEnabled()) { 117 log.warn("Caught exception instantiating class [" 118 + cacheTypeName 119 + "], using default: " 120 + ExceptionUtility.printStackTracesToString( 121 ite.getTargetException())); 122 } 123 } 124 } 125 126 if (log.isInfoEnabled()) { 127 log.info("Using instance of " + cache.getClass().getName() 128 + " to cache fetched configurations "); 129 } 130 131 return cache; 132 } 133 } 134 | Popular Tags |