Notice: This Wiki is now read only and edits are no longer 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 1: | Line 1: | ||
This page contains code snippets for service access. | This page contains code snippets for service access. | ||
− | You can access a service from a Rich UI application | + | You can access a service from a Rich UI application. <!-- |
'''''The details have changed in the days after version 0.81 Milestone 2. ''''' The current details are outlined here:<br> | '''''The details have changed in the days after version 0.81 Milestone 2. ''''' The current details are outlined here:<br> | ||
Line 84: | Line 84: | ||
= EDT version 0.8.1 = | = EDT version 0.8.1 = | ||
− | --> | + | -->You might set up the application to access a dedicated service at one time or an EGL REST-RPC service at another. The task is as follows:<br> |
− | + | #In your code, declare a binding variable and use it in a call statement. | |
− | + | #Define an entry in the EGL deployment descriptor. To change from accessing one kind of service to another, you can vary the entry in the same deployment descriptor or, more likely, can use a same-named entry in a different deployment descriptor. | |
− | #In your code, declare a binding variable and use it in a call statement. | + | |
− | #Define an entry in the EGL deployment descriptor. To change from accessing one kind of service to another, you | + | |
== Accessing a dedicated service == | == Accessing a dedicated service == |
Revision as of 13:51, 28 August 2012
This page contains code snippets for service access.
You can access a service from a Rich UI application. You might set up the application to access a dedicated service at one time or an EGL REST-RPC service at another. The task is as follows:
- In your code, declare a binding variable and use it in a call statement.
- Define an entry in the EGL deployment descriptor. To change from accessing one kind of service to another, you can vary the entry in the same deployment descriptor or, more likely, can use a same-named entry in a different deployment descriptor.
Accessing a dedicated service
/******************************************************** * Declare the binding variable * ********************************************************/ myBinding IHttp? = Resources.getResource("binding:myBinding"); /******************************************************** * Call the service * ********************************************************/ call MyServiceType.calculate(myList) using myBinding returning to theCallBack onException theExceptionHandler; /******************************************************** * Example: create a new EGL project for * * "Web 2.0 client 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) 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.GridLayoutData; import org.eclipse.edt.rui.widgets.TextField; import dojo.widgets.DojoButton; import dojo.widgets.DojoTextField; handler MyHandler type RUIhandler{initialUI =[ui], onConstructionFunction = start, cssFile = "css/MyClientAppWithService.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 ***************************/ myBinding IHttp? = Resources.getResource("binding:myBinding"); call MyServiceType.calculate(myList) using myBinding 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