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 2005-2007 The Kuali Foundation
 3  
  *
 4  
  *
 5  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 6  
  * you may not use this file except in compliance with the License.
 7  
  * You may obtain a copy of the License at
 8  
  *
 9  
  * http://www.opensource.org/licenses/ecl2.php
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.kuali.rice.kew.plugin;
 18  
 
 19  
 import java.io.File;
 20  
 import java.io.FileNotFoundException;
 21  
 import java.io.IOException;
 22  
 import java.net.MalformedURLException;
 23  
 import java.net.URL;
 24  
 
 25  
 import javax.xml.namespace.QName;
 26  
 
 27  
 import org.apache.log4j.Logger;
 28  
 import org.kuali.rice.core.config.Config;
 29  
 import org.kuali.rice.core.config.ConfigContext;
 30  
 import org.kuali.rice.core.resourceloader.ContextClassLoaderBinder;
 31  
 import org.kuali.rice.core.util.ClassLoaderUtils;
 32  
 import org.kuali.rice.kew.exception.InvalidXmlException;
 33  
 import org.kuali.rice.kew.util.Utilities;
 34  
 
 35  
 
 36  
 /**
 37  
  * Abstract base PluginLoader implementation.
 38  
  * Delegates to template methods to obtain plugin ClassLoader and plugin config file URL,
 39  
  * then load the config under the plugin ClassLoader, and constructs a Plugin object.
 40  
  *
 41  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 42  
  */
 43  
 public abstract class BasePluginLoader implements PluginLoader {
 44  0
     private static final Logger LOG = Logger.getLogger(BasePluginLoader.class);
 45  
 
 46  
     private static final String META_INF_PATH = "META-INF";
 47  
     private static final String PLUGIN_CONFIG_PATH = META_INF_PATH + "/workflow.xml";
 48  
 
 49  
     protected final String simplePluginName;
 50  
     protected String logPrefix;
 51  
 
 52  
     protected final ClassLoader parentClassLoader;
 53  
     protected final Config parentConfig;
 54  
     protected final File sharedPluginDirectory;
 55  0
     protected String pluginConfigPath = PLUGIN_CONFIG_PATH;
 56  
 
 57  0
     public BasePluginLoader(String simplePluginName, File sharedPluginDirectory, ClassLoader parentClassLoader, Config parentConfig) {
 58  0
         this.sharedPluginDirectory = sharedPluginDirectory;
 59  0
         if (parentClassLoader == null) {
 60  0
             parentClassLoader = ClassLoaderUtils.getDefaultClassLoader();
 61  
         }
 62  0
         this.parentClassLoader = parentClassLoader;
 63  0
         this.parentConfig = parentConfig;
 64  0
         this.simplePluginName = simplePluginName;
 65  0
         this.logPrefix = simplePluginName;
 66  0
     }
 67  
 
 68  
     protected String getLogPrefix() {
 69  0
         return logPrefix;
 70  
     }
 71  
     
 72  
     public String getPluginName() {
 73  0
         return simplePluginName;
 74  
     }
 75  
 
 76  
     public void setPluginConfigPath(String pluginConfigPath) {
 77  0
         this.pluginConfigPath = pluginConfigPath;
 78  0
     }
 79  
 
 80  
     protected String getSimplePluginName() {
 81  0
             return simplePluginName;
 82  
     }
 83  
 
 84  
     /**
 85  
      * Template method that subclasses should implement to supply an appropriate
 86  
      * plugin ClassLoader
 87  
      * @return an appropriate PluginClassLoader
 88  
      * @throws IOException if anything goes awry
 89  
      */
 90  
     protected abstract PluginClassLoader createPluginClassLoader() throws IOException;
 91  
     /**
 92  
      * Template method that subclasses should implement to supply an appropriate
 93  
      * URL to the plugin's configuration
 94  
      * @return an appropriate URL to the plugin's configuration
 95  
      * @throws IOException if anything goes awry
 96  
      */
 97  
     protected abstract URL getPluginConfigURL() throws PluginException, IOException;
 98  
 
 99  
     /**
 100  
      * Loads and creates the Plugin.
 101  
      */
 102  
     public Plugin load() throws Exception {
 103  0
         PluginClassLoader classLoader = createPluginClassLoader();
 104  0
         LOG.info("Created plugin ClassLoader: " + classLoader);
 105  0
         ContextClassLoaderBinder.bind(classLoader);
 106  
         try {
 107  0
             return loadWithinContextClassLoader(classLoader);
 108  
         } finally {
 109  0
             ContextClassLoaderBinder.unbind();
 110  
         }
 111  
     }
 112  
 
 113  
     public boolean isRemoved() {
 114  0
             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  0
             URL url = getPluginConfigURL();
 122  0
         PluginConfig pluginConfig = loadPluginConfig(url);
 123  0
         QName qPluginName = getPluginName(pluginConfig);
 124  0
         classLoader.setConfig(pluginConfig);
 125  0
         ConfigContext.init(classLoader, pluginConfig);
 126  0
         configureExtraClasspath(classLoader, pluginConfig);
 127  0
         this.logPrefix = PluginUtils.getLogPrefix(qPluginName).toString();
 128  0
         LOG.info("Constructing plugin '" + simplePluginName + "' with classloader: " + classLoader);
 129  0
         Plugin plugin = new Plugin(qPluginName, pluginConfig, classLoader);
 130  0
         installResourceLoader(plugin);
 131  0
         installPluginListeners(plugin);
 132  0
         return plugin;
 133  
     }
 134  
 
 135  
     protected void installResourceLoader(Plugin plugin) {
 136  0
             PluginUtils.installResourceLoader(plugin);
 137  0
     }
 138  
 
 139  
     protected void installPluginListeners(Plugin plugin) {
 140  0
             PluginUtils.installPluginListeners(plugin);
 141  0
     }
 142  
 
 143  
     protected void configureExtraClasspath(PluginClassLoader classLoader, PluginConfig config) throws MalformedURLException {
 144  0
                 String extraClassesDirs = config.getProperty(Config.EXTRA_CLASSES_DIR);
 145  0
                 if (!Utilities.isEmpty(extraClassesDirs)) {
 146  0
                         String[] extraClasses = extraClassesDirs.split(",");
 147  0
                         for (int index = 0; index < extraClasses.length; index++) {
 148  0
                                 File extraClassesDir = new File(extraClasses[index]);
 149  0
                                 if (extraClassesDir.exists()) {
 150  0
                                         classLoader.addClassesDirectory(extraClassesDir);
 151  
                                 }
 152  
                         }
 153  
                 }
 154  0
                 String extraLibDirs = config.getProperty(Config.EXTRA_LIB_DIR);
 155  0
                 if (!Utilities.isEmpty(extraLibDirs)) {
 156  0
                         String[] extraLibs = extraLibDirs.split(",");
 157  0
                         for (int index = 0; index < extraLibs.length; index++) {
 158  0
                                 File extraLibDir = new File(extraLibs[index]);
 159  0
                                 if (extraLibDir.exists()) {
 160  0
                                         classLoader.addLibDirectory(extraLibDir);
 161  
                                 }
 162  
                         }
 163  
                 }
 164  0
         }
 165  
 
 166  
 
 167  
     protected QName getPluginName(PluginConfig pluginConfig) {
 168  0
             String serviceNamespace = pluginConfig.getServiceNamespace();
 169  0
             QName qPluginName = null;
 170  0
         if (serviceNamespace == null) {
 171  0
                 qPluginName = new QName(ConfigContext.getCurrentContextConfig().getServiceNamespace(), simplePluginName);
 172  
         } else {
 173  0
                 qPluginName = new QName(serviceNamespace, simplePluginName);
 174  
         }
 175  0
             return qPluginName;
 176  
     }
 177  
 
 178  
     protected PluginConfig loadPluginConfig(URL url) {
 179  0
         PluginConfigParser parser = new PluginConfigParser();
 180  
         try {
 181  0
             PluginConfig pluginConfig  = parser.parse(url, parentConfig);
 182  0
             pluginConfig.parseConfig();
 183  0
             return pluginConfig;
 184  0
         } catch (FileNotFoundException e) {
 185  0
             throw new PluginException(getLogPrefix() + " Could not locate the plugin config file at path " + url, e);
 186  0
         } catch (IOException ioe) {
 187  0
             throw new PluginException(getLogPrefix() + " Could not read the plugin config file", ioe);
 188  0
         } catch (InvalidXmlException ixe) {
 189  0
             throw new PluginException(getLogPrefix() + " Could not parse the plugin config file", ixe);
 190  
         }
 191  
     }
 192  
 }