KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > gbean > management > ActiveMQManagerGBean


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.activemq.gbean.management;
18
19 import java.util.ArrayList JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.Set JavaDoc;
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 /**
48  * Implementation of the ActiveMQ management interface. These are the ActiveMQ
49  * management features available at runtime.
50  *
51  * @version $Revision: 478879 $ $Date: 2006-11-24 08:39:08 -0500 (Fri, 24 Nov 2006) $
52  */

53 public class ActiveMQManagerGBean implements ActiveMQManager {
54     private static final Log log = LogFactory.getLog(ActiveMQManagerGBean.class.getName());
55     private Kernel kernel;
56     private String JavaDoc objectName;
57
58     public ActiveMQManagerGBean(Kernel kernel, String JavaDoc objectName) {
59         this.kernel = kernel;
60         this.objectName = objectName;
61     }
62
63     public String JavaDoc getProductName() {
64         return "ActiveMQ";
65     }
66
67     public String JavaDoc 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 JavaDoc[] getContainers() {
84         ProxyManager proxyManager = kernel.getProxyManager();
85         AbstractNameQuery query = new AbstractNameQuery(ActiveMQBroker.class.getName());
86         Set JavaDoc names = kernel.listGBeans(query);
87         ActiveMQBroker[] results = new ActiveMQBroker[names.size()];
88         int i=0;
89         for (Iterator JavaDoc 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 JavaDoc[] getSupportedProtocols() {
97         // see files in modules/core/src/conf/META-INF/services/org/activemq/transport/server/
98
return new String JavaDoc[]{ "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 JavaDoc names = kernel.listGBeans(query);
105         ActiveMQConnector[] results = new ActiveMQConnector[names.size()];
106         int i=0;
107         for (Iterator JavaDoc 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 JavaDoc protocol) {
115         if(protocol == null) {
116             return getConnectors();
117         }
118         List JavaDoc result = new ArrayList JavaDoc();
119         ProxyManager proxyManager = kernel.getProxyManager();
120         AbstractNameQuery query = new AbstractNameQuery(ActiveMQConnector.class.getName());
121         Set JavaDoc names = kernel.listGBeans(query);
122         for (Iterator JavaDoc 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 JavaDoc 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 JavaDoc broker) {
136         AbstractName containerName = kernel.getAbstractNameFor(broker);
137         ProxyManager mgr = kernel.getProxyManager();
138         try {
139             List JavaDoc results = new ArrayList JavaDoc();
140             AbstractNameQuery query = new AbstractNameQuery(ActiveMQConnector.class.getName());
141             Set JavaDoc set = kernel.listGBeans(query); // all Jetty connectors
142
for (Iterator JavaDoc it = set.iterator(); it.hasNext();) {
143                 AbstractName name = (AbstractName) it.next(); // a single Jetty connector
144
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 JavaDoc e) {
152             throw (IllegalArgumentException JavaDoc) new IllegalArgumentException JavaDoc("Unable to look up connectors for ActiveMQ broker '"+containerName).initCause(e);
153         }
154     }
155
156     public NetworkConnector[] getConnectorsForContainer(Object JavaDoc broker, String JavaDoc 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 JavaDoc results = new ArrayList JavaDoc();
164             AbstractNameQuery query = new AbstractNameQuery(ActiveMQConnector.class.getName());
165             Set JavaDoc set = kernel.listGBeans(query); // all Jetty connectors
166
for (Iterator JavaDoc it = set.iterator(); it.hasNext();) {
167                 AbstractName name = (AbstractName) it.next(); // a single Jetty connector
168
GBeanData data = kernel.getGBeanData(name);
169                 ReferencePatterns refs = data.getReferencePatterns("brokerService");
170                 if(containerName.equals(refs.getAbstractName())) {
171                     try {
172                         String JavaDoc testProtocol = (String JavaDoc) kernel.getAttribute(name, "protocol");
173                         if(testProtocol != null && testProtocol.equals(protocol)) {
174                             results.add(mgr.createProxy(name, ActiveMQConnector.class.getClassLoader()));
175                         }
176                     } catch (Exception JavaDoc 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 JavaDoc e) {
184             throw (IllegalArgumentException JavaDoc)new IllegalArgumentException JavaDoc("Unable to look up connectors for ActiveMQ broker '"+containerName +"': ").initCause(e);
185         }
186     }
187
188     /**
189      * Returns a new JMSConnector. Note that
190      * the connector may well require further customization before being fully
191      * functional (e.g. SSL settings for a secure connector).
192      */

193     public JMSConnector addConnector(JMSBroker broker, String JavaDoc uniqueName, String JavaDoc protocol, String JavaDoc 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         //todo: if SSL is supported, need to add more properties or use a different GBean?
198
connector.setAttribute("protocol", protocol);
199         connector.setAttribute("host", host);
200         connector.setAttribute("port", new Integer JavaDoc(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 JavaDoc intfs = info.getInterfaces();
224             for (Iterator JavaDoc it = intfs.iterator(); it.hasNext();) {
225                 String JavaDoc intf = (String JavaDoc) 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 + "'"); //todo: what if we want to remove a failed GBean?
247
} catch (Exception JavaDoc 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 JavaDoc.class, false);
258         infoFactory.addInterface(ActiveMQManager.class);
259         infoFactory.setConstructor(new String JavaDoc[]{"kernel", "objectName"});
260         GBEAN_INFO = infoFactory.getBeanInfo();
261     }
262
263     public static GBeanInfo getGBeanInfo() {
264         return GBEAN_INFO;
265     }
266 }
267
Popular Tags