001/**
002 * Copyright 2005-2015 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.kew.plugin;
017
018import java.util.ArrayList;
019import java.util.Collections;
020import java.util.List;
021
022import javax.xml.namespace.QName;
023
024import org.kuali.rice.core.api.config.CoreConfigHelper;
025import org.kuali.rice.core.api.resourceloader.ResourceLoader;
026import org.kuali.rice.core.api.resourceloader.ResourceLoaderContainer;
027
028/**
029 * A base class for {@link PluginRegistry} implementations.  Is essentially a ResourceLoader 
030 * implementation that ensures plugins are the only ResourceLoaders used.  Also maintains
031 * information about the PluginEnvironments of the loaded plugins in this registry.
032 * 
033 * @see Plugin
034 * @see PluginEnvironment
035 * 
036 * @author Kuali Rice Team (rice.collab@kuali.org)
037 */
038public abstract class BasePluginRegistry extends ResourceLoaderContainer implements PluginRegistry {
039        
040        private List<PluginEnvironment> pluginEnvironments = Collections.synchronizedList(new ArrayList<PluginEnvironment>());
041        
042        public BasePluginRegistry() {
043                super(new QName(CoreConfigHelper.getApplicationId(), ResourceLoader.PLUGIN_REGISTRY_LOADER_NAME));
044        }
045        
046        public BasePluginRegistry(QName name) {
047                super(name);
048        }
049            
050    public PluginEnvironment getPluginEnvironment(String pluginName) {
051        for (PluginEnvironment environment : pluginEnvironments) {
052                if (environment.getPluginName().equals(pluginName)) {
053                        return environment;
054                }
055        }
056        return null;
057    }
058        
059        public void addPluginEnvironment(PluginEnvironment pluginEnvironment) {
060                // chances are that this plugin has already been added to the resource loader
061                if (pluginEnvironment.getPlugin() != null && !containsResourceLoader(pluginEnvironment.getPlugin())) {
062                        addResourceLoader(pluginEnvironment.getPlugin());
063                }
064                pluginEnvironments.add(pluginEnvironment);
065        }
066        
067        public PluginEnvironment removePluginEnvironment(String pluginName) {
068            PluginEnvironment environment = getPluginEnvironment(pluginName);
069        if (environment == null) {
070            return null;
071        }
072        if (environment.getPlugin() != null) {
073            super.removeResourceLoader(environment.getPlugin().getName());
074        }
075                if (!pluginEnvironments.remove(environment)) {
076                        return null;
077                }
078                return environment;
079        }
080
081        public Plugin getPlugin(QName pluginName) {
082                return (Plugin)getResourceLoader(pluginName);
083        }
084
085        public List<QName> getPluginNames() {
086                return super.getResourceLoaderNames();
087        }
088        
089        public List<PluginEnvironment> getPluginEnvironments() {
090                return Collections.unmodifiableList(pluginEnvironments);
091        }
092}