📄 groupmanager.java
字号:
package com.sams.jxta.groups;
import net.jxta.credential.AuthenticationCredential;
import net.jxta.credential.Credential;
import net.jxta.discovery.DiscoveryService;
import net.jxta.document.Advertisement;
import net.jxta.document.AdvertisementFactory;
import net.jxta.document.StructuredDocument;
import net.jxta.document.StructuredTextDocument;
import net.jxta.exception.PeerGroupException;
import net.jxta.exception.ProtocolNotSupportedException;
import net.jxta.impl.id.UUID.UUIDFactory;
import net.jxta.impl.protocol.PeerGroupAdv;
import net.jxta.peergroup.PeerGroupID;
import net.jxta.impl.peergroup.StdPeerGroupParamAdv;
import net.jxta.membership.Authenticator;
import net.jxta.membership.MembershipService;
import net.jxta.peergroup.PeerGroup;
import net.jxta.protocol.ModuleImplAdvertisement;
import net.jxta.peergroup.PeerGroupFactory;
import net.jxta.protocol.PeerGroupAdvertisement;
import java.util.Enumeration;
import java.util.Hashtable;
/**
* This class can be used to create a new group with a pluggable
* Membership service.It provides simple APIs for Apply,Join and Resign
* functionalities.
*/
public class GroupManager {
private static final org.apache.log4j.Category LOG = org.apache.log4j.Category.getInstance(GroupManager.class.getName());
// The parent group of the current peer group
//protected PeerGroup parent;
// The current peer group
protected static PeerGroup netPeerGroup;
protected PeerGroup activeGroup;
protected DiscoveryService disco;
// Any module(like a peer group) that is loaded by the parent group,must
// be compatible with the parent.Compatibility is checked by comparision
// with the stdCompactStatement
public StructuredTextDocument stdCompatStatement = // ??? Taken from StdPeerGroup
mkCS();
// An identifier to the implementation used // ??? Is it so ?
public String stdUri = "http://www.jxta.org/download/jxta.jar";// ??? Taken from StdPeerGroup
// The provider of the implementation // ??? Is it so ?
public String stdProvider = "sun.com";// ??? Taken from StdPeerGroup
// The mime type of all documents used in this example
public static final String DOCUMENT_MIME_TYPE="text";
// The base type of all documents
public static final String DOCUMENT_BASE_TYPE="xml";
// This is the root element documents created
public static final String DOCUMENT_ROOT_ELEMENT="Comp";
// Key ??? What is this used for ?
private static final String KEY="Efmt";
// Value ??? What is this used for ?
private static final String VALUE="JDK1.4";
// Key used to represent the binding
private static final String BINDING_KEY="Bind";
// Value of the binding key.It represents the binding
// of JXTA that we use.
private static final String BINDING_VALUE="V1.0 Ref Impl";
/**
* The constructor of the Group Manger.Initially
* the parent group is the also the active group.
*/
public GroupManager(PeerGroup parent) {
this.disco = parent.getDiscoveryService();
//this.parent = parent;
this.activeGroup = parent;
}
/*
* Method to add a new peer group and publish it.The
* groupMemebershipClassName is the fully qualified class
* name of the mermbership service class.
*
*/
public PeerGroup addGroup(String groupName,String groupMembershipClassName,
String groupDescription,
ModuleImplAdvertisement advertisement,
boolean conditional,PeerGroupID oldPeerGroupID){
// The first thing we do is to see if a group already exists by this name.
// If so we get the Group ID of the group. Then depending on the unconditional
// flag we proceed.
// If it is an conditional add ,we create a new group only if no other group
// with the same name already exists otherwise we throw an exception.
// If it is an unconditional add , we always create a group.If the user had previously
// created the group , we use the Old group ID .
if ( oldPeerGroupID == null){
oldPeerGroupID = alreadyExists(groupName);
}
//oldPeerGroupID = com.sams.jxta.pda.JPDA.JPDA_ROOT_GROUP_ID; //<<<<<<<<<<<<<<
// if(oldPeerGroupID != null && conditional==true)
// throw new GroupManagerException("A Group by this name already exists with id :"
// +oldPeerGroupID);
// If no Advertisement is provided, we create a fresh advertisement.
if (advertisement == null){
LOG.debug("Creating a new Advertisement");
// We use the existing advertisement of the standard peer group
// and add our service along with the other standard services
try{
// This is used as a base to create the new advertisement upon
PeerGroup util = netPeerGroup;
advertisement = util.getAllPurposePeerGroupImplAdvertisement();
LOG.debug("done: getAllPurposePeerGroupImplAdvertisement");
//advertisement = parent.getAllPurposePeerGroupImplAdvertisement();
StructuredDocument paramDoc = advertisement.getParam();
LOG.debug("done: getParam");
// The Param document used to make the StandradPeerGroup Advertisement
StdPeerGroupParamAdv paramAdv = new StdPeerGroupParamAdv(paramDoc);
LOG.debug("done: StdPeerGroupParamAdv");
// List of all the available standard services
Hashtable services = paramAdv.getServices();
// Make a ModuleImplAdvertisemnet for the membership service
ModuleImplAdvertisement moduleAdv = mkImplAdvBuiltin(PeerGroup.refMembershipSpecID,groupMembershipClassName, groupDescription);
LOG.debug("done: mkImplAdvBuiltin");
// Add this service along the other standard services
services.put(PeerGroup.membershipClassID, moduleAdv);
paramAdv.setServices(services);
LOG.debug("done: setServices");
advertisement.setParam((net.jxta.document.TextElement)paramAdv.getDocument( new net.jxta.document.MimeMediaType(DOCUMENT_MIME_TYPE, DOCUMENT_BASE_TYPE)));
LOG.debug("done: setParam");
}catch(PeerGroupException peerGroupException){
LOG.error("Error in creating Advertisement",peerGroupException);
throw new GroupManagerException(peerGroupException.getMessage());
}catch(Exception genericException){
LOG.error("Error in creating Advertisement",genericException);
throw new GroupManagerException(genericException.getMessage());
}
}
LOG.debug("Successfullt created ADVERTISEMENT");
// initialize but to no start the application
// this is done by the join command
LOG.debug("Creating the Peer Group");
PeerGroup peerGroup = null;
try {
// create a PeerGroup ID
PeerGroupID peerGroupID = null;
if(oldPeerGroupID != null)
peerGroupID = oldPeerGroupID;
else
peerGroupID = new net.jxta.impl.id.UUID.PeerGroupID(UUIDFactory.newUUID());
// create the PeerGroup
//peerGroup = parent.newGroup(peerGroupID, advertisement, groupName, groupDescription);////
peerGroup = activeGroup.newGroup(peerGroupID, advertisement, groupName, groupDescription);////
// initialize the peergroup
peerGroup.init(this.activeGroup,peerGroupID, advertisement);
//peerGroup.init(this.parent,peerGroupID, advertisement);
} catch (PeerGroupException peerGroupException) {
LOG.error("Unable to create a peer group !",peerGroupException);
throw new GroupManagerException(peerGroupException.getMessage());
}
// For debug purposes, print advertisement to console
//com.sams.jxta.Util.printAdvertismentDoc(advertisement);
// Try to publish the advertisement
LOG.debug("Trying to publish the advertisement");
publish(activeGroup,peerGroup, advertisement/*original advertisement???*/);
LOG.debug("Peer Group Advertisement successfully published");
return peerGroup;
}
/**
* Will check if a peer group with the same name exists on this peer
*/
public PeerGroupID alreadyExists(String name){
System.out.println("Searching for group:"+name);
//DiscoveryService discovery = parent.getDiscoveryService();
DiscoveryService discovery = activeGroup.getDiscoveryService();
Enumeration enumeration =null;
try{
enumeration = discovery.getLocalAdvertisements(discovery.GROUP, "Name",name);
} catch(java.io.IOException ioException) {
LOG.debug("Error in getting local advertisements ");
return null;
}
// If the group already exists either the enumeration is null
// or it does not contain any data
if(enumeration != null && enumeration.hasMoreElements()){
return ((PeerGroupAdv)enumeration.nextElement()).getPeerGroupID();
}else{
return null;
}
}
// Tries to publish the newly created peer group
// ??? Why do we need the original Advertisement ?
private void publish(PeerGroup parent, PeerGroup child,Advertisement pgAdv
//PeerGroupAdvertisement origAdv
) {
System.out.println("Publishing group");
//get the Discovery for this group
//Publish the New Peer in its group discovery
DiscoveryService discovery;
try {
discovery = parent.getDiscoveryService();
discovery.publish(pgAdv, DiscoveryService.GROUP);
discovery = activeGroup.getDiscoveryService();
discovery.publish(pgAdv, DiscoveryService.GROUP);
} catch (java.io.IOException ioException) {
LOG.error("Could not publish the service !",ioException);
throw new GroupManagerException(ioException.getMessage());
}
LOG.debug("Published the group successfully");
}
/**
* This is the code that will create a credential to join the group.
* Each group that we are joining has a specific membership requirement.
* Many groups will just be the NullMembership, which is the default.
*
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -