001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.activemq.broker.jmx;
018    
019    import java.util.Hashtable;
020    
021    import javax.management.MalformedObjectNameException;
022    import javax.management.ObjectName;
023    
024    import org.apache.activemq.broker.region.policy.AbortSlowConsumerStrategy;
025    import org.apache.activemq.command.ActiveMQDestination;
026    import org.apache.activemq.command.ConsumerInfo;
027    import org.apache.activemq.command.ProducerInfo;
028    import org.apache.activemq.transaction.XATransaction;
029    import org.apache.activemq.util.JMXSupport;
030    
031    public class BrokerMBeanSupport {
032    
033        // MBean Name Creation
034    
035        public static ObjectName createBrokerObjectName(String jmxDomainName, String brokerName) throws MalformedObjectNameException  {
036            String objectNameStr = jmxDomainName + ":type=Broker,brokerName=";
037            objectNameStr += JMXSupport.encodeObjectNamePart(brokerName);
038            return new ObjectName(objectNameStr);
039        }
040    
041        public static ObjectName createDestinationName(ObjectName brokerObjectName, ActiveMQDestination destination) throws MalformedObjectNameException {
042            return createDestinationName(brokerObjectName.toString(), destination);
043        }
044    
045        public static ObjectName createDestinationName(String brokerObjectName, ActiveMQDestination destination) throws MalformedObjectNameException {
046            String objectNameStr = brokerObjectName;
047            objectNameStr += createDestinationProperties(destination);
048            return new ObjectName(objectNameStr);
049        }
050    
051        public static ObjectName createDestinationName(String brokerObjectName, String type, String name) throws MalformedObjectNameException {
052            String objectNameStr = brokerObjectName;
053            objectNameStr += createDestinationProperties(type, name);
054            return new ObjectName(objectNameStr);
055        }
056    
057        private static String createDestinationProperties(ActiveMQDestination destination){
058            String result = "";
059            if (destination != null){
060                result = createDestinationProperties(destination.getDestinationTypeAsString(), destination.getPhysicalName());
061            }
062            return result;
063        }
064    
065        private static String createDestinationProperties(String type, String name){
066            return ",destinationType="+ JMXSupport.encodeObjectNamePart(type) +
067                   ",destinationName=" + JMXSupport.encodeObjectNamePart(name);
068        }
069    
070        public static ObjectName createSubscriptionName(ObjectName brokerObjectName, String connectionClientId, ConsumerInfo info) throws MalformedObjectNameException {
071            return createSubscriptionName(brokerObjectName.toString(), connectionClientId, info);
072        }
073    
074        public static ObjectName createSubscriptionName(String brokerObjectName, String connectionClientId, ConsumerInfo info) throws MalformedObjectNameException {
075            String objectNameStr = brokerObjectName;
076            objectNameStr += createDestinationProperties(info.getDestination()) + ",endpoint=Consumer";
077            objectNameStr += ",clientId=" + JMXSupport.encodeObjectNamePart(connectionClientId);
078            objectNameStr += ",consumerId=";
079    
080            if (info.isDurable()){
081                objectNameStr += "Durable(" + JMXSupport.encodeObjectNamePart(connectionClientId + ":" + info.getSubscriptionName()) +")";
082            } else {
083                objectNameStr += JMXSupport.encodeObjectNamePart(info.getConsumerId().toString());
084            }
085    
086            return new ObjectName(objectNameStr);
087        }
088    
089        public static ObjectName createProducerName(ObjectName brokerObjectName, String connectionClientId, ProducerInfo info) throws MalformedObjectNameException {
090            return createProducerName(brokerObjectName.toString(), connectionClientId, info);
091        }
092    
093        public static ObjectName createProducerName(String brokerObjectName, String connectionClientId, ProducerInfo producerInfo) throws MalformedObjectNameException {
094            String objectNameStr = brokerObjectName;
095    
096            if (producerInfo.getDestination() == null) {
097                objectNameStr += ",endpoint=dynamicProducer";
098            } else {
099                objectNameStr += createDestinationProperties(producerInfo.getDestination()) + ",endpoint=Producer";
100            }
101    
102            objectNameStr += ",clientId=" + JMXSupport.encodeObjectNamePart(connectionClientId);
103            objectNameStr += ",producerId=" + JMXSupport.encodeObjectNamePart(producerInfo.getProducerId().toString());
104    
105            return new ObjectName(objectNameStr);
106        }
107    
108        public static ObjectName createXATransactionName(ObjectName brokerObjectName, XATransaction transaction) throws MalformedObjectNameException {
109            return createXATransactionName(brokerObjectName.toString(), transaction);
110        }
111    
112        public static ObjectName createXATransactionName(String brokerObjectName, XATransaction transaction) throws MalformedObjectNameException {
113            String objectNameStr = brokerObjectName;
114    
115            objectNameStr += "," + "transactionType=RecoveredXaTransaction";
116            objectNameStr += "," + "Xid=" + JMXSupport.encodeObjectNamePart(transaction.getTransactionId().toString());
117    
118            return new ObjectName(objectNameStr);
119        }
120    
121        public static ObjectName createPersistenceAdapterName(String brokerObjectName, String name) throws MalformedObjectNameException {
122            String objectNameStr = brokerObjectName;
123    
124            objectNameStr += "," + "Service=PersistenceAdapter";
125            objectNameStr += "," + "InstanceName=" + JMXSupport.encodeObjectNamePart(name);
126    
127            return new ObjectName(objectNameStr);
128        }
129    
130        public static ObjectName createAbortSlowConsumerStrategyName(ObjectName brokerObjectName, AbortSlowConsumerStrategy strategy) throws MalformedObjectNameException {
131            return createAbortSlowConsumerStrategyName(brokerObjectName.toString(), strategy);
132        }
133    
134        public static ObjectName createAbortSlowConsumerStrategyName(String brokerObjectName, AbortSlowConsumerStrategy strategy) throws MalformedObjectNameException {
135            String objectNameStr = brokerObjectName;
136            objectNameStr += ",Service=SlowConsumerStrategy,InstanceName="+ JMXSupport.encodeObjectNamePart(strategy.getName());
137            ObjectName objectName = new ObjectName(objectNameStr);
138            return objectName;
139        }
140    
141        public static ObjectName createConnectorName(ObjectName brokerObjectName, String type, String name) throws MalformedObjectNameException {
142            return createConnectorName(brokerObjectName.toString(), type, name);
143        }
144    
145        public static ObjectName createConnectorName(String brokerObjectName, String type, String name) throws MalformedObjectNameException {
146            String objectNameStr = brokerObjectName;
147            objectNameStr += ",connector=" + type + ",connectorName="+ JMXSupport.encodeObjectNamePart(name);
148            ObjectName objectName = new ObjectName(objectNameStr);
149            return objectName;
150        }
151    
152        public static ObjectName createNetworkConnectorName(ObjectName brokerObjectName, String type, String name) throws MalformedObjectNameException {
153            return createNetworkConnectorName(brokerObjectName.toString(), type, name);
154        }
155    
156        public static ObjectName createNetworkConnectorName(String brokerObjectName, String type, String name) throws MalformedObjectNameException {
157            String objectNameStr = brokerObjectName;
158            objectNameStr += ",connector=" + type + ",networkConnectorName="+ JMXSupport.encodeObjectNamePart(name);
159            ObjectName objectName = new ObjectName(objectNameStr);
160            return objectName;
161        }
162    
163        public static ObjectName createConnectionViewByType(ObjectName connectorName, String type, String name) throws MalformedObjectNameException {
164            String objectNameStr = connectorName.toString();
165            objectNameStr += ",connectionViewType=" + JMXSupport.encodeObjectNamePart(type);
166            objectNameStr += ",connectionName="+ JMXSupport.encodeObjectNamePart(name);
167            return new ObjectName(objectNameStr);
168        }
169    
170        public static ObjectName createNetworkBridgeObjectName(ObjectName connectorName, String remoteAddress) throws MalformedObjectNameException {
171            Hashtable<String, String> map = new Hashtable<String, String>(connectorName.getKeyPropertyList());
172            map.put("networkBridge", JMXSupport.encodeObjectNamePart(remoteAddress));
173            return new ObjectName(connectorName.getDomain(), map);
174        }
175    
176        public static ObjectName createProxyConnectorName(ObjectName brokerObjectName, String type, String name) throws MalformedObjectNameException {
177            return createProxyConnectorName(brokerObjectName.toString(), type, name);
178        }
179    
180        public static ObjectName createProxyConnectorName(String brokerObjectName, String type, String name) throws MalformedObjectNameException {
181            String objectNameStr = brokerObjectName;
182            objectNameStr += ",connector=" + type + ",proxyConnectorName="+ JMXSupport.encodeObjectNamePart(name);
183            ObjectName objectName = new ObjectName(objectNameStr);
184            return objectName;
185        }
186    
187        public static ObjectName createJmsConnectorName(ObjectName brokerObjectName, String type, String name) throws MalformedObjectNameException {
188            return createJmsConnectorName(brokerObjectName.toString(), type, name);
189        }
190    
191        public static ObjectName createJmsConnectorName(String brokerObjectName, String type, String name) throws MalformedObjectNameException {
192            String objectNameStr = brokerObjectName;
193            objectNameStr += ",connector=" + type + ",JmsConnectors="+ JMXSupport.encodeObjectNamePart(name);
194            ObjectName objectName = new ObjectName(objectNameStr);
195            return objectName;
196        }
197    
198        public static ObjectName createJobSchedulerServiceName(ObjectName brokerObjectName) throws MalformedObjectNameException {
199            return createJobSchedulerServiceName(brokerObjectName.toString());
200        }
201    
202        public static ObjectName createJobSchedulerServiceName(String brokerObjectName) throws MalformedObjectNameException {
203            String objectNameStr = brokerObjectName;
204            objectNameStr += ",service=JobScheduler,name=JMS";
205            ObjectName objectName = new ObjectName(objectNameStr);
206            return objectName;
207        }
208    
209        public static ObjectName createHealthServiceName(ObjectName brokerObjectName) throws MalformedObjectNameException {
210            return createHealthServiceName(brokerObjectName.toString());
211        }
212    
213        public static ObjectName createHealthServiceName(String brokerObjectName) throws MalformedObjectNameException {
214            String objectNameStr = brokerObjectName;
215            objectNameStr += ",service=Health";
216            ObjectName objectName = new ObjectName(objectNameStr);
217            return objectName;
218        }
219    
220        // MBean Query Creation
221    
222        public static ObjectName createConnectionQuery(String jmxDomainName, String brokerName, String name) throws MalformedObjectNameException {
223            ObjectName brokerMBeanName = createBrokerObjectName(jmxDomainName, brokerName);
224            return createConnectionQuery(brokerMBeanName.toString(), name);
225        }
226    
227        public static ObjectName createConnectionQuery(String brokerMBeanName, String name) throws MalformedObjectNameException {
228            return new ObjectName(brokerMBeanName + ","
229                                  + "connector=*," + "connectorName=*," + "connectionViewType=*,"
230                                  + "connectionName=" + JMXSupport.encodeObjectNamePart(name));
231        }
232    
233        public static ObjectName createQueueQuery(String brokerMBeanName) throws MalformedObjectNameException {
234            return createConnectionQuery(brokerMBeanName, "*");
235        }
236    
237        public static ObjectName createQueueQuery(String brokerMBeanName, String destinationName) throws MalformedObjectNameException {
238            return new ObjectName(brokerMBeanName + ","
239                                  + "destinationType=Queue,"
240                                  + "destinationName=" + JMXSupport.encodeObjectNamePart(destinationName));
241        }
242    
243        public static ObjectName createTopicQuery(String brokerMBeanName) throws MalformedObjectNameException {
244            return createConnectionQuery(brokerMBeanName, "*");
245        }
246    
247        public static ObjectName createTopicQuery(String brokerMBeanName, String destinationName) throws MalformedObjectNameException {
248            return new ObjectName(brokerMBeanName + ","
249                                  + "destinationType=Topic,"
250                                  + "destinationName=" + JMXSupport.encodeObjectNamePart(destinationName));
251        }
252    
253        public static ObjectName createConsumerQueury(String jmxDomainName, String clientId) throws MalformedObjectNameException {
254            return createConsumerQueury(jmxDomainName, null, clientId);
255        }
256    
257        public static ObjectName createConsumerQueury(String jmxDomainName, String brokerName, String clientId) throws MalformedObjectNameException {
258            return new ObjectName(jmxDomainName + ":type=Broker,brokerName="
259                                  + (brokerName != null ? brokerName : "*") + ","
260                                  + "destinationType=*,destinationName=*,"
261                                  + "endpoint=Consumer,"
262                                  + "clientId=" + JMXSupport.encodeObjectNamePart(clientId) + ","
263                                  + "consumerId=*");
264        }
265    
266        public static ObjectName createProducerQueury(String jmxDomainName, String clientId) throws MalformedObjectNameException {
267            return createProducerQueury(jmxDomainName, null, clientId);
268        }
269    
270        public static ObjectName createProducerQueury(String jmxDomainName, String brokerName, String clientId) throws MalformedObjectNameException {
271            return new ObjectName(jmxDomainName + ":type=Broker,brokerName="
272                    + (brokerName != null ? brokerName : "*") + ","
273                    + "destinationType=*,destinationName=*,"
274                    + "endpoint=Producer,"
275                    + "clientId=" + JMXSupport.encodeObjectNamePart(clientId) + ","
276                    + "producerId=*");
277        }
278    
279    }