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.network;
018    
019    import java.util.HashMap;
020    import java.util.Map;
021    
022    import javax.management.MalformedObjectNameException;
023    import javax.management.ObjectName;
024    
025    import org.apache.activemq.broker.BrokerService;
026    import org.apache.activemq.broker.jmx.AnnotatedMBean;
027    import org.apache.activemq.broker.jmx.NetworkBridgeView;
028    import org.apache.activemq.broker.jmx.NetworkBridgeViewMBean;
029    import org.apache.activemq.util.JMXSupport;
030    import org.slf4j.Logger;
031    import org.slf4j.LoggerFactory;
032    
033    public class MBeanNetworkListener implements NetworkBridgeListener {
034    
035        private static final Logger LOG = LoggerFactory.getLogger(MBeanNetworkListener.class);
036    
037        BrokerService brokerService;
038        ObjectName connectorName;
039        boolean createdByDuplex = false;
040    
041        public MBeanNetworkListener(BrokerService brokerService, ObjectName connectorName) {
042            this.brokerService = brokerService;
043            this.connectorName = connectorName;
044        }
045    
046        @Override
047        public void bridgeFailed() {
048        }
049    
050        @Override
051        public void onStart(NetworkBridge bridge) {
052            if (!brokerService.isUseJmx()) {
053                return;
054            }
055            NetworkBridgeViewMBean view = new NetworkBridgeView(bridge);
056            ((NetworkBridgeView)view).setCreateByDuplex(createdByDuplex);
057            try {
058                ObjectName objectName = createNetworkBridgeObjectName(bridge);
059                AnnotatedMBean.registerMBean(brokerService.getManagementContext(), view, objectName);
060                bridge.setMbeanObjectName(objectName);
061                if (LOG.isDebugEnabled()) {
062                    LOG.debug("registered: " + bridge + " as: " + objectName);
063                }
064            } catch (Throwable e) {
065                LOG.debug("Network bridge could not be registered in JMX: " + e.getMessage(), e);
066            }
067        }
068    
069        @Override
070        public void onStop(NetworkBridge bridge) {
071            if (!brokerService.isUseJmx()) {
072                return;
073            }
074            try {
075                ObjectName objectName = bridge.getMbeanObjectName();
076                if (objectName != null) {
077                    brokerService.getManagementContext().unregisterMBean(objectName);
078                }
079            } catch (Throwable e) {
080                LOG.debug("Network bridge could not be unregistered in JMX: " + e.getMessage(), e);
081            }
082        }
083    
084        protected ObjectName createNetworkBridgeObjectName(NetworkBridge bridge) throws MalformedObjectNameException {
085            Map<String, String> map = new HashMap<String, String>(connectorName.getKeyPropertyList());
086            return new ObjectName(connectorName.getDomain() + ":" + "BrokerName=" + JMXSupport.encodeObjectNamePart((String) map.get("BrokerName")) + "," + "Type=NetworkBridge,"
087                                  + "NetworkConnectorName=" + JMXSupport.encodeObjectNamePart((String)map.get("NetworkConnectorName")) + "," + "Name="
088                                  + JMXSupport.encodeObjectNamePart(JMXSupport.encodeObjectNamePart(bridge.getRemoteAddress())));
089        }
090    
091        public void setCreatedByDuplex(boolean createdByDuplex) {
092            this.createdByDuplex = createdByDuplex;
093        }
094    }