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
|
/****************************************************************************
* Copyright (c) 2004 Composent, Inc. and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Composent, Inc. - initial API and implementation
*****************************************************************************/
package org.eclipse.ecf.core.util;
import java.lang.reflect.Method;
import org.eclipse.core.runtime.*;
import org.eclipse.ecf.internal.core.identity.Activator;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
/**
* Helper class for eliminating direct references to Platform static methods
* getAdapterManager and getExtensionRegistry. Note that instead of
* Platform.getAdapterManager(), clients can call
* PlatformHelper.getAdapterManager(). If this returns null, the Platform class
* is not available.
*/
public class PlatformHelper {
@SuppressWarnings("rawtypes")
private static Class platformClass = null;
private static IAdapterManager adapterManagerCache = null;
private static IExtensionRegistry extensionRegistryCache = null;
static {
Activator a = Activator.getDefault();
if (a != null) {
try {
BundleContext c = a.getBundleContext();
if (c != null) {
Bundle[] bundles = c.getBundles();
Bundle coreRuntime = null;
for (int i = 0; i < bundles.length; i++)
if (bundles[i].getSymbolicName().equals("org.eclipse.core.runtime")) { //$NON-NLS-1$
coreRuntime = bundles[i];
platformClass = coreRuntime.loadClass("org.eclipse.core.runtime.Platform"); //$NON-NLS-1$
break;
}
}
} catch (Exception e) {
// Platform not available...just leave platformClass == null and
// log
// as error
try {
a.log(new Status(IStatus.WARNING, Activator.PLUGIN_ID, IStatus.WARNING,
"Cannot load Platform class", //$NON-NLS-1$
e));
} catch (Throwable t) {
// Should never happen, if does irrecoverable
}
}
}
}
public synchronized static boolean isPlatformAvailable() {
return platformClass != null;
}
public synchronized static IAdapterManager getPlatformAdapterManager() {
if (adapterManagerCache != null)
return adapterManagerCache;
if (isPlatformAvailable()) {
try {
@SuppressWarnings("unchecked")
Method m = platformClass.getMethod("getAdapterManager", (Class[]) null); //$NON-NLS-1$
adapterManagerCache = (IAdapterManager) m.invoke(null, (Object[]) null);
return adapterManagerCache;
} catch (Exception e) {
Activator.getDefault().log(new Status(IStatus.WARNING, Activator.PLUGIN_ID, IStatus.WARNING,
"Cannot get PlatformAdapterManager()", e)); //$NON-NLS-1$
return null;
}
}
return null;
}
public synchronized static IExtensionRegistry getExtensionRegistry() {
if (extensionRegistryCache != null)
return extensionRegistryCache;
if (isPlatformAvailable()) {
try {
@SuppressWarnings("unchecked")
Method m = platformClass.getMethod("getExtensionRegistry", (Class[]) null); //$NON-NLS-1$
extensionRegistryCache = (IExtensionRegistry) m.invoke(null, (Object[]) null);
return extensionRegistryCache;
} catch (Exception e) {
Activator.getDefault().log(new Status(IStatus.WARNING, Activator.PLUGIN_ID, IStatus.WARNING,
"Cannot get PlatformExtensionRegistry()", e)); //$NON-NLS-1$
return null;
}
}
return null;
}
}
|