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,
071            PolicyEntry updatedEntry) {
072        PolicyEntryUtil.applyRetrospectively(runtimeBroker, updatedEntry, null);
073    }
074
075    /**
076     *
077     * Utility to properly apply an updated policy entry to all existing destinations that
078     * match this entry.  The destination will only be updated if the policy is the exact
079     * policy (most specific) that matches the destination.
080     *
081     * The includedProperties List is optional and is used to specify a list of properties
082     * to apply retrospectively to the matching destinations. This allows only certain properties
083     * to be reapplied.  If the list is null then all properties will be applied.
084     *
085     * @param runtimeBroker
086     * @param updatedEntry
087     * @param includedProperties
088     */
089    public static void applyRetrospectively(AbstractRuntimeConfigurationBroker runtimeBroker,
090            PolicyEntry updatedEntry, Set<String> includedProperties) {
091        RegionBroker regionBroker = (RegionBroker) runtimeBroker.getBrokerService().getRegionBroker();
092        for (Destination destination : regionBroker.getDestinations(updatedEntry.getDestination())) {
093            //Look up the policy that applies to the destination
094            PolicyEntry specificyPolicy = regionBroker.getDestinationPolicy().getEntryFor(
095                    destination.getActiveMQDestination());
096
097            //only update the destination if it matches the specific policy being updated
098            //currently just an identity check which is what we want
099            if (updatedEntry.equals(specificyPolicy)){
100                Destination target = destination;
101                while (target instanceof DestinationFilter) {
102                    target = ((DestinationFilter)target).getNext();
103                }
104                //If we are providing a list of properties to set then use them
105                //to set eligible properties that are in the includedProperties list
106                if (target.getActiveMQDestination().isQueue()) {
107                    updatedEntry.update((Queue) target, includedProperties);
108                } else if (target.getActiveMQDestination().isTopic()) {
109                    updatedEntry.update((Topic) target, includedProperties);
110                }
111                runtimeBroker.debug("applied update to:" + target);
112            }
113        }
114    }
115}