| 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.io.FileFilter; | 
  | 21 |  |  import java.io.FilenameFilter; | 
  | 22 |  |  import java.util.Iterator; | 
  | 23 |  |  import java.util.List; | 
  | 24 |  |   | 
  | 25 |  |  import javax.xml.namespace.QName; | 
  | 26 |  |   | 
  | 27 |  |  import org.apache.commons.lang.StringUtils; | 
  | 28 |  |  import org.apache.log4j.Logger; | 
  | 29 |  |  import org.kuali.rice.core.config.Config; | 
  | 30 |  |  import org.kuali.rice.core.config.ConfigContext; | 
  | 31 |  |  import org.kuali.rice.core.resourceloader.BaseResourceLoader; | 
  | 32 |  |  import org.kuali.rice.core.resourceloader.GlobalResourceLoader; | 
  | 33 |  |  import org.kuali.rice.core.resourceloader.ResourceLoader; | 
  | 34 |  |  import org.kuali.rice.core.resourceloader.ResourceLoaderUtil; | 
  | 35 |  |  import org.kuali.rice.kew.resourceloader.CoreResourceLoader; | 
  | 36 |  |   | 
  | 37 |  |   | 
  | 38 |  |   | 
  | 39 |  |   | 
  | 40 |  |   | 
  | 41 |  |   | 
  | 42 |  |   | 
  | 43 |  |  public final class PluginUtils { | 
  | 44 |  |   | 
  | 45 | 0 |      private static final Logger LOG = Logger.getLogger(PluginUtils.class); | 
  | 46 |  |      private static final String SHARED_DIR = "shared"; | 
  | 47 |  |   | 
  | 48 |  |       | 
  | 49 |  |       | 
  | 50 |  |      public static final long INFINITE_MAX_WAIT = -1; | 
  | 51 |  |       | 
  | 52 |  |      public static final long DEFAULT_MAX_WAIT = 90000; | 
  | 53 |  |   | 
  | 54 |  |       | 
  | 55 |  |       | 
  | 56 |  |       | 
  | 57 |  |       | 
  | 58 |  |       | 
  | 59 |  |      public static final long DEFAULT_SAFE_TIME = 60000; | 
  | 60 |  |   | 
  | 61 | 0 |      private static final FilenameFilter JAR_FILES_FILTER = new FilenameFilter() { | 
  | 62 |  |          public boolean accept(File dir, String name) { | 
  | 63 | 0 |              return name.matches(".+\\.jar"); | 
  | 64 |  |          } | 
  | 65 |  |      }; | 
  | 66 |  |   | 
  | 67 | 0 |      private static final FileFilter SHARED_DIR_FILTER = new FileFilter() { | 
  | 68 |  |          public boolean accept(File file) { | 
  | 69 | 0 |              return file.isDirectory() && file.getName().equals(SHARED_DIR); | 
  | 70 |  |          } | 
  | 71 |  |      }; | 
  | 72 |  |   | 
  | 73 |  |      public static class PluginDirectoryFilter implements FileFilter { | 
  | 74 |  |          private final File sharedPluginDirectory; | 
  | 75 | 0 |          public PluginDirectoryFilter(File sharedPluginDirectory) { | 
  | 76 | 0 |              this.sharedPluginDirectory = sharedPluginDirectory; | 
  | 77 | 0 |          } | 
  | 78 |  |          public boolean accept(File file) { | 
  | 79 | 0 |              return file.isDirectory() && !file.getName().equalsIgnoreCase("cvs") && | 
  | 80 |  |              (sharedPluginDirectory == null || | 
  | 81 |  |                      !file.getName().equals(sharedPluginDirectory.getName())); | 
  | 82 |  |          } | 
  | 83 |  |      } | 
  | 84 |  |   | 
  | 85 | 0 |      public static class PluginZipFileFilter implements FileFilter { | 
  | 86 |  |          public boolean accept(File file) { | 
  | 87 | 0 |              return file.isFile() && file.getName().endsWith(".zip"); | 
  | 88 |  |          } | 
  | 89 |  |      } | 
  | 90 |  |   | 
  | 91 |  |      public static String getLogPrefix(Plugin plugin) { | 
  | 92 | 0 |              return getLogPrefix(plugin.getName()); | 
  | 93 |  |      } | 
  | 94 |  |   | 
  | 95 |  |      public static String getLogPrefix(QName pluginName) { | 
  | 96 | 0 |              return "[Plugin: " + pluginName + "]"; | 
  | 97 |  |      } | 
  | 98 |  |   | 
  | 99 |  |      static File[] findJars(File libDir) { | 
  | 100 | 0 |          File[] jarFiles = new File[0]; | 
  | 101 | 0 |          if (libDir.isDirectory()) { | 
  | 102 | 0 |              jarFiles = libDir.listFiles(JAR_FILES_FILTER); | 
  | 103 |  |          } | 
  | 104 | 0 |          return jarFiles; | 
  | 105 |  |      } | 
  | 106 |  |   | 
  | 107 |  |      public static File findSharedDirectory(List pluginDirectories) { | 
  | 108 | 0 |          for (Iterator iterator = pluginDirectories.iterator(); iterator.hasNext();) { | 
  | 109 | 0 |              File dir = new File((String) iterator.next()); | 
  | 110 | 0 |              if (dir.isDirectory()) { | 
  | 111 | 0 |                  File[] subDirs = dir.listFiles(SHARED_DIR_FILTER); | 
  | 112 | 0 |                  if (subDirs.length > 0) { | 
  | 113 | 0 |                      return subDirs[0]; | 
  | 114 |  |                  } | 
  | 115 |  |              } | 
  | 116 | 0 |          } | 
  | 117 | 0 |          return null; | 
  | 118 |  |      } | 
  | 119 |  |   | 
  | 120 |  |      public static void validatePluginZipFile(File file) { | 
  | 121 | 0 |              if (file == null) { | 
  | 122 | 0 |                          throw new IllegalArgumentException("Given plugin file was 'null'"); | 
  | 123 | 0 |                  } else if (!file.exists()) { | 
  | 124 | 0 |                          throw new IllegalArgumentException("Given plugin file does not exist: " + file.getAbsolutePath()); | 
  | 125 | 0 |                  } else if (!file.isFile()) { | 
  | 126 | 0 |                          throw new IllegalArgumentException("Given plugin file is not a valid file: " + file.getAbsolutePath()); | 
  | 127 | 0 |                  } else if (!file.canRead()) { | 
  | 128 | 0 |                          throw new IllegalArgumentException("Permission denied to read given plugin file: " + file.getAbsolutePath()); | 
  | 129 | 0 |                  } else if (!file.getName().endsWith(".zip")) { | 
  | 130 | 0 |                          throw new IllegalArgumentException("Given plugin file does not end in .zip extension: " + file.getAbsolutePath()); | 
  | 131 |  |                  } | 
  | 132 |  |               | 
  | 133 | 0 |              File pluginDirectory = file.getParentFile(); | 
  | 134 | 0 |              validatePluginDirectory(pluginDirectory); | 
  | 135 |  |               | 
  | 136 | 0 |              if (pluginDirectory == null) { | 
  | 137 | 0 |                      throw new IllegalArgumentException("Given plugin directory was 'null'"); | 
  | 138 | 0 |              } else if (!pluginDirectory.canWrite()) { | 
  | 139 | 0 |                      throw new IllegalArgumentException("Impossible to write to plugin directory so plugin cannot be expanded: " + pluginDirectory.getAbsolutePath()); | 
  | 140 |  |              } | 
  | 141 | 0 |      } | 
  | 142 |  |   | 
  | 143 |  |      public static void validatePluginDirectory(File directory) { | 
  | 144 | 0 |              if (directory == null) { | 
  | 145 | 0 |                          throw new IllegalArgumentException("Given directory was 'null'"); | 
  | 146 | 0 |                  } else if (!directory.exists()) { | 
  | 147 | 0 |                          throw new IllegalArgumentException("Given directory does not exist: " + directory.getAbsolutePath()); | 
  | 148 | 0 |                  } else if (!directory.isDirectory()) { | 
  | 149 | 0 |                          throw new IllegalArgumentException("Given plugin directory is not a valid directory: " + directory.getAbsolutePath()); | 
  | 150 | 0 |                  } else if (!directory.canRead()) { | 
  | 151 | 0 |                          throw new IllegalArgumentException("Permission denied to read given plugin directory: " + directory.getAbsolutePath()); | 
  | 152 |  |                  } | 
  | 153 | 0 |      } | 
  | 154 |  |   | 
  | 155 |  |      public static PluginRegistry getPluginRegistry() { | 
  | 156 | 0 |              return ((CoreResourceLoader)GlobalResourceLoader.getResourceLoader(CoreResourceLoader.NAME)).getRegistry(); | 
  | 157 |  |      } | 
  | 158 |  |   | 
  | 159 |  |      public static void installResourceLoader(Plugin plugin) { | 
  | 160 | 0 |              if (plugin.getConfig() instanceof PluginConfig) { | 
  | 161 | 0 |                          PluginConfig pluginConfig = (PluginConfig)plugin.getConfig(); | 
  | 162 | 0 |                          if (!StringUtils.isEmpty(pluginConfig.getResourceLoaderClassname())) { | 
  | 163 | 0 |                  ResourceLoader resourceLoader = (ResourceLoader) ResourceLoaderUtil.createObject(pluginConfig.getResourceLoaderClassname(), plugin.getClassLoader()); | 
  | 164 | 0 |                  if (resourceLoader == null) { | 
  | 165 | 0 |                      LOG.warn("Could not create resource loader from plugin resource loader class: " + pluginConfig.getResourceLoaderClassname()); | 
  | 166 |  |                       | 
  | 167 | 0 |                                          resourceLoader = new BaseResourceLoader(plugin.getName()); | 
  | 168 |  |                  } | 
  | 169 | 0 |                                  plugin.addResourceLoader(resourceLoader); | 
  | 170 |  |                          } | 
  | 171 |  |                  } | 
  | 172 | 0 |      } | 
  | 173 |  |   | 
  | 174 |  |      public static void installPluginListeners(Plugin plugin) { | 
  | 175 | 0 |              if (plugin.getConfig() instanceof PluginConfig) { | 
  | 176 | 0 |                          PluginConfig pluginConfig = (PluginConfig)plugin.getConfig(); | 
  | 177 | 0 |                          for (Iterator iterator = pluginConfig.getListeners().iterator(); iterator.hasNext();) { | 
  | 178 | 0 |                      String pluginListenerClassName = (String) iterator.next(); | 
  | 179 |  |                      try { | 
  | 180 | 0 |                          Class listenerClass = Class.forName(pluginListenerClassName, true, plugin.getClassLoader()); | 
  | 181 | 0 |                          plugin.addPluginListener((PluginListener)listenerClass.newInstance()); | 
  | 182 | 0 |                      } catch (ClassNotFoundException e) { | 
  | 183 | 0 |                          throw new PluginException(getLogPrefix(plugin)+" Error finding listener class '"+pluginListenerClassName+"'.", e); | 
  | 184 | 0 |                      } catch (InstantiationException e) { | 
  | 185 | 0 |                          throw new PluginException(getLogPrefix(plugin)+" Error creating an instance of listener class '"+pluginListenerClassName+"'.", e); | 
  | 186 | 0 |                      } catch (IllegalAccessException e) { | 
  | 187 | 0 |                          throw new PluginException(getLogPrefix(plugin)+" Error creating an instance of listener class '"+pluginListenerClassName+"'.", e); | 
  | 188 | 0 |                      } catch (ClassCastException e) { | 
  | 189 | 0 |                          throw new PluginException(getLogPrefix(plugin)+" Listener class '"+pluginListenerClassName+"' does not implement PluginListener.", e); | 
  | 190 | 0 |                      } | 
  | 191 | 0 |                  } | 
  | 192 |  |                  } | 
  | 193 | 0 |      } | 
  | 194 |  |   | 
  | 195 | 0 |      private PluginUtils() { } | 
  | 196 |  |  } |