View Javadoc
1   /**
2    * Copyright 2005-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.kew.plugin;
17  
18  import java.util.ArrayList;
19  import java.util.Collections;
20  import java.util.List;
21  
22  import javax.xml.namespace.QName;
23  
24  import org.kuali.rice.core.api.config.CoreConfigHelper;
25  import org.kuali.rice.core.api.resourceloader.ResourceLoader;
26  import org.kuali.rice.core.api.resourceloader.ResourceLoaderContainer;
27  
28  /**
29   * A base class for {@link PluginRegistry} implementations.  Is essentially a ResourceLoader 
30   * implementation that ensures plugins are the only ResourceLoaders used.  Also maintains
31   * information about the PluginEnvironments of the loaded plugins in this registry.
32   * 
33   * @see Plugin
34   * @see PluginEnvironment
35   * 
36   * @author Kuali Rice Team (rice.collab@kuali.org)
37   */
38  public abstract class BasePluginRegistry extends ResourceLoaderContainer implements PluginRegistry {
39  	
40  	private List<PluginEnvironment> pluginEnvironments = Collections.synchronizedList(new ArrayList<PluginEnvironment>());
41  	
42  	public BasePluginRegistry() {
43  		super(new QName(CoreConfigHelper.getApplicationId(), ResourceLoader.PLUGIN_REGISTRY_LOADER_NAME));
44  	}
45  	
46  	public BasePluginRegistry(QName name) {
47  		super(name);
48  	}
49  	    
50      public PluginEnvironment getPluginEnvironment(String pluginName) {
51      	for (PluginEnvironment environment : pluginEnvironments) {
52      		if (environment.getPluginName().equals(pluginName)) {
53      			return environment;
54      		}
55      	}
56      	return null;
57      }
58  	
59  	public void addPluginEnvironment(PluginEnvironment pluginEnvironment) {
60  		// chances are that this plugin has already been added to the resource loader
61  		if (pluginEnvironment.getPlugin() != null && !containsResourceLoader(pluginEnvironment.getPlugin())) {
62  			addResourceLoader(pluginEnvironment.getPlugin());
63  		}
64  		pluginEnvironments.add(pluginEnvironment);
65  	}
66  	
67  	public PluginEnvironment removePluginEnvironment(String pluginName) {
68  	    PluginEnvironment environment = getPluginEnvironment(pluginName);
69          if (environment == null) {
70              return null;
71          }
72          if (environment.getPlugin() != null) {
73              super.removeResourceLoader(environment.getPlugin().getName());
74          }
75  		if (!pluginEnvironments.remove(environment)) {
76  			return null;
77  		}
78  		return environment;
79  	}
80  
81  	public Plugin getPlugin(QName pluginName) {
82  		return (Plugin)getResourceLoader(pluginName);
83  	}
84  
85  	public List<QName> getPluginNames() {
86  		return super.getResourceLoaderNames();
87  	}
88  	
89  	public List<PluginEnvironment> getPluginEnvironments() {
90  		return Collections.unmodifiableList(pluginEnvironments);
91  	}
92  }