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.plugin;
018
019import org.apache.activemq.network.DiscoveryNetworkConnector;
020import org.apache.activemq.network.NetworkConnector;
021import org.apache.activemq.schema.core.DtoNetworkConnector;
022import org.apache.activemq.util.IntrospectionSupport;
023
024import java.util.TreeMap;
025
026public class NetworkConnectorProcessor extends DefaultConfigurationProcessor {
027
028    public NetworkConnectorProcessor(RuntimeConfigurationBroker plugin, Class configurationClass) {
029        super(plugin, configurationClass);
030    }
031
032    @Override
033    public void addNew(Object o) {
034        DtoNetworkConnector networkConnector = (DtoNetworkConnector) o;
035        if (networkConnector.getUri() != null) {
036            try {
037                DiscoveryNetworkConnector nc = fromDto(networkConnector, new DiscoveryNetworkConnector());
038                plugin.getBrokerService().addNetworkConnector(nc);
039                nc.start();
040                plugin.info("started new network connector: " + nc);
041            } catch (Exception e) {
042                plugin.info("Failed to add new networkConnector " + networkConnector, e);
043            }
044        }
045    }
046
047    @Override
048    public void remove(Object o) {
049        DtoNetworkConnector toRemove = (DtoNetworkConnector) o;
050        for (NetworkConnector existingCandidate :
051                plugin.getBrokerService().getNetworkConnectors()) {
052            if (configMatch(toRemove, existingCandidate)) {
053                if (plugin.getBrokerService().removeNetworkConnector(existingCandidate)) {
054                    try {
055                        existingCandidate.stop();
056                        plugin.info("stopped and removed networkConnector: " + existingCandidate);
057                    } catch (Exception e) {
058                        plugin.info("Failed to stop removed network connector: " + existingCandidate);
059                    }
060                }
061            }
062        }
063    }
064
065    private boolean configMatch(DtoNetworkConnector dto, NetworkConnector candidate) {
066        TreeMap<String, String> dtoProps = new TreeMap<String, String>();
067        IntrospectionSupport.getProperties(dto, dtoProps, null);
068
069        TreeMap<String, String> candidateProps = new TreeMap<String, String>();
070        IntrospectionSupport.getProperties(candidate, candidateProps, null);
071
072        // every dto prop must be present in the candidate
073        for (String key : dtoProps.keySet()) {
074            if (!candidateProps.containsKey(key) || !candidateProps.get(key).equals(dtoProps.get(key))) {
075                return false;
076            }
077        }
078        return true;
079    }
080}