1 22 package org.jboss.cache.multiplexer; 23 24 import java.util.Collections ; 25 import java.util.HashSet ; 26 import java.util.Iterator ; 27 import java.util.Set ; 28 import java.util.StringTokenizer ; 29 30 import javax.management.MBeanServer ; 31 import javax.management.MBeanServerFactory ; 32 import javax.management.ObjectName ; 33 import javax.xml.parsers.DocumentBuilder ; 34 import javax.xml.parsers.DocumentBuilderFactory ; 35 36 import org.apache.commons.logging.Log; 37 import org.apache.commons.logging.LogFactory; 38 import org.jboss.cache.Cache; 39 import org.jgroups.JChannel; 40 import org.jgroups.JChannelFactory; 41 import org.jgroups.jmx.JChannelFactoryMBean; 42 import org.w3c.dom.Document ; 43 import org.w3c.dom.Element ; 44 45 52 public class MultiplexerTestHelper 53 { 54 private static final Log log = LogFactory.getLog(MultiplexerTestHelper.class); 55 56 public static final String MUX_STACK = "jbc-test"; 57 private static final String FACTORY_OBJECT_NAME_BASE = "jboss.cache:service=MuxChannelFactory,count="; 58 59 private MBeanServer mbeanServer; 60 private final Set factories = Collections.synchronizedSet(new HashSet ()); 61 private final Set caches = Collections.synchronizedSet(new HashSet ()); 62 63 64 public MultiplexerTestHelper() 65 { 66 mbeanServer = 67 MBeanServerFactory.createMBeanServer(); 68 } 69 70 79 public void configureCacheForMux(Cache cache) throws Exception 80 { 81 synchronized (caches) 82 { 83 ObjectName on = createMuxChannelFactory(cache); 84 cache.getConfiguration().setMultiplexerService(on.getCanonicalName()); 85 cache.getConfiguration().setMultiplexerStack(MUX_STACK); 86 cache.getConfiguration().getRuntimeConfig().setMbeanServer(mbeanServer); 87 } 91 } 92 93 102 public void configureCacheForMuxViaDirectInjection(Cache cache) throws Exception 103 { 104 synchronized (caches) 105 { 106 String props = getChannelProperties(cache); 107 108 JChannelFactoryMBean mbean = (JChannelFactoryMBean) createMuxChannelFactory(props, false); 109 cache.getConfiguration().getRuntimeConfig().setMuxChannelFactory(mbean); 110 cache.getConfiguration().setMultiplexerStack(MUX_STACK); 111 } 112 } 113 114 126 public ObjectName createMuxChannelFactory(Cache cache) throws Exception 127 { 128 return createMuxChannelFactory(getChannelProperties(cache)); 129 } 130 131 private String getChannelProperties(Cache cache) 132 { 133 String props = cache.getConfiguration().getClusterConfig(); 134 return (props == null ? JChannel.DEFAULT_PROTOCOL_STACK : props); 135 } 136 137 149 public ObjectName createMuxChannelFactory(String muxConfig) throws Exception 150 { 151 return (ObjectName ) createMuxChannelFactory(muxConfig, true); 152 } 153 154 private Object createMuxChannelFactory(String muxConfig, boolean returnObjectName) 155 throws Exception 156 { 157 synchronized (factories) 158 { 159 JChannelFactory factory = new JChannelFactory(); 160 factory.setDomain("jbc.mux.test"); 161 factory.setExposeChannels(false); 162 factory.setMultiplexerConfig(getClusterConfigElement(muxConfig)); 163 164 JChannelFactoryMBean muxFactory = new org.jgroups.jmx.JChannelFactory(factory); 165 ObjectName on = new ObjectName (FACTORY_OBJECT_NAME_BASE + factories.size()); 167 mbeanServer.registerMBean(muxFactory, on); 168 169 factories.add(on); 170 171 muxFactory.create(); 172 muxFactory.start(); 173 174 return returnObjectName ? on : muxFactory; 175 } 176 } 177 178 186 public static Element getClusterConfigElement(String clusterConfig) throws Exception 187 { 188 clusterConfig = clusterConfig.trim(); 189 DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 190 Document doc = db.newDocument(); 191 Element top = doc.createElement("protocol_stacks"); 192 doc.appendChild(top); 193 Element stack = doc.createElement("stack"); 194 stack.setAttribute("name", MUX_STACK); 195 top.appendChild(stack); 196 Element config = doc.createElement("config"); 197 198 StringTokenizer outer = new StringTokenizer (clusterConfig, ":"); 199 while (outer.hasMoreTokens()) 200 { 201 String protocol = outer.nextToken(); 202 String protName = protocol; 203 String attribs = null; 204 int nameEnd = protocol.indexOf('('); 205 if (nameEnd > 0) 206 { 207 protName = protocol.substring(0, nameEnd); 208 attribs = protocol.substring(nameEnd + 1, protocol.length() -1); 209 } 210 Element element = doc.createElement(protName); 211 if (attribs != null && attribs.length() > 0) 212 { 213 StringTokenizer inner = new StringTokenizer (attribs, ";"); 214 while (inner.hasMoreTokens()) 215 { 216 String attrib = inner.nextToken(); 217 int eq = attrib.indexOf('='); 218 String name = attrib.substring(0, eq); 219 String value = attrib.substring(eq +1); 220 element.setAttribute(name, value); 221 } 222 } 223 config.appendChild(element); 224 } 225 226 stack.appendChild(config); 227 return top; 228 } 229 230 235 public void tearDown() 236 { 237 try 238 { 239 for (Iterator it = caches.iterator(); it.hasNext(); ) 240 { 241 try 242 { 243 mbeanServer.unregisterMBean((ObjectName ) it.next()); 244 } 245 catch (Exception e) 246 { 247 log.error(e); 248 } 249 } 250 251 for (Iterator it = factories.iterator(); it.hasNext(); ) 252 { 253 try 254 { 255 mbeanServer.unregisterMBean((ObjectName ) it.next()); 256 } 257 catch (Exception e) 258 { 259 log.error(e); 260 } 261 } 262 } 263 catch (Exception e) 264 { 265 log.error(e); 266 } 267 finally 268 { 269 factories.clear(); 270 caches.clear(); 271 272 if (mbeanServer != null) 273 { 274 MBeanServerFactory.releaseMBeanServer(mbeanServer); 275 mbeanServer = null; 276 } 277 278 279 } 280 } 281 282 285 protected void finalize() throws Throwable 286 { 287 if (mbeanServer != null) 288 { 289 MBeanServerFactory.releaseMBeanServer(mbeanServer); 290 } 291 super.finalize(); 292 } 293 294 299 public static void main(String [] args) 300 { 301 MultiplexerTestHelper helper = new MultiplexerTestHelper(); 302 try 303 { 304 helper.createMuxChannelFactory(JChannel.DEFAULT_PROTOCOL_STACK); 305 } 306 catch (Exception e) 307 { 308 e.printStackTrace(); 309 } 310 finally 311 { 312 helper.tearDown(); 313 } 314 } 315 } 316 | Popular Tags |