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