Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.
Difference between revisions of "EDT:Accessing a service"
Line 3: | Line 3: | ||
You can access a service from a Rich UI application or (in the future) from code generated to Java. | You can access a service from a Rich UI application or (in the future) from code generated to Java. | ||
− | The details have changed in the days after version .081 Milestone 2. The current details are outlined here:<br> | + | '''''The details have changed in the days after version .081 Milestone 2. ''''' The current details are outlined here:<br> |
− | ::[[EDT:Resource Binding Services|Resource Binding for Services ]] | + | ::[[EDT:Resource Binding Services|Resource Binding for Services]] |
− | = EDT version . | + | = EDT version 0.80 = |
− | In EDT version . | + | In EDT version 0.80, your task is to code a '''call''' statement such as this one: |
<source lang="java"> | <source lang="java"> | ||
myBindingVar IHTTP? = Resources.getResource("binding:myResource"); | myBindingVar IHTTP? = Resources.getResource("binding:myResource"); | ||
call MyInterface.myOperation() using myBindingVar | call MyInterface.myOperation() using myBindingVar | ||
− | + | returning to myCallBackFunction | |
− | + | onException myExceptionHandler; | |
</source> | </source> | ||
Line 28: | Line 28: | ||
*Outside of Rich UI (in the future), the statement causes a synchronous invocation and does not identify a callback function or exception handler. If a returned value is expected, the statement identifies a variable to receive the returned value.<br>The synchronous '''call''' statement is now supported for accessing an IBM i called or service program, as described here: <br> [[EDT:Support for IBM i|Support for IBM i]].<br> | *Outside of Rich UI (in the future), the statement causes a synchronous invocation and does not identify a callback function or exception handler. If a returned value is expected, the statement identifies a variable to receive the returned value.<br>The synchronous '''call''' statement is now supported for accessing an IBM i called or service program, as described here: <br> [[EDT:Support for IBM i|Support for IBM i]].<br> | ||
− | <br> | + | <br> |
− | == Accessing a dedicated service (version . | + | == Accessing a dedicated service (version 0.80) == |
You can reference a Service type... <source lang="java"> | You can reference a Service type... <source lang="java"> | ||
Line 36: | Line 36: | ||
call MyService.functionName(InField.text) | call MyService.functionName(InField.text) | ||
− | + | using myBindingVar | |
− | + | returning to handleResponse | |
− | + | onException serviceExceptionHandler; | |
</source> | </source> | ||
...in one step: <source lang="java"> | ...in one step: <source lang="java"> | ||
call MyService.functionName(InField.text) | call MyService.functionName(InField.text) | ||
− | + | using new HttpProxy | |
− | + | returning to handleResponse | |
− | + | onException serviceExceptionHandler; | |
</source> | </source> | ||
Line 52: | Line 52: | ||
call IMyService.functionName(InField.text) | call IMyService.functionName(InField.text) | ||
− | + | using myBindingVar | |
− | + | returning to handleResponse | |
− | + | onException serviceExceptionHandler; | |
</source> | </source> | ||
...in one step:<source lang="java"> | ...in one step:<source lang="java"> | ||
− | + | call IMyService.functionName(InField.text) | |
− | + | using new HttpProxy("server.MyService") | |
− | + | returning to handleResponse | |
− | + | onException serviceExceptionHandler; | |
</source> | </source> | ||
− | == Accessing an EGL REST-RPC service (version . | + | == Accessing an EGL REST-RPC service (version 0.80) == |
− | <source lang="java"> | + | <source lang="java"> |
myBindingVar httpRest{@Resource {uri="binding:myEntry"}}; | myBindingVar httpRest{@Resource {uri="binding:myEntry"}}; | ||
call IMyServiceType.calculate(myList) | call IMyServiceType.calculate(myList) | ||
− | + | using myBindingVar | |
− | + | returning to theCallBack | |
− | + | onException theExceptionHandler; | |
</source> | </source> | ||
− | == Accessing a third-party REST service (version . | + | == Accessing a third-party REST service (version 0.80) == |
<source lang="java"> | <source lang="java"> | ||
Line 80: | Line 80: | ||
</source> | </source> | ||
− | == Accessing a called or service program on IBM i (version . | + | == Accessing a called or service program on IBM i (version 0.80 or beyond) == |
See [[EDT:Support for IBM i|Support for IBM i]]. | See [[EDT:Support for IBM i|Support for IBM i]]. | ||
Line 96: | Line 96: | ||
// 2. call the service | // 2. call the service | ||
call myService.theFunction() returning to theCallBack | call myService.theFunction() returning to theCallBack | ||
− | + | onException theExceptionHandler; | |
/* | /* | ||
Line 106: | Line 106: | ||
* Test the example in the Rich UI Preview tab | * Test the example in the Rich UI Preview tab | ||
* by typing valid input into the first text box; for example: | * by typing valid input into the first text box; for example: | ||
− | * | + | * 5, 12, 4 |
*/ | */ | ||
Line 114: | Line 114: | ||
Service MyServiceType | Service MyServiceType | ||
− | + | // variables and constants can be here | |
− | + | function calculate(myScores Int[] in) returns (Decimal(4,2)) | |
− | + | numberOfScores, i, mySum Int; | |
− | + | numberOfScores = myScores.getSize(); | |
− | + | for (i from 1 to numberOfScores by 1) | |
− | + | mySum = myScores[i] + mySum; | |
− | + | end | |
− | + | return(mySum/numberOfScores); | |
− | + | end | |
end | end | ||
Line 139: | Line 139: | ||
handler MyHandler type RUIhandler{initialUI =[ui], | handler MyHandler type RUIhandler{initialUI =[ui], | ||
− | + | onConstructionFunction = start, | |
− | + | cssFile = "css/ProjectInEDT.7.css", | |
− | + | title = "MyHandler"} | |
− | + | ui GridLayout{columns = 3, rows = 4, cellPadding = 4, | |
− | + | children =[myResult, myButton, scores]}; | |
− | + | scores TextField{layoutData = | |
− | + | new GridLayoutData{row = 2, column = 2}}; | |
− | + | myButton DojoButton{layoutData = | |
− | + | new GridLayoutData{row = 4, column = 2}, | |
− | + | text = "Calculate", | |
− | + | onClick ::= ui_onClick}; | |
− | + | myResult DojoTextField{layoutData = | |
− | + | new GridLayoutData{row = 4, column = 3}}; | |
− | + | function start() | |
− | + | end | |
− | + | function theExceptionHandler(exp AnyException in) | |
− | + | SysLib.writeStdOut(exp.messageID + " " + exp.message); | |
− | + | ||
− | + | if (exp isa ServiceInvocationException) | |
− | + | SysLib.writeStdOut((exp as ServiceInvocationException).detail1); | |
− | + | SysLib.writeStdOut((exp as ServiceInvocationException).detail2); | |
− | + | SysLib.writeStdOut((exp as ServiceInvocationException).detail3); | |
− | + | end | |
− | + | end | |
− | + | function theCallBack(retResult decimal(4, 2) in) | |
− | + | myResult.text = retResult; | |
− | + | end | |
− | + | function ui_onClick(event Event in) | |
− | + | inputLength int = scores.text.length(); | |
− | + | myDelimiters string = ", "; | |
− | + | myPosition int = 1; | |
− | + | myToken string; | |
− | + | myList int[]; | |
− | + | while(myPosition < inputLength) | |
− | + | myToken = StringLib.getNextToken(scores.text, | |
− | + | myPosition, myDelimiters); | |
− | + | if(myToken != null) | |
− | + | myList.appendElement(myToken as int); | |
− | + | end | |
− | + | end | |
− | + | ||
− | + | /************ Service access statements ***************************/ | |
− | + | ||
− | + | myService MyServiceType?{@DedicatedService}; | |
− | + | call myService.calculate(myList) returning to theCallBack | |
− | + | onException theExceptionHandler; | |
− | + | /*******************************************************************/ | |
− | + | end | |
end | end | ||
</source> | </source> | ||
Line 218: | Line 218: | ||
To retrieve the details from the HTTP response, add a parameter of type IHTTP to the callback function: <source lang="java"> | To retrieve the details from the HTTP response, add a parameter of type IHTTP to the callback function: <source lang="java"> | ||
function theCallBack(retResult decimal(4, 2) in, myHttp IHTTP in) | function theCallBack(retResult decimal(4, 2) in, myHttp IHTTP in) | ||
− | + | myResult.text = retResult; | |
− | + | // display the response in JSON format | |
− | + | SysLib.writeStdOut(myHttp.getResponse().body); | |
end | end | ||
</source> | </source> | ||
Line 231: | Line 231: | ||
</source> | </source> | ||
− | <br> <br><br> ♦ [[EDT:Code snippets|Code snippets main page]] <br> | + | <br><br><br>♦ [[EDT:Code snippets|Code snippets main page]] <br> |
[[Category:EDT]] | [[Category:EDT]] |
Revision as of 16:19, 5 July 2012
This page contains code snippets for service access.
You can access a service from a Rich UI application or (in the future) from code generated to Java.
The details have changed in the days after version .081 Milestone 2. The current details are outlined here:
Contents
EDT version 0.80
In EDT version 0.80, your task is to code a call statement such as this one:
myBindingVar IHTTP? = Resources.getResource("binding:myResource"); call MyInterface.myOperation() using myBindingVar returning to myCallBackFunction onException myExceptionHandler;
Your call statement typically has the following aspects:
- Invokes a specific operation by referring either to a function in a Service type or to a function prototype in an Interface type.
- Identifies service-access details by referring to a binding variable, which is specific to a kind of service binding. That variable includes service-access details. You might have retrieved the details from the EGL deployment descriptor and then customized them; or you might have created the details from scratch, in your code.
Otherwise, the call statement operates as follows:
- In a Rich UI application, the statement causes an asynchronous invocation and identifies a callback function and an exception handler.
- Outside of Rich UI (in the future), the statement causes a synchronous invocation and does not identify a callback function or exception handler. If a returned value is expected, the statement identifies a variable to receive the returned value.
The synchronous call statement is now supported for accessing an IBM i called or service program, as described here:
Support for IBM i.
Accessing a dedicated service (version 0.80)
You can reference a Service type...myBindingVar HttpProxy; call MyService.functionName(InField.text) using myBindingVar returning to handleResponse onException serviceExceptionHandler;
call MyService.functionName(InField.text) using new HttpProxy returning to handleResponse onException serviceExceptionHandler;
myBindingVar HttpProxy = new HttpProxy("server.MyService"); call IMyService.functionName(InField.text) using myBindingVar returning to handleResponse onException serviceExceptionHandler;
call IMyService.functionName(InField.text) using new HttpProxy("server.MyService") returning to handleResponse onException serviceExceptionHandler;
Accessing an EGL REST-RPC service (version 0.80)
myBindingVar httpRest{@Resource {uri="binding:myEntry"}}; call IMyServiceType.calculate(myList) using myBindingVar returning to theCallBack onException theExceptionHandler;
Accessing a third-party REST service (version 0.80)
Accessing a called or service program on IBM i (version 0.80 or beyond)
See Support for IBM i.
EDT version .7
In EDT version .7, your task follows this pattern: declare a service-access variable and use it in a call statement.
Accessing a dedicated service (version .7)
// 1. declare the service-access variable myService MyServiceType?{@dedicatedService} // 2. call the service call myService.theFunction() returning to theCallBack onException theExceptionHandler; /* * Example: create a new EGL project for * "Web 2.0 application with services". Add the * the Service type shown next to the server package, * and add the Handler type to the client package. * * Test the example in the Rich UI Preview tab * by typing valid input into the first text box; for example: * 5, 12, 4 */ // the file with a Service type package server; Service MyServiceType // variables and constants can be here function calculate(myScores Int[] in) returns (Decimal(4,2)) numberOfScores, i, mySum Int; numberOfScores = myScores.getSize(); for (i from 1 to numberOfScores by 1) mySum = myScores[i] + mySum; end return(mySum/numberOfScores); end end // the file with a Handler type package client; import server.MyServiceType; import org.eclipse.edt.rui.widgets.GridLayout; import org.eclipse.edt.rui.widgets.TextField; import org.eclipse.edt.rui.widgets.GridLayoutData; import dojo.widgets.DojoButton; import dojo.widgets.DojoTextField; handler MyHandler type RUIhandler{initialUI =[ui], onConstructionFunction = start, cssFile = "css/ProjectInEDT.7.css", title = "MyHandler"} ui GridLayout{columns = 3, rows = 4, cellPadding = 4, children =[myResult, myButton, scores]}; scores TextField{layoutData = new GridLayoutData{row = 2, column = 2}}; myButton DojoButton{layoutData = new GridLayoutData{row = 4, column = 2}, text = "Calculate", onClick ::= ui_onClick}; myResult DojoTextField{layoutData = new GridLayoutData{row = 4, column = 3}}; function start() end function theExceptionHandler(exp AnyException in) SysLib.writeStdOut(exp.messageID + " " + exp.message); if (exp isa ServiceInvocationException) SysLib.writeStdOut((exp as ServiceInvocationException).detail1); SysLib.writeStdOut((exp as ServiceInvocationException).detail2); SysLib.writeStdOut((exp as ServiceInvocationException).detail3); end end function theCallBack(retResult decimal(4, 2) in) myResult.text = retResult; end function ui_onClick(event Event in) inputLength int = scores.text.length(); myDelimiters string = ", "; myPosition int = 1; myToken string; myList int[]; while(myPosition < inputLength) myToken = StringLib.getNextToken(scores.text, myPosition, myDelimiters); if(myToken != null) myList.appendElement(myToken as int); end end /************ Service access statements ***************************/ myService MyServiceType?{@DedicatedService}; call myService.calculate(myList) returning to theCallBack onException theExceptionHandler; /*******************************************************************/ end end
Accessing an EGL REST-RPC service (version .7)
Access of an EGL REST-RPC function is similar to accessing a dedicated service, but typically involves coding the variable declaration to reference an entry in the EGL deployment descriptor.
For example, you might change the previous handler to reference a deployment descriptor entry named myService
. You can change the related variable declaration in one of two ways:
myService MyServiceType?{@Resource}; // or myService MyServiceType?{@Resource{bindingKey="myService"}};
You can demonstrate the access of a Service type under development only after you update two aspects of your deployment descriptor: Service Deployment and Resource Bindings. For details and a look at the version .8 code syntax, see Service bindings.
To retrieve the details from the HTTP response, add a parameter of type IHTTP to the callback function:function theCallBack(retResult decimal(4, 2) in, myHttp IHTTP in) myResult.text = retResult; // display the response in JSON format SysLib.writeStdOut(myHttp.getResponse().body); end