Skip to main content
summaryrefslogtreecommitdiffstats
blob: 8c55af49eb8f109fa347212051b07bdb2d5ad852 (plain) (blame)
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
package org.eclipse.core.internal.plugins;

/*
 * (c) Copyright IBM Corp. 2000, 2001.
 * All Rights Reserved.
 */

import org.eclipse.core.boot.BootLoader;
import org.eclipse.core.runtime.*;
import org.eclipse.core.runtime.model.*;
import org.eclipse.core.internal.boot.DelegatingURLClassLoader;
import org.eclipse.core.internal.boot.PlatformClassLoader;
import org.eclipse.core.internal.boot.PlatformURLBaseConnection;
import org.eclipse.core.internal.boot.PlatformURLHandler;
import org.eclipse.core.internal.boot.URLContentFilter;
import org.eclipse.core.internal.runtime.*;
import org.eclipse.core.internal.runtime.InternalPlatform;
import java.io.*;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.net.*;
import java.util.*;

public class PluginDescriptor extends PluginDescriptorModel implements IPluginDescriptor {

	private DelegatingURLClassLoader loader = null; // plugin loader
	private boolean active = false; // plugin is active
	private boolean activePending = false; // being activated
	private boolean deactivated = false; // plugin deactivated due to startup errors
	protected Plugin pluginObject = null; // plugin object
	private boolean useDevURLs = InternalPlatform.inVAJ() || InternalPlatform.inVAME();
	private boolean usePlatformURLs = true;
	private ResourceBundle bundle = null; // plugin.properties
	private Locale locale = null; // bundle locale
	private boolean bundleNotFound = false; // marker to prevent unnecessary lookups

	// constants
	static final String PLUGIN_URL = PlatformURLHandler.PROTOCOL + PlatformURLHandler.PROTOCOL_SEPARATOR + "/" + PlatformURLPluginConnection.PLUGIN + "/";
	static final String VERSION_SEPARATOR = "_";

	private static final String DEFAULT_BUNDLE_NAME = "plugin";
	private static final String KEY_PREFIX = "%";
	private static final String KEY_DOUBLE_PREFIX = "%%";

	private static final String URL_PROTOCOL_FILE = "file";

	// Development mode constants
	private static final String PLUGIN_JARS = "plugin.jars";
	private static final String VA_PROPERTIES = ".va.properties";
	private static final String KEY_PROJECT = "projects";
public PluginDescriptor() {
	super();
}
/**
 * concatenates start and end.  If end has a '.' construct at the beginning
 * trim off any leading '.' constructs.  Since the libSpec was a path, we
 * know that it was canonicalized and will only have at most one set
 * of '.' constructs at the beginning.  Returns <code>null</code> if the 
 * end is null or starts with '..'.
 */
private String concat(String start, String end) {
	if (end == null)
		return null;
	if (end.startsWith(".."))
		// ISSUE: should log an error here
		// error case.  Can't '..' out of the scope of a plugin.  Signal that this
		// should be ignored (return null).
		return null;
	if (end.startsWith("./"))
		return start + (end.substring(2));
	if (end.startsWith("."))
		return start + end.substring(1);
	return start + end;
}
public Object createExecutableExtension(String className, Object initData, IConfigurationElement cfig, String propertyName) throws CoreException {
	// load the requested class from this plugin
	Class classInstance = null;
	try {
		classInstance = getPluginClassLoader(true).loadClass(className);
	} catch (ClassNotFoundException e1) {
		throwException(Policy.bind("plugin.loadClassError", getId(), className), e1);
	}

	// create a new instance
	Object result = null;
	try {
		result = classInstance.newInstance();
	} catch (Exception e) {
		throwException(Policy.bind("plugin.instantiateClassError", getId(), className), e);
	}

	// check if we have extension adapter and initialize
	if (result instanceof IExecutableExtension) {
		try {
			// make the call even if the initialization string is null
			 ((IExecutableExtension) result).setInitializationData(cfig, propertyName, initData);
		} catch (CoreException ce) {
			// user code threw exception
			logError(ce.getStatus());
			throw new CoreException(ce.getStatus());
		} catch (Exception te) {
			// user code caused exception
			throwException(Policy.bind("policy.initObjectError", getId(), className), te);
		}
	}
	return result;
}
Object createExecutableExtension(String pluginName, String className, Object initData, IConfigurationElement cfig, String propertyName) throws CoreException {
	String id = getUniqueIdentifier(); // this plugin id
	// check if we need to delegate to some other plugin
	if (pluginName != null && !pluginName.equals("") && !pluginName.equals(id)) {
		PluginDescriptor plugin = null;
		plugin = (PluginDescriptor) getPluginRegistry().getPluginDescriptor(pluginName);
		return plugin.createExecutableExtension(className, initData, cfig, propertyName);
	}
	return createExecutableExtension(className, initData, cfig, propertyName);
}
synchronized void doPluginActivation() throws CoreException {
	// this method is called by the class loader just prior 
	// to getting a class. It needs to handle the
	// case where it is called multiple times during the activation
	// processing itself (as a result of other classes from this
	// plugin being directly referenced by the plugin class)

	// NOTE: there is a remote scenario where the plugin class can
	// deadlock, if it starts separate thread(s) within its
	// constructor or startup() method, and waits on those
	// threads before returning (ie. calls join()).

	boolean errorExit = true;

	// check if already activated or pending
	if (pluginActivationEnter()) {
		try {
			internalDoPluginActivation();
			errorExit = false;
		} finally {
			pluginActivationExit(errorExit);
		}
	}
}
synchronized void doPluginDeactivation() {
	loader = null;
	pluginObject = null;
	active = false;
	activePending = false;
	deactivated = false;
}
/**
 * convert a list of comma-separated tokens into an array
 */
private static String[] getArrayFromList(String prop) {
	if (prop == null || prop.trim().equals(""))
		return new String[0];
	Vector list = new Vector();
	StringTokenizer tokens = new StringTokenizer(prop, ",");
	while (tokens.hasMoreTokens()) {
		String token = tokens.nextToken().trim();
		if (!token.equals(""))
			list.addElement(token);
	}
	return list.isEmpty() ? new String[0] : (String[]) list.toArray(new String[0]);
}
/**
 * @see IPluginDescriptor
 */
public IExtension getExtension(String id) {
	if (id == null)
		return null;
	ExtensionModel[] list = getDeclaredExtensions();
	if (list == null)
		return null;
	for (int i = 0; i < list.length; i++) {
		if (id.equals(list[i].getId()))
			return (IExtension) list[i];
	}
	return null;
}
/**
 * @see IPluginDescriptor
 */
public IExtensionPoint getExtensionPoint(String extensionPointId) {
	if (extensionPointId == null)
		return null;
	ExtensionPointModel[] list = getDeclaredExtensionPoints();
	if (list == null)
		return null;
	for (int i = 0; i < list.length; i++) {
		if (extensionPointId.equals(list[i].getId()))
			return (IExtensionPoint) list[i];
	}
	return null;
}
/**
 * @see IPluginDescriptor
 */
public IExtensionPoint[] getExtensionPoints() {
	ExtensionPointModel[] list = getDeclaredExtensionPoints();
	if (list == null)
		return new IExtensionPoint[0];
	IExtensionPoint[] newValues = new IExtensionPoint[list.length];
	System.arraycopy(list, 0, newValues, 0, list.length);
	return newValues;
}
/**
 * @see IPluginDescriptor
 */
public IExtension[] getExtensions() {
	ExtensionModel[] list = getDeclaredExtensions();
	if (list == null)
		return new IExtension[0];
	IExtension[] newValues = new IExtension[list.length];
	System.arraycopy(list, 0, newValues, 0, list.length);
	return newValues;
}
/**
 * @see IPluginDescriptor
 */
public URL getInstallURL() {
	try {
		return new URL(PLUGIN_URL + toString() + "/");
	} catch (MalformedURLException e) {
		throw new IllegalStateException(); // unchecked
	}
}
public URL getInstallURLInternal() {
	String url = getLocation();
	try {
		return new URL(url);
	} catch (MalformedURLException e) {
		throw new IllegalStateException(); // unchecked
	}
}
/**
 * @see IPluginDescriptor
 */
public String getLabel() {
	String s = getName();
	return s == null ? "" : getResourceString(s);
}
/**
 * @see IPluginDescriptor
 */
public Plugin getPlugin() throws CoreException {
	if (pluginObject == null)
		doPluginActivation();
	return pluginObject;
}
/**
 * @see IPluginDescriptor
 */
public ClassLoader getPluginClassLoader() {
	return getPluginClassLoader(true);
}
public ClassLoader getPluginClassLoader(boolean eclipseURLs) {
	if (loader != null)
		return loader;

	Object[] path = getPluginClassLoaderPath(eclipseURLs);
	URL[] codePath = (URL[]) path[0];
	URLContentFilter[] codeFilters = (URLContentFilter[]) path[1];
	URL[] resourcePath = (URL[]) path[2];
	URLContentFilter[] resourceFilters = (URLContentFilter[]) path[3];
	// Create the classloader.  The parent should be the parent of the platform class loader.  
	// This allows us to decouple standard parent loading from platform loading.
	loader = new PluginClassLoader(codePath, codeFilters, resourcePath, resourceFilters, PlatformClassLoader.getDefault().getParent(), this);
	loader.initializeImportedLoaders();
	// Note: need to be able to give out a loader reference before
	// its prereqs are initialized. Otherwise loops in prereq
	// definition will cause endless loop in initializePrereqs()
	return loader;
}
private Object[] getPluginClassLoaderPath(boolean platformURLFlag) {
	// If running in development mode, check for a plugin.jars file. 
	// The file has entries corresponding to each of the <library>
	// elements in the plugin.xml. Each defined property is of form
	//    libname=source1,source2,source3,...
	// For example
	// plugin.xml
	//   <runtime>
	//     <library name=baseapi.jar>
	//       <export name="*"/>
	//     </library>
	//     <library name=another.jar/>
	//     <library name=base.jar/>
	//   </runtime>
	//
	// plugin.jars
	//    baseapi.jar=Base API Project
	//    base.jar=Base Project 1, Base Project 2
	//    another.jar=More Code
	//
	// The above results in a loader search path consisting of
	//    Base API Project
	//    another.jar
	//    Base Project 1, Base Project 2
	//
	// Any library access filters specified in the plugin.xml are
	// applied to the corresponding loader search path entries

	Properties jarDefinitions = loadJarDefinitions();
	// compute the base of the classpath urls.  If <code>eclipseURLs</code> is
	// true, we should use eclipse: URLs.  Otherwise the native URLs are used.
	usePlatformURLs = platformURLFlag;
	URL install = usePlatformURLs ? getInstallURL() : getInstallURLInternal();
	String execBase = install.toExternalForm();
	String devBase = null;
	if (useDevURLs)
		devBase = PlatformURLBaseConnection.PLATFORM_URL_STRING;
	else
		devBase = execBase;

	String[] exportAll = new String[] { "*" };
	ArrayList[] result = new ArrayList[4];
	result[0] = new ArrayList();
	result[1] = new ArrayList();
	result[2] = new ArrayList();
	result[3] = new ArrayList();

	// add in any development mode class paths and the export all filter
	if (DelegatingURLClassLoader.devClassPath != null) {
		String[] specs = getArrayFromList(DelegatingURLClassLoader.devClassPath);
		// convert dev class path into url strings
		for (int j = 0; j < specs.length; j++) {
			String spec = specs[j];
			char lastChar = spec.charAt(spec.length() - 1);
			// if the spec is not a jar and does not have a trailing slash, add one
			if (!(spec.endsWith(".jar") || (lastChar == '/' || lastChar == '\\')))
				spec = spec + "/";
			// add the dev path for the plugin itself
			addLibrary(devBase, spec, exportAll, ILibrary.CODE, false, result);
			// add the dev path for all of the fragments
			PluginFragmentModel[] fragments = getFragments();
			for (int i = 0; fragments != null && i < fragments.length; i++) {
				String subBase = getFragmentLocation(fragments[i]);
				addLibrary(subBase, spec, exportAll, ILibrary.CODE, false, result);
			}
		}
	}

	// add in the class path entries spec'd in the plugin.xml.  If in development mode, 
	// add the entries from the plugin.jars first.  
	ILibrary[] list = getRuntimeLibraries();
	for (int i = 0; i < list.length; i++) {
		ILibrary library = list[i];
		// if the library path is empty, skip it.
		if (library.getPath().isEmpty())
			continue;
		String[] filters = library.isFullyExported() ? exportAll : library.getContentFilters();
		// add in the plugin.jars entries
		String libSpec = library.getPath().toString();
		String jarDefinition = null;
		if (jarDefinitions != null && libSpec != null) {
			jarDefinition = jarDefinitions.getProperty(libSpec);
			String[] specs = getArrayFromList(jarDefinition);
			// convert jar spec into url strings
			for (int j = 0; j < specs.length; j++)
				resolveAndAddLibrary(devBase, specs[j] + "/", filters, library.getType(), true, result);
		}

		resolveAndAddLibrary(execBase, libSpec, filters, library.getType(), jarDefinition != null, result);
	}

	Object[] array = new Object[4];
	array[0] = result[0].toArray(new URL[result[0].size()]);
	array[1] = result[1].toArray(new URLContentFilter[result[1].size()]);
	array[2] = result[2].toArray(new URL[result[2].size()]);
	array[3] = result[3].toArray(new URLContentFilter[result[3].size()]);
	return array;
}

private boolean resolveAndAddLibrary(String base, String spec, String[] filters, String type, boolean hasJarSpec, ArrayList[] result) {
	if (spec.charAt(0) == '$') {
		IPath path = new Path(spec);
		String first = path.segment(0);
		if (first.equalsIgnoreCase("$ws$"))
			return resolveAndAddWSLibrary(base, spec, filters, type, hasJarSpec, result);
		if (first.equalsIgnoreCase("$os$"))
			return resolveAndAddOSLibrary(base, spec, filters, type, hasJarSpec, result);
		if (first.equalsIgnoreCase("$nl$"))
			return resolveAndAddNLLibrary(base, spec, filters, type, hasJarSpec, result);
	}
	return addLibraryWithFragments(base, spec, filters, type, hasJarSpec, result);
}

private boolean addLibraryWithFragments(String base, String spec, String[] filters, String type, boolean hasJarSpec, ArrayList[] result) {
	boolean added = addLibrary(base, spec, filters, type, hasJarSpec, result);
	if (added)
		return true;
	PluginFragmentModel[] fragments = getFragments();
	if (fragments == null)
		return false;
	for (int i = 0; !added && i < fragments.length; i++) {
		String subBase = getFragmentLocation (fragments[i]);
		added = addLibrary(subBase, spec, filters, type, hasJarSpec, result);
	}
	return added;
}

private boolean addLibrary(String base, String libSpec, String[] filters, String type, boolean hasJarSpec, ArrayList[] result) {
	// create path entries for all libraries except those which are files
	// and do not exist.
	String spec = concat(base, libSpec);
	if (spec == null)
		return false;

	// if the libspec is NOT considered a directory, treat as a jar
	if (!spec.endsWith("/")) {
		// if running in VAJ or VAME and there was a plugin.jars definition, ignore the plugin.xml
		// library entry (assume the plugin.jars entries covered all the bases.  Otherwise, 
		// convert the plugin.xml entry into a URL.
		if ((InternalPlatform.inVAJ() || InternalPlatform.inVAME()) && hasJarSpec)
			return false;
		if (spec.startsWith(PlatformURLHandler.PROTOCOL + PlatformURLHandler.PROTOCOL_SEPARATOR))
			spec += PlatformURLHandler.JAR_SEPARATOR;
		else
			spec = PlatformURLHandler.JAR + PlatformURLHandler.PROTOCOL_SEPARATOR + spec + PlatformURLHandler.JAR_SEPARATOR;
	}
	try {
		URL entry = new URL(spec);
		URL resolved = Platform.resolve(entry);
		boolean add = true;
		String file = getFileFromURL (resolved);
		if (file != null)
			add = new File(file).exists();
		if (add) {
			if (type.equals(ILibrary.CODE)) {
				result[0].add(resolved);
				result[1].add(new URLContentFilter(filters));
			} else
				if (type.equals(ILibrary.RESOURCE)) {
					result[2].add(resolved);
					result[3].add(new URLContentFilter(filters));
				}
			return true;
		}
	} catch (IOException e) {
		// skip bad URLs
	}
	return false;
}

public String getFileFromURL(URL target) {
	String protocol = target.getProtocol();
	if (protocol.equals(PlatformURLHandler.FILE))
		return target.getFile();
//	if (protocol.equals(PlatformURLHandler.VA))
//		return target.getFile();
	if (protocol.equals(PlatformURLHandler.JAR)) {
		// strip off the jar separator at the end of the url then do a recursive call
		// to interpret the sub URL.
		String file = target.getFile();
		file = file.substring(0, file.length() - PlatformURLHandler.JAR_SEPARATOR.length());
		try {
			return getFileFromURL(new URL(file));
		} catch (MalformedURLException e) {
		}
	}
	return null;
}

/**
 * @see IPluginDescriptor
 */
public IPluginPrerequisite[] getPluginPrerequisites() {
	PluginPrerequisiteModel[] list = getRequires();
	if (list == null)
		return new IPluginPrerequisite[0];
	IPluginPrerequisite[] newValues = new IPluginPrerequisite[list.length];
	System.arraycopy(list, 0, newValues, 0, list.length);
	return newValues;
}
public PluginRegistry getPluginRegistry() {
	return (PluginRegistry) getRegistry();
}
/**
 * @see IPluginDescriptor
 */
public String getProviderName() {
	String s = super.getProviderName();
	return s == null ? "" : getResourceString(s);
}
/**
 * @see IPluginDescriptor
 */
public ResourceBundle getResourceBundle() throws MissingResourceException {
	return getResourceBundle(Locale.getDefault());
}
public ResourceBundle getResourceBundle(Locale targetLocale) throws MissingResourceException {
	// we cache the bundle for a single locale 
	if (bundle != null && targetLocale.equals(locale))
		return bundle;

	// check if we already tried and failed
	if (bundleNotFound)
		throw new MissingResourceException(Policy.bind("plugin.bundleNotFound", getId(), DEFAULT_BUNDLE_NAME + "_" + targetLocale), DEFAULT_BUNDLE_NAME + "_" + targetLocale, "");

	// try to load bundle from this plugin install directory
	ClassLoader resourceLoader = new URLClassLoader(new URL[] { getInstallURL()}, null);
	ResourceBundle newBundle = null;
	try {
		newBundle = ResourceBundle.getBundle(DEFAULT_BUNDLE_NAME, targetLocale, resourceLoader);
		bundle = newBundle;
		locale = targetLocale;
	} catch (MissingResourceException e) {
		bundleNotFound = true;
		throw e;
	}

	return newBundle;
}
/**
 * @see IPluginDescriptor
 */
public String getResourceString(String value) {
	return getResourceString(value, null);
}
/**
 * @see IPluginDescriptor
 */
public String getResourceString(String value, ResourceBundle b) {

	String s = value.trim();
	
	if (!s.startsWith(KEY_PREFIX)) return s;

	if (s.startsWith(KEY_DOUBLE_PREFIX)) return s.substring(1);

	int ix = s.indexOf(" ");
	String key = ix == -1 ? s : s.substring(0,ix);
	String dflt = ix == -1 ? s : s.substring(ix+1);

	if (b==null) {
		try { b = getResourceBundle(); }
		catch (MissingResourceException e) {};
	}
	
	if (b==null) return dflt;
	
	try { return b.getString(key.substring(1)); }
	catch(MissingResourceException e) { return dflt; }
}
/**
 * @see IPluginDescriptor
 */
public ILibrary[] getRuntimeLibraries() {
	LibraryModel[] list = getRuntime();
	if (list == null)
		return new ILibrary[0];
	ILibrary[] newValues = new ILibrary[list.length];
	System.arraycopy(list, 0, newValues, 0, list.length);
	return newValues;
}
/**
 * @see IPluginDescriptor
 */
public String getUniqueIdentifier() {
	return getId();
}
/**
 * @see #toString
 */
public static String getUniqueIdentifierFromString(String pluginString) {	
	int ix = pluginString.indexOf(VERSION_SEPARATOR);
	return ix==-1 ? pluginString : pluginString.substring(0,ix);
}
/**
 * @see IPluginDescriptor
 */
public PluginVersionIdentifier getVersionIdentifier() {
	String version = getVersion();
	if (version == null)
		return new PluginVersionIdentifier("1.0.0");
	try {
		return new PluginVersionIdentifier(version);
	} catch (Exception e) {
		return new PluginVersionIdentifier("1.0.0");
	}
}
/**
 * @see #toString
 */
public static PluginVersionIdentifier getVersionIdentifierFromString(String pluginString) {
	int ix = pluginString.indexOf("_");
	if (ix==-1) return null;
	String vid = pluginString.substring(ix+1);	
	try {
		return new PluginVersionIdentifier(vid);
	} catch (Exception e) {
		return null;
	}
}
private void internalDoPluginActivation() throws CoreException {
	String errorMsg;
	// load the runtime class 
	String pluginClassName = getPluginClass();
	Class runtimeClass = null;
	try {
		if (pluginClassName == null || pluginClassName.equals(""))
			runtimeClass = DefaultPlugin.class;
		else
			runtimeClass = getPluginClassLoader(true).loadClass(pluginClassName);
	} catch (ClassNotFoundException e) {
		errorMsg = Policy.bind("plugin.loadClassError", getId(), pluginClassName);
		throwException(errorMsg, e);
	}

	// find the correct constructor
	Constructor construct = null;
	try {
		construct = runtimeClass.getConstructor(new Class[] { IPluginDescriptor.class });
	} catch (NoSuchMethodException eNoConstructor) {
		errorMsg = Policy.bind("plugin.instantiateClassError", getId(), pluginClassName );
		throwException(errorMsg, eNoConstructor);
	}

	// create a new instance
	Plugin result = null;
	try {
		pluginObject = (Plugin) construct.newInstance(new Object[] { this });
	} catch (ClassCastException e) {
		errorMsg = Policy.bind("plugin.notPluginClass", pluginClassName);
		throwException(errorMsg, e);
	} catch (Exception e) {
		errorMsg = Policy.bind("plugin.instantiateClassError", getId(), pluginClassName);
		throwException(errorMsg, e);
	} 

	// run startup()
	final String message = Policy.bind("plugin.startupProblems", getId());
	final MultiStatus multiStatus = new MultiStatus(Platform.PI_RUNTIME, Platform.PLUGIN_ERROR, message, null);
	ISafeRunnable code = new ISafeRunnable() {
		public void run() throws Exception {
			pluginObject.startup();
		}
		public void handleException(Throwable e) {
			multiStatus.add(new Status(Status.WARNING, Platform.PI_RUNTIME, Platform.PLUGIN_ERROR, message, e));
			try {
				pluginObject.shutdown();
			} catch (Exception ex) {
				// Ignore exceptions during shutdown. Since startup failed we are probably
				// in a weird state anyway.
			}
		}
	};
	InternalPlatform.run(code);
	if (!multiStatus.isOK())
		throw new CoreException(multiStatus);
}
/**
 * @see IPluginDescriptor
 */
public synchronized boolean isPluginActivated() {
	return active;
}
public synchronized boolean isPluginDeactivated() {
	return deactivated;
}
private Properties loadJarDefinitions() {
	// We only need to load the plugin.jars files if we are in VAJ or VAME.
	if (!InternalPlatform.inVAJ() && !InternalPlatform.inVAME())
		return null;
	Properties result = null;
	InputStream is;
	try {
		result = new Properties();
		URL props = new URL(getInstallURLInternal(), PLUGIN_JARS);
		is = props.openStream();
		try {
			result.load(is);
			return result;
		} finally {
			is.close();
		}
	} catch (IOException e) {
		result = null;
	}
	return result;
}
private void logError(IStatus status) {
	InternalPlatform.getRuntimePlugin().getLog().log(status);
	if (InternalPlatform.DEBUG)
		System.out.println(status.getMessage());
}
/**
 * Returns <code>true</code> if we should continue with the plugin activation.
 */
private boolean pluginActivationEnter() throws CoreException {
	if (deactivated) {
		// had permanent error on startup
		String errorMsg = Policy.bind("plugin.pluginDisabled", getId());
		throwException(errorMsg, null);
	}
	if (active || activePending) {
		// already up and running 
		return false;
	}
	activePending = true;
	// go ahead and try to activate
	return true;
}
private void pluginActivationExit(boolean errorExit) {
	// we are done with with activation
	activePending = false;
	if (errorExit) {
		active = false;
		deactivated = true;
	} else
		active = true;
}
private boolean resolveAndAddOSLibrary(String base, String spec, String[] filters, String type, boolean hasJarSpec, ArrayList[] result) {
	IPath path = new Path(spec).removeFirstSegments(1);
	String location = new Path("os/" + BootLoader.getOS()).append(path).toString();
	return addLibraryWithFragments(base, location, filters, type, hasJarSpec, result);
}

private boolean resolveAndAddWSLibrary(String base, String spec, String[] filters, String type, boolean hasJarSpec, ArrayList[] result) {
	IPath path = new Path(spec).removeFirstSegments(1);
	String location = new Path("ws/" + BootLoader.getWS()).append(path).toString();
	return addLibraryWithFragments(base, location, filters, type, hasJarSpec, result);
}

private boolean resolveAndAddNLLibrary(String base, String spec, String[] filters, String type, boolean hasJarSpec, ArrayList[] result) {
	String path = new Path(spec).removeFirstSegments(1).toString();
	boolean added = addLibraryNL(base, path, filters, type, hasJarSpec, result);
	if (added) 
		return true;
	PluginFragmentModel[] fragments = getFragments();
	if (fragments == null)
		return false;
	for (int i = 0; !added && i < fragments.length; i++) {
		String subBase = getFragmentLocation (fragments[i]);
		added = addLibraryNL(subBase, path, filters, type, hasJarSpec, result);
	}
	return added;
}

private String getFragmentLocation(PluginFragmentModel fragment) {
	if (useDevURLs) 
		return PlatformURLBaseConnection.PLATFORM_URL_STRING;
	if (usePlatformURLs)
		return FragmentDescriptor.FRAGMENT_URL + fragment.toString() + "/";
	return fragment.getLocation();
}

private boolean addLibraryNL(String base, String spec, String[] filters, String type, boolean hasJarSpec, ArrayList[] result) {
	String nl = BootLoader.getNL();
	boolean added = false;
	while (!added && nl.length() > 0) {
		String location = new Path("nl/" + nl).append(spec).toString();
		added = addLibrary(base, location, filters, type, hasJarSpec, result);
		int i = nl.lastIndexOf('_');
		if (i < 0)
			nl = "";
		else
			nl = nl.substring(0, i);
	}
	return added;
}


public void setPluginClassLoader(DelegatingURLClassLoader value) {
	loader = value;
}
public void setPluginClassLoader(PluginClassLoader value) {
	loader = value;
}
private void throwException(String message, Throwable exception) throws CoreException {
	IStatus status = new Status(IStatus.ERROR, Platform.PI_RUNTIME, Platform.PLUGIN_ERROR, message, exception);
	logError(status);
	throw new CoreException(status);
}
/**
 * @see #getUniqueIdentifierFromString
 * @see #getVersionIdentifierFromString
 */
public String toString() {
	return getUniqueIdentifier()+VERSION_SEPARATOR+getVersionIdentifier().toString();
}
}

Back to the top