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