JXTA

net.jxta.impl.peergroup
Class PeerGroupInterface

java.lang.Object
  extended by net.jxta.impl.peergroup.PeerGroupInterface
All Implemented Interfaces:
PeerGroup, Module, Service
Direct Known Subclasses:
RefCountPeerGroupInterface

 class PeerGroupInterface
extends Object
implements PeerGroup

PeerGroupInterface provides a pure interface object that permits interaction with the actual PeerGroup implementation without giving access to the real object.

This class defines immutable objects. It has no control over the wrapped peer group object's life cycle. It serves to make weak PeerGroup interface object.


Nested Class Summary
 
Nested classes/interfaces inherited from interface net.jxta.peergroup.PeerGroup
PeerGroup.GlobalRegistry
 
Field Summary
protected  PeerGroup groupImpl
          The peer group instance which backs this interface object.
 
Fields inherited from interface net.jxta.peergroup.PeerGroup
accessClassID, allPurposePeerGroupSpecID, applicationClassID, Both, DEFAULT_EXPIRATION, DEFAULT_LIFETIME, discoveryClassID, endpointClassID, FromParent, globalRegistry, Here, httpProtoClassID, membershipClassID, peerGroupClassID, peerinfoClassID, pipeClassID, proxyClassID, refAccessSpecID, refDiscoverySpecID, refEndpointSpecID, refHttpProtoSpecID, refMembershipSpecID, refNetPeerGroupSpecID, refPeerinfoSpecID, refPipeSpecID, refPlatformSpecID, refProxySpecID, refRelayProtoSpecID, refRendezvousSpecID, refResolverSpecID, refRouterProtoSpecID, refShellSpecID, refTcpProtoSpecID, refTlsProtoSpecID, relayProtoClassID, rendezvousClassID, resolverClassID, routerProtoClassID, tcpProtoClassID, tlsProtoClassID, WK_ID_PREFIX
 
Fields inherited from interface net.jxta.platform.Module
START_AGAIN_PROGRESS, START_AGAIN_STALLED, START_DISABLED, START_OK
 
Constructor Summary
PeerGroupInterface(PeerGroup theRealThing)
          Constructs an interface object that front-ends a given GenericPeerGroup.
 
Method Summary
 boolean compatible(Element compat)
          Return true if the provided compatibility statement is compatible with this group.
 boolean equals(Object target)
          
 AccessService getAccessService()
          Return the Access Service for this Peer Group.
 ModuleImplAdvertisement getAllPurposePeerGroupImplAdvertisement()
          Get an all purpose peerGroup ModuleImplAdvertisement that is compatible with this group.
 ConfigParams getConfigAdvertisement()
          Returns the config advertisement for this peer in this group (if any).
 DiscoveryService getDiscoveryService()
          Return the Discovery Service for this Peer Group.
 EndpointService getEndpointService()
          Return the Endpoint Service for this Peer Group.
 ThreadGroup getHomeThreadGroup()
          Returns the Thread Group in which threads for this peer group will live.
 Advertisement getImplAdvertisement()
          Returns the advertisement for this service.
 Service getInterface()
          Service objects are not manipulated directly to protect usage of the service.
 JxtaLoader getLoader()
          Returns the class loader for this group.
 MembershipService getMembershipService()
          Return the Membership Service for this Peer Group.
 PeerGroup getParentGroup()
          Returns the parent group of this group.
 PeerAdvertisement getPeerAdvertisement()
          Return the PeerAdvertisement of the local Peer within this Peer Group.
 PeerGroupAdvertisement getPeerGroupAdvertisement()
          Return the PeerGroupAdvertisement for this group.
 PeerGroupID getPeerGroupID()
          Return the Peer Group ID of this Peer Group.
 String getPeerGroupName()
          Return the Name of this group.
 PeerID getPeerID()
          Return the Peer ID by which this Peer is known within this Peer Group.
 PeerInfoService getPeerInfoService()
          Return the PeerInfo Service for this Peer Group.
 String getPeerName()
          Return the name of the local peer within this group.
 PipeService getPipeService()
          Return the Pipe Service for this Peer Group.
 RendezVousService getRendezVousService()
          Return the Rendezvous Service for this Peer Group.
 ResolverService getResolverService()
          Return the Resolver Service for this Peer Group.
 Iterator getRoleMap(ID name)
          Returns the map of the assigned IDs currently associated with the given ModuleClassID by this PeerGroup object.
 URI getStoreHome()
          Returns the location of the parent of all items that this peer group is using for persistently storing its preferences, cache, persistent store, properties, etc.
 PeerGroup getWeakInterface()
          Returns a weak interface object that represents this group.
 void init(PeerGroup pg, ID assignedID, Advertisement impl)
          Initialize the module, passing it its peer group and advertisement.
 boolean isRendezvous()
          Returns the whether the group member is a Rendezvous peer for the group.
 Module loadModule(ID assignedID, Advertisement impl)
          Load a Module from a ModuleImplAdv.
 Module loadModule(ID assignedID, ModuleSpecID specID, int where)
          Load a module from a ModuleSpecID

Advertisement is sought, compatibility is checked on all candidates and load is attempted.

 Service lookupService(ID name)
          Lookup for a service by name.
 Service lookupService(ID name, int roleIndex)
          Lookup for a service by class ID and index in a map.
 PeerGroup newGroup(Advertisement pgAdv)
          Instantiate a peer group from the provided advertisement.
 PeerGroup newGroup(PeerGroupID gid)
          Instantiate a group from its Peer Group ID only.
 PeerGroup newGroup(PeerGroupID gid, Advertisement impl, String name, String description)
          Instantiates a peer group from its elementary pieces and publishes the corresponding PeerGroupAdvertisement.
 void publishGroup(String name, String description)
          Publish this group's Peer Group Advertisement.
 int startApp(String[] arg)
          Complete any remaining initialization of the module.
 void stopApp()
          Stop a module.
 String toString()
          

An implementation suitable for debugging.

 void unref()
          Explicitly notifies a group interface that it will no-longer be used (similar to dispose).
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

groupImpl

protected PeerGroup groupImpl
The peer group instance which backs this interface object.

Constructor Detail

PeerGroupInterface

PeerGroupInterface(PeerGroup theRealThing)
Constructs an interface object that front-ends a given GenericPeerGroup.

Parameters:
theRealThing - the real PeerGroup
Method Detail

equals

public boolean equals(Object target)

Overrides:
equals in class Object

toString

public String toString()

An implementation suitable for debugging. Don't try to parse this string! All of the information is available from other sources.

Overrides:
toString in class Object

init

public void init(PeerGroup pg,
                 ID assignedID,
                 Advertisement impl)
Initialize the module, passing it its peer group and advertisement.

Note: when subclassing one of the existing PeerGroup implementations (which implement Module), it may not be recommended to overload the init method. See the documentation of the PeerGroup class being subclassed.

This is here for class hierarchy reasons. It is normaly ignored. By definition, the interface object protects the real object's start/stop methods from being called.

Specified by:
init in interface Module
Parameters:
pg - The PeerGroup from which this Module can obtain services. If this module is a Service, this is also the PeerGroup of which this module is a service.
assignedID - Identity of Module within group. modules can use it as a the root of their namespace to create names that are unique within the group but predictable by the same module on another peer. This is normally the ModuleClassID which is also the name under which the module is known by other modules. For a group it is the PeerGroupID itself. The parameters of a service, in the Peer configuration, are indexed by the assignedID of that service, and a Service must publish its run-time parameters in the Peer Advertisement under its assigned ID.
impl - The implementation advertisement for this Module. It is permissible to pass null if no implementation advertisement is available. This may happen if the implementation was selected by explicit class name rather than by following an implementation advertisement. Modules are not required to support that style of loading, but if they do, then their documentation should mention it.

startApp

public int startApp(String[] arg)
Complete any remaining initialization of the module. The module should be fully functional after startApp() is completed. That is also the opportunity to supply arbitrary arguments (mostly to applications).

If this module is a PeerGroup service, it may be invoked several times depending on its return value.

This is here for class hierarchy reasons. It is normaly ignored. By definition, the interface object protects the real object's start/stop methods from being called.

Specified by:
startApp in interface Module
Parameters:
arg - An array of Strings forming the parameters for this Module.
Returns:
int A status indication which may be one of Module.START_OK, Module.START_AGAIN_PROGRESS, Module.START_AGAIN_STALLED, which indicates partial or complete success, or any other value (negative values are recommended for future compatibility), which indicates failure.

stopApp

public void stopApp()
Stop a module. This may be called any time after init() completes and should not assume that startApp() has been called or completed.

The Module cannot be forced to comply, but in the future we might be able to deny it access to anything after some timeout.

This is here for class hierarchy reasons. It is normaly ignored. By definition, the interface object protects the real object's start/stop methods from being called.

Specified by:
stopApp in interface Module

getInterface

public Service getInterface()
Service objects are not manipulated directly to protect usage of the service. A Service interface is returned to access the service methods.

Specified by:
getInterface in interface Service
Returns:
Service public interface of the service

getWeakInterface

public PeerGroup getWeakInterface()
Returns a weak interface object that represents this group.

A weak interface object has no life-cycle privileges over the group that it represents and therefore its users have no accountability. A weak interface object is safe to give away but holds no promise of sustained validity.

Whatever code gave away a weak interface object retains the power of terminating the group object from which it was obtained, thereby making the weak interface object invalid.

A weak interface object is immutable; its unref and stopApp methods do nothing. Its validity is exactly that of the group or interface object from which it was obtained.

A weak interface object can be obtained from an interface object, or from a real group object, or from a weak interface object. In the later case, the object returned may be the original weak interface object since such objects are immutable.

Whatever code obtains a weak interface object from a group object or regular interface object, remains entirely liable for invoking unref on the initial object before discarding it. Giving away a weak interface object is not equivalent to transferring ownership of the original.

Specified by:
getWeakInterface in interface PeerGroup
Returns:
PeerGroup A weak interface object that represents this PeerGroup object.

unref

public void unref()
Explicitly notifies a group interface that it will no-longer be used (similar to dispose). Does nothing to a real group object, only has an effect on a group interface.

Does nothing.

Specified by:
unref in interface PeerGroup

getImplAdvertisement

public Advertisement getImplAdvertisement()
Returns the advertisement for this service.

Specified by:
getImplAdvertisement in interface Service
Returns:
Advertisement the advertisement. This is always a ModuleImplAdvertisement.

getHomeThreadGroup

public ThreadGroup getHomeThreadGroup()
Returns the Thread Group in which threads for this peer group will live. This is currently used only for debugging purposes so that the source of a thread can be determined.

Specified by:
getHomeThreadGroup in interface PeerGroup
Returns:
ThreadGroup

getStoreHome

public URI getStoreHome()
Returns the location of the parent of all items that this peer group is using for persistently storing its preferences, cache, persistent store, properties, etc. May be null if the peergroup has no defined location for storing persistent data.

Specified by:
getStoreHome in interface PeerGroup
Returns:
The location of the parent of all persistent items stored by this peer group.

getLoader

public JxtaLoader getLoader()
Returns the class loader for this group.

Specified by:
getLoader in interface PeerGroup
Returns:
JxtaLoader The JXTA Class loader used by this group.

isRendezvous

public boolean isRendezvous()
Returns the whether the group member is a Rendezvous peer for the group.

Specified by:
isRendezvous in interface PeerGroup
Returns:
boolean true if the peer is a rendezvous for the group.

getPeerGroupAdvertisement

public PeerGroupAdvertisement getPeerGroupAdvertisement()
Return the PeerGroupAdvertisement for this group.

Specified by:
getPeerGroupAdvertisement in interface PeerGroup
Returns:
PeerGroupAdvertisement this Group's advertisement.

getPeerAdvertisement

public PeerAdvertisement getPeerAdvertisement()
Return the PeerAdvertisement of the local Peer within this Peer Group.

Specified by:
getPeerAdvertisement in interface PeerGroup
Returns:
the PeerAdvertisement of the local Peer within this Peer Group.

lookupService

public Service lookupService(ID name)
                      throws ServiceNotFoundException
Lookup for a service by name.

Specified by:
lookupService in interface PeerGroup
Parameters:
name - the service identifier.
Returns:
Service, the Service registered by that name
Throws:
ServiceNotFoundException - could not find the service requested

lookupService

public Service lookupService(ID name,
                             int roleIndex)
                      throws ServiceNotFoundException
Lookup for a service by class ID and index in a map.

More than one service in a group may be of a given ModuleClass. However each of them has a unique assigned ID which serves as the index in the map of services. In most cases, there is only one service of each given Module Class, and the ID of that Module Class is the assigned ID. Otherwise, the group may have a list of existing assigned ID per base class. This routine may be used to retrieve services of the given Module Class and index in that list. In the absence of a mapping, index 0 is still valid and corresponds to the service which assigned ID is exactly the given ID. Group objects with a map are normally wrappers tailored specially by the loader of a module (often the group itself) in order to provide a map appropriate for that module. Modules that do not use more than one service of a given base class normally never need to call this method; lookupService(ID) is equivalent to lookupService(ID, 0) and will transparently remap index 0 to whatever the group's structure defines as the default for the invoking service.

Note: traditionally, the given ID is expected to be a base Module Class ID, and the assigned ID of a Module is a Class ID of the same base class with a role suffix to make it unique. If the given ID already contains a role suffix, there may exist an entry for it in the map anyway, if not (which is the expected use pattern), then only index 0 exists and the given ID is used whole and untranslated.

Specified by:
lookupService in interface PeerGroup
Parameters:
name - the service identifier
roleIndex - the index in the list of assigned IDs that match that identifier.
Returns:
Service, the corresponding Service
Throws:
ServiceNotFoundException - Could not find the service requested.

getRoleMap

public Iterator getRoleMap(ID name)
Returns the map of the assigned IDs currently associated with the given ModuleClassID by this PeerGroup object. The IDs are returned in the order of their index in the map. So the first ID returned will be identical to what would be returned by the lookup method for the given ID and index 0. If there is no explicit such map, this method will return a singleton containing the given ID as this is the default mapping. There is no guarantee that any of the returned IDs correspond to an actually registered service. This method only maps IDs.

Specified by:
getRoleMap in interface PeerGroup
Parameters:
name - The ModuleClassID for which the map is desired.
Returns:
Iterator An iterator on a collection of the IDs to which the given ID maps.

compatible

public boolean compatible(Element compat)
Return true if the provided compatibility statement is compatible with this group.

Specified by:
compatible in interface PeerGroup
Parameters:
compat - compatibility element
Returns:
boolean True if the statement is compatible.

loadModule

public Module loadModule(ID assignedID,
                         Advertisement impl)
                  throws ProtocolNotSupportedException,
                         PeerGroupException
Load a Module from a ModuleImplAdv.

Compatibility is checked and load is attempted. If compatible and loaded successfully, the resulting Module is initialized and returned. In most cases the other loadModule() method should be preferred, since unlike this one, it will seek many compatible implementation advertisements and try them all until one works. The home group of the new module (its' parent group if the new Module is a group) will be this group.

FIXME 20031103 jice Ideally, we'd need the groupAPI to offer a means to loadModule() without making a counted reference, so that group services can loadModule() things without preventing group termination. This could be achieved elegantly by making this the only behaviour available through a weak GroupInterface. So it would be enough to obtain a weak interface from one's group and then use its loadmodule method rather than that of the strong group reference. However, that's a bit too big a change to be decided without more careful consideration.

Specified by:
loadModule in interface PeerGroup
Parameters:
assignedID - Id to be assigned to that module (usually its ClassID).
impl - An implementation advertisement for that module.
Returns:
Module the module loaded and initialized.
Throws:
ProtocolNotSupportedException - The implementation described by the advertisement is incompatible with this peer. The module cannot be loaded.
PeerGroupException - The module could not be loaded or initialized

loadModule

public Module loadModule(ID assignedID,
                         ModuleSpecID specID,
                         int where)
Load a module from a ModuleSpecID

Advertisement is sought, compatibility is checked on all candidates and load is attempted. The first one that is compatible and loads successfully is initialized and returned.

Specified by:
loadModule in interface PeerGroup
Parameters:
assignedID - Id to be assigned to that module (usually its ClassID).
specID - The specID of this module.
where - May be one of: Here, FromParent, or Both, meaning that the implementation advertisement will be searched in this group, its parent or both. As a general guideline, the implementation advertisements of a group should be searched in its prospective parent (that is Here), the implementation advertisements of a group standard service should be searched in the same group than where this group's advertisement was found (that is, FromParent), while applications may be sought more freely (Both).
Returns:
Module the new module, or null if no usable implementation was found.

publishGroup

public void publishGroup(String name,
                         String description)
                  throws IOException
Publish this group's Peer Group Advertisement. The Advertisement will be published using the parent peer group's Discovery service.

Calling this method is only useful if the group is being created from scratch and the PeerGroup advertisement has not been created beforehand. In such a case, the group has never been named or described. Therefore this information has to be supplied here.

Specified by:
publishGroup in interface PeerGroup
Parameters:
name - The name of this group.
description - The description of this group.
Throws:
IOException - The publication could not be accomplished because of a network or storage failure.

newGroup

public PeerGroup newGroup(Advertisement pgAdv)
                   throws PeerGroupException
Instantiate a peer group from the provided advertisement. This peer group will be the parent of the newly instantiated peer group.

The pgAdv itself may be all new and unpublished. Therefore, the two typical uses of this routine are:

Specified by:
newGroup in interface PeerGroup
Parameters:
pgAdv - The advertisement for the group to be instantiated.
Returns:
PeerGroup the initialized (but not started) peergroup.
Throws:
PeerGroupException - For problems instantiating the peer group.

newGroup

public PeerGroup newGroup(PeerGroupID gid,
                          Advertisement impl,
                          String name,
                          String description)
                   throws PeerGroupException
Instantiates a peer group from its elementary pieces and publishes the corresponding PeerGroupAdvertisement. The pieces are: the groups implementation adv, the group id, the name and description.

The typical use of this routine is creating a whole new group based on a newly created and possibly unpublished implementation adv.

This is a convenience method equivalent to either:

 newGrp = thisGroup.loadModule(gid, impl);
 newGrp.publishGroup(name, description);
 
or, but only if the implementation advertisement has been published:

 newPGAdv = AdvertisementFactory.newAdvertisement(
                 PeerGroupAdvertisement.getAdvertisementType());
 newPGAdv.setPeerGroupID(gid);
 newPGAdv.setModuleSpecID(impl.getModuleSpecID());
 newPGAdv.setName(name);
 newPGAdv.setDescription(description);
 newGrp = thisGroup.newGroup(newPGAdv);
 

Specified by:
newGroup in interface PeerGroup
Parameters:
gid - The ID of that group. If null then a new group ID will be chosen.
impl - The advertisement of the implementation to be used.
name - The name of the group.
description - A description of this group.
Returns:
PeerGroup the initialized (but not started) peergroup.
Throws:
PeerGroupException - Thrown if the group could not be instantiated.

newGroup

public PeerGroup newGroup(PeerGroupID gid)
                   throws PeerGroupException
Instantiate a group from its Peer Group ID only. Use this when using a group that has already been published and discovered.

The typical uses of this routine are therefore:

To create a group from a known implAdv, just use PeerGroup.loadModule(ID,Advertisement) or even:

grp = new GroupSubClass(); grp.init(parentGroup, gid, impladv);

then, REMEMBER TO PUBLISH THE GROUP IF IT IS ALL NEW.

Specified by:
newGroup in interface PeerGroup
Parameters:
gid - the groupID.
Returns:
PeerGroup the initialized (but not started) peergroup.
Throws:
PeerGroupException - Thrown if the group could not be instantiated.

getRendezVousService

public RendezVousService getRendezVousService()
Return the Rendezvous Service for this Peer Group. This service is optional and may not be present in all groups.

Specified by:
getRendezVousService in interface PeerGroup
Returns:
The Rendezvous Service for this Peer Group or null if there is no Rendezvous Service in this Peer Group.

getEndpointService

public EndpointService getEndpointService()
Return the Endpoint Service for this Peer Group. This service is present in every Peer Group.

Specified by:
getEndpointService in interface PeerGroup
Returns:
EndpointService The Endpoint Service for this Peer Group.

getResolverService

public ResolverService getResolverService()
Return the Resolver Service for this Peer Group. This service is present in every Peer Group.

Specified by:
getResolverService in interface PeerGroup
Returns:
ResolverService The Resolver Service for this Peer Group.

getDiscoveryService

public DiscoveryService getDiscoveryService()
Return the Discovery Service for this Peer Group.

Specified by:
getDiscoveryService in interface PeerGroup
Returns:
The Discovery Service for this Peer Group or null if there is no PeerInfo Service in this Peer Group.

getPeerInfoService

public PeerInfoService getPeerInfoService()
Return the PeerInfo Service for this Peer Group.

Specified by:
getPeerInfoService in interface PeerGroup
Returns:
The PeerInfo Service for this Peer Group or null if there is no PeerInfo Service in this Peer Group.

getMembershipService

public MembershipService getMembershipService()
Return the Membership Service for this Peer Group. This service is present in every Peer Group.

Specified by:
getMembershipService in interface PeerGroup
Returns:
MembershipService The Membership Service for this Peer Group.

getPipeService

public PipeService getPipeService()
Return the Pipe Service for this Peer Group.

Specified by:
getPipeService in interface PeerGroup
Returns:
The Pipe Service for this Peer Group or null if there is no Pipe Service in this Peer Group.

getAccessService

public AccessService getAccessService()
Return the Access Service for this Peer Group. This service is present in every Peer Group.

Specified by:
getAccessService in interface PeerGroup
Returns:
AccessService The Access Service for this Peer Group.

getPeerGroupID

public PeerGroupID getPeerGroupID()
Return the Peer Group ID of this Peer Group.

Specified by:
getPeerGroupID in interface PeerGroup
Returns:
PeerGroupId The Peer Group ID of this Peer Group.

getPeerID

public PeerID getPeerID()
Return the Peer ID by which this Peer is known within this Peer Group.

Specified by:
getPeerID in interface PeerGroup
Returns:
the Peer ID by which this Peer is known within this Peer Group.

getPeerGroupName

public String getPeerGroupName()
Return the Name of this group. This name is not canonical, meaning that there may be other groups with the same name.

Specified by:
getPeerGroupName in interface PeerGroup
Returns:
String This groups's name or null if no name was specified.

getPeerName

public String getPeerName()
Return the name of the local peer within this group. This name is not canonical, meaning that there may be other peers with the same name.

Specified by:
getPeerName in interface PeerGroup
Returns:
String This peer's name or null if no name was specified.

getConfigAdvertisement

public ConfigParams getConfigAdvertisement()
Returns the config advertisement for this peer in this group (if any).

Specified by:
getConfigAdvertisement in interface PeerGroup
Returns:
The advertisement or null if none is available.

getAllPurposePeerGroupImplAdvertisement

public ModuleImplAdvertisement getAllPurposePeerGroupImplAdvertisement()
                                                                throws Exception
Get an all purpose peerGroup ModuleImplAdvertisement that is compatible with this group. This impl adv can be used to create any group that relies only on the standard services. Or to derive other impl advs, using this impl advertisement as a basis.

This defines a peergroup implementation that can be used for many purposes, and from which one may derive slightly different peergroup implementations.

This definition is always the same and has a well known ModuleSpecID. It includes the basic service, no protocols and the shell for main application.

The user must remember to change the specID if the set of services protocols or applications is altered before use.

Specified by:
getAllPurposePeerGroupImplAdvertisement in interface PeerGroup
Returns:
ModuleImplAdvertisement The new peergroup impl adv.
Throws:
Exception - if an error occurs while creating the implementation advertisement

getParentGroup

public PeerGroup getParentGroup()
Description copied from interface: net.jxta.peergroup.PeerGroup
Returns the parent group of this group. Not all groups have parents and some implementations may not reveal their parents.

Specified by:
getParentGroup in interface PeerGroup
Returns:
PeerGroup the parent group or null if a parent group if not available.

JXSE