Clover coverage report -
Coverage timestamp: Thu Jul 5 2007 20:02:32 EDT
file stats: LOC: 205   Methods: 8
NCLOC: 114   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
MultiplexerTestHelper.java 90% 90.2% 87.5% 89.9%
coverage coverage
 1    /*
 2    * JBoss, Home of Professional Open Source.
 3    * Copyright 2006, Red Hat Middleware LLC, and individual contributors
 4    * as indicated by the @author tags. See the copyright.txt file in the
 5    * distribution for a full listing of individual contributors.
 6    *
 7    * This is free software; you can redistribute it and/or modify it
 8    * under the terms of the GNU Lesser General Public License as
 9    * published by the Free Software Foundation; either version 2.1 of
 10    * the License, or (at your option) any later version.
 11    *
 12    * This software is distributed in the hope that it will be useful,
 13    * but WITHOUT ANY WARRANTY; without even the implied warranty of
 14    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 15    * Lesser General Public License for more details.
 16    *
 17    * You should have received a copy of the GNU Lesser General Public
 18    * License along with this software; if not, write to the Free
 19    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 20    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 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    * Utility class that can associate a cache with a multiplexer-enabled
 40    * JGroups ChannelFactory.
 41    *
 42    * @author <a href="brian.stansberry@jboss.com">Brian Stansberry</a>
 43    * @version $Revision: 1.4 $
 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    * Configures the given cache to get its JChannel from a
 59    * multiplexer-enabled JChannelFactory. The JChannelFactory will
 60    * produce MuxChannels configured with the same protocol stack as
 61    * whatever the provided cache is configured with.
 62    *
 63    * @param cache the cache
 64    * @throws Exception
 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    * Creates a JChannelFactory. The JChannelFactory will
 78    * produce MuxChannels configured with the same protocol stack as
 79    * whatever the provided cache is configured with.
 80    *
 81    * @param cache the cache from which the protocol stack config should
 82    * be obtained
 83    * @return the channel factory.
 84    * @throws Exception
 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    * Creates a JChannelFactory. The JChannelFactory will
 99    * produce MuxChannels configured according to the given
 100    * protocol stack(s).
 101    *
 102    * @param muxConfig Element that looks like the root element
 103    * of a multiplexer stacks.xml file.
 104    * @return the channel factory.
 105    * @throws Exception
 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    * Converts an old-style JGroups protocol stack config string to an Element
 124    * that looks like the root element of a multiplexer stacks.xml file.
 125    *
 126    * @param clusterConfig
 127    * @return
 128    * @throws Exception
 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    * Performs cleanup work. Once this method is invoked, this
 176    * object should no longer be used.
 177    */
 178  36 public void tearDown()
 179    {
 180  36 factories.clear();
 181  36 caches.clear();
 182    }
 183   
 184    /**
 185    * Tests creation of a channel factory.
 186    *
 187    * @param args
 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    }