Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 7f9431868f772ec50a2cd0d555334216809e3d87 (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
/****************************************************************************
 * 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;

/**
 * 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 {

	private static Class platformClass = null;

	private static IAdapterManager adapterManagerCache = null;

	private static IExtensionRegistry extensionRegistryCache = null;

	static {
		try {
			Bundle[] bundles = Activator.getDefault().getBundleContext().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 {
				Activator.getDefault().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, IStatus.ERROR, "Cannot load Platform class.  No adapter manager available", //$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 {
				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.ERROR, Activator.PLUGIN_ID, IStatus.ERROR, "Exception in PlatformHelper.getPlatformAdapterManager()", e)); //$NON-NLS-1$
				return null;
			}
		}
		return null;
	}

	public synchronized static IExtensionRegistry getExtensionRegistry() {
		if (extensionRegistryCache != null)
			return extensionRegistryCache;
		if (isPlatformAvailable()) {
			try {
				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.ERROR, Activator.PLUGIN_ID, IStatus.ERROR, "Exception in PlatformHelper.getExtensionRegistry()", e)); //$NON-NLS-1$
				return null;
			}

		}
		return null;
	}
}

Back to the top