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.broker.region.policy.PolicyEntry;
020import org.apache.activemq.broker.region.policy.PolicyMap;
021import org.apache.activemq.plugin.util.PolicyEntryUtil;
022
023public class PolicyEntryProcessor extends DefaultConfigurationProcessor {
024
025    public PolicyEntryProcessor(RuntimeConfigurationBroker plugin, Class<?> configurationClass) {
026        super(plugin, configurationClass);
027    }
028
029    @Override
030    public void addNew(Object o) {
031        PolicyEntry addition = fromDto(o, new PolicyEntry());
032        PolicyMap existingMap = plugin.getBrokerService().getDestinationPolicy();
033        existingMap.put(addition.getDestination(), addition);
034        applyRetrospectively(addition);
035        plugin.info("added policy for: " + addition.getDestination());
036    }
037
038    @Override
039    public void modify(Object existing, Object candidate) {
040        PolicyEntry updatedEntry = fromDto(candidate, new PolicyEntry());
041
042        //Look up an existing entry that matches the candidate
043        //First just look up by the destination type to see if anything matches
044        PolicyEntry existingEntry = PolicyEntryUtil.findEntryByDestination(plugin, updatedEntry);
045        if (existingEntry != null) {
046            //if found, update the policy and apply the updates to existing destinations
047            updatedEntry = fromDto(candidate, existingEntry);
048            applyRetrospectively(updatedEntry);
049            plugin.info("updated policy for: " + updatedEntry.getDestination());
050        } else {
051            plugin.info("cannot find policy entry candidate to update: " + updatedEntry + ", destination:" + updatedEntry.getDestination());
052        }
053    }
054
055    protected void applyRetrospectively(PolicyEntry updatedEntry) {
056        PolicyEntryUtil.applyRetrospectively(plugin, updatedEntry);
057    }
058}