Search in sources :

Example 26 with IRemoteService

use of org.eclipse.ecf.remoteservice.IRemoteService in project ecf by eclipse.

the class HelloConsumerApplication method addingService.

/**
 * Method called when a REMOTE IHello instance is registered.
 */
public Object addingService(ServiceReference reference) {
    System.out.println("IHello service proxy being added");
    // Since this reference is for a remote service,
    // The service object returned is a proxy implementing the
    // IHello interface
    IHello proxy = (IHello) bundleContext.getService(reference);
    // Call proxy synchronously.  Note that this call may block or fail due to
    // synchronous communication with remote service
    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();
    // this asynchronous interface to invoke methods asynchronously
    if (proxy instanceof IHelloAsync) {
        IHelloAsync helloA = (IHelloAsync) proxy;
        // Create callback for use in IHelloAsync
        IAsyncCallback 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...");
        Future<String> 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
            String 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();
        } catch (ExecutionException 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
            String 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();
        } catch (ExecutionException e) {
            System.out.println("COMPLETED remote call with callback INTERRUPTED with exception=" + e);
            System.out.println();
            e.printStackTrace();
        }
    }
    // OSGi 4.2 remote service spec requires a property named 'service.imported' to be
    // set to a non-null value.  In the case of any ECF provider, this 'service.imported' property
    // is set to the IRemoteService object associated with the remote service.
    IRemoteService remoteService = (IRemoteService) reference.getProperty(IDistributionConstants.SERVICE_IMPORTED);
    Assert.isNotNull(remoteService);
    // This IRemoteService instance allows allows non-blocking/asynchronous invocation of
    // remote methods.  This allows the client to decide (at runtime if necessary) whether
    // to do synchronous/blocking calls or asynchronous/non-blocking calls.
    // It's also possible to get the remote service directly from the proxy
    remoteService = ((IRemoteServiceProxy) proxy).getRemoteService();
    Assert.isNotNull(remoteService);
    // In this case, we will make an non-blocking call and immediately get a 'future'...which is
    // a placeholder for a result of the remote computation.  This will not block.
    System.out.println("STARTING async remote call via future...");
    IFuture future = RemoteServiceHelper.futureExec(remoteService, "hello", new Object[] { CONSUMER_NAME + " future" });
    System.out.println("LOCAL async future invocation complete");
    // Client can execute arbitrary code here...
    try {
        // This blocks until communication and computation have completed successfully
        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 (Exception e) {
        e.printStackTrace();
    }
    return proxy;
}
Also used : IHelloAsync(org.eclipse.ecf.examples.remoteservices.hello.IHelloAsync) HelloMessage(org.eclipse.ecf.examples.remoteservices.hello.HelloMessage) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) IFuture(org.eclipse.equinox.concurrent.future.IFuture) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) ExecutionException(java.util.concurrent.ExecutionException) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) IAsyncCallback(org.eclipse.ecf.remoteservice.IAsyncCallback) ExecutionException(java.util.concurrent.ExecutionException) IRemoteService(org.eclipse.ecf.remoteservice.IRemoteService) IHello(org.eclipse.ecf.examples.remoteservices.hello.IHello)

Example 27 with IRemoteService

use of org.eclipse.ecf.remoteservice.IRemoteService in project ecf by eclipse.

the class Activator method start.

/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.core.runtime.Plugins#start(org.osgi.framework.BundleContext)
	 */
public void start(BundleContext context) throws Exception {
    this.context = context;
    // 1. Create R-OSGi Container
    IContainerManager containerManager = getContainerManagerService();
    container = containerManager.getContainerFactory().createContainer("ecf.r_osgi.peer");
    // 2. Get remote service container adapter
    IRemoteServiceContainerAdapter containerAdapter = (IRemoteServiceContainerAdapter) container.getAdapter(IRemoteServiceContainerAdapter.class);
    // 3. Lookup IRemoteServiceReference
    IRemoteServiceReference[] helloReferences = containerAdapter.getRemoteServiceReferences(IDFactory.getDefault().createID(container.getConnectNamespace(), ROSGI_SERVICE_HOST), IHello.class.getName(), null);
    Assert.isNotNull(helloReferences);
    Assert.isTrue(helloReferences.length > 0);
    // 4. Get remote service for reference
    IRemoteService remoteService = containerAdapter.getRemoteService(helloReferences[0]);
    // 5. Get the proxy
    IHello proxy = (IHello) remoteService.getProxy();
    // 6. Finally...call the proxy
    proxy.hello("RemoteService Consumer");
    // Call asynchronously via listener
    callViaListener(remoteService);
}
Also used : IRemoteServiceReference(org.eclipse.ecf.remoteservice.IRemoteServiceReference) IRemoteServiceContainerAdapter(org.eclipse.ecf.remoteservice.IRemoteServiceContainerAdapter) IContainerManager(org.eclipse.ecf.core.IContainerManager) IRemoteService(org.eclipse.ecf.remoteservice.IRemoteService) IHello(org.eclipse.ecf.examples.remoteservices.hello.IHello)

Example 28 with IRemoteService

use of org.eclipse.ecf.remoteservice.IRemoteService in project ecf by eclipse.

the class Lab1Action method run.

/**
 * The action has been activated. The argument of the
 * method represents the 'real' action sitting
 * in the workbench UI.
 * @see IWorkbenchWindowActionDelegate#run
 */
public void run(IAction action) {
    try {
        IRemoteServiceContainerAdapter adapter = getContainerAdapter();
        // Create target ID
        String target = TARGET;
        ID targetID = createTargetID(container, target);
        // Get and resolve remote service reference
        IRemoteServiceReference[] ref = adapter.getRemoteServiceReferences(targetID, org.eclipse.ecf.examples.remoteservices.common.IRemoteEnvironmentInfo.class.getName(), null);
        IRemoteService svc = adapter.getRemoteService(ref[0]);
        // get proxy
        IRemoteEnvironmentInfo proxy = (IRemoteEnvironmentInfo) svc.getProxy();
        // Call it!
        String osArch = proxy.getOSArch();
        // Show result
        MessageDialog.openInformation(window.getShell(), "ECF Lab 1", "Target " + target + " has OS Arch=" + osArch);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
Also used : IRemoteServiceReference(org.eclipse.ecf.remoteservice.IRemoteServiceReference) IRemoteEnvironmentInfo(org.eclipse.ecf.examples.remoteservices.common.IRemoteEnvironmentInfo) IRemoteServiceContainerAdapter(org.eclipse.ecf.remoteservice.IRemoteServiceContainerAdapter) ID(org.eclipse.ecf.core.identity.ID) IRemoteService(org.eclipse.ecf.remoteservice.IRemoteService) ContainerCreateException(org.eclipse.ecf.core.ContainerCreateException)

Example 29 with IRemoteService

use of org.eclipse.ecf.remoteservice.IRemoteService in project ecf by eclipse.

the class DOSGiReflectiveRemoteServiceHandler method execute.

/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.core.commands.AbstractHandler#execute(org.eclipse.core.commands
	 * .ExecutionEvent)
	 */
public Object execute(ExecutionEvent event) throws ExecutionException {
    final IServiceInfo serviceInfo = DiscoveryHandlerUtil.getActiveIServiceInfoChecked(event);
    final IServiceProperties serviceProperties = serviceInfo.getServiceProperties();
    final String clazz = serviceProperties.getPropertyString(Constants.OBJECTCLASS);
    final String serviceId = new String(serviceProperties.getPropertyString(RemoteConstants.SERVICE_IMPORTED_ENDPOINT_SERVICE_ID));
    final String containerId = new String(serviceProperties.getPropertyString(RemoteConstants.SERVICE_IMPORTED_ENDPOINT_ID));
    // get the service via the osgi service registry
    final BundleContext context = Activator.getDefault().getBundle().getBundleContext();
    final Filter filter;
    try {
        filter = context.createFilter(// $NON-NLS-1$ //$NON-NLS-2$
        "(&(" + Constants.OBJECTCLASS + "=" + clazz + ")" + "(" + IDistributionConstants.SERVICE_IMPORTED + // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
        "=*" + ")" + "(" + RemoteConstants.SERVICE_IMPORTED_ENDPOINT_ID + // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
        "=" + containerId + ")(" + RemoteConstants.SERVICE_IMPORTED_ENDPOINT_SERVICE_ID + "=" + serviceId + // $NON-NLS-1$ //$NON-NLS-2$
        "))");
    } catch (InvalidSyntaxException e1) {
        MessageDialog.openError(null, Messages.DOSGiReflectiveRemoteServiceHandler_HandlerInvocationFailed, NLS.bind(Messages.DOSGiReflectiveRemoteServiceHandler_FilterCreationFailed, e1.getMessage()));
        return null;
    }
    final ServiceTracker serviceTracker = new ServiceTracker(context, filter, null);
    serviceTracker.open();
    final ServiceReference serviceReference = serviceTracker.getServiceReference();
    if (serviceReference == null) {
        MessageDialog.openError(null, Messages.DOSGiReflectiveRemoteServiceHandler_HandlerInvocationFailed, NLS.bind(Messages.DOSGiReflectiveRemoteServiceHandler_NoServiceMatch, filter.toString()));
        return null;
    }
    // obtain the remote service reference from the local service ref (cool
    // ECF feature, huh?)
    final IRemoteService remoteService = (IRemoteService) serviceReference.getProperty(IDistributionConstants.SERVICE_IMPORTED);
    if (remoteService == null) {
        MessageDialog.openError(null, Messages.DOSGiReflectiveRemoteServiceHandler_HandlerInvocationFailed, Messages.DOSGiReflectiveRemoteServiceHandler_RemoteServiceUnresolveable);
        return null;
    }
    try {
        executeMethodInvocationDialog(Class.forName(clazz), remoteService);
    } catch (ClassNotFoundException e) {
        MessageDialog.openError(null, Messages.DOSGiReflectiveRemoteServiceHandler_HandlerInvocationFailed, e.getLocalizedMessage());
        throw new ExecutionException(e.getMessage(), e);
    }
    return null;
}
Also used : Filter(org.osgi.framework.Filter) ServiceTracker(org.osgi.util.tracker.ServiceTracker) IServiceInfo(org.eclipse.ecf.discovery.IServiceInfo) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) IServiceProperties(org.eclipse.ecf.discovery.IServiceProperties) IRemoteService(org.eclipse.ecf.remoteservice.IRemoteService) ExecutionException(org.eclipse.core.commands.ExecutionException) BundleContext(org.osgi.framework.BundleContext) ServiceReference(org.osgi.framework.ServiceReference)

Example 30 with IRemoteService

use of org.eclipse.ecf.remoteservice.IRemoteService in project ecf by eclipse.

the class Activator method start.

/*
     * (non-Javadoc)
     * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
     */
public void start(BundleContext bundleContext) throws Exception {
    Activator.context = bundleContext;
    IContainerManager containerManager = getContainerManagerService();
    container = containerManager.getContainerFactory().createContainer(REST_CONTAINER_TYPE, getRestID(RSS_URL));
    adapter = getRestClientContainerAdapter();
    adapter.setResponseDeserializer(new SyndFeedResponseDeserializer());
    IRemoteService restClientService = adapter.getRemoteService(registerCall().getReference());
    asyncCall(restClientService);
}
Also used : IContainerManager(org.eclipse.ecf.core.IContainerManager) IRemoteService(org.eclipse.ecf.remoteservice.IRemoteService) SyndFeedResponseDeserializer(org.eclipse.ecf.remoteservice.rest.synd.SyndFeedResponseDeserializer)

Aggregations

IRemoteService (org.eclipse.ecf.remoteservice.IRemoteService)64 ServiceReference (org.osgi.framework.ServiceReference)11 IFuture (org.eclipse.equinox.concurrent.future.IFuture)10 ECFException (org.eclipse.ecf.core.util.ECFException)9 IRemoteServiceContainerAdapter (org.eclipse.ecf.remoteservice.IRemoteServiceContainerAdapter)9 IRemoteServiceReference (org.eclipse.ecf.remoteservice.IRemoteServiceReference)8 IRemoteCallListener (org.eclipse.ecf.remoteservice.IRemoteCallListener)6 IRemoteCallCompleteEvent (org.eclipse.ecf.remoteservice.events.IRemoteCallCompleteEvent)6 IRemoteCallEvent (org.eclipse.ecf.remoteservice.events.IRemoteCallEvent)6 IConcatService (org.eclipse.ecf.tests.remoteservice.IConcatService)6 Dictionary (java.util.Dictionary)5 IRemoteServiceProxy (org.eclipse.ecf.remoteservice.IRemoteServiceProxy)5 JSONObject (org.json.JSONObject)5 Hashtable (java.util.Hashtable)4 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)4 RemoteServiceTracker (org.eclipse.ecf.remoteservice.util.tracker.RemoteServiceTracker)4 IRemoteServiceRegistration (org.eclipse.ecf.remoteservice.IRemoteServiceRegistration)3 ExecutionException (java.util.concurrent.ExecutionException)2 ExecutionException (org.eclipse.core.commands.ExecutionException)2 IContainerManager (org.eclipse.ecf.core.IContainerManager)2