no.dusken.common.plugin.security.PluginAccessDecisionVoterManager.java Source code

Java tutorial

Introduction

Here is the source code for no.dusken.common.plugin.security.PluginAccessDecisionVoterManager.java

Source

/*
 Copyright 2006 - 2010 Under Dusken
    
 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at
    
 http://www.apache.org/licenses/LICENSE-2.0
    
 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 */
package no.dusken.common.plugin.security;

import no.dusken.common.plugin.DuskenPlugin;
import org.kantega.jexmec.PluginManager;
import org.kantega.jexmec.PluginManagerListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.AccessDecisionVoter;
import org.springframework.security.access.vote.AffirmativeBased;

import javax.annotation.PostConstruct;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

/**
 * @author Marvin B. Lillehaug <lillehau@underdusken.no>
 */
public class PluginAccessDecisionVoterManager extends AffirmativeBased
        implements PluginManagerListener<DuskenPlugin> {

    private List<PluginManager<DuskenPlugin>> pluginManagers;

    private int providerManagersStarted = 0;

    public PluginAccessDecisionVoterManager(List<AccessDecisionVoter> decisionVoters) {
        super(decisionVoters);
    }

    @PostConstruct
    public void init() {
        for (PluginManager<DuskenPlugin> pm : pluginManagers) {
            pm.addPluginManagerListener(this);
        }
    }

    @Autowired
    public void setPluginManagers(List<PluginManager<DuskenPlugin>> pluginManagers) {
        this.pluginManagers = pluginManagers;
    }

    @Override
    public void pluginManagerStarted() {
        if (++providerManagersStarted == pluginManagers.size()) {
            Set<AccessDecisionVoter> accessDecisionVoters = new HashSet<AccessDecisionVoter>();
            try {
                accessDecisionVoters.addAll(getDecisionVoters());
            } catch (IllegalArgumentException iae) {
                // No accessDecisionVoters!
            }
            for (PluginManager<DuskenPlugin> pm : pluginManagers) {
                for (DuskenPlugin plugin : pm.getPlugins()) {
                    AccessDecisionVoter voter = plugin.getAccessDecisionVoter();
                    if (voter != null) {
                        accessDecisionVoters.add(voter);
                    }
                }
            }
            setDecisionVoters(new LinkedList<AccessDecisionVoter>(accessDecisionVoters));
        }
    }

    @Override
    public void classLoaderAdded(ClassLoader classLoader) {
    }

    @Override
    public void classLoaderRemoved(ClassLoader classLoader) {
    }
}