Refactored ManagementContext to improve encapsulation - so all registrations of MBeans happen in one place

git-svn-id: https://svn.apache.org/repos/asf/activemq/trunk@800337 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Robert Davies 2009-08-03 12:38:00 +00:00
parent ac4545b40d
commit 93ccd9f17e
1 changed files with 107 additions and 161 deletions

View File

@ -16,18 +16,6 @@
*/ */
package org.apache.activemq.web; package org.apache.activemq.web;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import javax.management.MBeanServer;
import javax.management.MBeanServerConnection;
import javax.management.MBeanServerInvocationHandler;
import javax.management.ObjectName;
import org.apache.activemq.broker.jmx.BrokerViewMBean; import org.apache.activemq.broker.jmx.BrokerViewMBean;
import org.apache.activemq.broker.jmx.ConnectionViewMBean; import org.apache.activemq.broker.jmx.ConnectionViewMBean;
import org.apache.activemq.broker.jmx.ConnectorViewMBean; import org.apache.activemq.broker.jmx.ConnectorViewMBean;
@ -39,10 +27,16 @@ import org.apache.activemq.broker.jmx.QueueViewMBean;
import org.apache.activemq.broker.jmx.SubscriptionViewMBean; import org.apache.activemq.broker.jmx.SubscriptionViewMBean;
import org.apache.activemq.broker.jmx.TopicViewMBean; import org.apache.activemq.broker.jmx.TopicViewMBean;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import javax.management.ObjectName;
/** /**
* A useful base class for an implementation of {@link BrokerFacade} * A useful base class for an implementation of {@link BrokerFacade}
* *
* @version $Revision$ * @version $Revision$
*/ */
public abstract class BrokerFacadeSupport implements BrokerFacade { public abstract class BrokerFacadeSupport implements BrokerFacade {
@ -83,162 +77,114 @@ public abstract class BrokerFacadeSupport implements BrokerFacade {
return (TopicViewMBean) getDestinationByName(getTopics(), name); return (TopicViewMBean) getDestinationByName(getTopics(), name);
} }
protected DestinationViewMBean getDestinationByName( protected DestinationViewMBean getDestinationByName(Collection<? extends DestinationViewMBean> collection,
Collection<? extends DestinationViewMBean> collection, String name) { String name) {
Iterator<? extends DestinationViewMBean> iter = collection.iterator(); Iterator<? extends DestinationViewMBean> iter = collection.iterator();
while (iter.hasNext()) { while (iter.hasNext()) {
DestinationViewMBean destinationViewMBean = iter.next(); DestinationViewMBean destinationViewMBean = iter.next();
if (name.equals(destinationViewMBean.getName())) { if (name.equals(destinationViewMBean.getName())) {
return destinationViewMBean; return destinationViewMBean;
} }
} }
return null; return null;
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
protected <T> Collection<T> getManagedObjects(ObjectName[] names, protected <T> Collection<T> getManagedObjects(ObjectName[] names, Class<T> type) {
Class<T> type) { List<T> answer = new ArrayList<T>();
List<T> answer = new ArrayList<T>(); for (int i = 0; i < names.length; i++) {
MBeanServer mbeanServer = getManagementContext().getMBeanServer(); ObjectName name = names[i];
if (mbeanServer != null) { T value = (T) getManagementContext().newProxyInstance(name, type, true);
for (int i = 0; i < names.length; i++) { if (value != null) {
ObjectName name = names[i]; answer.add(value);
T value = (T) MBeanServerInvocationHandler.newProxyInstance( }
mbeanServer, name, type, true); }
if (value != null) { return answer;
answer.add(value); }
}
}
}
return answer;
}
/**
* Get the MBeanServer connection.
*
* @return not <code>null</code>
* @throws Exception
*/
protected MBeanServerConnection getMBeanServerConnection() throws Exception {
return getManagementContext().getMBeanServer();
}
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public Collection<ConnectionViewMBean> getConnections() throws Exception { public Collection<ConnectionViewMBean> getConnections() throws Exception {
MBeanServerConnection connection = getMBeanServerConnection(); String brokerName = getBrokerName();
String brokerName = getBrokerName(); ObjectName query = new ObjectName("org.apache.activemq:BrokerName=" + brokerName + ",Type=Connection,*");
ObjectName query = new ObjectName("org.apache.activemq:BrokerName=" System.out.println(query);
+ brokerName + ",Type=Connection,*"); Set<ObjectName> queryResult = getManagementContext().queryNames(query, null);
System.out.println(query); return getManagedObjects(queryResult.toArray(new ObjectName[queryResult.size()]), ConnectionViewMBean.class);
Set<ObjectName> queryResult = connection.queryNames(query, null); }
return getManagedObjects(queryResult.toArray(new ObjectName[queryResult
.size()]), ConnectionViewMBean.class);
}
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public Collection<String> getConnections(String connectorName) public Collection<String> getConnections(String connectorName) throws Exception {
throws Exception { String brokerName = getBrokerName();
MBeanServerConnection connection = getMBeanServerConnection(); ObjectName query = new ObjectName("org.apache.activemq:BrokerName=" + brokerName
String brokerName = getBrokerName(); + ",Type=Connection,ConnectorName=" + connectorName + ",*");
ObjectName query = new ObjectName("org.apache.activemq:BrokerName=" Set<ObjectName> queryResult = getManagementContext().queryNames(query, null);
+ brokerName + ",Type=Connection,ConnectorName=" Collection<String> result = new ArrayList<String>(queryResult.size());
+ connectorName + ",*"); for (ObjectName on : queryResult) {
Set<ObjectName> queryResult = connection.queryNames(query, null); String name = StringUtils.replace(on.getKeyProperty("Connection"), "_", ":");
result.add(name);
}
return result;
}
Collection<String> result = new ArrayList<String>(queryResult.size()); @SuppressWarnings("unchecked")
for (ObjectName on : queryResult) { public ConnectionViewMBean getConnection(String connectionName) throws Exception {
String name = StringUtils.replace(on.getKeyProperty("Connection"), connectionName = StringUtils.replace(connectionName, ":", "_");
"_", ":"); String brokerName = getBrokerName();
result.add(name); ObjectName query = new ObjectName("org.apache.activemq:BrokerName=" + brokerName
} + ",Type=Connection,*,Connection=" + connectionName);
return result; Set<ObjectName> queryResult = getManagementContext().queryNames(query, null);
} if (queryResult.size() == 0)
return null;
ObjectName objectName = queryResult.iterator().next();
return (ConnectionViewMBean) getManagementContext().newProxyInstance(objectName,
ConnectionViewMBean.class, true);
}
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public ConnectionViewMBean getConnection(String connectionName) public Collection<String> getConnectors() throws Exception {
throws Exception { String brokerName = getBrokerName();
connectionName = StringUtils.replace(connectionName, ":", "_"); ObjectName query = new ObjectName("org.apache.activemq:BrokerName=" + brokerName + ",Type=Connector,*");
Set<ObjectName> queryResult = getManagementContext().queryNames(query, null);
Collection<String> result = new ArrayList<String>(queryResult.size());
for (ObjectName on : queryResult)
result.add(on.getKeyProperty("ConnectorName"));
return result;
}
MBeanServerConnection connection = getMBeanServerConnection(); public ConnectorViewMBean getConnector(String name) throws Exception {
String brokerName = getBrokerName(); String brokerName = getBrokerName();
ObjectName query = new ObjectName("org.apache.activemq:BrokerName=" ObjectName objectName = new ObjectName("org.apache.activemq:BrokerName=" + brokerName
+ brokerName + ",Type=Connection,*,Connection=" + ",Type=Connector,ConnectorName=" + name);
+ connectionName); return (ConnectorViewMBean) getManagementContext().newProxyInstance(objectName,
Set<ObjectName> queryResult = connection.queryNames(query, null); ConnectorViewMBean.class, true);
if (queryResult.size() == 0) }
return null;
ObjectName objectName = queryResult.iterator().next();
return (ConnectionViewMBean) MBeanServerInvocationHandler
.newProxyInstance(connection, objectName,
ConnectionViewMBean.class, true);
}
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public Collection<String> getConnectors() throws Exception { public Collection<NetworkConnectorViewMBean> getNetworkConnectors() throws Exception {
MBeanServerConnection connection = getMBeanServerConnection(); String brokerName = getBrokerName();
String brokerName = getBrokerName(); ObjectName query = new ObjectName("org.apache.activemq:BrokerName=" + brokerName + ",Type=NetworkConnector,*");
ObjectName query = new ObjectName("org.apache.activemq:BrokerName=" Set<ObjectName> queryResult = getManagementContext().queryNames(query, null);
+ brokerName + ",Type=Connector,*"); return getManagedObjects(queryResult.toArray(new ObjectName[queryResult.size()]),
Set<ObjectName> queryResult = connection.queryNames(query, null); NetworkConnectorViewMBean.class);
}
Collection<String> result = new ArrayList<String>(queryResult.size()); @SuppressWarnings("unchecked")
for (ObjectName on : queryResult) public Collection<SubscriptionViewMBean> getQueueConsumers(String queueName) throws Exception {
result.add(on.getKeyProperty("ConnectorName")); String brokerName = getBrokerName();
return result; ObjectName query = new ObjectName("org.apache.activemq:BrokerName=" + brokerName
} + ",Type=Subscription,destinationType=Queue,destinationName=" + queueName + ",*");
Set<ObjectName> queryResult = getManagementContext().queryNames(query, null);
public ConnectorViewMBean getConnector(String name) throws Exception { return getManagedObjects(queryResult.toArray(new ObjectName[queryResult.size()]), SubscriptionViewMBean.class);
MBeanServerConnection connection = getMBeanServerConnection(); }
String brokerName = getBrokerName();
ObjectName objectName = new ObjectName(
"org.apache.activemq:BrokerName=" + brokerName
+ ",Type=Connector,ConnectorName=" + name);
return (ConnectorViewMBean) MBeanServerInvocationHandler
.newProxyInstance(connection, objectName,
ConnectorViewMBean.class, true);
}
@SuppressWarnings("unchecked")
public Collection<NetworkConnectorViewMBean> getNetworkConnectors()
throws Exception {
MBeanServerConnection connection = getMBeanServerConnection();
String brokerName = getBrokerName();
ObjectName query = new ObjectName("org.apache.activemq:BrokerName="
+ brokerName + ",Type=NetworkConnector,*");
Set<ObjectName> queryResult = connection.queryNames(query, null);
return getManagedObjects(queryResult.toArray(new ObjectName[queryResult
.size()]), NetworkConnectorViewMBean.class);
}
@SuppressWarnings("unchecked")
public Collection<SubscriptionViewMBean> getQueueConsumers(String queueName)
throws Exception {
MBeanServerConnection connection = getMBeanServerConnection();
String brokerName = getBrokerName();
ObjectName query = new ObjectName("org.apache.activemq:BrokerName="
+ brokerName
+ ",Type=Subscription,destinationType=Queue,destinationName="
+ queueName + ",*");
Set<ObjectName> queryResult = connection.queryNames(query, null);
return getManagedObjects(queryResult.toArray(new ObjectName[queryResult
.size()]), SubscriptionViewMBean.class);
}
@SuppressWarnings("unchecked")
public Collection<SubscriptionViewMBean> getConsumersOnConnection(
String connectionName) throws Exception {
connectionName = StringUtils.replace(connectionName, ":", "_");
MBeanServerConnection connection = getMBeanServerConnection();
String brokerName = getBrokerName();
ObjectName query = new ObjectName("org.apache.activemq:BrokerName="
+ brokerName + ",Type=Subscription,clientId=" + connectionName
+ ",*");
Set<ObjectName> queryResult = connection.queryNames(query, null);
return getManagedObjects(queryResult.toArray(new ObjectName[queryResult
.size()]), SubscriptionViewMBean.class);
}
@SuppressWarnings("unchecked")
public Collection<SubscriptionViewMBean> getConsumersOnConnection(String connectionName) throws Exception {
connectionName = StringUtils.replace(connectionName, ":", "_");
String brokerName = getBrokerName();
ObjectName query = new ObjectName("org.apache.activemq:BrokerName=" + brokerName
+ ",Type=Subscription,clientId=" + connectionName + ",*");
Set<ObjectName> queryResult = getManagementContext().queryNames(query, null);
return getManagedObjects(queryResult.toArray(new ObjectName[queryResult.size()]), SubscriptionViewMBean.class);
}
} }