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.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 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
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 | |
|
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 | |
|
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 | |
|
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 | |
|
136 | |
|
137 | |
|
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 | |
|
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 | |
} |