-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paths2_c.java
851 lines (725 loc) · 24.2 KB
/
s2_c.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
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
// $Id: ModuleLoader2.java 151 2014-06-16 09:40:44Z benjamin_klatt $
// Copyright (c) 2004-2008 The Regents of the University of California. All
// Rights Reserved. Permission to use, copy, modify, and distribute this
// software and its documentation without fee, and without a written
// agreement is hereby granted, provided that the above copyright notice
// and this paragraph appear in all copies. This software program and
// documentation are copyrighted by The Regents of the University of
// California. The software program and documentation are supplied "AS
// IS", without any accompanying services from The Regents. The Regents
// does not warrant that the operation of the program will be
// uninterrupted or error-free. The end-user understands that the program
// was developed for research purposes and is advised not to rely
// exclusively on the program for any reason. IN NO EVENT SHALL THE
// UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
// SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS,
// ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
// THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
// SUCH DAMAGE. THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
// PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
// CALIFORNIA HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE, SUPPORT,
// UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
package org.argouml.moduleloader;
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Modifier;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.jar.Attributes;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.jar.Manifest;
import org.argouml.application.api.AbstractArgoJPanel;
import org.argouml.application.api.Argo;
import org.argouml.i18n.Translator;
/**
* This is the module loader that loads modules implementing the
* ModuleInterface.<p>
*
* This is a singleton. There are convenience functions that are static
* to access the module.<p>
*
* @stereotype singleton
* @author Linus Tolke
* @since 0.15.4
*/
public final class ModuleLoader2 {
/**
* This map contains the module loader information about the module.<p>
*
* The keys is the list of available modules.
*/
private Map<ModuleInterface, ModuleStatus> moduleStatus;
/**
* List of locations that we've searched and/or loaded modules
* from. This is for information purposes only to allow it to
* be displayed in the settings Environment tab.
*/
private List<String> extensionLocations = new ArrayList<String>();
/**
* The module loader object.
*/
private static final ModuleLoader2 INSTANCE = new ModuleLoader2();
/**
* The prefix in URL:s that are files.
*/
private static final String FILE_PREFIX = "file:";
/**
* The prefix in URL:s that are jars.
*/
private static final String JAR_PREFIX = "jar:";
/**
* Class file suffix.
*/
public static final String CLASS_SUFFIX = ".class";
/**
* Constructor for this object.
*/
private ModuleLoader2() {
moduleStatus = new HashMap<ModuleInterface, ModuleStatus>();
computeExtensionLocations();
}
/**
* Get hold of the instance of this object.
*
* @return the instance
*/
public static ModuleLoader2 getInstance() {
return INSTANCE;
}
/**
* @return a list of Details panel tabs
*/
List<AbstractArgoJPanel> getDetailsTabs() {
List<AbstractArgoJPanel> result = new ArrayList<AbstractArgoJPanel>();
for (ModuleInterface module : getInstance().availableModules()) {
ModuleStatus status = moduleStatus.get(module);
if (status == null) {
continue;
}
if (status.isEnabled()) {
if (module instanceof DetailsTabProvider) {
result.addAll(
((DetailsTabProvider) module).getDetailsTabs());
}
}
}
return result;
}
/**
* Return a collection of all available modules.
*
* @return A Collection of all available modules.
*/
private Collection<ModuleInterface> availableModules() {
return Collections.unmodifiableCollection(moduleStatus.keySet());
}
// Access methods for program infrastructure.
/**
* Enables all selected modules and disabling all modules not selected.<p>
*
* In short this attempts to make the modules obey their selection.<p>
*
* @param failingAllowed is <code>true</code> if enabling or disabling of
* some of the modules is allowed to fail.
*/
public static void doLoad(boolean failingAllowed) {
getInstance().doInternal(failingAllowed);
}
// Access methods for modules that need to query about the status of
// other modules.
/**
* Gets the loaded status for some other module.
*
* @return true if the module exists and is enabled.
* @param name is the module name of the queried module
*/
public static boolean isEnabled(String name) {
return getInstance().isEnabledInternal(name);
}
// Access methods for the GUI that the user uses to enable and disable
// modules.
/**
* Get a Collection with all the names.
*
* @return all the names.
*/
public static Collection<String> allModules() {
Collection<String> coll = new HashSet<String>();
for (ModuleInterface mf : getInstance().availableModules()) {
coll.add(mf.getName());
}
return coll;
}
/**
* Get the selected.
*
* @param name The name of the module.
* @return <code>true</code> if the module is selected.
*/
public static boolean isSelected(String name) {
return getInstance().isSelectedInternal(name);
}
/**
* Get the selected.
*
* @see #isSelected(String)
* @param name The name of the module.
* @return <code>true</code> if the module is selected.
*/
private boolean isSelectedInternal(String name) {
Map.Entry<ModuleInterface, ModuleStatus> entry = findModule(name);
if (entry != null) {
ModuleStatus status = entry.getValue();
if (status == null) {
return false;
}
return status.isSelected();
}
return false;
}
/**
* Set the selected value.
*
* @param name The name of the module.
* @param value Selected or not.
*/
public static void setSelected(String name, boolean value) {
getInstance().setSelectedInternal(name, value);
}
/**
* Set the selected value.
*
* @see #setSelected(String, boolean)
* @param name The name of the module.
* @param value Selected or not.
*/
private void setSelectedInternal(String name, boolean value) {
Map.Entry<ModuleInterface, ModuleStatus> entry = findModule(name);
if (entry != null) {
ModuleStatus status = entry.getValue();
status.setSelected(value);
}
}
/**
* Create a description of the module based on the information provided
* by the module itself.
*
* @param name The name of the module.
* @return The description.
*/
public static String getDescription(String name) {
return getInstance().getDescriptionInternal(name);
}
/**
* Create a description of the module based on the information provided
* by the module itself.
*
* @see #getDescription(String)
* @param name The name of the module.
* @return The description.
*/
private String getDescriptionInternal(String name) {
Map.Entry<ModuleInterface, ModuleStatus> entry = findModule(name);
if (entry == null) {
throw new IllegalArgumentException("Module does not exist.");
}
ModuleInterface module = entry.getKey();
StringBuffer sb = new StringBuffer();
String desc = module.getInfo(ModuleInterface.DESCRIPTION);
if (desc != null) {
sb.append(desc);
sb.append("\n\n");
}
String author = module.getInfo(ModuleInterface.AUTHOR);
if (author != null) {
sb.append("Author: ").append(author);
sb.append("\n");
}
String version = module.getInfo(ModuleInterface.VERSION);
if (version != null) {
sb.append("Version: ").append(version);
sb.append("\n");
}
return sb.toString();
}
// Access methods for the program infrastructure
/**
* Enables all selected modules.
*
* @param failingAllowed is true if this is not the last attempt at
* turning on.
*/
private void doInternal(boolean failingAllowed) {
huntForModules();
boolean someModuleSucceeded;
do {
someModuleSucceeded = false;
for (ModuleInterface module : getInstance().availableModules()) {
ModuleStatus status = moduleStatus.get(module);
if (status == null) {
continue;
}
if (!status.isEnabled() && status.isSelected()) {
try {
if (module.enable()) {
someModuleSucceeded = true;
status.setEnabled();
}
}
// Catch all exceptions and errors, however severe
catch (Throwable e) {
}
} else if (status.isEnabled() && !status.isSelected()) {
try {
if (module.disable()) {
someModuleSucceeded = true;
status.setDisabled();
}
}
// Catch all exceptions and errors, however severe
catch (Throwable e) {
}
}
}
} while (someModuleSucceeded);
if (!failingAllowed) {
// Notify the user that the modules in the list that are selected
// but not enabled were not possible to enable and that are not
// selected that we cannot disable.
//
// Currently we just log this.
//
// TODO: We could eventually pop up some warning window.
//
for (ModuleInterface module : getInstance().availableModules()) {
ModuleStatus status = moduleStatus.get(module);
if (status == null) {
continue;
}
if (status.isEnabled() && status.isSelected()) {
continue;
}
if (!status.isEnabled() && !status.isSelected()) {
continue;
}
}
}
}
/**
* Gets the loaded status for some other module.
*
* @return true if the module exists and is enabled.
* @param name is the module name of the queried module
*/
private boolean isEnabledInternal(String name) {
Map.Entry<ModuleInterface, ModuleStatus> entry = findModule(name);
if (entry != null) {
ModuleStatus status = entry.getValue();
if (status == null) {
return false;
}
return status.isEnabled();
}
return false;
}
/**
* Return the ModuleInterface, ModuleStatus pair for the module
* with the given name or <code>null</code> if there isn't any.
*
* @param name The given name.
* @return A pair (Map.Entry).
*/
private Map.Entry<ModuleInterface, ModuleStatus> findModule(String name) {
for (Map.Entry<ModuleInterface, ModuleStatus> entry : moduleStatus
.entrySet()) {
ModuleInterface module = entry.getKey();
if (name.equalsIgnoreCase(module.getName())) {
return entry;
}
}
return null;
}
/**
* Tries to find as many available modules as possible.<p>
*
* As the modules are found they are appended to {@link #moduleStatus}.<p>
*/
private void huntForModules() {
}
/**
* Find and enable modules from our "ext" directory and from the
* directory specified in "argo.ext.dir".<p>
*/
private void huntForModulesFromExtensionDir() {
for (String location : extensionLocations) {
huntModulesFromNamedDirectory(location);
}
}
/**
* This does a calculation of where our "ext" directory is.
* TODO: We should eventually make sure that this calculation is
* only present in one place in the code and not several.
*/
private void computeExtensionLocations() {
// Use a little trick to find out where Argo is being loaded from.
// TODO: Use a different resource here. ARGOINI is unused and deprecated
String extForm = getClass().getResource(Argo.ARGOINI).toExternalForm();
String argoRoot =
extForm.substring(0,
extForm.length() - Argo.ARGOINI.length());
// If it's a jar, clean it up and make it look like a file url
if (argoRoot.startsWith(JAR_PREFIX)) {
argoRoot = argoRoot.substring(JAR_PREFIX.length());
if (argoRoot.endsWith("!")) {
argoRoot = argoRoot.substring(0, argoRoot.length() - 1);
}
}
String argoHome = null;
if (argoRoot != null) {
if (argoRoot.startsWith(FILE_PREFIX)) {
argoHome =
new File(argoRoot.substring(FILE_PREFIX.length()))
.getAbsoluteFile().getParent();
} else {
argoHome = new File(argoRoot).getAbsoluteFile().getParent();
}
try {
argoHome = java.net.URLDecoder.decode(argoHome,
Argo.getEncoding());
} catch (UnsupportedEncodingException e) {
}
}
if (argoHome != null) {
String extdir;
if (argoHome.startsWith(FILE_PREFIX)) {
extdir = argoHome.substring(FILE_PREFIX.length())
+ File.separator + "ext";
} else {
extdir = argoHome + File.separator + "ext";
}
extensionLocations.add(extdir);
}
String extdir = System.getProperty("argo.ext.dir");
if (extdir != null) {
extensionLocations.add(extdir);
}
}
/**
* Get the list of locations that we've loaded extension modules from.
* @return A list of the paths we've loaded from.
*/
public List<String> getExtensionLocations() {
return Collections.unmodifiableList(extensionLocations);
}
/**
* Find and enable a module from a given directory.
*
* @param dirname The name of the directory.
*/
private void huntModulesFromNamedDirectory(String dirname) {
File extensionDir = new File(dirname);
if (extensionDir.isDirectory()) {
File[] files = extensionDir.listFiles(new JarFileFilter());
for (File file : files) {
JarFile jarfile = null;
// Try-catch only the JarFile instantiation so we
// don't accidentally mask anything in ArgoJarClassLoader
// or processJarFile.
try {
jarfile = new JarFile(file);
if (jarfile != null) {
// TODO: Should we be delegating to a different
// classloader than the default here? - - tfm
ClassLoader classloader =
new URLClassLoader(new URL[] {
file.toURI().toURL(),
});
try {
processJarFile(classloader, file);
} catch (ClassNotFoundException e) {
return;
}
}
} catch (IOException ioe) {
}
}
}
}
/**
* Check a jar file for an ArgoUML extension/module.<p>
*
* If there isn't a manifest or it isn't readable, we fall back to using
* the raw JAR entries.
*
* @param classloader The classloader to use.
* @param file The file to process.
* @throws ClassNotFoundException if the manifest file contains a class
* that doesn't exist.
*/
private void processJarFile(ClassLoader classloader, File file)
throws ClassNotFoundException {
JarFile jarfile;
try {
jarfile = new JarFile(file);
} catch (IOException e) {
return;
}
Manifest manifest;
try {
manifest = jarfile.getManifest();
if (manifest == null) {
// We expect all extensions to have a manifest even though we
// can operate without one if necessary.
}
} catch (IOException e) {
return;
}
// TODO: It is a performance drain to load all classes at startup time.
// They should be lazy loaded when needed. Instead of scanning all
// classes for ones which implement our loadable module interface, we
// should use a manifest entry or a special name/name pattern that we
// look for to find the single main module class to load here. - tfm
boolean loadedClass = false;
if (manifest == null) {
Enumeration<JarEntry> jarEntries = jarfile.entries();
while (jarEntries.hasMoreElements()) {
JarEntry entry = jarEntries.nextElement();
loadedClass =
loadedClass
| processEntry(classloader, entry.getName());
}
} else {
Map<String, Attributes> entries = manifest.getEntries();
for (String key : entries.keySet()) {
// Look for our specification
loadedClass =
loadedClass
| processEntry(classloader, key);
}
}
// Add this to search list for I18N properties
// (Done for both modules & localized property file sets)
Translator.addClassLoader(classloader);
}
/**
* Process a JAR file entry, attempting to load anything that looks like a
* Java class.
*
* @param classloader
* the classloader to use when loading the class
* @param cname
* the class name
* @throws ClassNotFoundException
* @return true if class was a module class and loaded successfully
*/
private boolean processEntry(ClassLoader classloader, String cname)
throws ClassNotFoundException {
if (cname.endsWith(CLASS_SUFFIX)) {
int classNamelen = cname.length() - CLASS_SUFFIX.length();
String className = cname.substring(0, classNamelen);
className = className.replace('/', '.');
return addClass(classloader, className);
}
return false;
}
/**
* Add a class from the current class loader.
*
* @param classname The name of the class (including package).
* @throws ClassNotFoundException if the class classname is not found.
*/
public static void addClass(String classname)
throws ClassNotFoundException {
getInstance().addClass(ModuleLoader2.class.getClassLoader(),
classname);
}
/**
* Try to load a module from the given ClassLoader.<p>
*
* Only add it as a module if it is a module (i.e. it implements the
* {@link ModuleInterface} interface.
*
* @param classLoader The ClassLoader to load from.
* @param classname The name.
* @throws ClassNotFoundException if the class classname is not found.
*/
private boolean addClass(ClassLoader classLoader, String classname)
throws ClassNotFoundException {
Class moduleClass;
try {
moduleClass = classLoader.loadClass(classname);
} catch (UnsupportedClassVersionError e) {
return false;
} catch (NoClassDefFoundError e) {
return false;
} catch (Throwable e) {
if (e instanceof ClassNotFoundException) {
throw (ClassNotFoundException) e;
}
return false;
}
if (!ModuleInterface.class.isAssignableFrom(moduleClass)) {
return false;
}
Constructor defaultConstructor;
try {
defaultConstructor =
moduleClass.getDeclaredConstructor(new Class[] {});
} catch (SecurityException e) {
return false;
} catch (NoSuchMethodException e) {
return false;
} catch (NoClassDefFoundError e) {
return false;
} catch (Throwable e) {
return false;
}
if (!Modifier.isPublic(defaultConstructor.getModifiers())) {
return false;
}
Object moduleInstance;
try {
moduleInstance = defaultConstructor.newInstance(new Object[]{});
} catch (IllegalArgumentException e) {
return false;
} catch (InstantiationException e) {
return false;
} catch (IllegalAccessException e) {
return false;
} catch (InvocationTargetException e) {
return false;
} catch (NoClassDefFoundError e) {
return false;
} catch (Throwable e) {
return false;
}
// The following check should have been satisfied before we
// instantiated the module, but double check again
if (!(moduleInstance instanceof ModuleInterface)) {
return false;
}
ModuleInterface mf = (ModuleInterface) moduleInstance;
addModule(mf);
return true;
}
/**
* Add a newly found module to {@link #moduleStatus}. If we already
* know about it, don't add it.
*
* @param mf The module to add.
*/
private void addModule(ModuleInterface mf) {
// Since there is no way to compare the objects as equal,
// we have to search through the list at this point.
for (ModuleInterface foundMf : moduleStatus.keySet()) {
if (foundMf.getName().equals(mf.getName())) {
return;
}
}
// We havn't found it. Add it.
ModuleStatus ms = new ModuleStatus();
// Enable it.
// TODO: This by default selects all modules that are found.
// Eventually we would rather obey a default either from the
// modules themselves, from how they are found, and also
// have information on what modules are selected from the
// configuration.
ms.setSelected();
moduleStatus.put(mf, ms);
}
/**
* The file filter that selects Jar files.
*/
static class JarFileFilter implements FileFilter {
/*
* @see java.io.FileFilter#accept(java.io.File)
*/
public boolean accept(File pathname) {
return (pathname.canRead()
&& pathname.isFile()
&& pathname.getPath().toLowerCase().endsWith(".jar"));
}
}
}
/**
* Status for each of the available modules. This is created in one copy per
* available module.
*/
class ModuleStatus {
/**
* If the module is enabled.
*/
private boolean enabled;
/**
* If the module is selected.
*/
private boolean selected;
/**
* Tells if the module is enabled or not.
*
* @return true if the module is enabled.
*/
public boolean isEnabled() {
return enabled;
}
/**
* Setter for enabled.
*/
public void setEnabled() {
enabled = true;
}
/**
* Setter for enabled.
*/
public void setDisabled() {
enabled = false;
}
/**
* Tells if the module is selected by the user or not.
*
* @return true if it is selected.
*/
public boolean isSelected() {
return selected;
}
/**
* Setter for selected.
*/
public void setSelected() {
selected = true;
}
/**
* Setter for selected.
*/
public void setUnselect() {
selected = false;
}
/**
* Setter for selected.
*
* @param value The value to set.
*/
public void setSelected(boolean value) {
if (value) {
setSelected();
} else {
setUnselect();
}
}
}