diff options
author | slewis | 2014-03-27 05:48:17 +0000 |
---|---|---|
committer | slewis | 2014-03-27 05:48:17 +0000 |
commit | f9e6c4a0f113f6a7a42c9cfb30f0bd529318be0c (patch) | |
tree | 01e45fdea61fe128842b5cef4dcbfc1bb5f5e61d /framework/bundles/org.eclipse.ecf.identity/src/org | |
parent | 20964f407f28056294dae2ec4885a81c96f55284 (diff) | |
download | org.eclipse.ecf-f9e6c4a0f113f6a7a42c9cfb30f0bd529318be0c.tar.gz org.eclipse.ecf-f9e6c4a0f113f6a7a42c9cfb30f0bd529318be0c.tar.xz org.eclipse.ecf-f9e6c4a0f113f6a7a42c9cfb30f0bd529318be0c.zip |
Added ExtensionRegistryRunnable class and AdapterManagerTracker class
and updated usage in org.eclipse.ecf and org.eclipse.ecf.identity and
org.eclipse.ecf.sharedobject
Change-Id: Ie7d7c9e6df20f04ca7118ad1d6103a98ff9dd19c
Diffstat (limited to 'framework/bundles/org.eclipse.ecf.identity/src/org')
4 files changed, 128 insertions, 73 deletions
diff --git a/framework/bundles/org.eclipse.ecf.identity/src/org/eclipse/ecf/core/util/AdapterManagerTracker.java b/framework/bundles/org.eclipse.ecf.identity/src/org/eclipse/ecf/core/util/AdapterManagerTracker.java new file mode 100644 index 000000000..29d12a311 --- /dev/null +++ b/framework/bundles/org.eclipse.ecf.identity/src/org/eclipse/ecf/core/util/AdapterManagerTracker.java @@ -0,0 +1,44 @@ +/******************************************************************************* + * Copyright (c) 2014 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 org.eclipse.core.runtime.*; +import org.eclipse.ecf.internal.core.identity.Activator; +import org.osgi.framework.BundleContext; +import org.osgi.util.tracker.ServiceTracker; +import org.osgi.util.tracker.ServiceTrackerCustomizer; + +/** + * @since 3.3 + */ +public class AdapterManagerTracker extends ServiceTracker { + + public AdapterManagerTracker(BundleContext context, + ServiceTrackerCustomizer customizer) { + super(context, IAdapterManager.class.getName(), customizer); + } + + public AdapterManagerTracker(BundleContext context) { + this(context, null); + } + + public IAdapterManager getAdapterManager() { + IAdapterManager adapterManager = (IAdapterManager) 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) + Activator.getDefault().log( + new Status(IStatus.ERROR, Activator.PLUGIN_ID, + IStatus.ERROR, "Cannot get adapter manager", null)); //$NON-NLS-1$ + return adapterManager; + } + +} diff --git a/framework/bundles/org.eclipse.ecf.identity/src/org/eclipse/ecf/core/util/ExtensionRegistryRunnable.java b/framework/bundles/org.eclipse.ecf.identity/src/org/eclipse/ecf/core/util/ExtensionRegistryRunnable.java new file mode 100644 index 000000000..ca82a0987 --- /dev/null +++ b/framework/bundles/org.eclipse.ecf.identity/src/org/eclipse/ecf/core/util/ExtensionRegistryRunnable.java @@ -0,0 +1,64 @@ +/******************************************************************************* + * Copyright (c) 2014 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 org.eclipse.core.runtime.*; +import org.eclipse.ecf.internal.core.identity.Activator; +import org.osgi.framework.BundleContext; +import org.osgi.util.tracker.ServiceTracker; + +/** + * @since 3.3 + */ +public abstract class ExtensionRegistryRunnable implements ISafeRunnable { + + private BundleContext context; + + public ExtensionRegistryRunnable(BundleContext ctxt) { + this.context = ctxt; + } + + protected void runWithoutRegistry() throws Exception { + // by default do nothing + } + + protected abstract void runWithRegistry(IExtensionRegistry registry) + throws Exception; + + protected void logWarning(Throwable exception) { + Activator a = Activator.getDefault(); + if (a != null) + a.log(new Status(IStatus.WARNING, Activator.PLUGIN_ID, + IStatus.WARNING, "Warning: code cannot be run", exception)); //$NON-NLS-1$ + } + + public void run() throws Exception { + try { + runWithRegistry(getExtensionRegistry()); + } catch (NoClassDefFoundError e) { + runWithoutRegistry(); + } + } + + private IExtensionRegistry getExtensionRegistry() { + if (context == null) + return null; + ServiceTracker extensionRegistryTracker = new ServiceTracker(context, + IExtensionRegistry.class.getName(), null); + extensionRegistryTracker.open(); + IExtensionRegistry result = (IExtensionRegistry) extensionRegistryTracker + .getService(); + extensionRegistryTracker.close(); + return result; + } + + public void handleException(Throwable exception) { + logWarning(exception); + } +} diff --git a/framework/bundles/org.eclipse.ecf.identity/src/org/eclipse/ecf/core/util/OptionalCodeSafeRunnable.java b/framework/bundles/org.eclipse.ecf.identity/src/org/eclipse/ecf/core/util/OptionalCodeSafeRunnable.java deleted file mode 100644 index 463eb2bee..000000000 --- a/framework/bundles/org.eclipse.ecf.identity/src/org/eclipse/ecf/core/util/OptionalCodeSafeRunnable.java +++ /dev/null @@ -1,21 +0,0 @@ -package org.eclipse.ecf.core.util; - -import org.eclipse.core.runtime.*; -import org.eclipse.ecf.internal.core.identity.Activator; - -/** - * @since 3.3 - */ -public abstract class OptionalCodeSafeRunnable implements ISafeRunnable { - - public void handleException(Throwable exception) { - Activator a = Activator.getDefault(); - if (a != null) - a.log(new Status(IStatus.WARNING, Activator.PLUGIN_ID, - IStatus.WARNING, - "Warning: optional code cannot be run", exception)); //$NON-NLS-1$ - } - - public abstract void run() throws Exception; - -} 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 b4973022a..a4e5f21ff 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 @@ -45,15 +45,13 @@ public class Activator implements BundleActivator { private ServiceRegistration idFactoryServiceRegistration; - private ServiceTracker extensionRegistryTracker; - private ServiceTracker debugOptionsTracker; private ServiceTracker logServiceTracker; private LogService logService; - private ServiceTracker adapterManagerTracker; + private AdapterManagerTracker adapterManagerTracker; // This is object rather than typed to avoid referencing the // IRegistryChangedListener class directly @@ -64,21 +62,10 @@ public class Activator implements BundleActivator { return null; // First, try to get the adapter manager via if (adapterManagerTracker == null) { - adapterManagerTracker = new ServiceTracker(this.context, - IAdapterManager.class.getName(), null); + adapterManagerTracker = new AdapterManagerTracker(this.context); 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; + return adapterManagerTracker.getAdapterManager(); } /** @@ -88,17 +75,6 @@ public class Activator implements BundleActivator { // public null constructor } - synchronized IExtensionRegistry getExtensionRegistry() { - if (this.context == null) - return null; - if (extensionRegistryTracker == null) { - extensionRegistryTracker = new ServiceTracker(context, - IExtensionRegistry.class.getName(), null); - extensionRegistryTracker.open(); - } - return (IExtensionRegistry) extensionRegistryTracker.getService(); - } - public synchronized DebugOptions getDebugOptions() { if (context == null) return null; @@ -123,10 +99,10 @@ public class Activator implements BundleActivator { idFactoryServiceRegistration = context.registerService( IIDFactory.class.getName(), IDFactory.getDefault(), null); - SafeRunner.run(new OptionalCodeSafeRunnable() { - public void run() throws Exception { - final IExtensionRegistry reg = getExtensionRegistry(); - if (reg != null) { + SafeRunner.run(new ExtensionRegistryRunnable(ctxt) { + protected void runWithRegistry(IExtensionRegistry registry) + throws Exception { + if (registry != null) { registryManager = new IRegistryChangeListener() { public void registryChanged(IRegistryChangeEvent event) { final IExtensionDelta delta[] = event @@ -148,7 +124,7 @@ public class Activator implements BundleActivator { } } }; - reg.addRegistryChangeListener((IRegistryChangeListener) registryManager); + registry.addRegistryChangeListener((IRegistryChangeListener) registryManager); } } }); @@ -278,12 +254,11 @@ public class Activator implements BundleActivator { public void setupNamespaceExtensionPoint() { if (context != null) setupNamespaceServices(); - SafeRunner.run(new OptionalCodeSafeRunnable() { - public void run() throws Exception { - // Process extension points - final IExtensionRegistry reg = getExtensionRegistry(); - if (reg != null) { - final IExtensionPoint extensionPoint = reg + SafeRunner.run(new ExtensionRegistryRunnable(context) { + protected void runWithRegistry(IExtensionRegistry registry) + throws Exception { + if (registry != null) { + final IExtensionPoint extensionPoint = registry .getExtensionPoint(NAMESPACE_EPOINT); if (extensionPoint == null) return; @@ -304,11 +279,9 @@ public class Activator implements BundleActivator { public Object addingService(ServiceReference reference) { Namespace ns = (Namespace) context .getService(reference); - if (ns != null) { + if (ns != null && ns.getName() != null) IDFactory.addNamespace0(ns); - return ns; - } - return null; + return ns; } public void modifiedService(ServiceReference reference, @@ -331,14 +304,13 @@ public class Activator implements BundleActivator { * org.eclipse.core.runtime.Plugin#stop(org.osgi.framework.BundleContext) */ public void stop(BundleContext ctxt) throws Exception { - SafeRunner.run(new OptionalCodeSafeRunnable() { - public void run() throws Exception { - final IExtensionRegistry reg = getExtensionRegistry(); - if (reg != null) - reg.removeRegistryChangeListener((IRegistryChangeListener) registryManager); + SafeRunner.run(new ExtensionRegistryRunnable(ctxt) { + protected void runWithRegistry(IExtensionRegistry registry) + throws Exception { + if (registry != null) + registry.removeRegistryChangeListener((IRegistryChangeListener) registryManager); } }); - if (namespacesTracker != null) { namespacesTracker.close(); namespacesTracker = null; @@ -353,10 +325,6 @@ public class Activator implements BundleActivator { debugOptionsTracker.close(); debugOptionsTracker = null; } - if (extensionRegistryTracker != null) { - extensionRegistryTracker.close(); - extensionRegistryTracker = null; - } if (idFactoryServiceRegistration != null) { idFactoryServiceRegistration.unregister(); idFactoryServiceRegistration = null; |