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