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 java.io.File; |
20 | |
import java.util.ArrayList; |
21 | |
import java.util.HashSet; |
22 | |
import java.util.List; |
23 | |
import java.util.Map; |
24 | |
import java.util.Set; |
25 | |
import java.util.TreeMap; |
26 | |
import java.util.concurrent.Executors; |
27 | |
import java.util.concurrent.ScheduledExecutorService; |
28 | |
import java.util.concurrent.ScheduledFuture; |
29 | |
import java.util.concurrent.ThreadFactory; |
30 | |
import java.util.concurrent.TimeUnit; |
31 | |
|
32 | |
import javax.xml.namespace.QName; |
33 | |
|
34 | |
import org.kuali.rice.core.api.config.CoreConfigHelper; |
35 | |
import org.kuali.rice.core.api.config.property.Config; |
36 | |
import org.kuali.rice.core.api.config.property.ConfigContext; |
37 | |
import org.kuali.rice.core.api.resourceloader.ResourceLoader; |
38 | |
import org.kuali.rice.core.util.ClassLoaderUtils; |
39 | |
import org.kuali.rice.kew.plugin.PluginUtils.PluginZipFileFilter; |
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
public class ServerPluginRegistry extends BasePluginRegistry { |
47 | |
|
48 | 0 | private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(ServerPluginRegistry.class); |
49 | |
|
50 | 0 | private List<String> pluginDirectories = new ArrayList<String>(); |
51 | |
private File sharedPluginDirectory; |
52 | |
private Reloader reloader; |
53 | |
private HotDeployer hotDeployer; |
54 | |
|
55 | |
private ScheduledExecutorService scheduledExecutor; |
56 | |
private ScheduledFuture<?> reloaderFuture; |
57 | |
private ScheduledFuture<?> hotDeployerFuture; |
58 | |
|
59 | |
|
60 | |
public ServerPluginRegistry() { |
61 | 0 | super(new QName(CoreConfigHelper.getApplicationId(), ResourceLoader.PLUGIN_REGISTRY_LOADER_NAME)); |
62 | 0 | } |
63 | |
|
64 | |
public void start() throws Exception { |
65 | 0 | LOG.info("Starting server Plugin Registry..."); |
66 | 0 | scheduledExecutor = Executors.newScheduledThreadPool(2, new KEWThreadFactory()); |
67 | 0 | sharedPluginDirectory = loadSharedPlugin(); |
68 | 0 | reloader = new Reloader(); |
69 | 0 | hotDeployer = new HotDeployer(PluginUtils.getPluginRegistry(), sharedPluginDirectory, pluginDirectories); |
70 | 0 | loadPlugins(sharedPluginDirectory); |
71 | |
|
72 | 0 | this.reloaderFuture = scheduledExecutor.scheduleWithFixedDelay(reloader, 5, 5, TimeUnit.SECONDS); |
73 | 0 | this.hotDeployerFuture = scheduledExecutor.scheduleWithFixedDelay(hotDeployer, 5, 5, TimeUnit.SECONDS); |
74 | 0 | super.start(); |
75 | 0 | LOG.info("...server Plugin Registry successfully started."); |
76 | 0 | } |
77 | |
|
78 | |
public void stop() throws Exception { |
79 | 0 | LOG.info("Stopping server Plugin Registry..."); |
80 | 0 | stopReloader(); |
81 | 0 | stopHotDeployer(); |
82 | 0 | reloader = null; |
83 | 0 | hotDeployer = null; |
84 | |
|
85 | 0 | if (scheduledExecutor != null) { |
86 | 0 | scheduledExecutor.shutdownNow(); |
87 | 0 | scheduledExecutor = null; |
88 | |
} |
89 | 0 | super.stop(); |
90 | 0 | LOG.info("...server Plugin Registry successfully stopped."); |
91 | 0 | } |
92 | |
|
93 | |
protected void stopReloader() { |
94 | 0 | if (reloaderFuture != null) { |
95 | 0 | if (!reloaderFuture.cancel(true)) { |
96 | 0 | LOG.warn("Failed to cancel the plugin reloader."); |
97 | |
} |
98 | 0 | reloaderFuture = null; |
99 | |
} |
100 | 0 | } |
101 | |
|
102 | |
protected void stopHotDeployer() { |
103 | 0 | if (hotDeployerFuture != null) { |
104 | 0 | if (!hotDeployerFuture.cancel(true)) { |
105 | 0 | LOG.warn("Failed to cancel the hot deployer."); |
106 | |
} |
107 | 0 | hotDeployerFuture = null; |
108 | |
} |
109 | 0 | } |
110 | |
|
111 | |
protected void loadPlugins(File sharedPluginDirectory) { |
112 | 0 | Map<String, File> pluginLocations = new TreeMap<String, File>(new PluginNameComparator()); |
113 | 0 | PluginZipFileFilter pluginFilter = new PluginZipFileFilter(); |
114 | |
|
115 | 0 | Set<File> visitedFiles = new HashSet<File>(); |
116 | 0 | for (String pluginDir : pluginDirectories) { |
117 | 0 | LOG.info("Reading plugins from " + pluginDir); |
118 | 0 | File file = new File(pluginDir); |
119 | 0 | if (visitedFiles.contains(file)) { |
120 | 0 | LOG.info("Skipping visited directory: " + pluginDir); |
121 | 0 | continue; |
122 | |
} |
123 | 0 | visitedFiles.add(file); |
124 | 0 | if (!file.exists() || !file.isDirectory()) { |
125 | 0 | LOG.warn(file.getAbsoluteFile()+" is not a valid plugin directory."); |
126 | 0 | continue; |
127 | |
} |
128 | 0 | File[] pluginZips = file.listFiles(pluginFilter); |
129 | 0 | for (int i = 0; i < pluginZips.length; i++) { |
130 | 0 | File pluginZip = pluginZips[i]; |
131 | 0 | int indexOf = pluginZip.getName().lastIndexOf(".zip"); |
132 | 0 | String pluginName = pluginZip.getName().substring(0, indexOf); |
133 | 0 | if (pluginLocations.containsKey(pluginName)) { |
134 | 0 | LOG.warn("There already exists an installed plugin with the name '"+ pluginName + "', ignoring plugin " + pluginZip.getAbsolutePath()); |
135 | 0 | continue; |
136 | |
} |
137 | 0 | pluginLocations.put(pluginName, pluginZip); |
138 | |
} |
139 | 0 | } |
140 | 0 | for (String pluginName : pluginLocations.keySet()) { |
141 | 0 | File pluginZipFile = pluginLocations.get(pluginName); |
142 | |
try { |
143 | 0 | LOG.info("Loading plugin '" + pluginName + "'"); |
144 | 0 | ClassLoader parentClassLoader = ClassLoaderUtils.getDefaultClassLoader(); |
145 | 0 | Config parentConfig = ConfigContext.getCurrentContextConfig(); |
146 | 0 | ZipFilePluginLoader loader = new ZipFilePluginLoader(pluginZipFile, |
147 | |
sharedPluginDirectory, |
148 | |
parentClassLoader, |
149 | |
parentConfig); |
150 | 0 | PluginEnvironment environment = new PluginEnvironment(loader, this); |
151 | |
try { |
152 | 0 | environment.load(); |
153 | |
} finally { |
154 | |
|
155 | 0 | addPluginEnvironment(environment); |
156 | 0 | } |
157 | 0 | } catch (Exception e) { |
158 | 0 | LOG.error("Failed to read workflow plugin '"+pluginName+"'", e); |
159 | 0 | } |
160 | 0 | } |
161 | 0 | } |
162 | |
|
163 | |
@Override |
164 | |
public void addPluginEnvironment(PluginEnvironment pluginEnvironment) { |
165 | 0 | super.addPluginEnvironment(pluginEnvironment); |
166 | 0 | reloader.addReloadable(pluginEnvironment); |
167 | 0 | } |
168 | |
|
169 | |
@Override |
170 | |
public PluginEnvironment removePluginEnvironment(String pluginName) { |
171 | 0 | PluginEnvironment environment = super.removePluginEnvironment(pluginName); |
172 | 0 | reloader.removeReloadable(environment); |
173 | 0 | return environment; |
174 | |
} |
175 | |
|
176 | |
public File loadSharedPlugin() { |
177 | 0 | return PluginUtils.findSharedDirectory(pluginDirectories); |
178 | |
} |
179 | |
|
180 | |
public void setPluginDirectories(List<String> pluginDirectories) { |
181 | 0 | this.pluginDirectories = pluginDirectories; |
182 | 0 | } |
183 | |
|
184 | |
public void setSharedPluginDirectory(File sharedPluginDirectory) { |
185 | 0 | this.sharedPluginDirectory = sharedPluginDirectory; |
186 | 0 | } |
187 | |
|
188 | |
protected HotDeployer getHotDeployer() { |
189 | 0 | return hotDeployer; |
190 | |
} |
191 | |
|
192 | |
protected Reloader getReloader() { |
193 | 0 | return reloader; |
194 | |
} |
195 | |
|
196 | 0 | private static class KEWThreadFactory implements ThreadFactory { |
197 | |
|
198 | 0 | private ThreadFactory defaultThreadFactory = Executors.defaultThreadFactory(); |
199 | |
|
200 | |
public Thread newThread(Runnable runnable) { |
201 | 0 | Thread thread = defaultThreadFactory.newThread(runnable); |
202 | 0 | thread.setName("ServerPluginRegistry-" + thread.getName()); |
203 | 0 | return thread; |
204 | |
} |
205 | |
} |
206 | |
|
207 | |
} |