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.config.property.ConfigContext; |
22 | |
import org.kuali.rice.core.util.ClassLoaderUtils; |
23 | |
import org.kuali.rice.kew.plugin.PluginUtils.PluginZipFileFilter; |
24 | |
|
25 | |
import java.io.File; |
26 | |
import java.util.HashSet; |
27 | |
import java.util.List; |
28 | |
import java.util.Set; |
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
public class HotDeployer implements Runnable { |
37 | 0 | private static final Logger LOG = Logger.getLogger(HotDeployer.class); |
38 | |
|
39 | |
|
40 | |
private PluginRegistry registry; |
41 | |
private File sharedPluginDirectory; |
42 | |
private List<String> pluginDirectories; |
43 | |
|
44 | 0 | public HotDeployer(PluginRegistry registry, File sharedPluginDirectory, List<String> pluginDirectories) { |
45 | 0 | this.registry = registry; |
46 | 0 | this.sharedPluginDirectory = sharedPluginDirectory; |
47 | 0 | this.pluginDirectories = pluginDirectories; |
48 | 0 | } |
49 | |
|
50 | |
public synchronized void run() { |
51 | |
try { |
52 | 0 | LOG.debug("Checking for added and removed plugins..."); |
53 | 0 | Set<PluginEnvironment> removedPlugins = getRemovedPlugins(); |
54 | 0 | for (PluginEnvironment pluginContext : removedPlugins) { |
55 | 0 | LOG.info("Detected a removed plugin '" + pluginContext.getPluginName() + "', shutting down plugin."); |
56 | |
try { |
57 | 0 | pluginContext.unload(); |
58 | 0 | registry.removePluginEnvironment(pluginContext.getPluginName()); |
59 | 0 | } catch (Exception e) { |
60 | 0 | LOG.error("Failed to unload plugin '" + pluginContext.getPluginName() + "'", e); |
61 | 0 | } |
62 | |
} |
63 | 0 | Set<PluginEnvironment> addedPlugins = getAddedPlugins(); |
64 | 0 | for (PluginEnvironment pluginContext : addedPlugins) { |
65 | |
try { |
66 | 0 | LOG.info("Detected a new plugin. Loading plugin..."); |
67 | 0 | pluginContext.load(); |
68 | 0 | LOG.info("...plugin '" + pluginContext.getPluginName() + "' loaded."); |
69 | 0 | } catch (Exception e) { |
70 | |
|
71 | 0 | String pluginName= "<<unknown>>"; |
72 | 0 | if (pluginContext.getPlugin() != null) { |
73 | 0 | pluginName = String.valueOf(pluginContext.getPluginName()); |
74 | |
} |
75 | 0 | LOG.error("Failed to load plugin '" + pluginName + "'", e); |
76 | |
} finally { |
77 | |
|
78 | |
|
79 | 0 | registry.addPluginEnvironment(pluginContext); |
80 | 0 | } |
81 | |
} |
82 | 0 | } catch (Exception e) { |
83 | 0 | LOG.warn("Failed to check for hot deploy.", e); |
84 | 0 | } |
85 | 0 | } |
86 | |
|
87 | |
protected Set<PluginEnvironment> getRemovedPlugins() { |
88 | 0 | Set<PluginEnvironment> removedPlugins = new HashSet<PluginEnvironment>(); |
89 | 0 | for (PluginEnvironment environment : registry.getPluginEnvironments()) { |
90 | 0 | if (environment.getLoader().isRemoved()) { |
91 | 0 | removedPlugins.add(environment); |
92 | |
} |
93 | |
} |
94 | 0 | return removedPlugins; |
95 | |
} |
96 | |
|
97 | |
protected Set<PluginEnvironment> getAddedPlugins() throws Exception { |
98 | 0 | Set<PluginEnvironment> addedPlugins = new HashSet<PluginEnvironment>(); |
99 | 0 | Set<File> newPluginZipFiles = new HashSet<File>(); |
100 | |
|
101 | |
|
102 | |
|
103 | 0 | for (String pluginDirName : pluginDirectories) { |
104 | 0 | File pluginDir = new File(pluginDirName); |
105 | 0 | if (pluginDir.exists() && pluginDir.isDirectory()) { |
106 | 0 | File[] pluginDirFiles = pluginDir.listFiles(new PluginZipFileFilter()); |
107 | 0 | for (File pluginZip : pluginDirFiles) { |
108 | 0 | int indexOf = pluginZip.getName().lastIndexOf(".zip"); |
109 | 0 | String pluginName = pluginZip.getName().substring(0, indexOf); |
110 | |
|
111 | 0 | List<PluginEnvironment> currentEnvironments = registry.getPluginEnvironments(); |
112 | 0 | boolean pluginExists = false; |
113 | 0 | for (PluginEnvironment environment : currentEnvironments) { |
114 | 0 | if (environment.getPluginName().equals(pluginName)) { |
115 | 0 | pluginExists = true; |
116 | 0 | break; |
117 | |
} |
118 | |
} |
119 | 0 | if (!pluginExists) { |
120 | |
|
121 | 0 | long lastModified1 = pluginZip.lastModified(); |
122 | 0 | Thread.sleep(100); |
123 | 0 | long lastModified2 = pluginZip.lastModified(); |
124 | 0 | if (lastModified1 == lastModified2) { |
125 | 0 | newPluginZipFiles.add(pluginZip); |
126 | |
} else { |
127 | 0 | LOG.warn("Detected that the plugin zip is still being modified, holding off on hot deploy: " + pluginZip.getAbsolutePath()); |
128 | |
} |
129 | |
} |
130 | |
} |
131 | |
} |
132 | 0 | } |
133 | |
|
134 | 0 | ClassLoader parentClassLoader = ClassLoaderUtils.getDefaultClassLoader(); |
135 | 0 | Config parentConfig = ConfigContext.getCurrentContextConfig(); |
136 | 0 | for (File newPluginZipFile : newPluginZipFiles) { |
137 | 0 | PluginLoader loader = new ZipFilePluginLoader(newPluginZipFile, sharedPluginDirectory, parentClassLoader, parentConfig); |
138 | 0 | PluginEnvironment environment = new PluginEnvironment(loader, registry); |
139 | 0 | addedPlugins.add(environment); |
140 | 0 | } |
141 | 0 | return addedPlugins; |
142 | |
} |
143 | |
|
144 | |
} |