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