Newsletter sign-up
View all newsletters

Sign up for our technology specific newsletters.

Enterprise Java
Email Address:

Locate services with the Jini lookup service

Discover the power and limitations of the ServiceRegistrar interface

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
The Jini lookup service, the central component of Jini's runtime infrastructure, offers Jini clients a flexible and powerful way to find Jini services. It enables service providers to advertise their services and enables clients to locate and enlist the help of those services.

To interact with the lookup service, the client must first obtain a service registrar object via discovery, a network-level protocol used by Jini's runtime infrastructure. Discovery enables clients and services to locate lookup services. (For more information on discovery, see Resources.) The service registrar object, which implements the net.jini.core.lookup.ServiceRegistrar interface, enables the client to interact with the lookup service. To find desired services, clients build a ServiceTemplate, an instance of class net.jini.core.lookup.ServiceTemplate, and pass it to one of two lookup() methods declared in the ServiceRegistrar interface. Each lookup() method sends the service template to the lookup service, which performs the query and returns matching service objects to the client.

In general, a client looks up a service by Java type, usually an interface. For example, if a client needs to use a printer, it composes a service template that includes a Class object for a well-known interface to printer services. All printer services implement the interface. The lookup service returns a service object (or objects) that implement this interface. You can include attributes in the service template to narrow the number of matches for such a type-based search. The client uses the printer service by invoking on the service object the methods declared in the well-known interface.

The ServiceTemplate class

With the ServiceTemplate class, you can express the search criteria for Jini lookups. The class consists solely of these three public fields:

public Entry[] attributeSetTemplates;
public ServiceID serviceID;
public Class[] serviceTypes;


ServiceTemplate has no methods, and its instances merely serve as "struct"-like containers for lookup service queries. Matches are performed as described by the following excerpt from ServiceTemplate's javadoc page:

Items in the lookup service are matched using an instance of [ServiceTemplate]. A service item (item) matches a service template (tmpl) if:

  • item.serviceID equals tmpl.serviceID (or if tmpl.serviceID is null)
  • item.service [the service object] is an instance of every type in tmpl.serviceTypes
  • item.attributeSets contains at least one matching entry for each entry template in tmpl.attributeSetTemplates


An entry matches an entry template if the class of the template is the same as, or a superclass of, the class of the entry, and every non-null field in the template equals the corresponding field of the entry. Every entry can be used to match more than one template. Note that in a service template, for serviceTypes and attributeSetTemplates, a null field is equivalent to an empty array; both represent a wildcard.



As described here, the service template can include a reference to an array of Class objects. These objects indicate to the lookup service the Java type (or types) of the service object the client desires. The service template can also include a service ID, which uniquely identifies a service, and attributes, which must exactly match the attributes uploaded by the service provider in the service item. The service template can also contain wild cards for any of those fields. A wild card in the service ID field, for example, will match any service ID.

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Comment
Login
Forgot your account info?
Add comment
Anonymous comments subject to approval. Register here for member benefits.
Have a JavaWorld account? Log in here. Register now for a free account.
Resources