1 7 package org.jboss.cache.factories; 8 9 import java.io.InputStream ; 10 11 import org.jboss.cache.Cache; 12 import org.jboss.cache.CacheImpl; 13 import org.jboss.cache.config.CacheLoaderConfig; 14 import org.jboss.cache.config.Configuration; 15 import org.jboss.cache.config.ConfigurationException; 16 import org.jboss.cache.config.EvictionConfig; 17 import org.jboss.cache.config.Configuration.CacheMode; 18 import org.jboss.cache.xml.XmlHelper; 19 import org.w3c.dom.Element ; 20 import org.w3c.dom.NodeList ; 21 22 27 public class UnitTestCacheFactory 28 { 29 public static String JGROUPS_CHANNEL = "udp"; public static String JGROUPS_STACK_TYPE = "jgroups.stack"; 31 public static String DEFAULT_CONFIGURATION_FILE = "META-INF/unit-test-cache-service.xml"; 32 33 static 34 { 35 JGROUPS_CHANNEL = System.getProperty(JGROUPS_STACK_TYPE, JGROUPS_CHANNEL); 36 } 37 38 39 public static Cache createCache() throws ConfigurationException 40 { 41 return createCache(new Configuration()); 42 } 43 44 public static Cache createCache(CacheMode mode, boolean start) throws ConfigurationException 45 { 46 return createCache(createConfiguration(mode), start); 47 } 48 49 public static Configuration createConfiguration(CacheMode mode) throws ConfigurationException 50 { 51 return createConfiguration(mode, false, false); 52 } 53 54 public static Configuration createConfiguration(CacheMode mode, boolean useEviction) throws ConfigurationException 55 { 56 return createConfiguration(mode, useEviction, false); 57 } 58 59 public static Configuration createConfiguration(CacheMode mode, boolean useEviction, boolean usePassivation) throws ConfigurationException 60 { 61 UnitTestXmlConfigurationParser parser = new UnitTestXmlConfigurationParser(); 62 Configuration c = parser.parseFile(DEFAULT_CONFIGURATION_FILE,mode); 63 64 if(!useEviction) 65 { 66 c.setEvictionConfig(null); 67 } 68 69 if(!usePassivation) 70 { 71 c.setCacheLoaderConfig(null); 72 } 73 74 return c; 75 } 76 77 public static Cache createCache(Configuration configuration) throws ConfigurationException 78 { 79 return createCache(configuration, true); 80 } 81 82 public static Cache createCache(Configuration configuration, boolean start) throws ConfigurationException 83 { 84 try 85 { 86 CacheImpl cache = new CacheImpl(); 87 cache.setConfiguration(configuration); 88 if (start) cache.start(); 89 return cache; 90 } 91 catch (Exception e) 92 { 93 if (e instanceof ConfigurationException) throw (ConfigurationException) e; 94 throw new RuntimeException (e); 95 } 96 } 97 98 public static CacheLoaderConfig getSingleCacheLoaderConfig(String preload, String cacheloaderClass, String properties, boolean async, boolean fetchPersistentState, boolean shared) throws Exception 99 { 100 return getSingleCacheLoaderConfig(preload, cacheloaderClass, properties, async, fetchPersistentState, shared, false); 101 } 102 103 public static CacheLoaderConfig getSingleCacheLoaderConfig(String preload, String cacheloaderClass, String properties, boolean async, boolean fetchPersistentState, boolean shared, boolean purgeOnStartup) throws Exception 104 { 105 return getSingleCacheLoaderConfig(false, preload, cacheloaderClass, properties, async, fetchPersistentState, shared, purgeOnStartup); 106 } 107 108 protected static CacheLoaderConfig getSingleCacheLoaderConfig(boolean passivation, String preload, String cacheloaderClass, String properties, boolean async, boolean fetchPersistentState, boolean shared, boolean purgeOnStartup) throws Exception 109 { 110 String xml = "<config>\n" + 111 "<passivation>" + passivation + "</passivation>\n" + 112 "<preload>" + preload + "</preload>\n" + 113 "<cacheloader>\n" + 114 "<class>" + cacheloaderClass + "</class>\n" + 115 "<properties>" + properties + "</properties>\n" + 116 "<async>" + async + "</async>\n" + 117 "<shared>" + shared + "</shared>\n" + 118 "<fetchPersistentState>" + fetchPersistentState + "</fetchPersistentState>\n" + 119 "<purgeOnStartup>" + purgeOnStartup + "</purgeOnStartup>\n" + 120 "</cacheloader>\n" + 121 "</config>"; 122 Element element = XmlHelper.stringToElement(xml); 123 return XmlConfigurationParser.parseCacheLoaderConfig(element); 124 } 125 126 private static class UnitTestXmlConfigurationParser extends XmlConfigurationParser 127 { 128 129 public Configuration parseFile(String filename, CacheMode mode) 130 { 131 return parseStream(getAsInputStreamFromClassLoader(DEFAULT_CONFIGURATION_FILE),mode); 132 } 133 134 public Configuration parseStream(InputStream stream,CacheMode mode) 135 { 136 if (stream == null) throw new ConfigurationException("Input stream for configuration xml is null!"); 138 139 Element root= XmlHelper.getDocumentRoot(stream); 140 Element mbeanElement= getMBeanElement(root); 141 142 ParsedAttributes attributes = extractAttributes(mbeanElement); 143 144 String defaultEvictionClass = (String ) attributes.stringAttribs.remove("EvictionPolicyClass"); 147 148 String keepStats = (String ) attributes.stringAttribs.remove("UseMbean"); 150 if (keepStats != null && attributes.stringAttribs.get("ExposeManagementStatistics") == null) 151 { 152 attributes.stringAttribs.put("ExposeManagementStatistics", keepStats); 153 } 154 155 Configuration c = new Configuration(); 156 setValues(c, attributes.stringAttribs, false); 157 setXmlValues(c, attributes.xmlAttribs, defaultEvictionClass); 159 160 Element list = (Element ) root.getElementsByTagName("protocol_stacks").item(0); 161 NodeList stacks = list.getElementsByTagName("stack"); 162 163 for (int i = 0; i < stacks.getLength(); i++) 164 { 165 Element stack = (Element ) stacks.item(i); 166 String stackName = stack.getAttribute("name"); 167 if (stackName.startsWith(JGROUPS_CHANNEL)) 168 { 169 Element jgroupsStack = (Element ) stack.getElementsByTagName("config").item(0); 170 if(mode == CacheMode.REPL_ASYNC && !stackName.contains("-")) 171 { 172 c.setClusterConfig(jgroupsStack); 173 c.setCacheMode(CacheMode.REPL_ASYNC); 174 break; 175 } 176 else if (mode == CacheMode.REPL_SYNC && stackName.contains("-")) 177 { 178 c.setClusterConfig(jgroupsStack); 179 c.setCacheMode(CacheMode.REPL_SYNC); 180 break; 181 } 182 } 183 } 184 return c; 185 } 186 } 187 } 188 | Popular Tags |