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