diff options
author | slewis | 2007-03-17 01:35:04 +0000 |
---|---|---|
committer | slewis | 2007-03-17 01:35:04 +0000 |
commit | b85a774d6b480b32fd9c6a8d475926d2b3870634 (patch) | |
tree | d41eb075364024cec4a0cf996ea8a885e07eaa5e /framework/bundles/org.eclipse.ecf.identity | |
parent | cd2107554fb6d3bbb5485e31293de8b3884b5a1a (diff) | |
download | org.eclipse.ecf-b85a774d6b480b32fd9c6a8d475926d2b3870634.tar.gz org.eclipse.ecf-b85a774d6b480b32fd9c6a8d475926d2b3870634.tar.xz org.eclipse.ecf-b85a774d6b480b32fd9c6a8d475926d2b3870634.zip |
Added org.eclipse.ecf.util.PlatformHelper class to eliminate use of Platform directly (through dynamic classload of Platform class and use of reflection for calling getAdapterManager and/or getExtensionRegistry). Removed all ECF non-UI or example bundle references to Platform class and to org.eclipse.core.runtime package or bundle.
Diffstat (limited to 'framework/bundles/org.eclipse.ecf.identity')
3 files changed, 137 insertions, 9 deletions
diff --git a/framework/bundles/org.eclipse.ecf.identity/META-INF/MANIFEST.MF b/framework/bundles/org.eclipse.ecf.identity/META-INF/MANIFEST.MF index 5d5593285..abb18091f 100644 --- a/framework/bundles/org.eclipse.ecf.identity/META-INF/MANIFEST.MF +++ b/framework/bundles/org.eclipse.ecf.identity/META-INF/MANIFEST.MF @@ -12,8 +12,7 @@ Export-Package: org.eclipse.ecf.core.identity, org.eclipse.ecf.internal.core.identity;x-internal:=true Bundle-RequiredExecutionEnvironment: J2SE-1.3, CDC-1.0/Foundation-1.0 -Import-Package: org.eclipse.core.runtime, - org.eclipse.osgi.service.debug;version="1.0.0", +Import-Package: org.eclipse.osgi.service.debug;version="1.0.0", org.eclipse.osgi.util;version="1.0.0", org.osgi.framework;version="1.3.0", org.osgi.service.log;version="1.3.0", diff --git a/framework/bundles/org.eclipse.ecf.identity/src/org/eclipse/ecf/core/util/PlatformHelper.java b/framework/bundles/org.eclipse.ecf.identity/src/org/eclipse/ecf/core/util/PlatformHelper.java new file mode 100644 index 000000000..e2978a3ae --- /dev/null +++ b/framework/bundles/org.eclipse.ecf.identity/src/org/eclipse/ecf/core/util/PlatformHelper.java @@ -0,0 +1,109 @@ +/**************************************************************************** + * 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.IAdapterManager; +import org.eclipse.core.runtime.IExtensionRegistry; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.Status; +import org.eclipse.ecf.internal.core.identity.Activator; + +/** + * 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 { + platformClass = Class.forName("org.eclipse.core.runtime.Platform"); //$NON-NLS-1$ + } catch (ClassNotFoundException e) { + // Platform not available...just leave platformClass == null + } + } + + public synchronized static boolean isPlatformAvailable() { + return platformClass != null; + } + + public synchronized static IAdapterManager getPlatformAdapterManager() { + if (adapterManagerCache != null) + return adapterManagerCache; + if (!isPlatformAvailable()) { + Activator + .getDefault() + .log( + new Status( + IStatus.ERROR, + Activator.PLUGIN_ID, + IStatus.ERROR, + "org.eclipse.core.runtime.Platform class not available", //$NON-NLS-1$ + null)); + return null; + } else { + try { + Method m = platformClass.getMethod("getAdapterManager", null); //$NON-NLS-1$ + adapterManagerCache = (IAdapterManager) m.invoke(null, null); + return adapterManagerCache; + } catch (Exception e) { + Activator.getDefault().log( + new Status(IStatus.ERROR, Activator.PLUGIN_ID, + IStatus.ERROR, + "exception in getPlatformAdapterManager", e)); //$NON-NLS-1$ + return null; + } + } + } + + public synchronized static IExtensionRegistry getExtensionRegistry() { + if (extensionRegistryCache != null) + return extensionRegistryCache; + if (!isPlatformAvailable()) { + Activator + .getDefault() + .log( + new Status( + IStatus.ERROR, + Activator.PLUGIN_ID, + IStatus.ERROR, + "org.eclipse.core.runtime.Platform class not available", //$NON-NLS-1$ + null)); + return null; + } else { + try { + Method m = platformClass + .getMethod("getExtensionRegistry", null); //$NON-NLS-1$ + extensionRegistryCache = (IExtensionRegistry) m.invoke(null, + null); + return extensionRegistryCache; + } catch (Exception e) { + Activator.getDefault().log( + new Status(IStatus.ERROR, Activator.PLUGIN_ID, + IStatus.ERROR, + "exception in getExtensionRegistry", e)); //$NON-NLS-1$ + return null; + } + } + } + +} diff --git a/framework/bundles/org.eclipse.ecf.identity/src/org/eclipse/ecf/internal/core/identity/Activator.java b/framework/bundles/org.eclipse.ecf.identity/src/org/eclipse/ecf/internal/core/identity/Activator.java index 0f27c26a2..0e615139b 100644 --- a/framework/bundles/org.eclipse.ecf.identity/src/org/eclipse/ecf/internal/core/identity/Activator.java +++ b/framework/bundles/org.eclipse.ecf.identity/src/org/eclipse/ecf/internal/core/identity/Activator.java @@ -18,12 +18,12 @@ import org.eclipse.core.runtime.IExtensionRegistry; import org.eclipse.core.runtime.IRegistryChangeEvent; import org.eclipse.core.runtime.IRegistryChangeListener; import org.eclipse.core.runtime.IStatus; -import org.eclipse.core.runtime.Platform; import org.eclipse.core.runtime.Status; import org.eclipse.ecf.core.identity.IDFactory; import org.eclipse.ecf.core.identity.IIDFactory; import org.eclipse.ecf.core.identity.Namespace; import org.eclipse.ecf.core.util.LogHelper; +import org.eclipse.ecf.core.util.PlatformHelper; import org.eclipse.ecf.core.util.Trace; import org.eclipse.osgi.service.debug.DebugOptions; import org.osgi.framework.Bundle; @@ -71,6 +71,28 @@ public class Activator implements BundleActivator { private ServiceTracker logServiceTracker = null; + private ServiceTracker adapterManagerTracker = null; + + public IAdapterManager getAdapterManager() { + // First, try to get the adapter manager via + if (adapterManagerTracker == null) { + adapterManagerTracker = new ServiceTracker(this.context, + IAdapterManager.class.getName(), null); + adapterManagerTracker.open(); + } + IAdapterManager adapterManager = (IAdapterManager) adapterManagerTracker + .getService(); + // Then, if the service isn't there, try to get from Platform class via + // PlatformHelper class + if (adapterManager == null) + adapterManager = PlatformHelper.getPlatformAdapterManager(); + if (adapterManager == null) + getDefault().log( + new Status(IStatus.ERROR, PLUGIN_ID, IStatus.ERROR, + "Cannot get adapter manager", null)); //$NON-NLS-1$ + return adapterManager; + } + /** * The constructor */ @@ -207,12 +229,6 @@ public class Activator implements BundleActivator { } } - public IAdapterManager getAdapterManager() { - // XXX todo...replace with new adaptermanager service - return Platform.getAdapterManager(); - //return null; - } - /** * Add identity namespace extension point extensions * @@ -337,6 +353,10 @@ public class Activator implements BundleActivator { idFactoryServiceRegistration.unregister(); idFactoryServiceRegistration = null; } + if (adapterManagerTracker != null) { + adapterManagerTracker.close(); + adapterManagerTracker = null; + } context = null; } |