Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 3a09c540b6d70bbb756960e736785dc19f958493 (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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*******************************************************************************
 * Copyright (c) 2007 Versant Corp.
 * 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:
 *     Markus Kuppe (mkuppe <at> versant <dot> com) - initial API and implementation
 ******************************************************************************/
package org.eclipse.ecf.internal.provider.jslp;

import ch.ethz.iks.slp.Advertiser;
import ch.ethz.iks.slp.Locator;
import java.util.Properties;
import org.eclipse.ecf.core.ContainerConnectException;
import org.eclipse.ecf.core.IContainer;
import org.eclipse.ecf.core.util.Trace;
import org.eclipse.ecf.discovery.IDiscoveryAdvertiser;
import org.eclipse.ecf.discovery.IDiscoveryLocator;
import org.eclipse.ecf.discovery.service.IDiscoveryService;
import org.eclipse.ecf.provider.jslp.container.JSLPDiscoveryContainer;
import org.osgi.framework.*;
import org.osgi.util.tracker.ServiceTracker;

public class Activator implements BundleActivator {
	// The shared instance
	private static Activator plugin;
	public static final String PLUGIN_ID = "org.eclipse.ecf.provider.jslp"; //$NON-NLS-1$

	/**
	 * Returns the shared instance
	 * 
	 * @return the shared instance
	 */
	public static Activator getDefault() {
		return plugin;
	}

	// we need to keep a ref on our context
	// @see https://bugs.eclipse.org/bugs/show_bug.cgi?id=108214
	private volatile BundleContext bundleContext;

	private volatile ServiceTracker locatorSt;
	private volatile ServiceTracker advertiserSt;
	private volatile ServiceRegistration serviceRegistration;

	/**
	 * The constructor
	 */
	public Activator() {
		plugin = this;
	}

	public Bundle getBundle() {
		return bundleContext.getBundle();
	}

	public LocatorDecorator getLocator() {
		locatorSt.open();
		final Locator aLocator = (Locator) locatorSt.getService();
		if (aLocator == null) {
			return new NullPatternLocator();
		}
		return new LocatorDecoratorImpl(aLocator);
	}

	public Advertiser getAdvertiser() {
		advertiserSt.open();
		final Advertiser advertiser = (Advertiser) advertiserSt.getService();
		if (advertiser == null) {
			return new NullPatternAdvertiser();
		}
		return advertiser;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.core.runtime.Plugins#start(org.osgi.framework.BundleContext)
	 */
	public void start(final BundleContext context) throws Exception {
		bundleContext = context;

		// initially get the locator and add a life cycle listener
		locatorSt = new ServiceTracker(context, Locator.class.getName(), null);

		// initially get the advertiser and add a life cycle listener
		advertiserSt = new ServiceTracker(context, Advertiser.class.getName(), null);

		// register ourself as an OSGi service
		final Properties props = new Properties();
		props.put(IDiscoveryService.CONTAINER_NAME, JSLPDiscoveryContainer.NAME);
		props.put(Constants.SERVICE_RANKING, new Integer(500));

		String[] clazzes = new String[] {IDiscoveryService.class.getName(), IDiscoveryLocator.class.getName(), IDiscoveryAdvertiser.class.getName()};
		serviceRegistration = context.registerService(clazzes, serviceFactory, props);
	}

	private final DiscoveryServiceFactory serviceFactory = new DiscoveryServiceFactory();

	class DiscoveryServiceFactory implements ServiceFactory {
		private volatile JSLPDiscoveryContainer jdc;

		/* (non-Javadoc)
		 * @see org.osgi.framework.ServiceFactory#getService(org.osgi.framework.Bundle, org.osgi.framework.ServiceRegistration)
		 */
		public Object getService(final Bundle bundle, final ServiceRegistration registration) {
			if (jdc == null) {
				try {
					jdc = new JSLPDiscoveryContainer();
					jdc.connect(null, null);
				} catch (final ContainerConnectException e) {
					Trace.catching(Activator.PLUGIN_ID, Activator.PLUGIN_ID + "/debug/methods/tracing", this.getClass(), "getService(Bundle, ServiceRegistration)", e); //$NON-NLS-1$ //$NON-NLS-2$
					jdc = null;
				}
			}
			return jdc;
		}

		/**
		 * @return false if this factory has never created a service instance, true otherwise
		 */
		public boolean isActive() {
			return jdc != null;
		}

		/* (non-Javadoc)
		 * @see org.osgi.framework.ServiceFactory#ungetService(org.osgi.framework.Bundle, org.osgi.framework.ServiceRegistration, java.lang.Object)
		 */
		public void ungetService(final Bundle bundle, final ServiceRegistration registration, final Object service) {
			//TODO-mkuppe we later might want to dispose jSLP when the last!!! consumer ungets the service 
			//Though don't forget about the (ECF) Container which might still be in use
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.core.runtime.Plugin#stop(org.osgi.framework.BundleContext)
	 */
	public void stop(final BundleContext context) throws Exception {
		//TODO-mkuppe here we should do something like a deregisterAll(), but see ungetService(...);
		if (serviceRegistration != null && serviceFactory.isActive()) {
			ServiceReference reference = serviceRegistration.getReference();
			IDiscoveryLocator aLocator = (IDiscoveryLocator) context.getService(reference);

			serviceRegistration.unregister();

			IContainer container = (IContainer) aLocator.getAdapter(IContainer.class);
			container.disconnect();
			container.dispose();

			serviceRegistration = null;
		}
		plugin = null;
		bundleContext = null;
		if (advertiserSt != null) {
			advertiserSt.close();
			advertiserSt = null;
		}
		if (locatorSt != null) {
			locatorSt.close();
			locatorSt = null;
		}
	}
}

Back to the top