1 | // Copyright 2012 Georg-August-Universität Göttingen, Germany |
---|
2 | // |
---|
3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
---|
4 | // you may not use this file except in compliance with the License. |
---|
5 | // You may obtain a copy of the License at |
---|
6 | // |
---|
7 | // http://www.apache.org/licenses/LICENSE-2.0 |
---|
8 | // |
---|
9 | // Unless required by applicable law or agreed to in writing, software |
---|
10 | // distributed under the License is distributed on an "AS IS" BASIS, |
---|
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
12 | // See the License for the specific language governing permissions and |
---|
13 | // limitations under the License. |
---|
14 | |
---|
15 | package de.ugoe.cs.autoquest.plugin; |
---|
16 | |
---|
17 | import java.io.File; |
---|
18 | import java.io.FileInputStream; |
---|
19 | import java.io.FileNotFoundException; |
---|
20 | import java.io.FilenameFilter; |
---|
21 | import java.io.IOException; |
---|
22 | import java.lang.reflect.InvocationTargetException; |
---|
23 | import java.lang.reflect.Method; |
---|
24 | import java.net.MalformedURLException; |
---|
25 | import java.net.URL; |
---|
26 | import java.net.URLClassLoader; |
---|
27 | import java.util.Collection; |
---|
28 | import java.util.Collections; |
---|
29 | import java.util.LinkedList; |
---|
30 | import java.util.jar.JarInputStream; |
---|
31 | import java.util.jar.Manifest; |
---|
32 | |
---|
33 | /** |
---|
34 | * <p> |
---|
35 | * This class provides the functionality to load QUEST plug-ins from a |
---|
36 | * pre-defined folder. |
---|
37 | * </p> |
---|
38 | * |
---|
39 | * @author Steffen Herbold |
---|
40 | * @version 1.0 |
---|
41 | */ |
---|
42 | public class PluginLoader { |
---|
43 | |
---|
44 | /** |
---|
45 | * <p> |
---|
46 | * Handle of the plug-in directory. |
---|
47 | * </p> |
---|
48 | */ |
---|
49 | private final File pluginDir; |
---|
50 | |
---|
51 | /** |
---|
52 | * <p> |
---|
53 | * Collection of the loaded plug-ins. |
---|
54 | * </p> |
---|
55 | */ |
---|
56 | private final Collection<QuestPlugin> plugins; |
---|
57 | |
---|
58 | /** |
---|
59 | * <p> |
---|
60 | * Constructor. Creates a new PluginLoader that can load plug-ins the |
---|
61 | * defined directory. |
---|
62 | * </p> |
---|
63 | * |
---|
64 | * @param pluginDir |
---|
65 | * handle of the directory; in case the handle is |
---|
66 | * <code>null</code> or does not describe a directory, an |
---|
67 | * {@link IllegalArgumentException} is thrown |
---|
68 | */ |
---|
69 | public PluginLoader(File pluginDir) { |
---|
70 | if (pluginDir == null) { |
---|
71 | throw new IllegalArgumentException( |
---|
72 | "Parameter pluginDir must not be null!"); |
---|
73 | } |
---|
74 | if (!pluginDir.isDirectory()) { |
---|
75 | throw new IllegalArgumentException("File " + pluginDir.getPath() |
---|
76 | + " is not a directory"); |
---|
77 | } |
---|
78 | this.pluginDir = pluginDir; |
---|
79 | plugins = new LinkedList<QuestPlugin>(); |
---|
80 | } |
---|
81 | |
---|
82 | /** |
---|
83 | * <p> |
---|
84 | * Loads plug-ins from {@link #pluginDir}. |
---|
85 | * </p> |
---|
86 | * |
---|
87 | * @throws PluginLoaderException |
---|
88 | * thrown if there is a problem loading a plug-in or updating |
---|
89 | * the classpath |
---|
90 | */ |
---|
91 | public void load() throws PluginLoaderException { |
---|
92 | File[] jarFiles = pluginDir.listFiles(new FilenameFilter() { |
---|
93 | @Override |
---|
94 | public boolean accept(File dir, String name) { |
---|
95 | return checkNameConformity(name); |
---|
96 | } |
---|
97 | }); |
---|
98 | |
---|
99 | for (File jarFile : jarFiles) { |
---|
100 | updateClassLoader(jarFile); |
---|
101 | |
---|
102 | String pluginName = jarFile.getName().split("-")[2]; |
---|
103 | String pluginClassName = "de.ugoe.cs.autoquest.plugin." + pluginName |
---|
104 | + "." + pluginName.toUpperCase() + "Plugin"; |
---|
105 | |
---|
106 | Class<?> pluginClass = null; |
---|
107 | try { |
---|
108 | pluginClass = Class.forName(pluginClassName); |
---|
109 | } catch (ClassNotFoundException e) { |
---|
110 | throw new PluginLoaderException("No class '" + pluginClassName |
---|
111 | + "' found in " + pluginDir + "/" + jarFile.getName()); |
---|
112 | } |
---|
113 | try { |
---|
114 | QuestPlugin pluginObject = (QuestPlugin) pluginClass |
---|
115 | .newInstance(); |
---|
116 | plugins.add(pluginObject); |
---|
117 | } catch (InstantiationException e) { |
---|
118 | throw new PluginLoaderException("Could not instantiate " |
---|
119 | + pluginClassName); |
---|
120 | } catch (IllegalAccessException e) { |
---|
121 | throw new PluginLoaderException("Could not access " |
---|
122 | + pluginClassName); |
---|
123 | } catch (ClassCastException e) { |
---|
124 | throw new PluginLoaderException("Class " + pluginClassName |
---|
125 | + " not instance of QuestPlugin"); |
---|
126 | } |
---|
127 | } |
---|
128 | } |
---|
129 | |
---|
130 | /** |
---|
131 | * <p> |
---|
132 | * Retrieves the classpath from a Jar file's MANIFEST. |
---|
133 | * </p> |
---|
134 | * |
---|
135 | * @throws IOException |
---|
136 | * @throws FileNotFoundException |
---|
137 | */ |
---|
138 | protected String[] getClassPathFromJar(File jarFile) { |
---|
139 | String[] classPath; |
---|
140 | |
---|
141 | JarInputStream jarInputStream = null; |
---|
142 | Manifest manifest = null; |
---|
143 | try { |
---|
144 | FileInputStream fileStream = new FileInputStream(jarFile); |
---|
145 | try { |
---|
146 | jarInputStream = new JarInputStream(fileStream); |
---|
147 | manifest = jarInputStream.getManifest(); |
---|
148 | } finally { |
---|
149 | jarInputStream.close(); |
---|
150 | fileStream.close(); |
---|
151 | } |
---|
152 | } catch (FileNotFoundException e) { |
---|
153 | throw new AssertionError( |
---|
154 | "FileNotFoundException should be impossible!"); |
---|
155 | } catch (IOException e) { |
---|
156 | throw new PluginLoaderException(e); |
---|
157 | } |
---|
158 | |
---|
159 | String jarClassPath = manifest.getMainAttributes().getValue( |
---|
160 | "Class-Path"); |
---|
161 | |
---|
162 | if (jarClassPath != null) { |
---|
163 | String[] jarClassPathElements = jarClassPath.split(" "); |
---|
164 | classPath = new String[jarClassPathElements.length]; |
---|
165 | for (int i = 0; i < jarClassPathElements.length; i++) { |
---|
166 | classPath[i] = "file:" |
---|
167 | + jarFile.getParentFile().getAbsolutePath() + "/" |
---|
168 | + jarClassPathElements[i]; |
---|
169 | } |
---|
170 | try { |
---|
171 | jarInputStream.close(); |
---|
172 | } catch (IOException e) { |
---|
173 | throw new PluginLoaderException(e); |
---|
174 | } |
---|
175 | } else { |
---|
176 | classPath = new String[] {}; |
---|
177 | } |
---|
178 | return classPath; |
---|
179 | } |
---|
180 | |
---|
181 | /** |
---|
182 | * <p> |
---|
183 | * Updates the classpath of the {@link ClassLoader} to include the plug-in |
---|
184 | * jar as well as further libraries required by the plug-in jar as defined |
---|
185 | * in the <code>Class-Path</code> section of its manifest. |
---|
186 | * </p> |
---|
187 | * |
---|
188 | * @param jarFile |
---|
189 | * handle of the plug-in jar file |
---|
190 | * @throws PluginLoaderException |
---|
191 | * thrown if there is a problem updating the class loader or |
---|
192 | * loading the plug-in jar |
---|
193 | */ |
---|
194 | private void updateClassLoader(File jarFile) throws PluginLoaderException { |
---|
195 | String[] classPath = getClassPathFromJar(jarFile); |
---|
196 | URLClassLoader classLoader = (URLClassLoader) ClassLoader |
---|
197 | .getSystemClassLoader(); |
---|
198 | Method method; |
---|
199 | |
---|
200 | try { |
---|
201 | method = URLClassLoader.class.getDeclaredMethod("addURL", |
---|
202 | new Class[] { URL.class }); |
---|
203 | } catch (SecurityException e) { |
---|
204 | throw new PluginLoaderException( |
---|
205 | "addURL method of URLClassLoader not accessible via reflection."); |
---|
206 | } catch (NoSuchMethodException e) { |
---|
207 | throw new AssertionError( |
---|
208 | "URLClassLoader does not have addURL method. Should be impossible!!"); |
---|
209 | } |
---|
210 | method.setAccessible(true); |
---|
211 | |
---|
212 | try { |
---|
213 | method.invoke( |
---|
214 | classLoader, |
---|
215 | new Object[] { new URL("file:" + jarFile.getAbsoluteFile()) }); |
---|
216 | for (String element : classPath) { |
---|
217 | method.invoke(classLoader, new Object[] { new URL(element) }); |
---|
218 | } |
---|
219 | } catch (IllegalArgumentException e) { |
---|
220 | throw new AssertionError( |
---|
221 | "Illegal arguments for addURL method. Should be impossible!!"); |
---|
222 | } catch (MalformedURLException e) { |
---|
223 | throw new PluginLoaderException(e); |
---|
224 | } catch (IllegalAccessException e) { |
---|
225 | throw new PluginLoaderException( |
---|
226 | "addURL method of URLClassLoader not accessible via reflection."); |
---|
227 | } catch (InvocationTargetException e) { |
---|
228 | throw new PluginLoaderException(e); |
---|
229 | } |
---|
230 | } |
---|
231 | |
---|
232 | /** |
---|
233 | * <p> |
---|
234 | * Checks if the name of a file indicates that it defines a QUEST plug-in. |
---|
235 | * The structure of valid plug-in filenames is |
---|
236 | * <code>quest-plugin-%PLUGIN_NAME%-version.jar</code>, where |
---|
237 | * <code>%PLUGIN_NAME%</code> is replaced by the name of the plug-in. Note |
---|
238 | * that plug-in names must not contain any dashes. |
---|
239 | * </p> |
---|
240 | * |
---|
241 | * @param filename |
---|
242 | * filename that is checked |
---|
243 | * @return true if filename matches pattern of QUEST plug-in; false |
---|
244 | * otherwise |
---|
245 | */ |
---|
246 | protected boolean checkNameConformity(String filename) { |
---|
247 | if (filename == null) { |
---|
248 | return false; |
---|
249 | } |
---|
250 | return filename.startsWith("quest-plugin-") && !filename.startsWith("quest-plugin-core") |
---|
251 | && |
---|
252 | ((filename.split("-").length == 4 && filename.endsWith(".jar")) || |
---|
253 | filename.split("-").length == 5 && filename.endsWith("SNAPSHOT.jar")); |
---|
254 | } |
---|
255 | |
---|
256 | public Collection<QuestPlugin> getPlugins() { |
---|
257 | return Collections.unmodifiableCollection(plugins); |
---|
258 | } |
---|
259 | } |
---|