Coverage Report - org.kuali.rice.kew.plugin.BasePluginLoader
 
Classes in this File Line Coverage Branch Coverage Complexity
BasePluginLoader
0%
0/69
0%
0/16
2.067
 
 1  
 /*
 2  
  * Copyright 2006-2011 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  
 
 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  
  * Abstract base PluginLoader implementation.
 39  
  * Delegates to template methods to obtain plugin ClassLoader and plugin config file URL,
 40  
  * then load the config under the plugin ClassLoader, and constructs a Plugin object.
 41  
  *
 42  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 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  
      * Template method that subclasses should implement to supply an appropriate
 87  
      * plugin ClassLoader
 88  
      * @return an appropriate PluginClassLoader
 89  
      * @throws IOException if anything goes awry
 90  
      */
 91  
     protected abstract PluginClassLoader createPluginClassLoader() throws IOException;
 92  
     /**
 93  
      * Template method that subclasses should implement to supply an appropriate
 94  
      * URL to the plugin's configuration
 95  
      * @return an appropriate URL to the plugin's configuration
 96  
      * @throws IOException if anything goes awry
 97  
      */
 98  
     protected abstract URL getPluginConfigURL() throws PluginException, IOException;
 99  
 
 100  
     /**
 101  
      * Loads and creates the Plugin.
 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  
      * Executes loading of the plugin within the current context classloader set to the Plugin's classloader.
 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  
 }