1 17 package org.apache.activemq.gbean.management; 18 19 import java.util.ArrayList ; 20 import java.util.Iterator ; 21 import java.util.List ; 22 import java.util.Set ; 23 24 import org.apache.activemq.gbean.ActiveMQBroker; 25 import org.apache.activemq.gbean.ActiveMQConnector; 26 import org.apache.activemq.gbean.ActiveMQManager; 27 import org.apache.activemq.gbean.TransportConnectorGBeanImpl; 28 import org.apache.commons.logging.Log; 29 import org.apache.commons.logging.LogFactory; 30 import org.apache.geronimo.gbean.AbstractName; 31 import org.apache.geronimo.gbean.AbstractNameQuery; 32 import org.apache.geronimo.gbean.GBeanData; 33 import org.apache.geronimo.gbean.GBeanInfo; 34 import org.apache.geronimo.gbean.GBeanInfoBuilder; 35 import org.apache.geronimo.gbean.ReferencePatterns; 36 import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory; 37 import org.apache.geronimo.kernel.GBeanNotFoundException; 38 import org.apache.geronimo.kernel.Kernel; 39 import org.apache.geronimo.kernel.config.ConfigurationUtil; 40 import org.apache.geronimo.kernel.config.EditableConfigurationManager; 41 import org.apache.geronimo.kernel.config.InvalidConfigException; 42 import org.apache.geronimo.kernel.proxy.ProxyManager; 43 import org.apache.geronimo.management.geronimo.JMSBroker; 44 import org.apache.geronimo.management.geronimo.JMSConnector; 45 import org.apache.geronimo.management.geronimo.NetworkConnector; 46 47 53 public class ActiveMQManagerGBean implements ActiveMQManager { 54 private static final Log log = LogFactory.getLog(ActiveMQManagerGBean.class.getName()); 55 private Kernel kernel; 56 private String objectName; 57 58 public ActiveMQManagerGBean(Kernel kernel, String objectName) { 59 this.kernel = kernel; 60 this.objectName = objectName; 61 } 62 63 public String getProductName() { 64 return "ActiveMQ"; 65 } 66 67 public String getObjectName() { 68 return objectName; 69 } 70 71 public boolean isEventProvider() { 72 return false; 73 } 74 75 public boolean isStateManageable() { 76 return true; 77 } 78 79 public boolean isStatisticsProvider() { 80 return false; 81 } 82 83 public Object [] getContainers() { 84 ProxyManager proxyManager = kernel.getProxyManager(); 85 AbstractNameQuery query = new AbstractNameQuery(ActiveMQBroker.class.getName()); 86 Set names = kernel.listGBeans(query); 87 ActiveMQBroker[] results = new ActiveMQBroker[names.size()]; 88 int i=0; 89 for (Iterator it = names.iterator(); it.hasNext(); i++) { 90 AbstractName name = (AbstractName) it.next(); 91 results[i] = (ActiveMQBroker) proxyManager.createProxy(name, ActiveMQBroker.class.getClassLoader()); 92 } 93 return results; 94 } 95 96 public String [] getSupportedProtocols() { 97 return new String []{ "tcp", "stomp", "vm", "peer", "udp", "multicast", "failover"}; 99 } 100 101 public NetworkConnector[] getConnectors() { 102 ProxyManager proxyManager = kernel.getProxyManager(); 103 AbstractNameQuery query = new AbstractNameQuery(ActiveMQConnector.class.getName()); 104 Set names = kernel.listGBeans(query); 105 ActiveMQConnector[] results = new ActiveMQConnector[names.size()]; 106 int i=0; 107 for (Iterator it = names.iterator(); it.hasNext(); i++) { 108 AbstractName name = (AbstractName) it.next(); 109 results[i] = (ActiveMQConnector) proxyManager.createProxy(name, ActiveMQConnector.class.getClassLoader()); 110 } 111 return results; 112 } 113 114 public NetworkConnector[] getConnectors(String protocol) { 115 if(protocol == null) { 116 return getConnectors(); 117 } 118 List result = new ArrayList (); 119 ProxyManager proxyManager = kernel.getProxyManager(); 120 AbstractNameQuery query = new AbstractNameQuery(ActiveMQConnector.class.getName()); 121 Set names = kernel.listGBeans(query); 122 for (Iterator it = names.iterator(); it.hasNext();) { 123 AbstractName name = (AbstractName) it.next(); 124 try { 125 if (kernel.getAttribute(name, "protocol").equals(protocol)) { 126 result.add(proxyManager.createProxy(name, ActiveMQConnector.class.getClassLoader())); 127 } 128 } catch (Exception e) { 129 log.error("Unable to check the protocol for a connector", e); 130 } 131 } 132 return (ActiveMQConnector[]) result.toArray(new ActiveMQConnector[names.size()]); 133 } 134 135 public NetworkConnector[] getConnectorsForContainer(Object broker) { 136 AbstractName containerName = kernel.getAbstractNameFor(broker); 137 ProxyManager mgr = kernel.getProxyManager(); 138 try { 139 List results = new ArrayList (); 140 AbstractNameQuery query = new AbstractNameQuery(ActiveMQConnector.class.getName()); 141 Set set = kernel.listGBeans(query); for (Iterator it = set.iterator(); it.hasNext();) { 143 AbstractName name = (AbstractName) it.next(); GBeanData data = kernel.getGBeanData(name); 145 ReferencePatterns refs = data.getReferencePatterns("brokerService"); 146 if (containerName.equals(refs.getAbstractName())) { 147 results.add(mgr.createProxy(name, ActiveMQConnector.class.getClassLoader())); 148 } 149 } 150 return (ActiveMQConnector[]) results.toArray(new ActiveMQConnector[results.size()]); 151 } catch (Exception e) { 152 throw (IllegalArgumentException ) new IllegalArgumentException ("Unable to look up connectors for ActiveMQ broker '"+containerName).initCause(e); 153 } 154 } 155 156 public NetworkConnector[] getConnectorsForContainer(Object broker, String protocol) { 157 if(protocol == null) { 158 return getConnectorsForContainer(broker); 159 } 160 AbstractName containerName = kernel.getAbstractNameFor(broker); 161 ProxyManager mgr = kernel.getProxyManager(); 162 try { 163 List results = new ArrayList (); 164 AbstractNameQuery query = new AbstractNameQuery(ActiveMQConnector.class.getName()); 165 Set set = kernel.listGBeans(query); for (Iterator it = set.iterator(); it.hasNext();) { 167 AbstractName name = (AbstractName) it.next(); GBeanData data = kernel.getGBeanData(name); 169 ReferencePatterns refs = data.getReferencePatterns("brokerService"); 170 if(containerName.equals(refs.getAbstractName())) { 171 try { 172 String testProtocol = (String ) kernel.getAttribute(name, "protocol"); 173 if(testProtocol != null && testProtocol.equals(protocol)) { 174 results.add(mgr.createProxy(name, ActiveMQConnector.class.getClassLoader())); 175 } 176 } catch (Exception e) { 177 log.error("Unable to look up protocol for connector '"+name+"'",e); 178 } 179 break; 180 } 181 } 182 return (ActiveMQConnector[]) results.toArray(new ActiveMQConnector[results.size()]); 183 } catch (Exception e) { 184 throw (IllegalArgumentException )new IllegalArgumentException ("Unable to look up connectors for ActiveMQ broker '"+containerName +"': ").initCause(e); 185 } 186 } 187 188 193 public JMSConnector addConnector(JMSBroker broker, String uniqueName, String protocol, String host, int port) { 194 AbstractName brokerAbstractName = kernel.getAbstractNameFor(broker); 195 AbstractName name = kernel.getNaming().createChildName(brokerAbstractName, uniqueName, NameFactory.GERONIMO_SERVICE); 196 GBeanData connector = new GBeanData(name, TransportConnectorGBeanImpl.GBEAN_INFO); 197 connector.setAttribute("protocol", protocol); 199 connector.setAttribute("host", host); 200 connector.setAttribute("port", new Integer (port)); 201 connector.setReferencePattern("brokerService", brokerAbstractName); 202 EditableConfigurationManager mgr = ConfigurationUtil.getEditableConfigurationManager(kernel); 203 if(mgr != null) { 204 try { 205 mgr.addGBeanToConfiguration(brokerAbstractName.getArtifact(), connector, false); 206 return (JMSConnector) kernel.getProxyManager().createProxy(name, ActiveMQConnector.class.getClassLoader()); 207 } catch (InvalidConfigException e) { 208 log.error("Unable to add GBean", e); 209 return null; 210 } finally { 211 ConfigurationUtil.releaseConfigurationManager(kernel, mgr); 212 } 213 } else { 214 log.warn("The ConfigurationManager in the kernel does not allow editing"); 215 return null; 216 } 217 } 218 219 public void removeConnector(AbstractName connectorName) { 220 try { 221 GBeanInfo info = kernel.getGBeanInfo(connectorName); 222 boolean found = false; 223 Set intfs = info.getInterfaces(); 224 for (Iterator it = intfs.iterator(); it.hasNext();) { 225 String intf = (String ) it.next(); 226 if (intf.equals(ActiveMQConnector.class.getName())) { 227 found = true; 228 } 229 } 230 if (!found) { 231 throw new GBeanNotFoundException(connectorName); 232 } 233 EditableConfigurationManager mgr = ConfigurationUtil.getEditableConfigurationManager(kernel); 234 if (mgr != null) { 235 try { 236 mgr.removeGBeanFromConfiguration(connectorName.getArtifact(), connectorName); 237 } catch (InvalidConfigException e) { 238 log.error("Unable to add GBean", e); 239 } finally { 240 ConfigurationUtil.releaseConfigurationManager(kernel, mgr); 241 } 242 } else { 243 log.warn("The ConfigurationManager in the kernel does not allow editing"); 244 } 245 } catch (GBeanNotFoundException e) { 246 log.warn("No such GBean '" + connectorName + "'"); } catch (Exception e) { 248 log.error(e); 249 } 250 } 251 252 public static final GBeanInfo GBEAN_INFO; 253 254 static { 255 GBeanInfoBuilder infoFactory = new GBeanInfoBuilder("ActiveMQ Manager", ActiveMQManagerGBean.class); 256 infoFactory.addAttribute("kernel", Kernel.class, false); 257 infoFactory.addAttribute("objectName", String .class, false); 258 infoFactory.addInterface(ActiveMQManager.class); 259 infoFactory.setConstructor(new String []{"kernel", "objectName"}); 260 GBEAN_INFO = infoFactory.getBeanInfo(); 261 } 262 263 public static GBeanInfo getGBeanInfo() { 264 return GBEAN_INFO; 265 } 266 } 267 | Popular Tags |