| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
package org.kuali.rice.kew.plugin; |
| 18 | |
|
| 19 | |
import org.apache.commons.lang.StringUtils; |
| 20 | |
import org.apache.log4j.Logger; |
| 21 | |
import org.kuali.rice.core.api.CoreConstants; |
| 22 | |
import org.kuali.rice.core.api.config.CoreConfigHelper; |
| 23 | |
import org.kuali.rice.core.api.config.property.Config; |
| 24 | |
import org.kuali.rice.core.api.config.property.ConfigContext; |
| 25 | |
import org.kuali.rice.core.api.util.ClassLoaderUtils; |
| 26 | |
import org.kuali.rice.core.api.util.ContextClassLoaderBinder; |
| 27 | |
import org.kuali.rice.core.api.util.xml.XmlException; |
| 28 | |
|
| 29 | |
import javax.xml.namespace.QName; |
| 30 | |
import java.io.File; |
| 31 | |
import java.io.FileNotFoundException; |
| 32 | |
import java.io.IOException; |
| 33 | |
import java.net.MalformedURLException; |
| 34 | |
import java.net.URL; |
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
public abstract class BasePluginLoader implements PluginLoader { |
| 45 | 0 | private static final Logger LOG = Logger.getLogger(BasePluginLoader.class); |
| 46 | |
|
| 47 | |
private static final String META_INF_PATH = "META-INF"; |
| 48 | |
private static final String PLUGIN_CONFIG_PATH = META_INF_PATH + "/workflow.xml"; |
| 49 | |
|
| 50 | |
protected final String simplePluginName; |
| 51 | |
protected String logPrefix; |
| 52 | |
|
| 53 | |
protected final ClassLoader parentClassLoader; |
| 54 | |
protected final Config parentConfig; |
| 55 | |
protected final File sharedPluginDirectory; |
| 56 | 0 | protected String pluginConfigPath = PLUGIN_CONFIG_PATH; |
| 57 | |
|
| 58 | 0 | public BasePluginLoader(String simplePluginName, File sharedPluginDirectory, ClassLoader parentClassLoader, Config parentConfig) { |
| 59 | 0 | this.sharedPluginDirectory = sharedPluginDirectory; |
| 60 | 0 | if (parentClassLoader == null) { |
| 61 | 0 | parentClassLoader = ClassLoaderUtils.getDefaultClassLoader(); |
| 62 | |
} |
| 63 | 0 | this.parentClassLoader = parentClassLoader; |
| 64 | 0 | this.parentConfig = parentConfig; |
| 65 | 0 | this.simplePluginName = simplePluginName; |
| 66 | 0 | this.logPrefix = simplePluginName; |
| 67 | 0 | } |
| 68 | |
|
| 69 | |
protected String getLogPrefix() { |
| 70 | 0 | return logPrefix; |
| 71 | |
} |
| 72 | |
|
| 73 | |
public String getPluginName() { |
| 74 | 0 | return simplePluginName; |
| 75 | |
} |
| 76 | |
|
| 77 | |
public void setPluginConfigPath(String pluginConfigPath) { |
| 78 | 0 | this.pluginConfigPath = pluginConfigPath; |
| 79 | 0 | } |
| 80 | |
|
| 81 | |
protected String getSimplePluginName() { |
| 82 | 0 | return simplePluginName; |
| 83 | |
} |
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 87 | |
|
| 88 | |
|
| 89 | |
|
| 90 | |
|
| 91 | |
protected abstract PluginClassLoader createPluginClassLoader() throws IOException; |
| 92 | |
|
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | |
protected abstract URL getPluginConfigURL() throws PluginException, IOException; |
| 99 | |
|
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
public Plugin load() throws Exception { |
| 104 | 0 | PluginClassLoader classLoader = createPluginClassLoader(); |
| 105 | 0 | LOG.info("Created plugin ClassLoader: " + classLoader); |
| 106 | 0 | ContextClassLoaderBinder.bind(classLoader); |
| 107 | |
try { |
| 108 | 0 | return loadWithinContextClassLoader(classLoader); |
| 109 | |
} finally { |
| 110 | 0 | ContextClassLoaderBinder.unbind(); |
| 111 | |
} |
| 112 | |
} |
| 113 | |
|
| 114 | |
public boolean isRemoved() { |
| 115 | 0 | return false; |
| 116 | |
} |
| 117 | |
|
| 118 | |
|
| 119 | |
|
| 120 | |
|
| 121 | |
protected Plugin loadWithinContextClassLoader(PluginClassLoader classLoader) throws PluginException, IOException { |
| 122 | 0 | URL url = getPluginConfigURL(); |
| 123 | 0 | PluginConfig pluginConfig = loadPluginConfig(url); |
| 124 | 0 | QName qPluginName = getPluginName(pluginConfig); |
| 125 | 0 | classLoader.setConfig(pluginConfig); |
| 126 | 0 | ConfigContext.init(classLoader, pluginConfig); |
| 127 | 0 | configureExtraClasspath(classLoader, pluginConfig); |
| 128 | 0 | this.logPrefix = PluginUtils.getLogPrefix(qPluginName).toString(); |
| 129 | 0 | LOG.info("Constructing plugin '" + simplePluginName + "' with classloader: " + classLoader); |
| 130 | 0 | Plugin plugin = new Plugin(qPluginName, pluginConfig, classLoader); |
| 131 | 0 | installResourceLoader(plugin); |
| 132 | 0 | installPluginListeners(plugin); |
| 133 | 0 | return plugin; |
| 134 | |
} |
| 135 | |
|
| 136 | |
protected void installResourceLoader(Plugin plugin) { |
| 137 | 0 | PluginUtils.installResourceLoader(plugin); |
| 138 | 0 | } |
| 139 | |
|
| 140 | |
protected void installPluginListeners(Plugin plugin) { |
| 141 | 0 | PluginUtils.installPluginListeners(plugin); |
| 142 | 0 | } |
| 143 | |
|
| 144 | |
protected void configureExtraClasspath(PluginClassLoader classLoader, PluginConfig config) throws MalformedURLException { |
| 145 | 0 | String extraClassesDirs = config.getProperty(Config.EXTRA_CLASSES_DIR); |
| 146 | 0 | if (!org.apache.commons.lang.StringUtils.isEmpty(extraClassesDirs)) { |
| 147 | 0 | String[] extraClasses = extraClassesDirs.split(","); |
| 148 | 0 | for (int index = 0; index < extraClasses.length; index++) { |
| 149 | 0 | File extraClassesDir = new File(extraClasses[index]); |
| 150 | 0 | if (extraClassesDir.exists()) { |
| 151 | 0 | classLoader.addClassesDirectory(extraClassesDir); |
| 152 | |
} |
| 153 | |
} |
| 154 | |
} |
| 155 | 0 | String extraLibDirs = config.getProperty(Config.EXTRA_LIB_DIR); |
| 156 | 0 | if (!org.apache.commons.lang.StringUtils.isEmpty(extraLibDirs)) { |
| 157 | 0 | String[] extraLibs = extraLibDirs.split(","); |
| 158 | 0 | for (int index = 0; index < extraLibs.length; index++) { |
| 159 | 0 | File extraLibDir = new File(extraLibs[index]); |
| 160 | 0 | if (extraLibDir.exists()) { |
| 161 | 0 | classLoader.addLibDirectory(extraLibDir); |
| 162 | |
} |
| 163 | |
} |
| 164 | |
} |
| 165 | 0 | } |
| 166 | |
|
| 167 | |
|
| 168 | |
protected QName getPluginName(PluginConfig pluginConfig) { |
| 169 | 0 | String applicationId = pluginConfig.getProperty(CoreConstants.Config.APPLICATION_ID); |
| 170 | 0 | QName qPluginName = null; |
| 171 | 0 | if (StringUtils.isBlank(applicationId)) { |
| 172 | 0 | qPluginName = new QName(CoreConfigHelper.getApplicationId(), simplePluginName); |
| 173 | |
} else { |
| 174 | 0 | qPluginName = new QName(applicationId, simplePluginName); |
| 175 | |
} |
| 176 | 0 | return qPluginName; |
| 177 | |
} |
| 178 | |
|
| 179 | |
protected PluginConfig loadPluginConfig(URL url) { |
| 180 | 0 | PluginConfigParser parser = new PluginConfigParser(); |
| 181 | |
try { |
| 182 | 0 | PluginConfig pluginConfig = parser.parse(url, parentConfig); |
| 183 | 0 | pluginConfig.parseConfig(); |
| 184 | 0 | return pluginConfig; |
| 185 | 0 | } catch (FileNotFoundException e) { |
| 186 | 0 | throw new PluginException(getLogPrefix() + " Could not locate the plugin config file at path " + url, e); |
| 187 | 0 | } catch (IOException ioe) { |
| 188 | 0 | throw new PluginException(getLogPrefix() + " Could not read the plugin config file", ioe); |
| 189 | 0 | } catch (XmlException ixe) { |
| 190 | 0 | throw new PluginException(getLogPrefix() + " Could not parse the plugin config file", ixe); |
| 191 | |
} |
| 192 | |
} |
| 193 | |
} |