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.util;
018
019import java.util.Set;
020
021import org.apache.activemq.broker.region.Destination;
022import org.apache.activemq.broker.region.DestinationFilter;
023import org.apache.activemq.broker.region.Queue;
024import org.apache.activemq.broker.region.RegionBroker;
025import org.apache.activemq.broker.region.Topic;
026import org.apache.activemq.broker.region.policy.PolicyEntry;
027import org.apache.activemq.broker.region.policy.PolicyMap;
028import org.apache.activemq.plugin.AbstractRuntimeConfigurationBroker;
029
030
031public class PolicyEntryUtil {
032
033
034    /**
035     * Find a matching PolicyEntry by looking up the Set of entries from the map and
036     * then comparing the destination to find the exact match.  This lets us be able to
037     * find the correct policy entry to update even though there might be multiple that
038     * are returned from the get method of the PolicyMap.
039     *
040     * @param runtimeBroker
041     * @param entry
042     * @return
043     */
044    public static PolicyEntry findEntryByDestination(AbstractRuntimeConfigurationBroker runtimeBroker,
045            PolicyEntry entry) {
046
047        PolicyMap existingMap = runtimeBroker.getBrokerService().getDestinationPolicy();
048        @SuppressWarnings("unchecked")
049        Set<PolicyEntry> existingEntries = existingMap.get(entry.getDestination());
050
051        //First just look up by the destination type to see if anything matches
052        PolicyEntry existingEntry = null;
053        for (PolicyEntry ee: existingEntries) {
054            if (ee.getDestination().equals(entry.getDestination())) {
055                existingEntry = ee;
056                break;
057            }
058        }
059        return existingEntry;
060    }
061
062    /**
063     * Utility to properly apply an updated policy entry to all existing destinations that
064     * match this entry.  The destination will only be updated if the policy is the exact
065     * policy (most specific) that matches the destination.
066     *
067     * @param runtimeBroker
068     * @param updatedEntry
069     */
070    public static void applyRetrospectively(AbstractRuntimeConfigurationBroker runtimeBroker, PolicyEntry updatedEntry) {
071        RegionBroker regionBroker = (RegionBroker) runtimeBroker.getBrokerService().getRegionBroker();
072        for (Destination destination : regionBroker.getDestinations(updatedEntry.getDestination())) {
073            //Look up the policy that applies to the destination
074            PolicyEntry specificyPolicy = regionBroker.getDestinationPolicy().getEntryFor(
075                    destination.getActiveMQDestination());
076
077            //only update the destination if it matches the specific policy being updated
078            //currently just an identity check which is what we want
079            if (updatedEntry.equals(specificyPolicy)){
080                Destination target = destination;
081                if (destination instanceof DestinationFilter) {
082                    target = ((DestinationFilter)destination).getNext();
083                }
084                if (target.getActiveMQDestination().isQueue()) {
085                    updatedEntry.update((Queue) target);
086                } else if (target.getActiveMQDestination().isTopic()) {
087                    updatedEntry.update((Topic) target);
088                }
089                runtimeBroker.debug("applied update to:" + target);
090            }
091        }
092    }
093}