Coverage Report - org.kuali.rice.kew.plugin.Plugin
 
Classes in this File Line Coverage Branch Coverage Complexity
Plugin
0%
0/82
0%
0/18
2.062
 
 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.log4j.Logger;
 20  
 import org.kuali.rice.core.api.config.property.Config;
 21  
 import org.kuali.rice.core.api.util.ContextClassLoaderBinder;
 22  
 import org.kuali.rice.core.impl.resourceloader.BaseWrappingResourceLoader;
 23  
 import org.kuali.rice.kew.api.WorkflowRuntimeException;
 24  
 
 25  
 import javax.xml.namespace.QName;
 26  
 import java.util.ArrayList;
 27  
 import java.util.Iterator;
 28  
 import java.util.List;
 29  
 
 30  
 
 31  
 /**
 32  
  * A KEW Plugin.  A Plugin represents a distinct classloading space living below (as a child) of the core
 33  
  * KEW classloader.  It allows for loading of plugin resources from core components of the system.
 34  
  * Essentially a Plugin is a specialized ResourceLoader with a custom classloader and attached configuration.
 35  
  *
 36  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 37  
  */
 38  
 public class Plugin extends BaseWrappingResourceLoader {
 39  
 
 40  0
         private static final Logger LOG = Logger.getLogger(Plugin.class);
 41  
     private Config config;
 42  0
     private List<PluginListener> pluginListeners = new ArrayList<PluginListener>();
 43  
 
 44  0
     private boolean supressStartupFailure = true;
 45  0
     private boolean started = false;
 46  0
     private boolean startupFailure = false;
 47  
 
 48  
     public Plugin(QName name, Config config, ClassLoader classLoader) {
 49  0
             super(name, classLoader);
 50  0
             this.config = config;
 51  0
     }
 52  
 
 53  
     /**
 54  
      * Starts the plugin.
 55  
      */
 56  
     public synchronized void start() {
 57  0
         if (started) {
 58  0
             LOG.info(getLogPrefix()+" has already been started.");
 59  0
             return;
 60  
         }
 61  0
         LOG.info(getLogPrefix()+" Starting...");
 62  
         try {
 63  0
             bindThread();
 64  
             try {
 65  0
                 startupFailure = false;
 66  0
                 started = true;
 67  0
                 super.start();
 68  0
                 LOG.info("Starting plugin listeners");
 69  0
                 startPluginListeners();
 70  
             } finally {
 71  0
                 unbindThread();
 72  0
             }
 73  0
             ClassLoader classLoader = getClassLoader();
 74  0
             LOG.info(getLogPrefix()+" ...started." + (classLoader != null ? classLoader.toString() : ""));
 75  0
         } catch (Throwable t) {
 76  0
             LOG.error(getLogPrefix()+" Failure starting plugin.", t);
 77  0
             startupFailure = true;
 78  0
             started = true;
 79  0
             stop();
 80  0
             if (!supressStartupFailure) {
 81  0
                     if (t instanceof Error) {
 82  0
                             throw (Error)t;
 83  0
                     } else if (t instanceof RuntimeException) {
 84  0
                             throw (RuntimeException)t;
 85  
                     }
 86  0
                     throw new WorkflowRuntimeException("Failed to startup plugin.", t);
 87  
             }
 88  0
         }
 89  0
     }
 90  
 
 91  
     /**
 92  
      * Stops the plugin.
 93  
      */
 94  
     public synchronized void stop() {
 95  0
         if (!started) {
 96  0
             LOG.info(getLogPrefix()+" has already been stopped.");
 97  0
             return;
 98  
         }
 99  0
         LOG.info(getLogPrefix()+" Stopping...");
 100  0
         bindThread();
 101  
         try {
 102  0
             started = false;
 103  0
             stopPluginListeners();
 104  
             // stop resource loaders of super class
 105  0
             super.stop();
 106  0
         } catch (Throwable t) {
 107  0
                 LOG.error(getLogPrefix()+" Failed when attempting to stop the plugin.", t);
 108  
         } finally {
 109  0
             unbindThread();
 110  0
         }
 111  0
         resetPlugin();
 112  0
         LOG.info(getLogPrefix()+" ...stopped.");
 113  0
     }
 114  
 
 115  
     public boolean isStarted() {
 116  0
         return started;
 117  
     }
 118  
 
 119  
     public void addPluginListener(PluginListener pluginListener) {
 120  0
             pluginListeners.add(pluginListener);
 121  0
     }
 122  
 
 123  
     public void removePluginListener(PluginListener pluginListener) {
 124  0
             pluginListeners.remove(pluginListener);
 125  0
     }
 126  
 
 127  
     protected void startPluginListeners() {
 128  0
         for (Iterator iterator = pluginListeners.iterator(); iterator.hasNext();) {
 129  0
             PluginListener listener = (PluginListener) iterator.next();
 130  0
             listener.pluginInitialized(this);
 131  0
         }
 132  0
     }
 133  
 
 134  
     /**
 135  
      * If we fail to stop a plugin listener, try the next one but don't propogate any
 136  
      * exceptions out of this method.  Otherwise the plugin ends up dying and can't be
 137  
      * reloaded from a hot deploy.
 138  
      */
 139  
     protected void stopPluginListeners() {
 140  0
         for (Iterator iterator = pluginListeners.iterator(); iterator.hasNext();) {
 141  0
             PluginListener listener = (PluginListener) iterator.next();
 142  
             try {
 143  0
                     listener.pluginDestroyed(this);
 144  0
             } catch (Throwable t) {
 145  0
                     LOG.error(getLogPrefix()+" Failed when invoking pluginDestroyed on Plugin Listener '"+listener.getClass().getName()+"'.", t);
 146  0
             }
 147  0
         }
 148  0
     }
 149  
 
 150  
     public boolean isSupressStartupFailure() {
 151  0
                 return supressStartupFailure;
 152  
         }
 153  
 
 154  
         public void setSupressStartupFailure(boolean supressStartupFailure) {
 155  0
                 this.supressStartupFailure = supressStartupFailure;
 156  0
         }
 157  
 
 158  
         public void bindThread() {
 159  0
                 ContextClassLoaderBinder.bind(getClassLoader());
 160  0
     }
 161  
 
 162  
     public void unbindThread() {
 163  0
         ContextClassLoaderBinder.unbind();
 164  0
     }
 165  
 
 166  
         /**
 167  
      * Cleanup plugin resources.
 168  
      */
 169  
     private void resetPlugin() {
 170  0
         if (!startupFailure) {
 171  0
                 setClassLoader(null);
 172  
         }
 173  0
         pluginListeners.clear();
 174  0
     }
 175  
 
 176  
     private String getLogPrefix() {
 177  0
         return toString();
 178  
     }
 179  
 
 180  
     public Config getConfig() {
 181  0
             return config;
 182  
     }
 183  
 
 184  
     public String toString() {
 185  0
         return "[Plugin: " + this.getName() + "]";
 186  
     }
 187  
 
 188  
 }