Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorWim Jongman2011-06-07 16:16:13 +0000
committerWim Jongman2011-06-07 16:16:13 +0000
commit62f461d896ae953cd65561a76ad8ce142cf52d34 (patch)
treea2e3d331ff1f1ef77bbe9874b4451ed50890dd86
parentd2ebddcf3d0a659b6f4a04ca0aafac86006961d3 (diff)
downloadorg.eclipse.ecf-62f461d896ae953cd65561a76ad8ce142cf52d34.tar.gz
org.eclipse.ecf-62f461d896ae953cd65561a76ad8ce142cf52d34.tar.xz
org.eclipse.ecf-62f461d896ae953cd65561a76ad8ce142cf52d34.zip
345678: [zoodiscovery][examples] hello example consumer sometimes
throws BundleStateException https://bugs.eclipse.org/bugs/show_bug.cgi?id=345678
-rw-r--r--providers/bundles/org.eclipse.ecf.provider.zookeeper/src/org/eclipse/ecf/provider/zookeeper/DiscoveryActivator.java236
1 files changed, 121 insertions, 115 deletions
diff --git a/providers/bundles/org.eclipse.ecf.provider.zookeeper/src/org/eclipse/ecf/provider/zookeeper/DiscoveryActivator.java b/providers/bundles/org.eclipse.ecf.provider.zookeeper/src/org/eclipse/ecf/provider/zookeeper/DiscoveryActivator.java
index b2290ef9c..637d963a0 100644
--- a/providers/bundles/org.eclipse.ecf.provider.zookeeper/src/org/eclipse/ecf/provider/zookeeper/DiscoveryActivator.java
+++ b/providers/bundles/org.eclipse.ecf.provider.zookeeper/src/org/eclipse/ecf/provider/zookeeper/DiscoveryActivator.java
@@ -1,115 +1,121 @@
-/*******************************************************************************
- * Copyright (c)2010 REMAIN B.V. The Netherlands. (http://www.remainsoftware.com).
- * 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:
- * Wim Jongman - initial API and implementation
- * Ahmed Aadel - initial API and implementation
- *******************************************************************************/
-package org.eclipse.ecf.provider.zookeeper;
-
-import java.util.Dictionary;
-import java.util.HashSet;
-import java.util.Properties;
-import java.util.Set;
-
-import org.eclipse.ecf.discovery.IDiscoveryAdvertiser;
-import org.eclipse.ecf.discovery.IDiscoveryLocator;
-import org.eclipse.ecf.provider.zookeeper.core.ZooDiscoveryContainer;
-import org.eclipse.ecf.provider.zookeeper.core.ZooDiscoveryContainerInstantiator;
-import org.eclipse.ecf.provider.zookeeper.core.internal.BundleStoppingListener;
-import org.eclipse.ecf.provider.zookeeper.util.Logger;
-import org.eclipse.ecf.provider.zookeeper.util.PrettyPrinter;
-import org.osgi.framework.BundleActivator;
-import org.osgi.framework.BundleContext;
-import org.osgi.framework.ServiceReference;
-import org.osgi.framework.ServiceRegistration;
-import org.osgi.service.log.LogService;
-import org.osgi.util.tracker.ServiceTracker;
-
-public class DiscoveryActivator implements BundleActivator {
-
- private static BundleContext context;
- private ServiceRegistration discoveryRegistration;
- private static Set<BundleStoppingListener> stopListeners = new HashSet<BundleStoppingListener>();
- private ServiceTracker confTracker, logServiceTraker;
-
- public void start(final BundleContext ctxt) {
- context = ctxt;
- //spawn asynchronously to avoid deadlocks during OSGi bundle startup
- new Thread(new Runnable() {
- public void run() {
- startup(ctxt);
- }
- }).start();
- }
-
- private void startup(final BundleContext ctxt) {
- Properties props = new Properties();
- props.put(IDiscoveryLocator.CONTAINER_NAME,
- ZooDiscoveryContainerInstantiator.NAME);
- props.put(IDiscoveryAdvertiser.CONTAINER_NAME,
- ZooDiscoveryContainerInstantiator.NAME);
- /*
- * Make us available as IDiscoveryLocator and IDiscoveryAdvertiser
- * services for OSGi trackers
- */
- discoveryRegistration = ctxt.registerService(new String[] {
- IDiscoveryLocator.class.getName(),
- IDiscoveryAdvertiser.class.getName() }, ZooDiscoveryContainer
- .getSingleton(), (Dictionary) props);
- ZooDiscoveryContainer.getSingleton().setDiscoveryProperties(props);
-
- // track OSGi log services
- DiscoveryActivator.this.logServiceTraker = new ServiceTracker(ctxt,
- org.osgi.service.log.LogService.class.getName(), null) {
- public Object addingService(ServiceReference reference) {
- Logger.bindLogService((LogService) context
- .getService(reference));
- return super.addingService(reference);
- }
-
- public void removedService(ServiceReference reference,
- Object service) {
- Logger.unbindLogService((LogService) service);
- super.removedService(reference, service);
- }
- };
- logServiceTraker.open(true);
- }
-
- public void stop(BundleContext c) throws Exception {
- dispose();
- // prompt we'r gone!
- PrettyPrinter.prompt(PrettyPrinter.DEACTIVATED, null);
- }
-
- private void dispose() {
- for (BundleStoppingListener l : stopListeners) {
- l.bundleStopping();
- }
- stopListeners.clear();
- ZooDiscoveryContainer.getSingleton().shutdown();
- if (this.discoveryRegistration != null) {
- this.discoveryRegistration.unregister();
- }
- if (this.confTracker != null) {
- this.confTracker.close();
- }
- // Initiates an orderly shutdown of all our cached threads
- ZooDiscoveryContainer.CACHED_THREAD_POOL.shutdown();
-
- }
-
- public static BundleContext getContext() {
- return context;
- }
-
- public static void registerBundleStoppingListner(BundleStoppingListener l) {
- stopListeners.add(l);
- }
-
-}
+/*******************************************************************************
+ * Copyright (c)2010 REMAIN B.V. The Netherlands. (http://www.remainsoftware.com).
+ * 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:
+ * Wim Jongman - initial API and implementation
+ * Ahmed Aadel - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.ecf.provider.zookeeper;
+
+import java.util.Dictionary;
+import java.util.HashSet;
+import java.util.Properties;
+import java.util.Set;
+
+import org.eclipse.ecf.discovery.IDiscoveryAdvertiser;
+import org.eclipse.ecf.discovery.IDiscoveryLocator;
+import org.eclipse.ecf.provider.zookeeper.core.ZooDiscoveryContainer;
+import org.eclipse.ecf.provider.zookeeper.core.ZooDiscoveryContainerInstantiator;
+import org.eclipse.ecf.provider.zookeeper.core.internal.BundleStoppingListener;
+import org.eclipse.ecf.provider.zookeeper.util.Logger;
+import org.eclipse.ecf.provider.zookeeper.util.PrettyPrinter;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleActivator;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceFactory;
+import org.osgi.framework.ServiceReference;
+import org.osgi.framework.ServiceRegistration;
+import org.osgi.service.log.LogService;
+import org.osgi.util.tracker.ServiceTracker;
+
+public class DiscoveryActivator implements BundleActivator {
+
+ private static BundleContext context;
+ private ServiceRegistration discoveryRegistration;
+ private static Set<BundleStoppingListener> stopListeners = new HashSet<BundleStoppingListener>();
+ private ServiceTracker confTracker, logServiceTracker;
+
+ public void start(final BundleContext ctxt) {
+ context = ctxt;
+
+ final Properties props = new Properties();
+ props.put(IDiscoveryLocator.CONTAINER_NAME,
+ ZooDiscoveryContainerInstantiator.NAME);
+ props.put(IDiscoveryAdvertiser.CONTAINER_NAME,
+ ZooDiscoveryContainerInstantiator.NAME);
+ // register ourselves using a service factory
+ discoveryRegistration = ctxt.registerService(new String[] {
+ IDiscoveryLocator.class.getName(),
+ IDiscoveryAdvertiser.class.getName() }, new ServiceFactory() {
+ private volatile ZooDiscoveryContainer zdc;
+
+ public Object getService(Bundle bundle,
+ ServiceRegistration registration) {
+ if (zdc == null) {
+ zdc = ZooDiscoveryContainer.getSingleton();
+ zdc.setDiscoveryProperties(props);
+ }
+ return zdc;
+ }
+
+ public void ungetService(Bundle bundle,
+ ServiceRegistration registration, Object service) {
+ //TODO-slewis we later might want to dispose zoodiscovery when the last!!! consumer ungets the service
+ //Though don't forget about the (ECF) Container which might still be in use
+ }}, (Dictionary) props);
+
+ // setup and open log service tracker
+ logServiceTracker = new ServiceTracker(ctxt,
+ org.osgi.service.log.LogService.class.getName(), null) {
+ public Object addingService(ServiceReference reference) {
+ Logger.bindLogService((LogService) context
+ .getService(reference));
+ return super.addingService(reference);
+ }
+
+ public void removedService(ServiceReference reference,
+ Object service) {
+ Logger.unbindLogService((LogService) service);
+ super.removedService(reference, service);
+ }
+ };
+ logServiceTracker.open(true);
+ }
+
+ public void stop(BundleContext c) throws Exception {
+ dispose();
+ // prompt we'r gone!
+ PrettyPrinter.prompt(PrettyPrinter.DEACTIVATED, null);
+ }
+
+ private void dispose() {
+ for (BundleStoppingListener l : stopListeners) {
+ l.bundleStopping();
+ }
+ stopListeners.clear();
+ ZooDiscoveryContainer.getSingleton().shutdown();
+ if (this.discoveryRegistration != null) {
+ this.discoveryRegistration.unregister();
+ }
+ if (this.confTracker != null) {
+ this.confTracker.close();
+ }
+ // Initiates an orderly shutdown of all our cached threads
+ ZooDiscoveryContainer.CACHED_THREAD_POOL.shutdown();
+
+ }
+
+ public static BundleContext getContext() {
+ return context;
+ }
+
+ public static void registerBundleStoppingListner(BundleStoppingListener l) {
+ stopListeners.add(l);
+ }
+
+}
+

Back to the top