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 java.util.ArrayList;
020import java.util.List;
021
022import org.apache.activemq.broker.region.CompositeDestinationInterceptor;
023import org.apache.activemq.broker.region.DestinationInterceptor;
024import org.apache.activemq.broker.region.RegionBroker;
025import org.apache.activemq.broker.region.virtual.CompositeQueue;
026import org.apache.activemq.broker.region.virtual.CompositeTopic;
027import org.apache.activemq.broker.region.virtual.VirtualDestination;
028import org.apache.activemq.broker.region.virtual.VirtualDestinationInterceptor;
029import org.apache.activemq.broker.region.virtual.VirtualTopic;
030import org.apache.activemq.schema.core.DtoCompositeQueue;
031import org.apache.activemq.schema.core.DtoCompositeTopic;
032import org.apache.activemq.schema.core.DtoVirtualDestinationInterceptor;
033import org.apache.activemq.schema.core.DtoVirtualTopic;
034
035public class VirtualDestinationInterceptorProcessor extends DefaultConfigurationProcessor {
036
037    public VirtualDestinationInterceptorProcessor(RuntimeConfigurationBroker plugin, Class configurationClass) {
038        super(plugin, configurationClass);
039    }
040
041    @Override
042    public void addNew(Object o) {
043        final DtoVirtualDestinationInterceptor dto = (DtoVirtualDestinationInterceptor) o;
044
045        plugin.addDestinationWork.add(new UpdateVirtualDestinationsTask(plugin) {
046
047            @Override
048            protected VirtualDestination[] getVirtualDestinations() {
049                return fromDto(dto);
050            }
051
052        });
053
054    }
055
056    @Override
057    public void remove(Object o) {
058        // whack it
059        plugin.addDestinationWork.add(new Runnable() {
060            @Override
061            public void run() {
062                List<DestinationInterceptor> interceptorsList = new ArrayList<DestinationInterceptor>();
063                for (DestinationInterceptor candidate : plugin.getBrokerService().getDestinationInterceptors()) {
064                    if (!(candidate instanceof VirtualDestinationInterceptor)) {
065                        interceptorsList.add(candidate);
066                    }
067                }
068                DestinationInterceptor[] destinationInterceptors = interceptorsList.toArray(new DestinationInterceptor[]{});
069                plugin.getBrokerService().setDestinationInterceptors(destinationInterceptors);
070                ((CompositeDestinationInterceptor) ((RegionBroker) plugin.getBrokerService().getRegionBroker()).getDestinationInterceptor()).setInterceptors(destinationInterceptors);
071                plugin.info("removed VirtualDestinationInterceptor from: " + interceptorsList);
072            }
073        });
074    }
075
076    private VirtualDestination[] fromDto(DtoVirtualDestinationInterceptor virtualDestinationInterceptor) {
077        List<VirtualDestination> answer = new ArrayList<VirtualDestination>();
078        for (Object vd : filter(virtualDestinationInterceptor, DtoVirtualDestinationInterceptor.VirtualDestinations.class)) {
079            for (Object vt : filter(vd, DtoVirtualTopic.class)) {
080                answer.add(fromDto(vt, new VirtualTopic()));
081            }
082            for (Object vt : filter(vd, DtoCompositeTopic.class)) {
083                answer.add(fromDto(vt, new CompositeTopic()));
084            }
085            for (Object vt : filter(vd, DtoCompositeQueue.class)) {
086                answer.add(fromDto(vt, new CompositeQueue()));
087            }
088        }
089        VirtualDestination[] array = new VirtualDestination[answer.size()];
090        answer.toArray(array);
091        return array;
092    }
093
094
095}