Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 6a64df5894f69ae2ede71fefa7b62aede2ed8721 (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
168
169
170
171
172
173
174
/****************************************************************************
 * Copyright (c) 2013 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.examples.internal.remoteservices.hello.consumer2;

import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.ecf.examples.remoteservices.hello.HelloMessage;
import org.eclipse.ecf.examples.remoteservices.hello.IHello;
import org.eclipse.ecf.examples.remoteservices.hello.IHelloAsync;
import org.eclipse.ecf.osgi.services.distribution.IDistributionConstants;
import org.eclipse.ecf.remoteservice.IAsyncCallback;
import org.eclipse.equinox.concurrent.future.IFuture;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.Filter;
import org.osgi.framework.InvalidSyntaxException;
import org.osgi.framework.ServiceReference;
import org.osgi.util.tracker.ServiceTracker;
import org.osgi.util.tracker.ServiceTrackerCustomizer;

@SuppressWarnings("restriction")
public class Activator implements BundleActivator, IDistributionConstants {

	private static BundleContext context;

	private ServiceTracker<IHello, IHello> helloServiceTracker;

	private static final String CONSUMER_NAME = "slewis";
	
	public void start(BundleContext bundleContext) throws Exception {
		Activator.context = bundleContext;
		helloServiceTracker = new ServiceTracker<IHello, IHello>(context,
				createFilter(), new HelloTrackerCustomizer());
		helloServiceTracker.open();
	}

	private Filter createFilter() throws InvalidSyntaxException {
		return context.createFilter("(&("
				+ org.osgi.framework.Constants.OBJECTCLASS + "="
				+ IHello.class.getName() + ")(" + SERVICE_IMPORTED + "=*))");
	}

	class HelloTrackerCustomizer implements
			ServiceTrackerCustomizer<IHello, IHello> {

		public IHello addingService(ServiceReference<IHello> reference) {
			
			IHello proxy = context.getService(reference);
			
			useHelloService(proxy);
			
			return proxy;
		}

		private void useHelloService(IHello proxy) {
			System.out.println("STARTING remote call via proxy...");
			proxy.hello(CONSUMER_NAME+" via proxy");
			System.out.println("COMPLETED remote call via proxy");
			System.out.println();
			// Call other helloMessage method
			System.out.println("STARTING remote call via proxy...");
			proxy.helloMessage(new HelloMessage(CONSUMER_NAME+" via proxy","howdy"));
			System.out.println("COMPLETED remote call via proxy");
			System.out.println();

			// If the proxy is also an instance of IHelloAsync then use
			// this asynchronous interface to invoke methods asynchronously
			if (proxy instanceof IHelloAsync) {
				IHelloAsync helloA = (IHelloAsync) proxy;
				// Create callback for use in IHelloAsync
				IAsyncCallback<String> callback = new IAsyncCallback<String>() {
					public void onSuccess(String result) {
						System.out.println("COMPLETED remote call with callback SUCCESS with result="+result);
						System.out.println();
					}
					public void onFailure(Throwable t) {
						System.out.println("COMPLETED remote call with callback FAILED with exception="+t);
						System.out.println();
					}
				};
				
				// Call asynchronously with callback
				System.out.println("STARTING async remote call via callback...");
				helloA.helloAsync(CONSUMER_NAME + " via async proxy with listener", callback);
				System.out.println("LOCAL async invocation complete");
				System.out.println();
				
				// Call asynchronously with future
				System.out.println("STARTING async remote call via future...");
				IFuture future = helloA.helloAsync(CONSUMER_NAME + " via async proxy with future");
				System.out.println("LOCAL async future invocation complete");
				System.out.println();
				try {
					while (!future.isDone()) {
						// do some other stuff
						System.out.println("LOCAL future not yet done...so we're doing other stuff while waiting for future to be done");
						Thread.sleep(200);
					}
					// Now it's done, so this will not block
					Object result = future.get();
					System.out.println("COMPLETED remote call with future SUCCEEDED with result="+result);
					System.out.println();
				} catch (OperationCanceledException e) {
					System.out.println("COMPLETED remote call with callback CANCELLED with exception="+e);
					System.out.println();
					e.printStackTrace();
				} catch (InterruptedException e) {
					System.out.println("COMPLETED remote call with callback INTERRUPTED with exception="+e);
					System.out.println();
					e.printStackTrace();
				}
				
				// Call other helloMessage method
				// Call asynchronously with callback
				System.out.println("STARTING async remote call via callback...");
				helloA.helloMessageAsync(new HelloMessage(CONSUMER_NAME + " via async proxy with listener","howdy"), callback);
				System.out.println("LOCAL async invocation complete");
				System.out.println();
				
				// Call asynchronously with future
				System.out.println("STARTING async remote call via future...");
				future = helloA.helloMessageAsync(new HelloMessage(CONSUMER_NAME + " via async proxy with future","howdy"));
				System.out.println("LOCAL async future invocation complete");
				System.out.println();
				try {
					while (!future.isDone()) {
						// do some other stuff
						System.out.println("LOCAL future not yet done...so we're doing other stuff while waiting for future to be done");
						Thread.sleep(200);
					}
					// Now it's done, so this will not block
					Object result = future.get();
					System.out.println("COMPLETED remote call with future SUCCEEDED with result="+result);
					System.out.println();
				} catch (OperationCanceledException e) {
					System.out.println("COMPLETED remote call with callback CANCELLED with exception="+e);
					System.out.println();
					e.printStackTrace();
				} catch (InterruptedException e) {
					System.out.println("COMPLETED remote call with callback INTERRUPTED with exception="+e);
					System.out.println();
					e.printStackTrace();
				}

			}
			
		}

		public void modifiedService(ServiceReference<IHello> reference,
				IHello service) {
		}

		public void removedService(ServiceReference<IHello> reference,
				IHello service) {
		}

	}

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

}

Back to the top