]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/examples/org/simantics/databoard/example/old/MethodInterfaceExample.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.databoard / examples / org / simantics / databoard / example / old / MethodInterfaceExample.java
1 package org.simantics.databoard.example.old;
2 import java.util.Arrays;
3
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;
10
11 /*******************************************************************************
12  *  Copyright (c) 2010 Association for Decentralized Information Management in
13  *  Industry THTH ry.
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
18  *
19  *  Contributors:
20  *      VTT Technical Research Centre of Finland - initial API and implementation
21  *******************************************************************************/
22
23 /**
24  * In this example we bind an instance (myProcessor) with to a InterfaceType (commandProcessorInterface),
25  * to create InterfaceBinding (myProcessorBinding).
26  * 
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
30  * 
31  */
32 public class MethodInterfaceExample {
33
34         public interface ICommandProcessor {
35                 String execute(String command, int...args) throws Error1;
36         }
37
38         // Description of the ICommandProcessor interface
39         public final static Interface commandProcessorInterface = Methods.getInterfaceTypeUnchecked(ICommandProcessor.class);
40         
41
42         public static class Error1 extends Exception {
43                 
44                 private static final long serialVersionUID = 1L;
45                 
46                 public String msg;
47                 public Error1(String msg) {
48                         this.msg = msg;
49                 }
50                 @Override
51                 public String toString() {
52                         return msg;
53                 }
54         }
55         
56         public static void main(String[] args) throws Exception {
57
58                 ////// Server //////
59                 // Create service handler
60                 ICommandProcessor myProcessor = new ICommandProcessor() {
61                         @Override                       
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);
65                         }                                       
66                 };              
67
68                 ////// Create Binding //////            
69                 // Adapt the myProcessor to an InterfaceBinding
70                 MethodInterface myProcessorBinding = Methods.bindInterface(commandProcessorInterface, myProcessor);
71                 
72                 
73                 ////// Invoke Binding //////
74                 {
75                         // Get method access
76                         MethodTypeBinding b = Methods.getMethodTypeBinding( ICommandProcessor.class.getMethod("execute", String.class, int[].class) );
77                         Method m = myProcessorBinding.getMethod( b );
78
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() );                    
82                 }
83                 
84                 ////// Invoke Proxy //////
85                 {
86                         // Create proxy implementation that implements CommandProcessor and invokes myProcessorbinding
87                         ICommandProcessor myProxy = Methods.createProxy(ICommandProcessor.class, myProcessorBinding);
88                 
89                         // Execute command "start"
90                         try {
91                                 String result = myProxy.execute("start", 5, 6, 72, 7423);
92                                 System.out.println(result);
93                         } catch (Error1 e) {
94                                 System.err.println(e);
95                         }
96
97                         // Execute command "fault"
98                         try {
99                                 String result = myProxy.execute("fault");
100                                 System.out.println(result);
101                         } catch (Error1 e) {
102                                 System.err.println(e);
103                         }
104                 }
105
106                 
107         }
108         
109 }
110