1 7 package org.jboss.cache.factories; 8 9 import org.apache.commons.logging.Log; 10 import org.apache.commons.logging.LogFactory; 11 import org.jboss.cache.Cache; 12 import org.jboss.cache.CacheImpl; 13 import org.jboss.cache.config.Configuration; 14 import org.jboss.cache.config.ConfigurationException; 15 16 21 public class DefaultCacheFactory implements CacheFactory 22 { 23 private static CacheFactory singleton = null; 24 private Log log = LogFactory.getLog(DefaultCacheFactory.class); 25 26 29 protected DefaultCacheFactory() 30 { 31 } 33 34 public static CacheFactory getInstance() 35 { 36 if (singleton == null) 37 { 38 synchronized (DefaultCacheFactory.class) 39 { 40 if (singleton == null) 41 { 42 singleton = new DefaultCacheFactory(); 43 } 44 } 45 } 46 return singleton; 47 } 48 49 public Cache createCache() throws ConfigurationException 50 { 51 return createCache(new Configuration(), true, false); 53 } 54 55 public Cache createCache(String configFileName) throws ConfigurationException 56 { 57 return createCache(configFileName, true); 58 } 59 60 public Cache createCache(String configFileName, boolean start) throws ConfigurationException 61 { 62 XmlConfigurationParser parser = new XmlConfigurationParser(); 63 Configuration c = parser.parseFile(configFileName); 64 return createCache(c, start, false); 66 } 67 68 75 public Cache createCache(Configuration configuration) throws ConfigurationException 76 { 77 return createCache(configuration, true, true); 79 } 80 81 89 public Cache createCache(Configuration configuration, boolean start) throws ConfigurationException 90 { 91 return createCache(configuration, start, true); 94 } 95 96 105 private Cache createCache(Configuration configuration, boolean start, boolean cloneConfiguration) throws ConfigurationException 106 { 107 Configuration toUse = configuration; 108 if (cloneConfiguration) 109 { 110 try 111 { 112 toUse = configuration.clone(); 113 } 114 catch (CloneNotSupportedException e) 115 { 116 log.error("Unable to clone configuration?", e); 117 } 118 } 119 try 120 { 121 CacheImpl cache = new CacheImpl(); 122 cache.setConfiguration(toUse); 123 if (start) cache.start(); 124 return cache; 125 } 126 catch (Exception e) 127 { 128 if (e instanceof ConfigurationException) throw (ConfigurationException) e; 129 throw new RuntimeException (e); 130 } 131 } 132 } 133 | Popular Tags |