Difference between revisions of "Ic-client"

From Gcube Wiki
Jump to: navigation, search
(Created page with '<code>ic-client</code> is a client library for the <code>Information Collector</code> service. It helps clients formulating queries for gCube resource descriptions, submitting th…')
 
(First Examples)
Line 31: Line 31:
  
 
= First Examples =
 
= First Examples =
 +
 +
The <code>ic-client</code> API may be introduced with a first example that show the submission of a query for published endpoints of generic services:
 +
 +
<source lang="java5">
 +
 +
import static org.gcube.resources.discovery.icclient.ICFactory.*;
 +
 +
...
 +
 +
Query query = queryFor(ServiceEndpoint.class);
 +
 +
DiscoveryClient<ServiceEndpoint> client = clientFor(ServiceEndpoint.class);
 +
 +
List<ServiceEndpoint> resources = client.execute(query);
 +
 +
</source>
 +
 +
<code>queryFor()</code> and <code>clientFor</code> are static factory methods of the <code>ICFactory</code> class, which we import. The first method gives us a pre-prepared <code>Query</code> for service endpoints, which we invoke with the corresponding resource class of the resource model in <code>common-gcore-resources</code>. Similarly, the second method gives us a <code>DiscoveryClient</code> which can execute the query. Again, we specialise the client to bind the results of the query to instances of the <code>ServiceEndpoint</code> class. Finally, we ask the client to execute the query, collecting the parsed results.
 +
 +
In the following example, we refine the query to filter out some service endpoints and return only their addresses. We also specify that results will not need parsing:
 +
 +
<source lang="java5">
 +
 +
import static org.gcube.resources.discovery.icclient.ICFactory.*;
 +
 +
...
 +
 +
SimpleXQuery query = queryFor(ServiceEndpoint.class);
 +
 +
query.addCondition("$resource/Profile/Category/text() eq 'Database'");
 +
query.addResult("$resource/Profile/AccessPoint/Interface/Endpoint/text()");
 +
 +
DiscoveryClient<String> client = client();
 +
 +
List<String> addresses = client.execute(query);
 +
 +
</source>
 +
 +
Once again we obtain a pre-defined query for service endpoints, but this type we type it under a more specific interface, <code>SimpleXQuery</code>, which enables customisations. We first add an XQuery condition -- the query language of the <code>Information Collector</code> -- which filters out endpoints that do not give access to databases. We then customise the result expression to get back only their addresses. In both cases, we rely on the resource schema for service endpoints to formulate expressions, and convene to use <code>$resource</code> as a variable to range over target resources. We remain otherwise oblivious to the structure of the XML database in which the <code>Information Collector</code> keeps resource descriptions.
 +
 +
In the next example, we focus on larger portions of service endpoints and retrieve not only addresses but all the information available about access. Hence we reverse to parsing, but pass this time no kore and no less than the JAXB class of the resource model that binds to the retrieved portion of resources:
 +
 +
<source lang="java5">
 +
 +
import static org.gcube.resources.discovery.icclient.ICFactory.*;
 +
 +
...
 +
 +
SimpleXQuery query = queryFor(ServiceEndpoint.class);
 +
 +
query.addCondition("$resource/Profile/Category/text() eq 'Database'");
 +
query.addResult("$resource/Profile/AccessPoint");
 +
 +
DiscoveryClient<AccessPoint> client = client(AccessPoint.class);
 +
 +
List<AccessPoint> accesspoints = client.execute(query);
 +
 +
for (AccessPoint point : accesspoints) {
 +
    ...point.name()....point.address()....
 +
}
 +
</source>
  
 
= Queries =
 
= Queries =
  
 
= Query Submission =
 
= Query Submission =

Revision as of 17:34, 23 November 2012

ic-client is a client library for the Information Collector service. It helps clients formulating queries for gCube resource descriptions, submitting them to the service, and processing their results. Similar facilities for resource discovery are traditionally provided by the gCube Application Framework (gCF) and the IS Client library. ic-client improves over the latter in a number of ways, most noticeably:

  • it is completely independent from the gCore stack.
ic-client can be easily embedded in a variety of client runtimes without out-of-band installation or configuration requirements. Clients may be external to gCube, or they may be 2nd-generation gCube services developed and running on stacks other than gCore stack. In this sense, ic-client is a key part of the Featherweight Stack for gCube clients.
  • it helps formulating a wider range of queries based only on knowledge of resource schemas.
simple queries can be configured with custom namespaces and custom result expressions. As a result, clients can retrieve only parts of resource descriptions or arbitrary combinations of parts. Fine-grained results are more easily processed and improve the performance of both clients and service.
  • it offers increased flexibility in how query results are processed.
clients can configure how results ought to be parsed, or else take direct responsibility for parsing them. For example, clients may configure their own JAXB object bindings, while preprepared bindings for whole resource descriptions or specific properties thereof are readily available.

ic-client is available in our Maven repositories with the following coordinates:

<artifactId>ic-client</artifactId>
<groupId>org.gcube.resources.discovery</groupId>
<version>...</version>

The library depends on a small set of components of the Featherweight Stack. Among these, the following are visible to library clients:

  • common-gcore-resources: the object-based implementation of the gCube resource model.
clients may and normally will use the classes in common-gcore-resources to parse and process query results.
  • discovery-client: a layer of interfaces and abstract implementations for queries and query submission API.
ic- client customises this layer for queries to the Information Collector.

Note: in what follows, we blur the distinction from ic- client and discovery-client, which reflects modular choices for the design of the library but is otherwise of little consequence for its clients. The visibility of discovery-client is limited only to the package of certain components discussed below, which starts with org.gcube.resource.discovery.client when the components belong to discovery-client and with org.gcube.discovery.icclient when they belong to ic-client.

First Examples

The ic-client API may be introduced with a first example that show the submission of a query for published endpoints of generic services:

import static org.gcube.resources.discovery.icclient.ICFactory.*;
 
...
 
Query query = queryFor(ServiceEndpoint.class);
 
DiscoveryClient<ServiceEndpoint> client = clientFor(ServiceEndpoint.class);
 
List<ServiceEndpoint> resources = client.execute(query);

queryFor() and clientFor are static factory methods of the ICFactory class, which we import. The first method gives us a pre-prepared Query for service endpoints, which we invoke with the corresponding resource class of the resource model in common-gcore-resources. Similarly, the second method gives us a DiscoveryClient which can execute the query. Again, we specialise the client to bind the results of the query to instances of the ServiceEndpoint class. Finally, we ask the client to execute the query, collecting the parsed results.

In the following example, we refine the query to filter out some service endpoints and return only their addresses. We also specify that results will not need parsing:

import static org.gcube.resources.discovery.icclient.ICFactory.*;
 
...
 
SimpleXQuery query = queryFor(ServiceEndpoint.class);
 
query.addCondition("$resource/Profile/Category/text() eq 'Database'");
query.addResult("$resource/Profile/AccessPoint/Interface/Endpoint/text()");
 
DiscoveryClient<String> client = client();
 
List<String> addresses = client.execute(query);

Once again we obtain a pre-defined query for service endpoints, but this type we type it under a more specific interface, SimpleXQuery, which enables customisations. We first add an XQuery condition -- the query language of the Information Collector -- which filters out endpoints that do not give access to databases. We then customise the result expression to get back only their addresses. In both cases, we rely on the resource schema for service endpoints to formulate expressions, and convene to use $resource as a variable to range over target resources. We remain otherwise oblivious to the structure of the XML database in which the Information Collector keeps resource descriptions.

In the next example, we focus on larger portions of service endpoints and retrieve not only addresses but all the information available about access. Hence we reverse to parsing, but pass this time no kore and no less than the JAXB class of the resource model that binds to the retrieved portion of resources:

import static org.gcube.resources.discovery.icclient.ICFactory.*;
 
...
 
SimpleXQuery query = queryFor(ServiceEndpoint.class);
 
query.addCondition("$resource/Profile/Category/text() eq 'Database'");
query.addResult("$resource/Profile/AccessPoint");
 
DiscoveryClient<AccessPoint> client = client(AccessPoint.class);
 
List<AccessPoint> accesspoints = client.execute(query);
 
for (AccessPoint point : accesspoints) {
    ...point.name()....point.address()....
}

Queries

Query Submission