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