⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 visitormembership.java

📁 Java p2p程序设计2002年版
💻 JAVA
字号:
/*
 * VisitorMembership.java
 *
 * Created on September 24, 2001, 8:01 PM
 */

package com.sams.jxta.groups;

import net.jxta.document.Advertisement;
import net.jxta.document.Element;
import net.jxta.service.Service;
import net.jxta.credential.Credential;
import net.jxta.credential.AuthenticationCredential;
import net.jxta.protocol.ModuleImplAdvertisement;
import org.apache.log4j.Category;
import net.jxta.peergroup.PeerGroup;
import net.jxta.impl.peergroup.GenericPeerGroup;
import java.util.Vector;
import java.util.Hashtable;
import java.util.Enumeration;
import net.jxta.membership.MembershipService;
import net.jxta.membership.Authenticator;
import net.jxta.exception.PeerGroupException;
import net.jxta.exception.ProtocolNotSupportedException;
import net.jxta.id.ID;

// Debug


public class VisitorMembership extends MembershipService {
   
   private static final Category LOG = org.apache.log4j.Category.getInstance(VisitorMembership.class.getName());  
  
   private ModuleImplAdvertisement advertisement = null;
   
   
   
   
   PeerGroup peergroup = null;
   Vector current = null;
   
   public VisitorMembership(){
      super();
   }
       /**
     *  Initialize the module, passing it its peer group and advertisement.
     *
     *  @param group PeerGroup this Module is started from.
     *  @param 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 predictible by the
     *  same module on another peer. This is normaly the ModuleClassID
     *  which is also the name under which the module is known by other
     *  modules. For a group it is the PeerGroupID itself.
     *  @param adv The implementation advertisement for this Module.
     *  @exception PeerGroupException failure to initialize this Module.
     *
     */
    public void init(PeerGroup group,
                     ID assignedID,
                     Advertisement implAdv ) throws PeerGroupException{
      
      if (implAdv == null){
            this.advertisement = (ModuleImplAdvertisement)getImplAdvertisement();
      }else{
         this.advertisement = (ModuleImplAdvertisement) advertisement;
      }

      peergroup = group;
      resign();
   }
   
   /**
    * Service objects are not manipulated directly to protect usage
    * of the service. A Service interface is returned to access the service
    * methods.
    *
    * @return Service public interface of the service
    *
    * @version $Revision: 1.1 $
    * @since JXTA 1.0
    */
   public Service getInterface() {
      return this;
   }
   
   /**
    * Supply arguments and starts this service if it hadn't started by itself.
    *
    * Currently this service starts by itself and does not expect
    * arguments.
    *
    * @param arg A table of strings arguments.
    * @return int status indication.
    */
   public int startApp(String[] arg) {
      return 0;
   }
   
   /**
    * Ask this service to stop.
    *
    * This request is currently ignored.
    */
   public void stopApp() {
   }
   
   public PeerGroup getPeerGroup() {
      return peergroup;
   }
   
   /**
    * Returns the advertisement for that service.
    *
    * @return Advertisement the advertisement.
    *
    * @version $Revision: 1.1 $
    * @since JXTA 1.0
    */
   public Advertisement getImplAdvertisement() {
 
      return advertisement;
   }
   static ModuleImplAdvertisement getNewAdvertisement(PeerGroup parent){
        // get the default membership to act as a source of some of the data.
       // Note that some of these may be inapropriate!!!!
        ModuleImplAdvertisement groupMembership = (ModuleImplAdvertisement)((GenericPeerGroup)parent).getMembershipService().getImplAdvertisement();
        
        ModuleImplAdvertisement advertisement = (ModuleImplAdvertisement)net.jxta.document.AdvertisementFactory.newAdvertisement(ModuleImplAdvertisement.getAdvertisementType() );
        //advertisement.setName("jxta.service.membership");//"VisitorMembership");
          //advertisement.setName(grpMembershipServiceAdv.getName());
       
          advertisement.setCode("com.sams.jxta.updateCMS.VisitorMembership");
    

         // advertisement.setKeywords(groupMembership.getKeywords());
          // No parameters yet
          // satellaGroupMembershipServiceAdv.setParams(paramVector);


          //advertisement.setPipeAdvertisement(groupMembership.getPipeService());

          advertisement.setProvider (groupMembership.getProvider());

          //advertisement.setSecurity (groupMembership.getSecurity());

          advertisement.setUri (groupMembership.getUri());

          //advertisement.setVersion(groupMembership.getVersion()); 
          
          return advertisement;
   }
   
   /**
    * Request the necessary credentials to join the group with which this
    * service is associated.
    *
    * @param credential the original credential that justify the issuance of the
    * the appropriate authenticator
    * @return PeerGroupCredential the desired credential
    * @throws ProtocolNotSupportedException if the authentication method requested
    *         in the application is not supported by this service.
    */
   public Authenticator apply(AuthenticationCredential application) throws PeerGroupException, ProtocolNotSupportedException {
      String methodMatch = "VisitorAuthenticator";
      String method = application.getMethod();
      
      if( (null != method) && !methodMatch.equals( method ) ){
         String message = "Found the wrong method! Wanted:'"+methodMatch+"' found:'"+method+"'.";
         System.out.println(message);
         throw new ProtocolNotSupportedException(message );
      }else{
         return new VisitorAuthenticator( this, application );
      }
   }
   
   /**
    * Returns the current credential for this peer.
    */
   public synchronized Enumeration getCurrentCredentials() throws PeerGroupException {
      return current.elements();
   }
   
   /**
    * Returns the current credentials for this peer. The elements of the
    * enumeration are all of types derived from "AuthenticationCredential"
    */
   public Enumeration getAuthCredentials() throws PeerGroupException {
      return current.elements();
   }
   
   /**
    * Join the groupwit a valid authentication.
    *
    * @param authenticated the completed authentication.
    */
   public synchronized Credential join(Authenticator authenticated) throws PeerGroupException {
      System.out.println(this.getClass().getName()+" **** join");

      if( !(authenticated instanceof VisitorAuthenticator) ){
         throw new ClassCastException( "This is not my authenticator!" );
      }else if( !authenticated.isReadyForJoin() ){
         throw new PeerGroupException( "Not Ready to join!" );
      }else{         
         // This creates a credential based on information in the authenticator.
         // The reason for this is that the authenticator 
         Credential newCred = new VisitorCredential( this, (VisitorAuthenticator)authenticated );
         current.addElement( newCred );
         return newCred;
      }
   }
   
   /**
    * Leave the group to which this service is attached.
    */
   public synchronized void resign() throws PeerGroupException {
      current = new Vector();
      current.addElement( new VisitorCredential( this) );
   }
   
   /**
    *  Given a fragment of a StructuredDocument, reconstruct a Credential object
    *  from that fragment. This is a marshaling of an XML credential to a
    *  credential object. This should be changed to locate the tags by name.
    *
    *  @param  element The StructuredDocument fragment to use for building the
    *  credential.
    *  @return Credential The created credential
    */
   public Credential makeCredential(Element element) throws PeerGroupException, Exception {
      System.out.println(this.getClass().getName()+" **** Creating credential");
      return new VisitorCredential( this, element );
   }
   
   
}
// End of VisitorMembership

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -