1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| |
16 |
| |
17 |
| |
18 |
| |
19 |
| |
20 |
| |
21 |
| |
22 |
| package org.jboss.cache.multiplexer; |
23 |
| |
24 |
| import org.jboss.cache.Cache; |
25 |
| import org.jgroups.ChannelFactory; |
26 |
| import org.jgroups.JChannel; |
27 |
| import org.jgroups.JChannelFactory; |
28 |
| import org.w3c.dom.Document; |
29 |
| import org.w3c.dom.Element; |
30 |
| |
31 |
| import javax.xml.parsers.DocumentBuilder; |
32 |
| import javax.xml.parsers.DocumentBuilderFactory; |
33 |
| import java.util.Collections; |
34 |
| import java.util.HashSet; |
35 |
| import java.util.Set; |
36 |
| import java.util.StringTokenizer; |
37 |
| |
38 |
| |
39 |
| |
40 |
| |
41 |
| |
42 |
| |
43 |
| |
44 |
| |
45 |
| public class MultiplexerTestHelper |
46 |
| { |
47 |
| public static final String MUX_STACK = "jbc-test"; |
48 |
| |
49 |
| private final Set factories = Collections.synchronizedSet(new HashSet()); |
50 |
| private final Set caches = Collections.synchronizedSet(new HashSet()); |
51 |
| |
52 |
| |
53 |
36
| public MultiplexerTestHelper()
|
54 |
| { |
55 |
| } |
56 |
| |
57 |
| |
58 |
| |
59 |
| |
60 |
| |
61 |
| |
62 |
| |
63 |
| |
64 |
| |
65 |
| |
66 |
69
| public void configureCacheForMux(Cache cache) throws Exception
|
67 |
| { |
68 |
69
| synchronized (caches)
|
69 |
| { |
70 |
69
| ChannelFactory factory = createMuxChannelFactory(cache);
|
71 |
69
| cache.getConfiguration().getRuntimeConfig().setMuxChannelFactory(factory);
|
72 |
69
| cache.getConfiguration().setMultiplexerStack(MUX_STACK);
|
73 |
| } |
74 |
| } |
75 |
| |
76 |
| |
77 |
| |
78 |
| |
79 |
| |
80 |
| |
81 |
| |
82 |
| |
83 |
| |
84 |
| |
85 |
| |
86 |
69
| public ChannelFactory createMuxChannelFactory(Cache cache) throws Exception
|
87 |
| { |
88 |
69
| return createMuxChannelFactory(getChannelProperties(cache));
|
89 |
| } |
90 |
| |
91 |
69
| private String getChannelProperties(Cache cache)
|
92 |
| { |
93 |
69
| String props = cache.getConfiguration().getClusterConfig();
|
94 |
69
| return (props == null ? JChannel.DEFAULT_PROTOCOL_STACK : props);
|
95 |
| } |
96 |
| |
97 |
| |
98 |
| |
99 |
| |
100 |
| |
101 |
| |
102 |
| |
103 |
| |
104 |
| |
105 |
| |
106 |
| |
107 |
69
| public ChannelFactory createMuxChannelFactory(String muxConfig) throws Exception
|
108 |
| { |
109 |
69
| synchronized (factories)
|
110 |
| { |
111 |
69
| JChannelFactory factory = new JChannelFactory();
|
112 |
69
| factory.setDomain("jbc.mux.test");
|
113 |
69
| factory.setExposeChannels(false);
|
114 |
69
| factory.setMultiplexerConfig(getClusterConfigElement(muxConfig));
|
115 |
| |
116 |
69
| factories.add(factory);
|
117 |
| |
118 |
69
| return factory;
|
119 |
| } |
120 |
| } |
121 |
| |
122 |
| |
123 |
| |
124 |
| |
125 |
| |
126 |
| |
127 |
| |
128 |
| |
129 |
| |
130 |
70
| public static Element getClusterConfigElement(String clusterConfig) throws Exception
|
131 |
| { |
132 |
70
| clusterConfig = clusterConfig.trim();
|
133 |
70
| DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
|
134 |
70
| Document doc = db.newDocument();
|
135 |
70
| Element top = doc.createElement("protocol_stacks");
|
136 |
70
| doc.appendChild(top);
|
137 |
70
| Element stack = doc.createElement("stack");
|
138 |
70
| stack.setAttribute("name", MUX_STACK);
|
139 |
70
| top.appendChild(stack);
|
140 |
70
| Element config = doc.createElement("config");
|
141 |
| |
142 |
70
| StringTokenizer outer = new StringTokenizer(clusterConfig, ":");
|
143 |
70
| while (outer.hasMoreTokens())
|
144 |
| { |
145 |
875
| String protocol = outer.nextToken();
|
146 |
875
| String protName = protocol;
|
147 |
875
| String attribs = null;
|
148 |
875
| int nameEnd = protocol.indexOf('(');
|
149 |
875
| if (nameEnd > 0)
|
150 |
| { |
151 |
804
| protName = protocol.substring(0, nameEnd);
|
152 |
804
| attribs = protocol.substring(nameEnd + 1, protocol.length() - 1);
|
153 |
| } |
154 |
875
| Element element = doc.createElement(protName);
|
155 |
875
| if (attribs != null && attribs.length() > 0)
|
156 |
| { |
157 |
804
| StringTokenizer inner = new StringTokenizer(attribs, ";");
|
158 |
804
| while (inner.hasMoreTokens())
|
159 |
| { |
160 |
3724
| String attrib = inner.nextToken();
|
161 |
3724
| int eq = attrib.indexOf('=');
|
162 |
3724
| String name = attrib.substring(0, eq);
|
163 |
3724
| String value = attrib.substring(eq + 1);
|
164 |
3724
| element.setAttribute(name, value);
|
165 |
| } |
166 |
| } |
167 |
875
| config.appendChild(element);
|
168 |
| } |
169 |
| |
170 |
70
| stack.appendChild(config);
|
171 |
70
| return top;
|
172 |
| } |
173 |
| |
174 |
| |
175 |
| |
176 |
| |
177 |
| |
178 |
36
| public void tearDown()
|
179 |
| { |
180 |
36
| factories.clear();
|
181 |
36
| caches.clear();
|
182 |
| } |
183 |
| |
184 |
| |
185 |
| |
186 |
| |
187 |
| |
188 |
| |
189 |
0
| public static void main(String[] args)
|
190 |
| { |
191 |
0
| MultiplexerTestHelper helper = new MultiplexerTestHelper();
|
192 |
0
| try
|
193 |
| { |
194 |
0
| helper.createMuxChannelFactory(JChannel.DEFAULT_PROTOCOL_STACK);
|
195 |
| } |
196 |
| catch (Exception e) |
197 |
| { |
198 |
0
| e.printStackTrace();
|
199 |
| } |
200 |
| finally |
201 |
| { |
202 |
0
| helper.tearDown();
|
203 |
| } |
204 |
| } |
205 |
| } |