Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 130e649a7ed15ede9f157d2e438f6a0968d2c8de (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
/*******************************************************************************
 * Copyright (c) 2013 Composent, Inc. 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: Scott Lewis - initial API and implementation
 ******************************************************************************/
package com.mycorp.examples.timeservice.consumer;

import org.eclipse.ecf.osgi.services.remoteserviceadmin.DebugRemoteServiceAdminListener;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.osgi.service.remoteserviceadmin.RemoteServiceAdminListener;
import org.osgi.util.tracker.ServiceTracker;
import org.osgi.util.tracker.ServiceTrackerCustomizer;

import com.mycorp.examples.timeservice.ITimeService;

public class Activator implements BundleActivator, ServiceTrackerCustomizer<ITimeService,ITimeService> {

	private BundleContext context;
	private ServiceTracker<ITimeService,ITimeService> timeServiceTracker;
	
	public void start(BundleContext context) throws Exception {
		this.context = context;
		// If the verboseRemoteServiceAdmin system property is set
		// then register debug listener
		if (Boolean.getBoolean("verboseRemoteServiceAdmin"))
			context.registerService(RemoteServiceAdminListener.class,
					new DebugRemoteServiceAdminListener(), null);
		
		// Create and open ITimeService tracker
		this.timeServiceTracker = new ServiceTracker<ITimeService,ITimeService>(this.context,ITimeService.class,this);
		this.timeServiceTracker.open();
	}

	public void stop(BundleContext context) throws Exception {
		if (timeServiceTracker != null) {
			timeServiceTracker.close();
			timeServiceTracker = null;
		}
	}

	/**
	 * NOTE:  The method will be called when the ITimeService is discovered.
	 */
	public ITimeService addingService(
			ServiceReference<ITimeService> reference) {
		// XXX Here is where the ITimeService is received, when discovered.
		System.out.println("ITimeService discovered!");
		System.out.println("Service Reference="+reference);
		// Get the time service proxy
		ITimeService timeService = this.context.getService(reference);
		System.out.println("Calling timeService="+timeService);
		// Call the service!
		Long time = timeService.getCurrentTime();
		// Print out the result
		System.out.println("Call Done.  Current time given by ITimeService.getCurrentTime() is: "+time);
		return timeService;
	}

	public void modifiedService(ServiceReference<ITimeService> reference,
			ITimeService service) {
		// do nothing
	}

	public void removedService(ServiceReference<ITimeService> reference,
			ITimeService service) {
		System.out.println("ITimeService undiscovered!");
	}

}

Back to the top