The introduction here is again quite straightforward. However, we do need to import a couple of Nodality-specific interfaces. We also need to use a class called NetletID, which we'll discuss when we get to it.
package org.jtrix.project.helloworld; import org.jtrix.base.*; import org.jtrix.project.nodality.facet.INodeAdminFacet; import org.jtrix.project.nodality.facet.INodeFacetCollection; import org.jtrix.project.nodality.facet.NetletID; /** Netlet to provide an IHelloFacet to the node, so it can then offer it * to other netlets. */ public class Hello2Provider implements INetlet {
The initialise() method is where we offer our FacetProvider. Have a look at it first; we'll discuss it afterwards.
/** * Called by the node to initialise the netlet, this method adds * its facets to the node's current collection.<p> * * The procedure is: (1) Bind the node's administration facet. * (2) Use this to give our FacetProvider object. */ public byte[] initialise(INode node, Object bean, byte[] unsigned) throws InitialiseException { try { String na_name = INodeAdminFacet.class.getName(); INodeAdminFacet na = (INodeAdminFacet)node.bindFacet(na_name); na.addNodeFacets(new FacetProvider(), null); } catch (Exception e) { System.out.println("Netlet failed to start"); e.printStackTrace(); throw new InitialiseException(e.toString()); } System.out.println("Netlet started"); return null; }
Again we bind a node facet, this time the INodeAdminFacet, through which we add our node facet which the client will use. The addNodeFacet() method has two parameters:
public void terminate(long date, INetlet.IShutdownProgress progress) { // Nothing to clean up when we terminate System.out.println("Netlet stopped"); } public IService bindService(Warrant warrant, IService consumer) throws ServiceBindException { throw new ServiceBindException(); } public String[] getFacets() { return new String[0]; } public IRemote bindFacet(String facet) throws FacetBindException { throw new FacetBindException(); }
Here's how we implement the FacetProvider. This is the one we give to Nodality which can, if needed, produce the facets. Again, let's look at it first and discuss it after.
/** * This provides a hello world facet to the node, so the node can * offer a hello world service to netlets as part of its hosting. * This class is an INodeFacetCollection, which means it must be able to * say what facets it offers and then bind them. */ private class FacetProvider implements INodeFacetCollection { private final String _facet_name = IHelloFacet.class.getName(); /** Say what facets we offer (only the one). */ public String[] getFacets() { return new String[]{ _facet_name }; } /** Bind the named node facet, as requested by a particular netlet. * @param id Some Nodality-specific ID of the netlet requesting the * node facet. * @param facet The name of the facet being requested. */ public IRemote bindNodeFacet(NetletID id, String facet) throws FacetBindException { System.out.println("Instantiating "+facet); if(!facet.equals(_facet_name)) { throw new FacetBindException(); } System.out.println("Facet instantiated"); return new FacetHandle(new MessageGiver(), facet); } } // FacetProvider
As mentioned, this is an INodeFacetProvider, which is simply what Nodality requires of any such object, and that just means it implements the methods called getFacets() and bindNodeFacet().
The method getFacets() just returns an array saying which facets we're offering. In this case, just the one, the IHelloFacet.
The method bindNodeFacet() is what the node calls whenever that facet is needed. The first parameter is just an identifier of the consumer netlet; this is the NetletID class we had to import earlier. The second parameter is the name of the facet it wants to bind. The method can't cope with a request for anything but the IHelloFacet. But if that is what's requested it can return the implementation of IHelloFacet--a MessageGiver. As mentioned above, it must wrap the MessageGiver in a FacetHandle, so the node can proxy it properly.
Finally, here's the MessageGiver:
/** The implementation of the IHelloFacet. */ private class MessageGiver implements IHelloFacet { public String getMessage() { return "Hello, world"; } } // MessageGiver } // Hello2Provider
Nik Silver 2002-03-09