Coverage Report - org.kuali.rice.kew.plugin.ServerPluginRegistry
 
Classes in this File Line Coverage Branch Coverage Complexity
ServerPluginRegistry
0%
0/88
0%
0/24
1.929
ServerPluginRegistry$1
N/A
N/A
1.929
ServerPluginRegistry$KEWThreadFactory
0%
0/5
N/A
1.929
 
 1  
 /*
 2  
  * Copyright 2005-2007 The Kuali Foundation
 3  
  *
 4  
  *
 5  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 6  
  * you may not use this file except in compliance with the License.
 7  
  * You may obtain a copy of the License at
 8  
  *
 9  
  * http://www.opensource.org/licenses/ecl2.php
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 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.config.Config;
 35  
 import org.kuali.rice.core.config.ConfigContext;
 36  
 import org.kuali.rice.core.resourceloader.ResourceLoader;
 37  
 import org.kuali.rice.core.util.ClassLoaderUtils;
 38  
 import org.kuali.rice.kew.plugin.PluginUtils.PluginZipFileFilter;
 39  
 
 40  
 /**
 41  
  * A PluginRegistry implementation which loads plugins from the file system on the server.
 42  
  *
 43  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 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(ConfigContext.getCurrentContextConfig().getServiceNamespace(), 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  
                 // TODO make the delay configurable
 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  
         //PluginDirectoryFilter pluginFilter = new PluginDirectoryFilter(sharedPluginDirectory);
 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  0
                         } finally {
 153  
                             // regardless of whether the plugin loads or not, let's add it to the environment
 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  
 }