001 /**
002 * Copyright 2005-2011 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 */
016 package org.kuali.rice.kew.plugin;
017
018 import org.apache.commons.lang.StringUtils;
019 import org.apache.log4j.Logger;
020 import org.kuali.rice.core.api.CoreConstants;
021 import org.kuali.rice.core.api.config.CoreConfigHelper;
022 import org.kuali.rice.core.api.config.property.Config;
023 import org.kuali.rice.core.api.config.property.ConfigContext;
024 import org.kuali.rice.core.api.util.ClassLoaderUtils;
025 import org.kuali.rice.core.api.util.ContextClassLoaderBinder;
026 import org.kuali.rice.core.api.util.xml.XmlException;
027
028 import javax.xml.namespace.QName;
029 import java.io.File;
030 import java.io.FileNotFoundException;
031 import java.io.IOException;
032 import java.net.MalformedURLException;
033 import java.net.URL;
034
035
036 /**
037 * Abstract base PluginLoader implementation.
038 * Delegates to template methods to obtain plugin ClassLoader and plugin config file URL,
039 * then load the config under the plugin ClassLoader, and constructs a Plugin object.
040 *
041 * @author Kuali Rice Team (rice.collab@kuali.org)
042 */
043 public abstract class BasePluginLoader implements PluginLoader {
044 private static final Logger LOG = Logger.getLogger(BasePluginLoader.class);
045
046 private static final String META_INF_PATH = "META-INF";
047 private static final String PLUGIN_CONFIG_PATH = META_INF_PATH + "/workflow.xml";
048
049 protected final String simplePluginName;
050 protected String logPrefix;
051
052 protected final ClassLoader parentClassLoader;
053 protected final Config parentConfig;
054 protected final File sharedPluginDirectory;
055 protected String pluginConfigPath = PLUGIN_CONFIG_PATH;
056
057 public BasePluginLoader(String simplePluginName, File sharedPluginDirectory, ClassLoader parentClassLoader, Config parentConfig) {
058 this.sharedPluginDirectory = sharedPluginDirectory;
059 if (parentClassLoader == null) {
060 parentClassLoader = ClassLoaderUtils.getDefaultClassLoader();
061 }
062 this.parentClassLoader = parentClassLoader;
063 this.parentConfig = parentConfig;
064 this.simplePluginName = simplePluginName;
065 this.logPrefix = simplePluginName;
066 }
067
068 protected String getLogPrefix() {
069 return logPrefix;
070 }
071
072 public String getPluginName() {
073 return simplePluginName;
074 }
075
076 public void setPluginConfigPath(String pluginConfigPath) {
077 this.pluginConfigPath = pluginConfigPath;
078 }
079
080 protected String getSimplePluginName() {
081 return simplePluginName;
082 }
083
084 /**
085 * Template method that subclasses should implement to supply an appropriate
086 * plugin ClassLoader
087 * @return an appropriate PluginClassLoader
088 * @throws IOException if anything goes awry
089 */
090 protected abstract PluginClassLoader createPluginClassLoader() throws IOException;
091 /**
092 * Template method that subclasses should implement to supply an appropriate
093 * URL to the plugin's configuration
094 * @return an appropriate URL to the plugin's configuration
095 * @throws IOException if anything goes awry
096 */
097 protected abstract URL getPluginConfigURL() throws PluginException, IOException;
098
099 /**
100 * Loads and creates the Plugin.
101 */
102 public Plugin load() throws Exception {
103 PluginClassLoader classLoader = createPluginClassLoader();
104 LOG.info("Created plugin ClassLoader: " + classLoader);
105 ContextClassLoaderBinder.bind(classLoader);
106 try {
107 return loadWithinContextClassLoader(classLoader);
108 } finally {
109 ContextClassLoaderBinder.unbind();
110 }
111 }
112
113 public boolean isRemoved() {
114 return false;
115 }
116
117 /**
118 * Executes loading of the plugin within the current context classloader set to the Plugin's classloader.
119 */
120 protected Plugin loadWithinContextClassLoader(PluginClassLoader classLoader) throws PluginException, IOException {
121 URL url = getPluginConfigURL();
122 PluginConfig pluginConfig = loadPluginConfig(url);
123 QName qPluginName = getPluginName(pluginConfig);
124 classLoader.setConfig(pluginConfig);
125 ConfigContext.init(classLoader, pluginConfig);
126 configureExtraClasspath(classLoader, pluginConfig);
127 this.logPrefix = PluginUtils.getLogPrefix(qPluginName).toString();
128 LOG.info("Constructing plugin '" + simplePluginName + "' with classloader: " + classLoader);
129 Plugin plugin = new Plugin(qPluginName, pluginConfig, classLoader);
130 installResourceLoader(plugin);
131 installPluginListeners(plugin);
132 return plugin;
133 }
134
135 protected void installResourceLoader(Plugin plugin) {
136 PluginUtils.installResourceLoader(plugin);
137 }
138
139 protected void installPluginListeners(Plugin plugin) {
140 PluginUtils.installPluginListeners(plugin);
141 }
142
143 protected void configureExtraClasspath(PluginClassLoader classLoader, PluginConfig config) throws MalformedURLException {
144 String extraClassesDirs = config.getProperty(Config.EXTRA_CLASSES_DIR);
145 if (!org.apache.commons.lang.StringUtils.isEmpty(extraClassesDirs)) {
146 String[] extraClasses = extraClassesDirs.split(",");
147 for (int index = 0; index < extraClasses.length; index++) {
148 File extraClassesDir = new File(extraClasses[index]);
149 if (extraClassesDir.exists()) {
150 classLoader.addClassesDirectory(extraClassesDir);
151 }
152 }
153 }
154 String extraLibDirs = config.getProperty(Config.EXTRA_LIB_DIR);
155 if (!org.apache.commons.lang.StringUtils.isEmpty(extraLibDirs)) {
156 String[] extraLibs = extraLibDirs.split(",");
157 for (int index = 0; index < extraLibs.length; index++) {
158 File extraLibDir = new File(extraLibs[index]);
159 if (extraLibDir.exists()) {
160 classLoader.addLibDirectory(extraLibDir);
161 }
162 }
163 }
164 }
165
166
167 protected QName getPluginName(PluginConfig pluginConfig) {
168 String applicationId = pluginConfig.getProperty(CoreConstants.Config.APPLICATION_ID);
169 QName qPluginName = null;
170 if (StringUtils.isBlank(applicationId)) {
171 qPluginName = new QName(CoreConfigHelper.getApplicationId(), simplePluginName);
172 } else {
173 qPluginName = new QName(applicationId, simplePluginName);
174 }
175 return qPluginName;
176 }
177
178 protected PluginConfig loadPluginConfig(URL url) {
179 PluginConfigParser parser = new PluginConfigParser();
180 try {
181 PluginConfig pluginConfig = parser.parse(url, parentConfig);
182 pluginConfig.parseConfig();
183 return pluginConfig;
184 } catch (FileNotFoundException e) {
185 throw new PluginException(getLogPrefix() + " Could not locate the plugin config file at path " + url, e);
186 } catch (IOException ioe) {
187 throw new PluginException(getLogPrefix() + " Could not read the plugin config file", ioe);
188 } catch (XmlException ixe) {
189 throw new PluginException(getLogPrefix() + " Could not parse the plugin config file", ixe);
190 }
191 }
192 }