KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > factories > UnitTestCacheFactory


1 /*
2  * JBoss, Home of Professional Open Source
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.cache.factories;
8
9 import java.io.InputStream JavaDoc;
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 JavaDoc;
20 import org.w3c.dom.NodeList JavaDoc;
21
22 /**
23  * Cache factory used by unit tests.
24  *
25  *
26  */

27 public class UnitTestCacheFactory
28 {
29    public static String JavaDoc JGROUPS_CHANNEL = "udp"; //use udp by default
30
public static String JavaDoc JGROUPS_STACK_TYPE = "jgroups.stack";
31    public static String JavaDoc 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 JavaDoc e)
92       {
93          if (e instanceof ConfigurationException) throw (ConfigurationException) e;
94          throw new RuntimeException JavaDoc(e);
95       }
96    }
97    
98    public static CacheLoaderConfig getSingleCacheLoaderConfig(String JavaDoc preload, String JavaDoc cacheloaderClass, String JavaDoc properties, boolean async, boolean fetchPersistentState, boolean shared) throws Exception JavaDoc
99    {
100       return getSingleCacheLoaderConfig(preload, cacheloaderClass, properties, async, fetchPersistentState, shared, false);
101    }
102
103    public static CacheLoaderConfig getSingleCacheLoaderConfig(String JavaDoc preload, String JavaDoc cacheloaderClass, String JavaDoc properties, boolean async, boolean fetchPersistentState, boolean shared, boolean purgeOnStartup) throws Exception JavaDoc
104    {
105       return getSingleCacheLoaderConfig(false, preload, cacheloaderClass, properties, async, fetchPersistentState, shared, purgeOnStartup);
106    }
107
108    protected static CacheLoaderConfig getSingleCacheLoaderConfig(boolean passivation, String JavaDoc preload, String JavaDoc cacheloaderClass, String JavaDoc properties, boolean async, boolean fetchPersistentState, boolean shared, boolean purgeOnStartup) throws Exception JavaDoc
109    {
110       String JavaDoc 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 JavaDoc 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 JavaDoc filename, CacheMode mode)
130       {
131          return parseStream(getAsInputStreamFromClassLoader(DEFAULT_CONFIGURATION_FILE),mode);
132       }
133       
134       public Configuration parseStream(InputStream JavaDoc stream,CacheMode mode)
135       {
136           // loop through all elements in XML.
137
if (stream == null) throw new ConfigurationException("Input stream for configuration xml is null!");
138
139           Element JavaDoc root= XmlHelper.getDocumentRoot(stream);
140           Element JavaDoc mbeanElement= getMBeanElement(root);
141
142           ParsedAttributes attributes = extractAttributes(mbeanElement);
143           
144           // Special handling for the old separate property for
145
// eviction policy -- just cache it and use with the eviction XML
146
String JavaDoc defaultEvictionClass = (String JavaDoc) attributes.stringAttribs.remove("EvictionPolicyClass");
147
148           // Deal with rename of the old property that controlled MBean registration
149
String JavaDoc keepStats = (String JavaDoc) 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           // Special handling for XML elements -- we hard code the parsing
158
setXmlValues(c, attributes.xmlAttribs, defaultEvictionClass);
159           
160           Element JavaDoc list = (Element JavaDoc) root.getElementsByTagName("protocol_stacks").item(0);
161           NodeList JavaDoc stacks = list.getElementsByTagName("stack");
162           
163           for (int i = 0; i < stacks.getLength(); i++)
164           {
165              Element JavaDoc stack = (Element JavaDoc) stacks.item(i);
166              String JavaDoc stackName = stack.getAttribute("name");
167              if (stackName.startsWith(JGROUPS_CHANNEL))
168              {
169                 Element JavaDoc jgroupsStack = (Element JavaDoc) 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