1 /**
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one or more
4 * contributor license agreements. See the NOTICE file distributed with
5 * this work for additional information regarding copyright ownership.
6 * The ASF licenses this file to You under the Apache License, Version 2.0
7 * (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18 package org.apache.activemq.gbean.management;
19
20 import java.util.ArrayList;
21 import java.util.Iterator;
22 import java.util.List;
23 import java.util.Set;
24
25 import org.apache.activemq.gbean.ActiveMQBroker;
26 import org.apache.activemq.gbean.ActiveMQConnector;
27 import org.apache.activemq.gbean.ActiveMQManager;
28 import org.apache.activemq.gbean.TransportConnectorGBeanImpl;
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31 import org.apache.geronimo.gbean.AbstractName;
32 import org.apache.geronimo.gbean.AbstractNameQuery;
33 import org.apache.geronimo.gbean.GBeanData;
34 import org.apache.geronimo.gbean.GBeanInfo;
35 import org.apache.geronimo.gbean.GBeanInfoBuilder;
36 import org.apache.geronimo.gbean.ReferencePatterns;
37 import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory;
38 import org.apache.geronimo.kernel.GBeanNotFoundException;
39 import org.apache.geronimo.kernel.Kernel;
40 import org.apache.geronimo.kernel.config.ConfigurationUtil;
41 import org.apache.geronimo.kernel.config.EditableConfigurationManager;
42 import org.apache.geronimo.kernel.config.InvalidConfigException;
43 import org.apache.geronimo.kernel.proxy.ProxyManager;
44 import org.apache.geronimo.management.geronimo.JMSBroker;
45 import org.apache.geronimo.management.geronimo.JMSConnector;
46 import org.apache.geronimo.management.geronimo.NetworkConnector;
47
48 /**
49 * Implementation of the ActiveMQ management interface. These are the ActiveMQ
50 * management features available at runtime.
51 *
52 * @version $Revision: 1.0$
53 */
54 public class ActiveMQManagerGBean implements ActiveMQManager {
55 private static final Log log = LogFactory.getLog(ActiveMQManagerGBean.class.getName());
56 private Kernel kernel;
57 private String objectName;
58
59 public ActiveMQManagerGBean(Kernel kernel, String objectName) {
60 this.kernel = kernel;
61 this.objectName = objectName;
62 }
63
64 public String getProductName() {
65 return "ActiveMQ";
66 }
67
68 public String getObjectName() {
69 return objectName;
70 }
71
72 public boolean isEventProvider() {
73 return false;
74 }
75
76 public boolean isStateManageable() {
77 return true;
78 }
79
80 public boolean isStatisticsProvider() {
81 return false;
82 }
83
84 public Object[] getContainers() {
85 ProxyManager proxyManager = kernel.getProxyManager();
86 AbstractNameQuery query = new AbstractNameQuery(ActiveMQBroker.class.getName());
87 Set names = kernel.listGBeans(query);
88 ActiveMQBroker[] results = new ActiveMQBroker[names.size()];
89 int i=0;
90 for (Iterator it = names.iterator(); it.hasNext(); i++) {
91 AbstractName name = (AbstractName) it.next();
92 results[i] = (ActiveMQBroker) proxyManager.createProxy(name, ActiveMQBroker.class.getClassLoader());
93 }
94 return results;
95 }
96
97 public String[] getSupportedProtocols() {
98
99 return new String[]{ "tcp", "stomp", "vm", "peer", "udp", "multicast", "failover"};
100 }
101
102 public NetworkConnector[] getConnectors() {
103 ProxyManager proxyManager = kernel.getProxyManager();
104 AbstractNameQuery query = new AbstractNameQuery(ActiveMQConnector.class.getName());
105 Set names = kernel.listGBeans(query);
106 ActiveMQConnector[] results = new ActiveMQConnector[names.size()];
107 int i=0;
108 for (Iterator it = names.iterator(); it.hasNext(); i++) {
109 AbstractName name = (AbstractName) it.next();
110 results[i] = (ActiveMQConnector) proxyManager.createProxy(name, ActiveMQConnector.class.getClassLoader());
111 }
112 return results;
113 }
114
115 public NetworkConnector[] getConnectors(String protocol) {
116 if(protocol == null) {
117 return getConnectors();
118 }
119 List result = new ArrayList();
120 ProxyManager proxyManager = kernel.getProxyManager();
121 AbstractNameQuery query = new AbstractNameQuery(ActiveMQConnector.class.getName());
122 Set names = kernel.listGBeans(query);
123 for (Iterator it = names.iterator(); it.hasNext();) {
124 AbstractName name = (AbstractName) it.next();
125 try {
126 if (kernel.getAttribute(name, "protocol").equals(protocol)) {
127 result.add(proxyManager.createProxy(name, ActiveMQConnector.class.getClassLoader()));
128 }
129 } catch (Exception e) {
130 log.error("Unable to check the protocol for a connector", e);
131 }
132 }
133 return (ActiveMQConnector[]) result.toArray(new ActiveMQConnector[names.size()]);
134 }
135
136 public NetworkConnector[] getConnectorsForContainer(Object broker) {
137 AbstractName containerName = kernel.getAbstractNameFor(broker);
138 ProxyManager mgr = kernel.getProxyManager();
139 try {
140 List results = new ArrayList();
141 AbstractNameQuery query = new AbstractNameQuery(ActiveMQConnector.class.getName());
142 Set set = kernel.listGBeans(query);
143 for (Iterator it = set.iterator(); it.hasNext();) {
144 AbstractName name = (AbstractName) it.next();
145 GBeanData data = kernel.getGBeanData(name);
146 ReferencePatterns refs = data.getReferencePatterns("brokerService");
147 if (containerName.equals(refs.getAbstractName())) {
148 results.add(mgr.createProxy(name, ActiveMQConnector.class.getClassLoader()));
149 }
150 }
151 return (ActiveMQConnector[]) results.toArray(new ActiveMQConnector[results.size()]);
152 } catch (Exception e) {
153 throw (IllegalArgumentException) new IllegalArgumentException("Unable to look up connectors for ActiveMQ broker '"+containerName).initCause(e);
154 }
155 }
156
157 public NetworkConnector[] getConnectorsForContainer(Object broker, String protocol) {
158 if(protocol == null) {
159 return getConnectorsForContainer(broker);
160 }
161 AbstractName containerName = kernel.getAbstractNameFor(broker);
162 ProxyManager mgr = kernel.getProxyManager();
163 try {
164 List results = new ArrayList();
165 AbstractNameQuery query = new AbstractNameQuery(ActiveMQConnector.class.getName());
166 Set set = kernel.listGBeans(query);
167 for (Iterator it = set.iterator(); it.hasNext();) {
168 AbstractName name = (AbstractName) it.next();
169 GBeanData data = kernel.getGBeanData(name);
170 ReferencePatterns refs = data.getReferencePatterns("brokerService");
171 if(containerName.equals(refs.getAbstractName())) {
172 try {
173 String testProtocol = (String) kernel.getAttribute(name, "protocol");
174 if(testProtocol != null && testProtocol.equals(protocol)) {
175 results.add(mgr.createProxy(name, ActiveMQConnector.class.getClassLoader()));
176 }
177 } catch (Exception e) {
178 log.error("Unable to look up protocol for connector '"+name+"'",e);
179 }
180 break;
181 }
182 }
183 return (ActiveMQConnector[]) results.toArray(new ActiveMQConnector[results.size()]);
184 } catch (Exception e) {
185 throw (IllegalArgumentException)new IllegalArgumentException("Unable to look up connectors for ActiveMQ broker '"+containerName +"': ").initCause(e);
186 }
187 }
188
189 /**
190 * Returns a new JMSConnector. Note that
191 * the connector may well require further customization before being fully
192 * functional (e.g. SSL settings for a secure connector).
193 */
194 public JMSConnector addConnector(JMSBroker broker, String uniqueName, String protocol, String host, int port) {
195 AbstractName brokerAbstractName = kernel.getAbstractNameFor(broker);
196 AbstractName name = kernel.getNaming().createChildName(brokerAbstractName, uniqueName, NameFactory.GERONIMO_SERVICE);
197 GBeanData connector = new GBeanData(name, TransportConnectorGBeanImpl.GBEAN_INFO);
198
199 connector.setAttribute("protocol", protocol);
200 connector.setAttribute("host", host);
201 connector.setAttribute("port", new Integer(port));
202 connector.setReferencePattern("activeMQContainer", brokerAbstractName);
203 EditableConfigurationManager mgr = ConfigurationUtil.getEditableConfigurationManager(kernel);
204 if(mgr != null) {
205 try {
206 mgr.addGBeanToConfiguration(brokerAbstractName.getArtifact(), connector, false);
207 return (JMSConnector) kernel.getProxyManager().createProxy(name, ActiveMQConnector.class.getClassLoader());
208 } catch (InvalidConfigException e) {
209 log.error("Unable to add GBean", e);
210 return null;
211 } finally {
212 ConfigurationUtil.releaseConfigurationManager(kernel, mgr);
213 }
214 } else {
215 log.warn("The ConfigurationManager in the kernel does not allow editing");
216 return null;
217 }
218 }
219
220 public void removeConnector(AbstractName connectorName) {
221 try {
222 GBeanInfo info = kernel.getGBeanInfo(connectorName);
223 boolean found = false;
224 Set intfs = info.getInterfaces();
225 for (Iterator it = intfs.iterator(); it.hasNext();) {
226 String intf = (String) it.next();
227 if (intf.equals(ActiveMQConnector.class.getName())) {
228 found = true;
229 }
230 }
231 if (!found) {
232 throw new GBeanNotFoundException(connectorName);
233 }
234 EditableConfigurationManager mgr = ConfigurationUtil.getEditableConfigurationManager(kernel);
235 if (mgr != null) {
236 try {
237 mgr.removeGBeanFromConfiguration(connectorName.getArtifact(), connectorName);
238 } catch (InvalidConfigException e) {
239 log.error("Unable to add GBean", e);
240 } finally {
241 ConfigurationUtil.releaseConfigurationManager(kernel, mgr);
242 }
243 } else {
244 log.warn("The ConfigurationManager in the kernel does not allow editing");
245 }
246 } catch (GBeanNotFoundException e) {
247 log.warn("No such GBean '" + connectorName + "'");
248 } catch (Exception e) {
249 log.error(e);
250 }
251 }
252
253 public static final GBeanInfo GBEAN_INFO;
254
255 static {
256 GBeanInfoBuilder infoFactory = new GBeanInfoBuilder("ActiveMQ Manager", ActiveMQManagerGBean.class);
257 infoFactory.addAttribute("kernel", Kernel.class, false);
258 infoFactory.addAttribute("objectName", String.class, false);
259 infoFactory.addInterface(ActiveMQManager.class);
260 infoFactory.setConstructor(new String[]{"kernel", "objectName"});
261 GBEAN_INFO = infoFactory.getBeanInfo();
262 }
263
264 public static GBeanInfo getGBeanInfo() {
265 return GBEAN_INFO;
266 }
267 }