Skip to main content

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.

Jump to: navigation, search

Difference between revisions of "EDT:Resource Binding Introduction"

 
(93 intermediate revisions by the same user not shown)
Line 1: Line 1:
= Introduction for Resource binding<br> =
+
<span style="font-size:smaller;">&lt;&nbsp;[[EDT|EDT wiki home]]</span> <br> One of the more elegant aspects of EGL is its use of '''resource bindings''', each of which is a value that describes how to access a service or database. In most cases, you maintain bindings in an EGL deployment descriptor, which is external to your logic. The deployment descriptor provides the access details when you are developing or deploying your application.
  
One of the more elegant aspects of EGL is its use of '''resource bindings''', each of which is a value that describes how to access a service or database. In most cases, you maintain bindings in an EGL deployment descriptor, which is external to your logic and provides access details when you are developing or deploying your application.  
+
This use of the deployment descriptor is flexible. You can change the details stored there and redeploy the code.  
  
This use of the deployment descriptor is safe and flexible. You can change the details stored there and redeploy the code without changing the logic and without spending the time to regenerate output.  
+
By "redeploy," we mean to fulfill the EGL deployment step to repackage the code for subsequent distribution. The redeployment is quick; you neither change the logic nor regenerate your output.'''<br> '''
  
The binding mechanism is the same for service and database access. The typical process is to write a resource binding in an EGL deployment descriptor and to relate a variable to the stored resource binding as follows:
+
= The typical process =
  
*By invoking the '''SysLib.getResource''' function; or
+
The binding mechanism is the same for service and database access. The typical process is as follows:
*By writing a '''Resource''' annotation.
+
  
Here is an example use of the function, which can be invoked only inside an EGL function:
+
#Write a resource binding in an EGL deployment descriptor.<br>
<pre>myService MyInterfaceType?;
+
#Relate a variable to the stored resource binding. You relate the two either by invoking the <span style="font-weight: bold;">Resources</span>'''.getResource''' function or by writing a '''Resource''' annotation.&nbsp; A variable that includes binding detail is called a ''binding variable''.
 +
#Place the binding variable in an EGL ''action statement'', which is a statement that interacts with logic that is external to the code you are writing. If you are accessing external logic, you use the '''call''' statement. If you are accessing a database management system, you use one of the statements that read or write data; for example, the '''add''' or '''get''' statement.
  
myService = SysLib.getResource("MyBinding");  
+
The essential point is that when you are writing your logic, you often fulfill a two-step process:&nbsp; declare a binding variable and include it in an action statement. <br><br>When you declare a binding variable, you might use the '''Resources.getResource''' function, which can be invoked only inside an EGL function:
 +
<pre>myBindingVar IHttp? = Resources.getResource("binding:myEntry");
 
</pre>  
 
</pre>  
Here is the equivalent, simpler annotation, which you can specify anywhere that you can declare a variable:  
+
The call to '''Resources.getResource''' requires a single argument, which identifies an entry in the EGL deployment descriptor.
<pre>myService MyInterfaceType?{@Resource{bindingKey = "myBinding"}};
+
 
 +
A simpler option is to use a '''Resource''' annotation when you declare a variable: <br>
 +
<pre>myBindingVar IHttp?{@Resource{uri="binding:myEntry"}};
 
</pre>  
 
</pre>  
In either case, access to the stored binding occurs at run time, when the generated output invokes code that is equivalent to the EGL '''SysLib.getResource''' function.<br><br>A resource binding includes a series of fields that is characteristic of a particular type of binding. For example, a REST service binding has fields that are different from those in a database binding. The existence of binding types means that you can go beyond the typical process described earlier:  
+
The '''uri''' annotation field is optional and refers by default to a resource binding that has the same name as the variable. For example, the missing value for the '''uri''' field in the following annotation is <code>"mybinding:myBindingVar"</code>:<br>
 +
<pre>myBindingVar IHttp? {@Resource};
 +
</pre>
 +
Whether you specify the '''Resources.getResource''' function or '''Resource''' annotation, you can use an extended format (<code>"binding:file:''fileName''#''entry''"</code>) to identify the EGL deployment descriptor that contains the entry. Here is an example:
 +
<pre>myBindingVar IHttp? = Resources.getResource("binding:file:myDDFIle#myEntry");
  
*You might define a variable that is of the appropriate binding type. You can assign field values to that variable and use the variable for resource access. In this case, the resource binding is solely in your code.
 
*In relation to service bindings, you can initialize the variable with values from the EGL deployment descriptor and then update the fields in your code.
 
  
The next sections give further details on all these capabilities.
+
// equivalent annotation
 +
myBindingVar IHttp?{@Resource{uri = "binding:file:myDDFile#myEntry"}};
 +
</pre>
 +
If you do not use the extended format, the behavior is as follows:
  
= Binding DB with Reference in workspace<br> =
+
*At development time, the code is referencing the development deployment descriptor. That descriptor is the one that is identified in the following project property: '''Development Deployment Descriptor'''.&nbsp;
 +
*At deployment time, the code is referencing the deployment descriptor that you deploy. <br>
  
== Add Database Binding<br> ==
+
You might have multiple deployment descriptors; for example, one for a development environment, one for a test environment, and one for production.<br>
  
Open the egl deploy description file ***.elgdd and switch to “Resource Bindings” Tab, you will find there is a list for all the Resource binding of this project.<br>
+
= Bindings in your code  =
  
<br>
+
A resource binding includes a series of fields that are characteristic of a particular type of binding. For example, a REST service binding has fields that are different from those in an SQL database binding. The existence of binding types means that you can go beyond the typical process described earlier:
  
[[Image:Img1.JPG]]<br>
+
*You might declare a variable that is of the appropriate binding type. You can assign field values to that variable and use the variable for resource access. In this case, the resource binding is solely in your code.  
 
+
*In relation to service bindings, you can initialize the variable with values from the EGL deployment descriptor and then update the fields in your code.
<br>
+
 
+
You can add new database bind by click the Add button. After clicked the Add button, a “Add a Resource Binding” dialog will pop and please check the Radio button of “SQL database binding” then click “Next” button<br>
+
 
+
[[Image:Bind Img2.JPG]]<br>
+
 
+
<br>
+
 
+
== Select database biding type<br>  ==
+
 
+
In the “Add a SQL Database Binding” dialog you can see, there are two types of SQL Binding<br>
+
 
+
*Reference the selected workspace connection below(retrieved at runtime)
+
*Add the information from the selected connection below(hard-coded information)
+
 
+
There are differences between these two types of DB bindings. If you selected Reference binding, the DB information will be stored in deploy descriptor and you can update or edit it anytime via the egldd file without to touch the source code in order that to avoid the potential risk.
+
 
+
Select type of Reference the selected workspace connection below (retrieved at runtime)<br><br>
+
 
+
[[Image:Bind Img3.JPG]]<br>
+
 
+
<br>
+
 
+
Select a database connection from the list or you can follow the instruction [[EDT:Tutorial: Access a database with EGL Rich UI Lesson 3|EDT:Tutorial:_Access_a_database_with_EGL_Rich_UI_Lesson_3]] to create a database connection for your project.<br>Click the “Finish” button to complete the database connection binding. The database binding will be completed.<br><br>
+
 
+
[[Image:Bind img4.JPG]]<br>
+
 
+
<br>
+
 
+
This type of database binding is retrieved at runtime, the database information depends on the database connection in this project. If you edited the database connection information, the database binding will also change accordingly.<br>
+
 
+
== Using the database binding in code<br>  ==
+
 
+
In project you can use this database binding to define a SQLDataSource. <br>For example:
+
 
+
<br> <source lang="java">
+
//Define the SQLDataSource
+
ds SQLDataSource?{@resource{propertyFileName = "crud" , bindingkey = "NewMySQL"}};
+
</source> or <source lang="java">
+
ds SQLDataSource = SysLib.getResource("NewMySQL");
+
</source>
+
 
+
<source lang="java">
+
// Function Declarations
+
function getTable() returns (expense[])
+
    exp expense[];
+
    get exp from ds;
+
    return (exp);
+
end
+
</source> <br>
+
 
+
“propertyFileName” is the egldd file name of the project.<br>“bindingkey” is the SQL DataBase Binding Name which you created in 2.2<br><br>
+
 
+
= Binding DB with hard-coded<br>  =
+
 
+
== Add database binding<br>  ==
+
 
+
Please refer to section 2.1 for the detail&lt;span style="text-decoration: underline;" /&gt;
+
 
+
== Select database binding type<br>  ==
+
 
+
Select type of Add the information from the selected connection below(hard-coded information). You can also use the existing database connection or create a new database connection. The completed database binding should like below:<br>
+
 
+
[[Image:Bind Img5.JPG]]<br>
+
 
+
<br>
+
 
+
In can also update or exit the database information manually. After updated database information manually, do NOT forget to click the “Test Connection” to verify the database connection worked.<br>
+
 
+
== Using the database binding in code<br>  ==
+
 
+
Please refer to section 2.3<br>
+
 
+
== Other way to define SQL database connection<br>  ==
+
 
+
Besides the resource binding, we can also define the SQL database connection directly in our source code.<br>For example:<br><br>
+
 
+
<source lang="java">
+
// the data source includes a connection string, as well as
+
// security details that are stored in a dictionary.
+
 
+
connectURL string = "jdbc:derby:SomeDB;create=true;";
+
properties Dictionary{user = "MyID", password = "MyPassword"};
+
ds SQLDataSource? = new SQLDataSource(connectURL, properties);
+
</source>
+
 
+
= How to binding a service in EDT<br>  =
+
 
+
There are two type of service binding in EDT current.<br>
+
 
+
*@Resource
+
*@DedicatedService
+
 
+
In this section. You can learn how to binding a service in your project by these two types of binding.<br>Firstly, we should assume that we have already defined a service named: '''''myService'''''.<br>
+
 
+
== Binding service @Resource<br><br>  ==
+
 
+
In order to binding the service via @Resource, you should add the service into the Services Deployment. <br>Open the egldd file and switch to “Service Deployment” tab. Then click the Add button.<br><br>
+
 
+
[[Image:Bind Img6.JPG]]<br>
+
 
+
You can add the service myService into your Services Deployment setting up.<br>
+
 
+
[[Image:Bind Img7.JPG]]<br>
+
 
+
<br>
+
 
+
Then switch to Resource Bindings tab to add the REST service binding. Click Add button and select “Rest Service Binding” to add the service binding.<br>
+
 
+
[[Image:Bind Img8.JPG]]<br>
+
 
+
You should give a name for your service binding. For the Base URI you can use two types of URI as the instruction in the dialog.<br>
+
 
+
*Deployed URI.<br>
+
 
+
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; '''''Myhostname''''':8080 is your project server’s host and port number.<br>
+
 
+
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; “'''''myTargetWebProject'''''” is the project name which you want to deploy into. You can find the name in “Overview” tab of the elgdd file
+
 
+
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; “'''''restservices'''''”. Fixed string. Please do NOT modify it<br>
+
 
+
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; “'''''myService'''''”&nbsp;: your service’s name. <br>
+
 
+
*Workspace URI
+
 
+
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; “'''''myServiceProject'''''”&nbsp;: your project’s name.<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; “'''''myPackage.myservice'''''”: your service’s name and its project’s name
+
 
+
Click Finish to complete the binding.<br><br>
+
 
+
== Use the service binding in code<br>  ==
+
 
+
In source code you can use the service binding your defined in section 3.1 as below:<br>
+
 
+
<br>
+
 
+
<br> <source lang="java">
+
mysrv  mysrv ?{@Resource {}};
+
</source> or <source lang="java">
+
mysrv mysrv?{ @Resource { propertyFileName = “egldd file name” , bindingkey = “ REST service name”}}
+
</source>
+
 
+
Then you can call the methods in '''''myService'''''<br>
+
  
<br>  
+
The typical process is unchanged: you declare a binding variable and include it in an action statement.<br><br>The next sections give further details:
  
== Binding service @DedicatedService<br>  ==
+
*[[EDT:Resource Binding Services|Service bindings]]
 +
*[[EDT:Resource Binding Databases|SQL database bindings]]
  
This type of service binding is simple and did not need to setup in elgdd file. You can just code in your source as below:<br> <source lang="java">
+
The following topic gives an overview on IBM i support, which also involves resource bindings:  
mySrv myService?{@DedicatedService {}};
+
</source> or <source lang="java">
+
mySrv myService?{@DedicatedService { serviceName = “service name” }};
+
</source>
+
  
<br>
+
*[[EDT:Support for IBM i|Support for IBM i]]
  
Then you can use the methods you defined in service: '''''myService'''''.<br>
+
<br> <br>

Latest revision as of 10:26, 27 August 2012

EDT wiki home
One of the more elegant aspects of EGL is its use of resource bindings, each of which is a value that describes how to access a service or database. In most cases, you maintain bindings in an EGL deployment descriptor, which is external to your logic. The deployment descriptor provides the access details when you are developing or deploying your application.

This use of the deployment descriptor is flexible. You can change the details stored there and redeploy the code.

By "redeploy," we mean to fulfill the EGL deployment step to repackage the code for subsequent distribution. The redeployment is quick; you neither change the logic nor regenerate your output.

The typical process

The binding mechanism is the same for service and database access. The typical process is as follows:

  1. Write a resource binding in an EGL deployment descriptor.
  2. Relate a variable to the stored resource binding. You relate the two either by invoking the Resources.getResource function or by writing a Resource annotation.  A variable that includes binding detail is called a binding variable.
  3. Place the binding variable in an EGL action statement, which is a statement that interacts with logic that is external to the code you are writing. If you are accessing external logic, you use the call statement. If you are accessing a database management system, you use one of the statements that read or write data; for example, the add or get statement.

The essential point is that when you are writing your logic, you often fulfill a two-step process:  declare a binding variable and include it in an action statement.

When you declare a binding variable, you might use the Resources.getResource function, which can be invoked only inside an EGL function:

myBindingVar IHttp? = Resources.getResource("binding:myEntry");

The call to Resources.getResource requires a single argument, which identifies an entry in the EGL deployment descriptor.

A simpler option is to use a Resource annotation when you declare a variable:

myBindingVar IHttp?{@Resource{uri="binding:myEntry"}};

The uri annotation field is optional and refers by default to a resource binding that has the same name as the variable. For example, the missing value for the uri field in the following annotation is "mybinding:myBindingVar":

myBindingVar IHttp? {@Resource};

Whether you specify the Resources.getResource function or Resource annotation, you can use an extended format ("binding:file:fileName#entry") to identify the EGL deployment descriptor that contains the entry. Here is an example:

myBindingVar IHttp? = Resources.getResource("binding:file:myDDFIle#myEntry");


// equivalent annotation
myBindingVar IHttp?{@Resource{uri = "binding:file:myDDFile#myEntry"}};

If you do not use the extended format, the behavior is as follows:

  • At development time, the code is referencing the development deployment descriptor. That descriptor is the one that is identified in the following project property: Development Deployment Descriptor
  • At deployment time, the code is referencing the deployment descriptor that you deploy.

You might have multiple deployment descriptors; for example, one for a development environment, one for a test environment, and one for production.

Bindings in your code

A resource binding includes a series of fields that are characteristic of a particular type of binding. For example, a REST service binding has fields that are different from those in an SQL database binding. The existence of binding types means that you can go beyond the typical process described earlier:

  • You might declare a variable that is of the appropriate binding type. You can assign field values to that variable and use the variable for resource access. In this case, the resource binding is solely in your code.
  • In relation to service bindings, you can initialize the variable with values from the EGL deployment descriptor and then update the fields in your code.

The typical process is unchanged: you declare a binding variable and include it in an action statement.

The next sections give further details:

The following topic gives an overview on IBM i support, which also involves resource bindings:



Back to the top