|
Added
Link Here
|
| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2008 Versant Corp. |
| 3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 5 |
* which accompanies this distribution, and is available at |
| 6 |
* http://www.eclipse.org/legal/epl-v10.html |
| 7 |
* |
| 8 |
* Contributors: |
| 9 |
* Markus Kuppe (mkuppe <at> versant <dot> com) - initial API and implementation |
| 10 |
******************************************************************************/ |
| 11 |
package org.eclipse.ecf.internal.remoteservices.ui.handlers; |
| 12 |
|
| 13 |
import java.lang.reflect.InvocationTargetException; |
| 14 |
import java.util.Arrays; |
| 15 |
|
| 16 |
import org.eclipse.core.commands.AbstractHandler; |
| 17 |
import org.eclipse.core.commands.ExecutionEvent; |
| 18 |
import org.eclipse.core.commands.ExecutionException; |
| 19 |
import org.eclipse.core.commands.IHandler; |
| 20 |
import org.eclipse.ecf.core.IContainer; |
| 21 |
import org.eclipse.ecf.core.identity.ID; |
| 22 |
import org.eclipse.ecf.core.util.IAsyncResult; |
| 23 |
import org.eclipse.ecf.internal.remoteservices.ui.RemoteServiceHandlerUtil; |
| 24 |
import org.eclipse.ecf.remoteservice.IRemoteCall; |
| 25 |
import org.eclipse.ecf.remoteservice.IRemoteCallListener; |
| 26 |
import org.eclipse.ecf.remoteservice.IRemoteService; |
| 27 |
import org.eclipse.ecf.remoteservice.IRemoteServiceContainerAdapter; |
| 28 |
import org.eclipse.ecf.remoteservice.IRemoteServiceReference; |
| 29 |
import org.eclipse.ecf.remoteservice.events.IRemoteCallCompleteEvent; |
| 30 |
import org.eclipse.ecf.remoteservice.events.IRemoteCallEvent; |
| 31 |
import org.eclipse.ecf.remoteservices.ui.MethodInvocationDialog; |
| 32 |
import org.eclipse.jface.dialogs.MessageDialog; |
| 33 |
import org.eclipse.jface.window.Window; |
| 34 |
import org.eclipse.osgi.util.NLS; |
| 35 |
import org.eclipse.swt.widgets.Display; |
| 36 |
import org.eclipse.swt.widgets.Shell; |
| 37 |
|
| 38 |
public class ReflectiveRemoteServiceHandler extends AbstractHandler implements |
| 39 |
IHandler { |
| 40 |
|
| 41 |
/* |
| 42 |
* (non-Javadoc) |
| 43 |
* @see |
| 44 |
* org.eclipse.core.commands.AbstractHandler#execute(org.eclipse.core.commands |
| 45 |
* .ExecutionEvent) |
| 46 |
*/ |
| 47 |
public Object execute(ExecutionEvent event) throws ExecutionException { |
| 48 |
final String clazz = event.getParameter("org.eclipse.ecf.remoteservices.ui.commands.reflectiveMethodDialogParameter"); |
| 49 |
final IRemoteServiceContainerAdapter adapter = RemoteServiceHandlerUtil.getActiveIRemoteServiceContainerAdapterChecked(event); |
| 50 |
final IRemoteServiceReference[] references = RemoteServiceHandlerUtil.getActiveIRemoteServiceReferencesChecked(event); |
| 51 |
final IRemoteService remoteService = adapter.getRemoteService(references[0]); |
| 52 |
try { |
| 53 |
executeMethodInvocationDialog(Class.forName(clazz), remoteService); |
| 54 |
} catch (ClassNotFoundException e) { |
| 55 |
throw new ExecutionException(e.getMessage(), e); |
| 56 |
} |
| 57 |
return null; |
| 58 |
} |
| 59 |
|
| 60 |
private void executeMethodInvocationDialog(final Class cls, final IRemoteService remoteService) { |
| 61 |
final MethodInvocationDialog mid = new MethodInvocationDialog( |
| 62 |
(Shell) null, cls); |
| 63 |
if (mid.open() == Window.OK) { |
| 64 |
final int timeout = (mid.getTimeout() > 0) ? mid.getTimeout() |
| 65 |
: 30000; |
| 66 |
final String methodName = mid.getMethod().getName(); |
| 67 |
final Object[] methodArgs = mid.getMethodArguments(); |
| 68 |
final IRemoteCall remoteCall = new IRemoteCall() { |
| 69 |
public String getMethod() { |
| 70 |
return methodName; |
| 71 |
} |
| 72 |
|
| 73 |
public Object[] getParameters() { |
| 74 |
return methodArgs; |
| 75 |
} |
| 76 |
|
| 77 |
public long getTimeout() { |
| 78 |
return timeout; |
| 79 |
} |
| 80 |
}; |
| 81 |
final int invokeType = mid.getInvocationType(); |
| 82 |
try { |
| 83 |
switch (invokeType) { |
| 84 |
case MethodInvocationDialog.ASYNC_FIRE_AND_GO: |
| 85 |
remoteService.callAsynch(remoteCall); |
| 86 |
break; |
| 87 |
case MethodInvocationDialog.ASYNC_FUTURE_RESULT: |
| 88 |
invokeFuture(cls, remoteService, remoteCall); |
| 89 |
break; |
| 90 |
case MethodInvocationDialog.ASYNC_LISTENER: |
| 91 |
invokeAsyncListener(cls, remoteService, remoteCall); |
| 92 |
break; |
| 93 |
case MethodInvocationDialog.OSGI_SERVICE_PROXY: |
| 94 |
throw new UnsupportedOperationException(); |
| 95 |
// invokeOSGiProxy(interfaceClass, remoteCall); |
| 96 |
// break; |
| 97 |
case MethodInvocationDialog.REMOTE_SERVICE_PROXY: |
| 98 |
throw new UnsupportedOperationException(); |
| 99 |
// invokeProxy(interfaceClass, remoteService, remoteCall); |
| 100 |
// break; |
| 101 |
case MethodInvocationDialog.SYNCHRONOUS: |
| 102 |
throw new UnsupportedOperationException(); |
| 103 |
// invokeSync(cls, remoteService, remoteCall); |
| 104 |
// break; |
| 105 |
default: |
| 106 |
break; |
| 107 |
} |
| 108 |
} catch (final Exception e) { |
| 109 |
showException(e); |
| 110 |
} |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
protected void showException(final Throwable t, final IContainer container, |
| 115 |
ID targetID) { |
| 116 |
Display.getDefault().asyncExec(new Runnable() { |
| 117 |
public void run() { |
| 118 |
String msg = t.toString(); |
| 119 |
if (t.getCause() != null) { |
| 120 |
msg += t.getCause().toString(); |
| 121 |
} |
| 122 |
MessageDialog.openInformation(null, "Received Exception", NLS |
| 123 |
.bind("Exception: {0}", msg)); |
| 124 |
container.disconnect(); |
| 125 |
} |
| 126 |
}); |
| 127 |
} |
| 128 |
|
| 129 |
protected void invokeFuture(Class cls, IRemoteService remoteService, |
| 130 |
IRemoteCall remoteCall) throws InterruptedException, |
| 131 |
InvocationTargetException { |
| 132 |
// Make async call with future result |
| 133 |
final IAsyncResult asyncResult = remoteService.callAsynch(remoteCall); |
| 134 |
// Call blocking get and show result |
| 135 |
showResult(cls.getName(), remoteCall, asyncResult.get()); |
| 136 |
} |
| 137 |
|
| 138 |
private void invokeAsyncListener(final Class interfaceClass, |
| 139 |
final IRemoteService remoteService, final IRemoteCall remoteCall) { |
| 140 |
// Make async call |
| 141 |
remoteService.callAsynch(remoteCall, new IRemoteCallListener() { |
| 142 |
public void handleEvent(IRemoteCallEvent event) { |
| 143 |
if (event instanceof IRemoteCallCompleteEvent) { |
| 144 |
final IRemoteCallCompleteEvent complete = (IRemoteCallCompleteEvent) event; |
| 145 |
if (complete.hadException()) { |
| 146 |
showException(complete.getException()); |
| 147 |
} else |
| 148 |
showResult(interfaceClass.getName(), remoteCall, |
| 149 |
complete.getResponse()); |
| 150 |
} |
| 151 |
} |
| 152 |
}); |
| 153 |
} |
| 154 |
|
| 155 |
protected void showException(final Throwable t) { |
| 156 |
Display.getDefault().asyncExec(new Runnable() { |
| 157 |
public void run() { |
| 158 |
String msg = t.toString(); |
| 159 |
if (t.getCause() != null) { |
| 160 |
msg += t.getCause().toString(); |
| 161 |
} |
| 162 |
MessageDialog.openInformation(null, "Received Exception", NLS |
| 163 |
.bind("Exception: {0}", msg)); |
| 164 |
} |
| 165 |
}); |
| 166 |
} |
| 167 |
|
| 168 |
protected void showResult(final String serviceInterface, |
| 169 |
final IRemoteCall remoteCall, final Object result) { |
| 170 |
final Object display = (result != null && result.getClass().isArray()) ? Arrays |
| 171 |
.asList((Object[]) result) |
| 172 |
: result; |
| 173 |
final Object[] bindings = new Object[] { serviceInterface, |
| 174 |
remoteCall.getMethod(), |
| 175 |
Arrays.asList(remoteCall.getParameters()), display }; |
| 176 |
Display.getDefault().asyncExec(new Runnable() { |
| 177 |
public void run() { |
| 178 |
MessageDialog |
| 179 |
.openInformation( |
| 180 |
null, |
| 181 |
"Received Response", |
| 182 |
NLS |
| 183 |
.bind( |
| 184 |
"Service Interface:\n{0}\n\nMethod: {1}\nParameters: {2}\n\nResult: {3}", |
| 185 |
bindings)); |
| 186 |
} |
| 187 |
}); |
| 188 |
} |
| 189 |
} |