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

📄 groupmanager.java

📁 Java p2p程序设计2002年版
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
     */
    
    public Credential joinGroup(PeerGroup newGroup,StructuredDocument credentials,
    String authenticationMethod)
    throws GroupManagerAuthenticationException,
    ProtocolNotSupportedException{
        
        MembershipService membership = (MembershipService) newGroup.getMembershipService();
        // The first step is to apply to the membership service
        Authenticator authenticator =
        applyToMembershipService(newGroup,credentials,authenticationMethod);
        // Next step is to allow the user to "fill up" the authenticator
        // by creating a dialog
        LOG.debug("Apply process successful");
        authenticate(authenticator);
        // third , try to join the service
        Credential authenticatedCredential =joinMembershipService(authenticator,newGroup);
        
        // All is fine ! We can switch to the new group
        activeGroup = newGroup;
        LOG.debug("JOIN SUCCESSFUL !");
        return authenticatedCredential;
        
    }
    
    /**
     * This code demonstrates how to apply to a membership service.
     */
    
    private Authenticator applyToMembershipService(PeerGroup peerGroup,
    StructuredDocument credentials,
    String authenticationMethod)
    throws GroupManagerAuthenticationException,
    ProtocolNotSupportedException{
        Authenticator authenticator = null;
        // Here we create an authentication credential and
        // try to apply for a Authenticator.An exception is thrown in case
        // this step fails.
        try {
            
            AuthenticationCredential authenticationCredential =
            new AuthenticationCredential(peerGroup,authenticationMethod,credentials );
            MembershipService membership = (MembershipService) peerGroup.getMembershipService();
            authenticator = (Authenticator)membership.apply( authenticationCredential );
            
        } catch( PeerGroupException peerGroupException ) {
            // This denotes a failure in the Apply process.We
            // consider this as an Authentication exception.
            LOG.error("Apply process failed !! ",peerGroupException);
            throw new GroupManagerAuthenticationException(peerGroupException.getMessage());
        } catch (ProtocolNotSupportedException protocolNotSupportedException){
            LOG.error(protocolNotSupportedException);
            throw protocolNotSupportedException;
        }
        return authenticator;
    }
    
    
    /**
     * This method will help the user to actually "fill up" the authenticator
     * details. It will display a dialog and enable the user to visually
     * provide details to each parameter of the Authenticator.The parameters
     * of the Authenticator are found by Introspection.
     */
    
    private void authenticate( Authenticator authenticator ){
        // The following bean looks for standard bean parameters to create the
        // contents to set the authenticator. There is no real spec so this
        // will have to do.
        AuthenticatorBean viewBean = new AuthenticatorBean(null,
        true,authenticator,
        "Please Enter Required Group Info");
        viewBean.setVisible(true);
    }
    
    /**
     * This code demonstrates how to join a membership service.
     */
    
    private Credential joinMembershipService(Authenticator authenticator,
    PeerGroup peerGroup)
    throws GroupManagerAuthenticationException {
        
        Credential authenticatedCredential = null;
        // We check if the user is authentic
        if( !authenticator.isReadyForJoin() ) {
            LOG.error( "Authenticator is not ready to join");
            throw new GroupManagerAuthenticationException("Authenticator is not ready to join !");
        }
        
        // Since the user is authentic , we allow the
        // user to join the service.But the service may reject the
        // user as well.
        
        try{
            MembershipService membership = (MembershipService) peerGroup.getMembershipService();
            authenticatedCredential= membership.join(authenticator);
        } catch(PeerGroupException peerGroupException) {
            LOG.error( "Error in the Join Process",peerGroupException);
            throw new GroupManagerAuthenticationException("Error in the Join Process");
        }
        return authenticatedCredential;
    }
    
    /**
     * Method used to quit the current active group.
     */
    
    public void leaveGroup(){
        
        MembershipService memberShipService = activeGroup.getMembershipService();
        try{
            memberShipService.resign();
        } catch(PeerGroupException peerGroupException){
            LOG.error("Exception in resign",peerGroupException);
            throw new GroupManagerException(peerGroupException.getMessage());
        }
        activeGroup = null;
        purgeGroups();
    }
    
    /*
     * The method to renew the membership to the currently active group.
     */
    
    public void renew(Credential credential){
        // ??? Seems that the implementation does not have a concept of renewal
    }
    
    /**
     * Purge all groups from the cache. This help prevent us from creating
     * a copy of a group with the same ID.
     *
     */
    
    public void purgeGroups(){
        //DiscoveryService discovery = parent.getDiscoveryService();
        DiscoveryService discovery = activeGroup.getDiscoveryService();
        try{
            discovery.flushAdvertisements(null,DiscoveryService.GROUP);
        } catch (java.io.IOException ioException) {
            LOG.error("Error in purging Groups",ioException);
            throw new GroupManagerException(ioException.getMessage());
        }
    }
    
    /**
     * Creates an ModuleImplAdvertisement which in this context is a
     * MembershipService.
     *
     */
    
    private ModuleImplAdvertisement mkImplAdvBuiltin(
    net.jxta.platform.ModuleSpecID specID,
    String code,String descr) {
        
        String moduleImplAdvType = ModuleImplAdvertisement.getAdvertisementType();
        ModuleImplAdvertisement implAdvertisement = (ModuleImplAdvertisement)
        AdvertisementFactory.newAdvertisement(moduleImplAdvType);
        implAdvertisement.setModuleSpecID(specID);
        implAdvertisement.setCompat(stdCompatStatement);
        implAdvertisement.setCode(code);
        implAdvertisement.setUri(stdUri);
        implAdvertisement.setProvider(stdProvider);
        implAdvertisement.setDescription(descr);
        return implAdvertisement;
    }
    
    
    /**
     * Creates a compatibility segment for a ModuleImplAdvertisement.
     * This is needed so that our parent group can load modules of the
     * reference implementation also.
     */
    
    //??? Taken from StdPeerGroup because it was private. This should have
    //* been in a utility class.
    protected StructuredTextDocument mkCS() {
        
        StructuredTextDocument doc = (StructuredTextDocument)
        net.jxta.document.StructuredDocumentFactory.newStructuredDocument(
        new net.jxta.document.MimeMediaType(DOCUMENT_MIME_TYPE,
        DOCUMENT_BASE_TYPE),
        DOCUMENT_ROOT_ELEMENT);
        net.jxta.document.Element e = doc.createElement(KEY, VALUE);
        doc.appendChild(e);
        e = doc.createElement(BINDING_KEY, BINDING_VALUE);
        doc.appendChild(e);
        return doc;
    }
    
    /**
     * This method is used for refreshing the peer with remotely published advertisements.
     * //???Makes little sense without a listener.Moreover the method is deprecated
     */
    
    public void refreshGroups(){
        
        //disco.getRemoteAdvertisements(null, DiscoveryService.GROUP, null, null,100);
    }
    
    /*
     * Method used to get the currently active group.
     */
    
    public PeerGroup getActiveGroup(){
        
        return activeGroup;
    }
    
    /** Getter for property netPeerGroup.
     * @return Value of property netPeerGroup.
     */
    public net.jxta.peergroup.PeerGroup getNetPeerGroup() {
        return netPeerGroup;
    }
    
    /** Setter for property netPeerGroup.
     * @param netPeerGroup New value of property netPeerGroup.
     */
    public static void setNetPeerGroup(net.jxta.peergroup.PeerGroup group) {
        netPeerGroup = group;
    }
    
}

⌨️ 快捷键说明

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