1 package org.simantics.databoard.example.old;
2 import java.util.Arrays;
4 import org.simantics.databoard.Methods;
5 import org.simantics.databoard.method.Interface;
6 import org.simantics.databoard.method.MethodInterface;
7 import org.simantics.databoard.method.MethodInterface.AsyncResult;
8 import org.simantics.databoard.method.MethodInterface.Method;
9 import org.simantics.databoard.method.MethodTypeBinding;
11 /*******************************************************************************
12 * Copyright (c) 2010 Association for Decentralized Information Management in
14 * All rights reserved. This program and the accompanying materials
15 * are made available under the terms of the Eclipse Public License v1.0
16 * which accompanies this distribution, and is available at
17 * http://www.eclipse.org/legal/epl-v10.html
20 * VTT Technical Research Centre of Finland - initial API and implementation
21 *******************************************************************************/
24 * In this example we bind an instance (myProcessor) with to a InterfaceType (commandProcessorInterface),
25 * to create InterfaceBinding (myProcessorBinding).
27 * myProcessor is accessed 2 ways:
28 * 1. acquiring a method access from myProcessorBinding and invoking that
29 * 2. creating a proxy implementation to ICommandProcessor and invoke that
32 public class MethodInterfaceExample {
34 public interface ICommandProcessor {
35 String execute(String command, int...args) throws Error1;
38 // Description of the ICommandProcessor interface
39 public final static Interface commandProcessorInterface = Methods.getInterfaceTypeUnchecked(ICommandProcessor.class);
42 public static class Error1 extends Exception {
44 private static final long serialVersionUID = 1L;
47 public Error1(String msg) {
51 public String toString() {
56 public static void main(String[] args) throws Exception {
59 // Create service handler
60 ICommandProcessor myProcessor = new ICommandProcessor() {
62 public String execute(String command, int...args) throws Error1 {
63 if (!command.equals("start")) throw new Error1("Unknown command "+command);
64 return "Program started "+command+" with args "+Arrays.toString(args);
68 ////// Create Binding //////
69 // Adapt the myProcessor to an InterfaceBinding
70 MethodInterface myProcessorBinding = Methods.bindInterface(commandProcessorInterface, myProcessor);
73 ////// Invoke Binding //////
76 MethodTypeBinding b = Methods.getMethodTypeBinding( ICommandProcessor.class.getMethod("execute", String.class, int[].class) );
77 Method m = myProcessorBinding.getMethod( b );
79 // Invoke the method access (similiar to reflection)
80 AsyncResult res = m.invoke( new Object[] { "start", new int[] {666, 777} } );
81 System.out.println( res.waitForResponse() );
84 ////// Invoke Proxy //////
86 // Create proxy implementation that implements CommandProcessor and invokes myProcessorbinding
87 ICommandProcessor myProxy = Methods.createProxy(ICommandProcessor.class, myProcessorBinding);
89 // Execute command "start"
91 String result = myProxy.execute("start", 5, 6, 72, 7423);
92 System.out.println(result);
94 System.err.println(e);
97 // Execute command "fault"
99 String result = myProxy.execute("fault");
100 System.out.println(result);
102 System.err.println(e);