How to attach a user interface to a Jini service

An in-depth look at the serviceui project from the Jini community

1 2 3 Page 3
Page 3 of 3
  1. Get a service object

    Client builds a ServiceTemplate that describes the service it desires, and invokes Object lookup(ServiceTemplate) on a ServiceRegistrar. The ServiceTemplate can include one or more UIFactoryEntry objects; these help the search to just the requested services that offer one or more UIs the client may potentially use.
  2. Get the service ID

    The client makes sure the service object implements Identifiable with instanceof. If it does, the client casts the service object reference to Identifiable, and invokes getServiceID() on that reference. The client now has the service ID.

    Note that if the service object does not implement Identifiable, the client has to use the other form of lookup() to get an entire ServiceItem. This consumes more resources, but it yields a local copy of all attributes, which enables the client to search locally for an appropriate UI. Using this alternative approach may not be practical (or possible) for a resource-constrained client, which is why providers of services that these clients can be expected to request should implement Identifiable.

  3. Retrieve property maps for candidate UIs

    The client builds a ServiceTemplate composed of the ServiceID object, which uniquely identifies the service, and an instance of a subclass of UIFactoryEntry, which indicates the desired UI type and potentially some desired UI characteristics. (For example, if the client is interested in a java.awt.Panel, it would create an instance of PanelFactoryEntry and put that in the ServiceTemplate.)

    Once the client has the ServiceTemplate, it invokes the getFieldValues(ServiceTemplate tmpl, int setIndex, String field) method on the ServiceRegistrar. In the tmpl argument, it passes a reference to the ServiceTemplate. In the setIndex argument, it passes zero (or whatever index the UIFactoryEntry exists in the ServiceTemplate's attr field). In the field argument, it passes the string properties.

    The client gets an array of Map objects as the return value of the getFieldValues() method invocation. Each Map object contains properties that characterize a UI whose entry matched the type specified in the ServiceTemplate. The client can then use the property maps to select an appropriate UI.

    For example, if the client has a small screen size, it may look for a minscreensize property in each Map, then compare the minimum screen size properties to find the UI object that is most suitable for the client's display capabilities. Other properties can include locales that the UI directly supports, so a client with a French-speaking user can find UIs that offer a French option. Any useful way to characterize a UI can be given a well-known property name and added to the Maps that sit in the UIFactoryEntry objects.

  4. Retrieve one UIFactory

    When the client decides which Map represents the best UI for its needs, it looks up the name property on that Map. The name property should contain a String with an identical value to the String referenced by the name field of the corresponding UIFactoryEntry subclass. For example, if the name field of a JPanelFactoryEntry refers to a String with the value Bob's Groovy UI, then you should get a String with the value Bob's Groovy UI when you look up the name property on the Map contained in the properties field of that same JPanelFactoryEntry.

    Having retrieved the name property from the selected Map, the client builds a new ServiceTemplate composed of the ServiceID object and an instance of UIFactoryEntry with the name field set to the retrieved value of the name property. Then, the client once again invokes getFieldValues() on the ServiceRegistrar. It passes in its new ServiceTemplate in the tmpl argument, the appropriate index (probably zero) in the setIndex argument, and the String factory in the field argument.

    The name field of each UI factory-entry object in a particular service item's attributes array should be unique. Thus, the ServiceTemplate passed to the most recent invocation of getFieldValues() should match only the one UI factory entry that has the requested name. Therefore, getFieldValues() will return one UIFactory object. This object generates the UI object the client deemed to be most appropriate given its capabilities and user's preferences.

  5. Create the UI

    Lastly, the client simply invokes getUI() on the UIFactory, passing in a reference to the service object. The getUI() method returns a new UI object. Although the type of reference is an object, the client can downcast that reference to a more specific type. If the client is looking for a UI among PanelFactoryEntrys, for example, it can downcast the reference returned by the UIFactory to java.awt.Panel.

A resource-rich client

To find an appropriate UI for a service, a resource-rich client could:

  1. Get a set of service items

    Client builds a ServiceTemplate that describes the service it desires, then invokes Object lookup(ServiceTemplate, int) on a ServiceRegistrar. The ServiceTemplate can include one or more UIFactoryEntry objects to help narrow the search to just the requested services that offer one or more UIs that the client may potentially use.
  2. Search for an appropriate service and UI

    This resource-rich client already has a set of matches for its query that includes full information about matching services. The client can simply search through this information to find an appropriate service and UI.
  3. Create the UI

    Lastly, the client simply invokes getUI() on the chosen UIFactory, passing in a reference to the chosen service object.


To discuss the material presented in this article, including submitting feedback about the serviceui proposal, visit the discussion forum at

Next month

In next month's Jiniology, Eric Freeman and Susanne Hupfer introduce you to JavaSpaces.

Bill Venners has been writing software professionally for 14 years. Based in Silicon Valley, he provides software consulting and training services under the name Artima Software Company. Over the years, he has developed software for the consumer electronics, education, semiconductor, and life insurance industries. He has programmed in many languages on many platforms: assembly language on various microprocessors, C on Unix, C++ on Windows, Java on the Web. He is the author of Inside the Java 2.0 Virtual Machine, (Enterprise Computing, October 1999).

Learn more about this topic

1 2 3 Page 3
Page 3 of 3