1 /*------------------------------------------------------------------------------
  2 Name:      ConnectorFactory.java
  3 Project:   xmlBlaster.org
  4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
  5 ------------------------------------------------------------------------------*/
  6 package org.xmlBlaster.client.jmx;
  7 
  8 import javax.management.*;
  9 
 10 import java.lang.reflect.*;
 11 
 12 import java.util.logging.Logger;
 13 
 14 import org.xmlBlaster.util.Global;
 15 import java.util.TreeMap;
 16 import java.util.Iterator;
 17 
 18 public class ConnectorFactory  {
 19 
 20    private volatile static ConnectorFactory singletonFactory;
 21    private static Global applicationGlobal;
 22    private static Object syncObject = new Object();
 23 
 24    private Global global;
 25    private static Logger log = Logger.getLogger(ConnectorFactory.class.getName());
 26    private TreeMap childGlobals;
 27    private TreeMap servers;
 28 
 29    public static ConnectorFactory getInstance(Global global) {
 30       if (singletonFactory == null) {
 31          synchronized(syncObject) {
 32             if (singletonFactory == null) {
 33                singletonFactory = new ConnectorFactory(global);
 34                applicationGlobal = global;
 35             }
 36          }
 37       }
 38       if (global != applicationGlobal) {
 39          log.severe("getInstance: The global used for this invocation is not the same as the application global");
 40          Thread.dumpStack();
 41       }
 42       return singletonFactory;
 43    }
 44 
 45    private ConnectorFactory(Global global) {
 46       this.global = global;
 47 
 48       this.childGlobals = new TreeMap();
 49       this.servers = new TreeMap();
 50    }
 51 
 52 
 53    /**
 54     * Creates an instance of an AsyncMBeanServer. 
 55     * @param transport the string identifying the communication type. Currently only
 56     * xmlBlaster is supported.
 57     * @param serverName the name of the server to create. If null is passed, '127.0.0.1' is
 58     * assumed.
 59     */
 60    private synchronized AsyncMBeanServer addAsyncConnector(String transport, String serverName) 
 61       throws ConnectorException {
 62       AsyncMBeanServer server = null;
 63 
 64       if (transport.equalsIgnoreCase("xmlBlaster")) {
 65          Global childGlobal = this.global.getClone(null);
 66          this.childGlobals.put(serverName, childGlobal);
 67          try {
 68             server = (AsyncMBeanServer)Proxy.newProxyInstance(
 69                Thread.currentThread().getContextClassLoader(),
 70                new Class[] { AsyncMBeanServer.class },
 71                new XmlBlasterInvocationHandler(serverName, childGlobal));
 72             this.servers.put(serverName, server);
 73             return server;
 74          }
 75          catch (Exception e) {
 76             e.printStackTrace();
 77             throw new ConnectorException("Error connecting to xmlBlaster Service", e);
 78          }
 79       }
 80       else throw new ConnectorException("Unknown transport " + transport);
 81    }
 82 
 83    /** 
 84     * returns the async mbean server within this child global. It returns null if no one
 85     * has been added yet.
 86     */
 87    private AsyncMBeanServer getExistingAsyncConnector(String serverName) {
 88       return (AsyncMBeanServer)this.servers.get(serverName);
 89    }
 90 
 91    /**
 92     * Gets the async mbean server specified with the given name.
 93     * @param global is the parent global, i.e. the global configured on application start.
 94     */
 95    public AsyncMBeanServer getMBeanServer(String serverName) throws ConnectorException {
 96       String transport = "xmlBlaster"; // in future it could be passed in the args.
 97       AsyncMBeanServer server = getExistingAsyncConnector(serverName);
 98       try {
 99          if (server == null) {
100             server = addAsyncConnector(transport, serverName);
101             ObjectName requestBroker_name = new ObjectName(transport + ":name=requestBroker");
102             server.createMBean("org.xmlBlaster.engine.RequestBroker",requestBroker_name);
103          }
104       }
105       catch (Exception ex) {
106          log.severe("Exception occured in 'getMBeanServers': " + ex.getMessage());
107          ex.printStackTrace();
108          throw new ConnectorException("Error when retreiving mbean server ", ex);
109       }
110       return server;
111    }
112 
113 
114    public String[] getMBeanServerList() {
115       Iterator iter = this.servers.values().iterator();
116       String[] ret = new String[this.servers.size()];
117       int i=0;
118       try {
119          while (iter.hasNext()) {
120             AsyncMBeanServer server = (AsyncMBeanServer)iter.next();
121             ObjectName requestBroker_name = new ObjectName("xmlBlaster:name=requestBroker");
122             ret[i] = server.getAttribute(requestBroker_name,"NodeList").get().toString();
123             i++;
124          }
125       }
126       catch(Exception ex) {
127          log.severe("Exception occured in 'getMBeanServerList': " + ex.getMessage());
128          ex.printStackTrace();
129       }
130       return ret;
131    }
132 
133 }


syntax highlighted by Code2HTML, v. 0.9.1