JINI Code Snippets

  1. How Do I Find JINI Services?
  2. How do I find a JINI Service by Name?
  3. Proxy Verification
  4. How Do I Create My Own ServiceId?

Service Lookup

This topic is quite large and covered in a separate article here.




Finding a JINI Service by Name

There are many ways of performing a lookup (see Service Lookup), for this example, we'll use the ServiceDiscoveryManager:

import net.jini.discovery.*;

import net.jini.lookup.*;

import net.jini.lookup.entry.Name;

import net.jini.core.entry.Entry;

import net.jini.core.lookup.ServiceItem;
import net.jini.core.lookup.ServiceTemplate;

import net.jini.space.JavaSpace;

// Class Definition etc........

/*
  We use LookupDiscoveryManager because it gives us a great deal
  of power in controlling the LUS'en we will do searches on.
  We can for example, disable all searching via multicast and use
  just a set of LookupLocators or we can do just multicast lookup
  on all groups (or a specific group) or we can do lookup based on
  a combination of multicast and LookupLocators.
*/
LookupDiscoveryManager myLDM =
    new LookupDiscoveryManager(DiscoveryGroupManagement.ALL_GROUPS,
                               null, null);

ServiceDiscoveryManager myManager =
    new ServiceDiscoveryManager(myLDM, null);

/*
  Create the search attributes, in this case, we want to find by name
  as well as interface.
*/
Entry[] myAttrs = new Entry[] {new Name("aServiceName")};
ServiceTemplate myTemplate =
    new ServiceTemplate(null, new Class[] {JavaSpace.class}, myAttrs);

/*
  Search for 5 seconds
*/
try {
    ServiceItem myMatch = myManager.lookup(myTemplate, null, 5000);

    if (myMatch != null) {
        System.out.println("*** Found a match with a blocking lookup ***");
		// Do stuff......
    }
} catch (InterruptedException anIE) {
    System.err.println("!!! Whoops blocking lookup interrupted :( !!!");
}

// Rest of class......



Proxy Verification and ProxyPreparer

If you're looking for information on how to support proxy verification in your service see "Support for Proxy Verification" in Creating a JINI 2.0 Service.

Proxy verification breaks down into three steps:

  1. Verifying Trust - ensuring the code comes from a trustable source or contains only trusted code.
  2. Setting Constraints - such as specifying connection timeouts or restrictions on method usage.
  3. Granting Permissions - to allow the downloaded code to perform certain privileged operations.
All of these steps are done using a single class which should be an instance of ProxyPreparer. The most commonly used instance of ProxyPreparer is net.jini.security.BasicProxyPreparer. BasicProxyPreparer has several constructors, the most complicated of which is:
BasicProxyPreparer(boolean verify, MethodConstraints methodConstraints, Permission[] permissions);
Which accepts a flag indicating whether to perform trust verification, a set of constraints to apply to the proxy and a set of permissions which should be granted to the proxy. A typical example of usage would be:
   /* Lookup proxy in LUS */
   Object server = .......
   
	/* Recover a preparer from our configuration file */
	ProxyPreparer preparer = (ProxyPreparer) config.getEntry(
	    "org.dancres.example.Client",
	    "preparer", ProxyPreparer.class, new BasicProxyPreparer());

	/* Prepare the server proxy */
	Thingy preparedServer = (Thingy) preparer.prepareProxy(server);
BasicProxyPreparer does it's work in the following order:
  1. Verify trust
  2. If the verification succeeds, grant permissions
  3. If the grant succeeds, set constraints and return a new proxy instance

For more details, see "3.2 Proxy Preparation Using BasicProxyPreparer" in the JINI 2.0 overview.




How Do I Create My Own ServiceId?

import net.jini.id.UuidFactory;
import net.jini.id.Uuid;
import net.jini.core.lookup.ServiceID;

......
	Uuid myUuid = UuidFactory.generate();
	ServiceId myServiceId =	new ServiceID(myUuid.getMostSignificantBits(),
                                              myUuid.getLeastSignificantBits());
......

© Copyright 2003 Dan Creswell

Getting Started With JINI 2.0