Java tutorial
/* 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.authentication.AuthenticationProvider; import org.springframework.security.authentication.ProviderManager; import java.util.HashSet; import java.util.LinkedList; import java.util.List; import java.util.Set; /** * @author Marvin B. Lillehaug <lillehau@underdusken.no> * Custom providermanager that adds AuthenticationProviders from plugins. */ public class PluginAuthenticationProviderManager implements PluginManagerListener<DuskenPlugin> { private List<PluginManager<DuskenPlugin>> pluginManagers; private ProviderManager authenticationManager; private int providerManagersStarted = 0; public void init() { for (PluginManager<DuskenPlugin> pm : pluginManagers) { pm.addPluginManagerListener(this); } } @Autowired public void setPluginManagers(List<PluginManager<DuskenPlugin>> pluginManagers) { this.pluginManagers = pluginManagers; } @Autowired public void setAuthenticationManager(ProviderManager authenticationManager) { this.authenticationManager = authenticationManager; } public void pluginManagerStarted() { if (++providerManagersStarted == pluginManagers.size()) { Set providers = new HashSet(); try { providers.addAll(authenticationManager.getProviders()); } catch (IllegalArgumentException iae) { // No providers! } for (PluginManager<DuskenPlugin> pm : pluginManagers) { for (DuskenPlugin plugin : pm.getPlugins()) { AuthenticationProvider provider = plugin.getAuthenticationProvider(); if (provider != null) { providers.add(provider); } } } authenticationManager.setProviders(new LinkedList(providers)); } } public void classLoaderAdded(ClassLoader classLoader) { // Do not care about this. } public void classLoaderRemoved(ClassLoader classLoader) { // Do not care about this. } }