Skip to main content
summaryrefslogtreecommitdiffstats
blob: b2290ef9c1b8f5593774393d4ca26507b6d8edc7 (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*******************************************************************************
 *  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);
	}

}

Back to the top