-
Notifications
You must be signed in to change notification settings - Fork 13
/
PlugInManager.java
595 lines (524 loc) · 22.8 KB
/
PlugInManager.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
/*
* The Unified Mapping Platform (JUMP) is an extensible, interactive GUI for
* visualizing and manipulating spatial features with geometry and attributes.
*
* Copyright (C) 2003 Vivid Solutions
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc., 59 Temple
* Place - Suite 330, Boston, MA 02111-1307, USA.
*
* For more information, contact:
*
* Vivid Solutions Suite #1A 2328 Government Street Victoria BC V8T 5G5 Canada
*
* (250)385-6040 www.vividsolutions.com
*/
package com.vividsolutions.jump.workbench.plugin;
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import org.locationtech.jts.util.Assert;
import com.vividsolutions.jump.I18N;
import com.vividsolutions.jump.JUMPException;
import com.vividsolutions.jump.task.TaskMonitor;
import com.vividsolutions.jump.util.FileUtil;
import com.vividsolutions.jump.util.StringUtil;
import com.vividsolutions.jump.util.Timer;
import com.vividsolutions.jump.workbench.Logger;
import com.vividsolutions.jump.workbench.WorkbenchContext;
import com.vividsolutions.jump.workbench.WorkbenchProperties;
import com.vividsolutions.jump.workbench.ui.cursortool.CursorTool;
import com.vividsolutions.jump.workbench.ui.cursortool.CursorToolPluginWrapper;
import com.vividsolutions.jump.workbench.ui.plugin.FeatureInstaller;
/**
* Loads plug-ins (or more precisely, Extensions), and any JAR files that they
* depend on, from the plug-in directory.
*/
public class PlugInManager {
private static final String NOT_INITIALIZED = "com.vividsolutions.jump.workbench.plugin.PlugInManager.could-not-be-initialized";
private static final String LOADING = "com.vividsolutions.jump.workbench.plugin.PlugInManager.loading";
private static final String LOADING_ERROR = "com.vividsolutions.jump.workbench.plugin.PlugInManager.throwable-encountered-loading";
private TaskMonitor monitor;
private WorkbenchContext context;
private ArrayList<Configuration> configurations = new ArrayList<Configuration>();
private List<File> extensionDirs = new ArrayList<File>();
// switch to parse only jar/zips in extension dir root for extensions
// enabled by default, if disabled also classes/zip recursively are parsed
private boolean limitExtensionLookup = true;
private PlugInClassLoader classLoader;
/**
* @param plugInDirectory
* null to leave unspecified
*/
public PlugInManager(WorkbenchContext context, TaskMonitor monitor) throws Exception {
this.monitor = monitor;
// class ExtendedURLClassLoader extends URLClassLoader{
//
// public ExtendedURLClassLoader(URL[] urls) {
// super(urls);
// }
//
// /**
// * not really necessary now, but we keep it for reference for a future
// * classloader per extension for allowing extensions to use differently
// * versioned dependency jars in separate subfolders under
// * lib/ext/<extension_subfolder>/
// */
// @Override
// public Class loadClass(String name) throws ClassNotFoundException {
// Class c = findLoadedClass(name);
// if (c == null) {
// try {
// // disabled but not removed: here is the place to enforce plugin
// // cl for specific classes/paths
// // // these have to be handled like external packages
// if (name.matches("(?i).*WFS.*"))
// System.out.println(name);
// if (!name.startsWith("de.latlon.deejump.wfs")
// && !name.startsWith("org.deegree.")){
//// if (!name.matches(".*WFSExtension"))
// c = getParent().loadClass(name);
// }
// } catch (ClassNotFoundException e) {
// }
// if (c == null)
// c = findClass(name);
// }
// return c;
// }
//
// /**
// * allow adding urls, any time
// * @param urls
// */
// public void addUrls( URL[] urls ){
// for (URL url : urls) {
// addURL(url);
// }
// }
// };
//
// ExtendedURLClassLoader mycl = new ExtendedURLClassLoader(new URL[]{});
// System.out.println("A:"+ClassLoader.getSystemClassLoader().getClass().getClassLoader());
// System.out.println("B:"+PlugInClassLoader.class.getClassLoader());;
try {
classLoader = (PlugInClassLoader) ClassLoader.getSystemClassLoader();
} catch (ClassCastException e) {
Exception je = new JUMPException(
"Wrong classloader. Make sure to run JRE with property -Djava.system.class.loader=com.vividsolutions.jump.workbench.plugin.PlugInClassLoader set!",e);
throw je;
}
this.context = context;
// fetch default-plugins early, to enqueue them at the beginning
configurations.addAll(findConfigurations(context.getWorkbench()
.getProperties().getConfigurationClassNames()));
}
public void setLimitExtensionLookup(boolean limitExtensionLookup) {
this.limitExtensionLookup = limitExtensionLookup;
}
/**
* add an extensions folder
* - adds folder root to classpath
* - adds all contained jars to classpath
* - looks for extensions in jars only in folder root (to speedup start)
* - looks for extensions in any jar file and as class files
* recursively if limit-ext-lookup is 'false'
*
* @param dir
*/
public void addExtensionsFolder(File dir) {
// already added? we don't want duplicates
if (extensionDirs.contains(dir)) {
Logger.error("extensions-dir '"+dir+"' was already added before!");
return;
}
if (!dir.exists()) {
Logger.error("given extensions-dir '"+dir+"' does not exist!");
return;
}
// add contained jars/zips _and_ base folder to classpath
ArrayList<File> files = new ArrayList();
files.add( dir );
files.addAll( findFilesRecursively( dir, jarOrFolderFilter, true) );
classLoader.addUrls(toURLs(files));
// reload translation resource bundles after changing classpath
I18N.reset();
// memorize to internal list
extensionDirs.add(dir);
configurations.addAll(findConfigurations(dir));
}
/**
* add all jar files in a folder recursively to classpath
* @param dir
*/
public void addJarsFolder(File dir) {
// add contained jars/zips and base folder to classloader
ArrayList<File> files = new ArrayList();
files.addAll( findFilesRecursively( dir, jarOrFolderFilter, true) );
classLoader.addUrls(toURLs(files));
// reload translation resource bundles after changing classpath
I18N.reset();
}
/**
* search extension directories in the order they were added
* for a given file or folder
* return first found or null if none exists
*
* @param fileOrFolder
* @return file object or null
*/
public File findFileOrFolderInExtensionDirs( String fileOrFolder ) {
for (File extDir : extensionDirs) {
File candidate = new File( extDir, fileOrFolder );
if ( candidate.exists() )
return candidate;
}
return null;
}
// pretty much the main method, finds and loads extensions
public void load() throws Exception {
if (Logger.isDebugEnabled())
Logger.debug("Pluginclassloader contains the folowing urls ->\n" + Arrays.asList(classLoader.getURLs()).stream()
.map(Object::toString).collect(java.util.stream.Collectors.joining("\n")).toString());
// load plugins from workbench-properties
loadPlugIns(context.getWorkbench().getProperties());
if (Logger.isDebugEnabled())
Logger.debug("The following extensions were found and are about to be loaded ->\n" + configurations.stream()
.map(Object::toString).collect(java.util.stream.Collectors.joining("\n")).toString());
long start = Timer.milliSecondsSince(0);
loadConfigurations();
Logger.info("Loading all OJ extensions took "
+ Timer.secondsSinceString(start) + "s");
FeatureInstaller f = FeatureInstaller.getInstance(context);
// enable autoseparating in installer for plugins possibly installed later on
f.setSeparatingEnabled(true);
// for performance reasons we separate menu entries once
// after all plugins/extensions were installed
f.updateSeparatorsInAllMenus();
}
private void loadConfigurations() {
PlugInContext pc = context.createPlugInContext();
for (Iterator i = configurations.iterator(); i.hasNext();) {
Configuration configuration = (Configuration) i.next();
long start = Timer.milliSecondsSince(0);
try {
monitor.report(I18N.getInstance().get(LOADING) + " " + name(configuration) + " "
+ version(configuration));
// we used the plugin classloader to instantiate extensions already above
configuration.configure(pc);
//System.out.println(Arrays.toString(((URLClassLoader)classLoader).getURLs()));
Logger.info("Loading Config " + name(configuration) + " "
+ version(configuration) + " took " + Timer.secondsSinceString(start)
+ "s");
}
catch (Throwable e) {
context.getErrorHandler().handleThrowable(e);
context.getWorkbench().getFrame()
.log(configuration.getClass().getName() + " " + I18N.getInstance().get(NOT_INITIALIZED), this.getClass());
}
}
}
private void loadPlugIns(WorkbenchProperties props) {
PlugInContext pc = context.createPlugInContext();
// List<String> classNames = props.getPlugInClassNames();
Map<String, Map<String, String>> pluginSettings = props
.getSettings(new String[]{WorkbenchProperties.KEY_PLUGIN});
for (String className : pluginSettings.keySet()) {
// System.out.println(i++ + "/"+ className);
String initSetting = pluginSettings.get(className).get(
WorkbenchProperties.ATTR_INITIALIZE);
if (initSetting instanceof String
&& initSetting.equals(WorkbenchProperties.ATTR_VALUE_FALSE))
continue;
monitor.report(I18N.getInstance().get(LOADING) + " " + className);
Class plugInClass = null;
try {
long start = Timer.milliSecondsSince(0);
// make sure we use the plugin classloader for plugins
plugInClass = classLoader.loadClass(className);
if (plugInClass == null)
throw new JUMPException("class '"+className+"' is not available in the class path!");
Object o = plugInClass.newInstance();
PlugIn plugIn;
if (o instanceof CursorTool) {
plugIn = new CursorToolPluginWrapper((CursorTool) o);
} else {
plugIn = (PlugIn) o;
}
plugIn.initialize(pc);
// get plugin's menu settings
Map<String, Map> menuSettings = props.getSettings(new String[] {
WorkbenchProperties.KEY_PLUGIN, className,
WorkbenchProperties.KEY_MENUS});
// interpret menu settings
for (Map.Entry<String, Map> entry : menuSettings.entrySet()) {
String menuKey = entry.getKey();
if (pc.getFeatureInstaller().fetchMenuForKey(menuKey)==null){
if (menuKey != "order_id")
Logger.error("'"+menuKey+"' is an invalid menu handle.");
continue;
}
// install me to menu?
String installSetting = props.getSetting(new String[] {
WorkbenchProperties.KEY_PLUGIN, className,
WorkbenchProperties.KEY_MENUS,
menuKey,
WorkbenchProperties.ATTR_INSTALL});
// String orderSetting = props.getSetting(new String[] {
// WorkbenchProperties.KEY_PLUGIN, className,
// WorkbenchProperties.KEY_MENUS,
// menuKey,
// WorkbenchProperties.ATTR_ORDERID});
// log (order) info
// context
// .getWorkbench()
// .getFrame()
// .log(
// "install " + className + " to " + menuKey + " = "
// + installSetting + " with orderid = " + orderSetting);
// install, or not
if (installSetting.equals(WorkbenchProperties.ATTR_VALUE_TRUE))
pc.getFeatureInstaller().addMenuPlugin(menuKey, plugIn);
}
// register shortcuts of plugins
pc.getFeatureInstaller().registerShortcuts(plugIn);
context
.getWorkbench()
.getFrame()
.log(
"Loading Plugin " + className + " took " + Timer.secondsSinceString(start)
+ "s " );
} catch (Throwable e) {
context.getErrorHandler().handleThrowable(e);
context.getWorkbench().getFrame()
.log(className + " " + I18N.getInstance().get(NOT_INITIALIZED), this.getClass());
}
}
}
public static String name(Configuration configuration) {
if (configuration instanceof Extension) {
return ((Extension) configuration).getName();
}
return StringUtil.toFriendlyName(configuration.getClass().getName(),
"Configuration")
+ " (" + configuration.getClass().getPackage().getName() + ")";
}
public static String version(Configuration configuration) {
if (configuration instanceof Extension) {
return ((Extension) configuration).getVersion();
}
return "";
}
public static String message(Configuration configuration) {
if (configuration instanceof Extension) {
return ((Extension) configuration).getMessage();
}
return "";
}
/**
* filter all Configurations from a list of class names
* @param classNames name of Configuration classes
* @return a collection of Configurations
*/
private Collection<Configuration> findConfigurations(List<String> classNames) {
List<Configuration> configurations = new ArrayList<>();
for (Iterator i = classNames.iterator(); i.hasNext();) {
String name = (String) i.next();
try {
// find class using the plugin classloader
Class clazz = Class.forName(name, false, classLoader);
if ( Configuration.class.isAssignableFrom(clazz) ) {
Configuration configuration = (Configuration) clazz.newInstance();
configurations.add(configuration);
}
}
// make sure ClassVersionErrors or such do not break OJ startup
catch (Throwable t) {
context.getErrorHandler().handleThrowable(t);
continue;
}
}
return configurations;
}
static Pattern jarFilePattern = Pattern.compile(".*\\.(?i:jar|zip)$");
static FileFilter jarOrFolderFilter = new FileFilter(){
public boolean accept(File f) {
return jarFilePattern.matcher(f.getName()).matches() || f.isDirectory();
}
};
static FileFilter extensionClassOrFolderFilter = new FileFilter(){
public boolean accept(File f) {
return isExtensionClassByName(f.getName()) || f.isDirectory();
}
};
// no $ ensures that inner classes are ignored as well [ede]
// Include "Configuration" for backwards compatibility. [Jon Aquino]
static Pattern extensionClassPattern = Pattern.compile("[^$]+(Extension|Configuration)\\.class");
private static boolean isExtensionClassByName( String name ) {
return extensionClassPattern.matcher(name).matches();
}
private Collection<File> findFilesRecursively(File directory, FileFilter filter, boolean recursive) {
Collection<File> filesFiltered = new ArrayList();
// add only jars/zips/classes if filter is set (may be null), recursively if requested
File[] files = directory.listFiles(filter);
if (files == null) files = new File[0];
for (File file : files) {
if (file.isDirectory() && recursive) {
filesFiltered.addAll(findFilesRecursively(file, filter, recursive));
}
else if (file.isFile())
filesFiltered.add(file);
}
return filesFiltered;
}
private Collection<Configuration> findConfigurations(File plugInDirectory) {
ArrayList<Configuration> configurations = new ArrayList<Configuration>();
long start = Timer.milliSecondsSince(0);
String folder = plugInDirectory.getPath();
String msg = I18N.getInstance().get("com.vividsolutions.jump.workbench.plugin.PlugInManager.scan", folder);
monitor.report(msg);
// find class files that are extensions if '-limit-ext-lookup false'
for (Iterator<File> i = findFilesRecursively(plugInDirectory, extensionClassOrFolderFilter, true)
.iterator(); !limitExtensionLookup && i.hasNext();) {
File file = (File) i.next();
String relativePath = plugInDirectory.toURI().relativize(file.toURI()).getPath();
Configuration c = toConfiguration(relativePath);
if (c != null)
configurations.add(c);
}
// we're looking into jar files in ext-folder root only by default (to speedup OJ
// start), may be disabled for testing via cmd line param '-limit-ext-lookup false'
for (Iterator i = findFilesRecursively(plugInDirectory, jarOrFolderFilter, !limitExtensionLookup).iterator(); i
.hasNext();) {
File file = (File) i.next();
ZipFile zipFile = null;
try {
zipFile = new ZipFile(file);
for (Enumeration e = zipFile.entries(); e.hasMoreElements();) {
ZipEntry entry = (ZipEntry) e.nextElement();
// Filter by filename; otherwise we'll be loading all the classes,
// which takes significantly longer [Jon Aquino]
// no $ ensures that inner classes are ignored as well [ede]
// Include "Configuration" for backwards compatibility. [Jon Aquino]
if (!isExtensionClassByName(entry.getName()) || entry.isDirectory())
continue;
Configuration c = toConfiguration(entry.getName());
if (c != null)
configurations.add(c);
}
} catch (IOException e) {
Logger.error("Configuration failed for " + file.getPath());
Logger.error(e);
} finally {
FileUtil.close(zipFile);
}
}
Logger.info("Scanning " + folder + " took " + Timer.secondsSinceString(start) + "s. Found "
+ configurations.size() + " configurations.");
return configurations;
}
private URL[] toURLs(Collection<File> files) {
URL[] urls = new URL[files.size()];
int i = 0;
for (File file : files) {
try {
// TODO: if "jar:file:" not explicitely defined VertexSymbols Extensions die with
// "java.lang.SecurityException: sealing violation: can't seal package <>: already loaded"
// [ede 05.2012]
// disabled as it chokes ClassGraph, also it doesn't seem to be needed anymore
// for VertexSymbols [ede 01.2022]
//urls[i++] = file.isFile() ? new URL("jar:file:" + file.getPath() + "!/") :
// file.toURI().toURL();
urls[i++] = file.toURI().toURL();
} catch (MalformedURLException e) {
Assert.shouldNeverReachHere(e.toString());
}
}
return urls;
}
private Configuration toConfiguration( String fileName )
{
String className = toClassName(fileName);
Class<?> candidate;
try {
candidate = Class.forName(className, false, classLoader);
// make sure we got us a real configuration here
if ( Configuration.class.isAssignableFrom(candidate) ) {
return (Configuration) candidate.getDeclaredConstructor().newInstance();
}
} catch (ClassNotFoundException e) {
throw new RuntimeException("This should never happen. Check classpath!", e);
} catch (Throwable t) {
throw new RuntimeException(I18N.getInstance().get(LOADING_ERROR) + " " + className, t);
//e.g. java.lang.VerifyError: class
// org.apache.xml.serialize.XML11Serializer
//overrides final method [Jon Aquino]
}
return null;
}
private String toClassName(String className)
{
className = className.substring(0, className.length()
- ".class".length());
className = StringUtil.replaceAll(className, "/", ".");
return className;
}
public Collection<Configuration> getConfigurations() {
return Collections.unmodifiableCollection(configurations);
}
/**
* To access extension classes, use this ClassLoader rather than the default
* ClassLoader. Extension classes will not be present in the latter.
*
* @deprecated use {@link #getPlugInClassLoader()} instead
*/
public ClassLoader getClassLoader() {
return classLoader;
}
/**
* To access extension classes, use this ClassLoader rather than the default
* ClassLoader. Extension classes will not be present in the latter.
*/
public PlugInClassLoader getPlugInClassLoader() {
return classLoader;
}
/**
* fetch a list of folders holding extension jars that were added during start
*/
public List<File> getExtensionDirs(){
return Collections.unmodifiableList(extensionDirs);
}
/**
* get extension folder, cloned to prevent modification
* @deprecated use {@link #getExtensionDirs()}
* @return possibly null
*/
@Deprecated
public File getPlugInDirectory() {
return extensionDirs.isEmpty() ? null : new File(extensionDirs.get(0).toURI());
}
}