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.filter.DestinationMapEntry;
020import org.apache.activemq.security.*;
021import org.apache.activemq.schema.core.DtoAuthorizationPlugin;
022import org.apache.activemq.schema.core.DtoAuthorizationMap;
023import org.apache.activemq.schema.core.DtoAuthorizationEntry;
024
025import java.util.LinkedList;
026import java.util.List;
027
028public class AuthorizationPluginProcessor extends DefaultConfigurationProcessor {
029
030    public AuthorizationPluginProcessor(RuntimeConfigurationBroker plugin, Class configurationClass) {
031        super(plugin, configurationClass);
032    }
033
034    @Override
035    public void modify(Object existing, Object candidate) {
036        try {
037            // replace authorization map - need exclusive write lock to total broker
038            AuthorizationBroker authorizationBroker =
039                    (AuthorizationBroker) plugin.getBrokerService().getBroker().getAdaptor(AuthorizationBroker.class);
040
041            authorizationBroker.setAuthorizationMap(fromDto(filter(candidate, DtoAuthorizationPlugin.Map.class)));
042        } catch (Exception e) {
043            plugin.info("failed to apply modified AuthorizationMap to AuthorizationBroker", e);
044        }
045    }
046
047    private AuthorizationMap fromDto(List<Object> map) {
048        XBeanAuthorizationMap xBeanAuthorizationMap = new XBeanAuthorizationMap();
049        for (Object o : map) {
050            if (o instanceof DtoAuthorizationPlugin.Map) {
051                DtoAuthorizationPlugin.Map dtoMap = (DtoAuthorizationPlugin.Map) o;
052                List<DestinationMapEntry> entries = new LinkedList<DestinationMapEntry>();
053                // revisit - would like to map getAuthorizationMap to generic getContents
054                for (Object authMap : filter(dtoMap.getAuthorizationMap(), DtoAuthorizationMap.AuthorizationEntries.class)) {
055                    for (Object entry : filter(getContents(authMap), DtoAuthorizationEntry.class)) {
056                        entries.add(fromDto(entry, new XBeanAuthorizationEntry()));
057                    }
058                }
059                xBeanAuthorizationMap.setAuthorizationEntries(entries);
060                xBeanAuthorizationMap.setGroupClass(dtoMap.getAuthorizationMap().getGroupClass());
061                try {
062                    xBeanAuthorizationMap.afterPropertiesSet();
063                } catch (Exception e) {
064                    plugin.info("failed to update xBeanAuthorizationMap auth entries:", e);
065                }
066
067                for (Object entry : filter(dtoMap.getAuthorizationMap(), DtoAuthorizationMap.TempDestinationAuthorizationEntry.class)) {
068                    // another restriction - would like to be getContents
069                    DtoAuthorizationMap.TempDestinationAuthorizationEntry dtoEntry = (DtoAuthorizationMap.TempDestinationAuthorizationEntry) entry;
070                    xBeanAuthorizationMap.setTempDestinationAuthorizationEntry(fromDto(dtoEntry.getTempDestinationAuthorizationEntry(), new TempDestinationAuthorizationEntry()));
071                }
072            } else {
073                plugin.info("No support for updates to: " + o);
074            }
075        }
076        return xBeanAuthorizationMap;
077    }
078}