1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| package org.jboss.cache.factories; |
8 |
| |
9 |
| import org.jboss.cache.config.CacheLoaderConfig; |
10 |
| import org.jboss.cache.config.Configuration; |
11 |
| import org.jboss.cache.config.Configuration.CacheMode; |
12 |
| import org.jboss.cache.config.ConfigurationException; |
13 |
| import org.jboss.cache.xml.XmlHelper; |
14 |
| import org.w3c.dom.Element; |
15 |
| import org.w3c.dom.NodeList; |
16 |
| |
17 |
| import java.io.InputStream; |
18 |
| |
19 |
| |
20 |
| |
21 |
| |
22 |
| public class UnitTestCacheConfigurationFactory |
23 |
| { |
24 |
| public static String JGROUPS_CHANNEL = "udp"; |
25 |
| public static String JGROUPS_STACK_TYPE = "jgroups.stack"; |
26 |
| public static String DEFAULT_CONFIGURATION_FILE = "META-INF/unit-test-cache-service.xml"; |
27 |
| |
28 |
| static |
29 |
| { |
30 |
75
| JGROUPS_CHANNEL = System.getProperty(JGROUPS_STACK_TYPE, JGROUPS_CHANNEL);
|
31 |
| } |
32 |
| |
33 |
649
| public static Configuration createConfiguration(CacheMode mode) throws ConfigurationException
|
34 |
| { |
35 |
649
| return createConfiguration(mode, false, false);
|
36 |
| } |
37 |
| |
38 |
11
| public static Configuration createConfiguration(CacheMode mode, boolean useEviction) throws ConfigurationException
|
39 |
| { |
40 |
11
| return createConfiguration(mode, useEviction, false);
|
41 |
| } |
42 |
| |
43 |
660
| public static Configuration createConfiguration(CacheMode mode, boolean useEviction, boolean usePassivation) throws ConfigurationException
|
44 |
| { |
45 |
660
| UnitTestXmlConfigurationParser parser = new UnitTestXmlConfigurationParser();
|
46 |
660
| Configuration c = parser.parseFile(DEFAULT_CONFIGURATION_FILE, mode);
|
47 |
| |
48 |
660
| if (!useEviction)
|
49 |
| { |
50 |
649
| c.setEvictionConfig(null);
|
51 |
| } |
52 |
| |
53 |
660
| if (!usePassivation)
|
54 |
| { |
55 |
660
| c.setCacheLoaderConfig(null);
|
56 |
| } |
57 |
| |
58 |
660
| return c;
|
59 |
| } |
60 |
| |
61 |
0
| public static CacheLoaderConfig getSingleCacheLoaderConfig(String preload, String cacheloaderClass, String properties, boolean async, boolean fetchPersistentState, boolean shared) throws Exception
|
62 |
| { |
63 |
0
| return getSingleCacheLoaderConfig(preload, cacheloaderClass, properties, async, fetchPersistentState, shared, false);
|
64 |
| } |
65 |
| |
66 |
0
| public static CacheLoaderConfig getSingleCacheLoaderConfig(String preload, String cacheloaderClass, String properties, boolean async, boolean fetchPersistentState, boolean shared, boolean purgeOnStartup) throws Exception
|
67 |
| { |
68 |
0
| return getSingleCacheLoaderConfig(false, preload, cacheloaderClass, properties, async, fetchPersistentState, shared, purgeOnStartup);
|
69 |
| } |
70 |
| |
71 |
0
| protected static CacheLoaderConfig getSingleCacheLoaderConfig(boolean passivation, String preload, String cacheloaderClass, String properties, boolean async, boolean fetchPersistentState, boolean shared, boolean purgeOnStartup) throws Exception
|
72 |
| { |
73 |
0
| String xml = "<config>\n" +
|
74 |
| "<passivation>" + passivation + "</passivation>\n" + |
75 |
| "<preload>" + preload + "</preload>\n" + |
76 |
| "<cacheloader>\n" + |
77 |
| "<class>" + cacheloaderClass + "</class>\n" + |
78 |
| "<properties>" + properties + "</properties>\n" + |
79 |
| "<async>" + async + "</async>\n" + |
80 |
| "<shared>" + shared + "</shared>\n" + |
81 |
| "<fetchPersistentState>" + fetchPersistentState + "</fetchPersistentState>\n" + |
82 |
| "<purgeOnStartup>" + purgeOnStartup + "</purgeOnStartup>\n" + |
83 |
| "</cacheloader>\n" + |
84 |
| "</config>"; |
85 |
0
| Element element = XmlHelper.stringToElement(xml);
|
86 |
0
| return XmlConfigurationParser.parseCacheLoaderConfig(element);
|
87 |
| } |
88 |
| |
89 |
| private static class UnitTestXmlConfigurationParser extends XmlConfigurationParser |
90 |
| { |
91 |
| |
92 |
660
| public Configuration parseFile(String filename, CacheMode mode)
|
93 |
| { |
94 |
660
| return parseStream(getAsInputStreamFromClassLoader(DEFAULT_CONFIGURATION_FILE), mode);
|
95 |
| } |
96 |
| |
97 |
660
| public Configuration parseStream(InputStream stream, CacheMode mode)
|
98 |
| { |
99 |
| |
100 |
0
| if (stream == null) throw new ConfigurationException("Input stream for configuration xml is null!");
|
101 |
| |
102 |
660
| Element root = XmlHelper.getDocumentRoot(stream);
|
103 |
660
| Element mbeanElement = getMBeanElement(root);
|
104 |
| |
105 |
660
| ParsedAttributes attributes = extractAttributes(mbeanElement);
|
106 |
| |
107 |
| |
108 |
660
| String keepStats = attributes.stringAttribs.remove("UseMbean");
|
109 |
660
| if (keepStats != null && attributes.stringAttribs.get("ExposeManagementStatistics") == null)
|
110 |
| { |
111 |
0
| attributes.stringAttribs.put("ExposeManagementStatistics", keepStats);
|
112 |
| } |
113 |
| |
114 |
660
| Configuration c = new Configuration();
|
115 |
660
| setValues(c, attributes.stringAttribs, false);
|
116 |
| |
117 |
660
| setXmlValues(c, attributes.xmlAttribs);
|
118 |
| |
119 |
660
| Element list = (Element) root.getElementsByTagName("protocol_stacks").item(0);
|
120 |
660
| NodeList stacks = list.getElementsByTagName("stack");
|
121 |
| |
122 |
660
| for (int i = 0; i < stacks.getLength(); i++)
|
123 |
| { |
124 |
1165
| Element stack = (Element) stacks.item(i);
|
125 |
1165
| String stackName = stack.getAttribute("name");
|
126 |
1165
| if (stackName.startsWith(JGROUPS_CHANNEL))
|
127 |
| { |
128 |
1161
| Element jgroupsStack = (Element) stack.getElementsByTagName("config").item(0);
|
129 |
1161
| if (mode == CacheMode.REPL_ASYNC && !stackName.contains("-"))
|
130 |
| { |
131 |
159
| c.setClusterConfig(jgroupsStack);
|
132 |
159
| c.setCacheMode(CacheMode.REPL_ASYNC);
|
133 |
159
| break;
|
134 |
| } |
135 |
1002
| else if (mode == CacheMode.REPL_SYNC && stackName.contains("-"))
|
136 |
| { |
137 |
499
| c.setClusterConfig(jgroupsStack);
|
138 |
499
| c.setCacheMode(CacheMode.REPL_SYNC);
|
139 |
499
| break;
|
140 |
| } |
141 |
| } |
142 |
| } |
143 |
660
| return c;
|
144 |
| } |
145 |
| } |
146 |
| } |