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