Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 559559adbad25c894f88f08b06b7d98f7c9e9cab (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
175
176
177
178
package org.eclipse.ecf.internal.examples.remoteservices.client;

import java.io.NotSerializableException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.eclipse.ecf.core.IContainer;
import org.eclipse.ecf.discovery.ui.handlers.AbstractRemoteServiceAccessHandler;
import org.eclipse.ecf.examples.remoteservices.common.IRemoteEnvironmentInfo;
import org.eclipse.ecf.remoteservice.IRemoteCall;
import org.eclipse.ecf.remoteservice.IRemoteCallListener;
import org.eclipse.ecf.remoteservice.IRemoteService;
import org.eclipse.ecf.remoteservice.IRemoteServiceContainerAdapter;
import org.eclipse.ecf.remoteservice.IRemoteServiceReference;
import org.eclipse.ecf.remoteservice.events.IRemoteCallCompleteEvent;
import org.eclipse.ecf.remoteservice.events.IRemoteCallEvent;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.ActionContributionItem;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.action.IContributionItem;
import org.eclipse.jface.dialogs.InputDialog;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.window.Window;
import org.eclipse.osgi.util.NLS;
import org.eclipse.swt.widgets.Display;

public class RemoteEnvironmentInfoServiceAccessHandler extends AbstractRemoteServiceAccessHandler {

	public RemoteEnvironmentInfoServiceAccessHandler() {
	}

	protected List getRemoteServiceContainerAdapters() {
		final List results = new ArrayList();
		final IContainer container = Activator.getDefault().getConnectedContainer();
		if (container == null)
			return results;
		// If it's not connected already, then return empty list
		if (container.getConnectedID() == null)
			return results;
		final IRemoteServiceContainerAdapter adapter = (IRemoteServiceContainerAdapter) container.getAdapter(IRemoteServiceContainerAdapter.class);
		// If namespaces match and there's an adapter then add it to list
		if (container.getConnectNamespace().getName().equals(getConnectNamespace()) && adapter != null)
			results.add(adapter);
		return results;
	}

	protected IRemoteCall createGetPropertyRemoteCall() throws ClassNotFoundException, NotSerializableException {
		IRemoteEnvironmentInfo.class.getDeclaredMethods();
		final InputDialog input = new InputDialog(null, "Get property", "Enter key", "user.name", null);
		input.setBlockOnOpen(true);
		final Object[] params = new Object[1];
		if (input.open() == Window.OK) {
			params[0] = input.getValue();
			return new IRemoteCall() {

				public String getMethod() {
					return "getProperty";
				}

				public Object[] getParameters() {
					return params;
				}

				public long getTimeout() {
					return 30000;
				}

			};
		} else
			return null;
	}

	protected void showResult(final Object result) {
		final Object display = (result != null && result.getClass().isArray()) ? Arrays.asList((Object[]) result) : result;
		Display.getDefault().asyncExec(new Runnable() {
			public void run() {
				MessageDialog.openInformation(null, "Received Response", NLS.bind("{0}", display));
			}
		});
	}

	protected void showException(final Throwable t) {
		Display.getDefault().asyncExec(new Runnable() {
			public void run() {
				MessageDialog.openInformation(null, "Received Exception", NLS.bind("Exception: {0}", t.getLocalizedMessage()));
			}
		});
	}

	protected IContributionItem[] getContributionItemsForService(final IRemoteServiceContainerAdapter adapter) {
		final String className = getRemoteServiceClass();
		if (className == null)
			return NOT_AVAILABLE_CONTRIBUTION;
		else if (className.equals(IRemoteEnvironmentInfo.class.getName()))
			return getContributionItemsForRemoteEnvironmentService(adapter);
		else
			return NOT_AVAILABLE_CONTRIBUTION;
	}

	private IContributionItem createContributionItem(final IRemoteService remoteService, final int invokeMode) {
		final IAction action = new Action() {
			public void run() {
				try {
					final IRemoteCall remoteCall = createGetPropertyRemoteCall();
					if (remoteCall != null) {
						switch (invokeMode) {
							// callSynch
							case 0 :
								showResult(remoteService.callSynch(remoteCall));
								break;
							// callAsynch (listener)
							case 1 :
								remoteService.callAsynch(remoteCall, new IRemoteCallListener() {
									public void handleEvent(IRemoteCallEvent event) {
										if (event instanceof IRemoteCallCompleteEvent) {
											IRemoteCallCompleteEvent complete = (IRemoteCallCompleteEvent) event;
											if (complete.hadException()) {
												showException(complete.getException());
											} else
												showResult(complete.getResponse());
										}
									}
								});
								break;
							// callAsynch (future)
							case 2 :
								showResult(remoteService.callAsynch(remoteCall).get());
								break;
							// proxy
							case 3 :
								IRemoteEnvironmentInfo proxy = (IRemoteEnvironmentInfo) remoteService.getProxy();
								showResult(proxy.getProperty((String) remoteCall.getParameters()[0]));
								break;
						}
					}
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		};
		switch (invokeMode) {
			case 0 :
				action.setText("getProperty (synch)");
				break;
			case 1 :
				action.setText("getProperty (async)");
				break;
			case 2 :
				action.setText("getProperty (future)");
				break;
			case 3 :
				action.setText("getProperty (proxy)");
				break;
		}
		return new ActionContributionItem(action);
	}

	/**
	 * @param adapter
	 * @return
	 */
	private IContributionItem[] getContributionItemsForRemoteEnvironmentService(final IRemoteServiceContainerAdapter adapter) {
		try {
			final IRemoteServiceReference[] references = getRemoteServiceReferences(adapter);
			if (references == null)
				return NOT_AVAILABLE_CONTRIBUTION;
			final IRemoteService remoteService = adapter.getRemoteService(references[0]);
			return new IContributionItem[] {createContributionItem(remoteService, 0), createContributionItem(remoteService, 1), createContributionItem(remoteService, 2), createContributionItem(remoteService, 3)};
		} catch (final Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
			return NOT_AVAILABLE_CONTRIBUTION;
		}

	}
}

Back to the top