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

📄 nullmembershipservice.java

📁 jxme的一些相关程序,主要是手机上程序开发以及手机和计算机通信的一些程序资料,程序编译需要Ant支持
💻 JAVA
字号:
/*
 * Copyright (c) 2001 Sun Microsystems, Inc.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. The end-user documentation included with the redistribution,
 *    if any, must include the following acknowledgment:
 *       "This product includes software developed by the
 *       Sun Microsystems, Inc. for Project JXTA."
 *    Alternately, this acknowledgment may appear in the software itself,
 *    if and wherever such third-party acknowledgments normally appear.
 *
 * 4. The names "Sun", "Sun Microsystems, Inc.", "JXTA" and "Project JXTA" must
 *    not be used to endorse or promote products derived from this
 *    software without prior written permission. For written
 *    permission, please contact Project JXTA at http://www.jxta.org.
 *
 * 5. Products derived from this software may not be called "JXTA",
 *    nor may "JXTA" appear in their name, without prior written
 *    permission of Sun.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL SUN MICROSYSTEMS OR
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 * ====================================================================
 *
 * This software consists of voluntary contributions made by many
 * individuals on behalf of Project JXTA.  For more
 * information on Project JXTA, please see
 * <http://www.jxta.org/>.
 *
 * This license is based on the BSD license adopted by the Apache Foundation.
 *
 * $Id: NullMembershipService.java,v 1.2 2002/03/04 21:42:58 echtcherbina Exp $
 */

package net.jxta.impl.membership;

import java.net.URL;
import java.util.Enumeration;
import java.util.Vector;

import net.jxta.credential.AuthenticationCredential;
import net.jxta.credential.Credential;
import net.jxta.document.Advertisement;
import net.jxta.document.Element;
import net.jxta.document.MimeMediaType;
import net.jxta.document.StructuredDocument;
import net.jxta.document.StructuredDocumentFactory;
import net.jxta.document.Advertisement;
import net.jxta.id.ID;
import net.jxta.id.IDFactory;
import net.jxta.membership.Authenticator;
import net.jxta.membership.MembershipService;
import net.jxta.peer.PeerID;
import net.jxta.peergroup.PeerGroup;
import net.jxta.peergroup.PeerGroupID;
import net.jxta.protocol.ModuleImplAdvertisement;
import net.jxta.service.Service;

import net.jxta.exception.ProtocolNotSupportedException;
import net.jxta.exception.PeerGroupException;

/**
 *  The null membership service provides a MembershipService implementation which is
 *  is intended to be used with peer groups which require no real
 *  authentication. The null service allows you to claim any identity within
 *  the peergroup, but for peergroups which use this MembershipService method, it is
 *  likely that the identity is used only for informational purposes.
 *
 * @version $Revision: 1.2 $
 * @since JXTA 1.0
 */

public class NullMembershipService extends MembershipService {
    

    private ModuleImplAdvertisement implAdvertisement = null;

    class NullCredential implements Credential {
        
        NullMembershipService source;
        
        String whoami;
        
        ID peerid;
        
        protected NullCredential( NullMembershipService source, String whoami ) {
            
            this.source = (NullMembershipService) source;
            this.whoami = whoami;
            this.peerid = source.getPeerGroup().getPeerID();
        }
        
        protected NullCredential( NullMembershipService source, PeerGroupID peergroup, PeerID peer, String whoami ) throws PeerGroupException {
            
            this.source = (NullMembershipService) source;
            
            if( !source.getPeerGroup().getPeerGroupID().equals( peergroup ) )
                throw new PeerGroupException( "Cannot credential for a different peer group." );
            
            this.whoami = whoami;
            this.peerid = peer;
        }
        
        /**
         * Returns the service which generated this credential.
         */
        public MembershipService getSourceService() {
            return source;
        }
        
        public ID getPeerGroupID() {
            return source.getPeerGroup().getPeerGroupID();
        }
        
        public ID getPeerID() {
            return peerid;
        }
        
        /**
         * Returns the document representation of this credential.
         */
        public StructuredDocument getDocument(MimeMediaType as) throws Exception {
            StructuredDocument doc =
                StructuredDocumentFactory.newStructuredDocument( as,
                "NullCrendenital" );
            
            Element e = doc.createElement( "PeerGroupID", peergroup.getPeerGroupID().toString() );
            doc.appendChild( e );
            
            e = doc.createElement( "PeerID", peerid.toString() );
            doc.appendChild( e );
            
            e = doc.createElement( "Identity", whoami );
            doc.appendChild( e );
            
            return doc;
        }
        
        /* From here on are the methods specific to this credential type */
        
        public String getIdentity() {
            return whoami;
        }
    }
    
    
    public class NullAuthenticator implements Authenticator {
        
        MembershipService source;
        AuthenticationCredential application;
        
        String whoami = "nobody";
        
        NullAuthenticator( MembershipService source, AuthenticationCredential application ) {
            this.source = source;
            this.application = application;
        }
        
        /**
         * Returns the service which generated this authenticator.
         */
        public MembershipService getSourceService() {
            return source;
        }
        
        /**
         * Returns true if this Authenticator has been satisfied and is ready
         * for the join operation. Some authenticators may behave in an
         * asynchronously and this method can be used to determine if the
         * authentication process has completed. This method makes no
         * distinction between incomplete authentication and failed authentication.
         */
        synchronized public boolean isReadyForJoin() {
            // always ready.
            return true;
        }
        
        /**
         * Returns the name of this authentication method. This should be the same
         * name which was used in the Authentication credential.
         */
        public String getMethodName() {
            return "NullAuthentication";
        }
        
        public AuthenticationCredential getAuthenticationCredential() {
            return application;
        }

        public void setAuth1Identity( String who ) {
            whoami = who;
        }
        
        public String getAuth1Identity() {
            return whoami;
        }
        
    }
    
    PeerGroup peergroup = null;
    Vector current = null;
    
    /**
     * Initialize the application
     *
     * @param group PeerGroup this application is started from
     * @since   JXTA 1.0
     */
    public void init(PeerGroup group, ID assignedID, Advertisement impl)
        throws PeerGroupException {

        implAdvertisement = (ModuleImplAdvertisement) impl;

        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.2 $
     * @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.2 $
     * @since JXTA 1.0 
     */
    public Advertisement getImplAdvertisement() {
        return implAdvertisement;
    }

    /**
     * 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 method = application.getMethod();
        
        if( (null != method) && !"NullAuthentication".equals( method ) )
            throw new ProtocolNotSupportedException(
            "Authentication method not recognized" );
        
        return new NullAuthenticator( 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 {
        Vector none = new Vector();
        return none.elements();
    }

    /**
     * Join the group by virtue of the completed authentication provided.
     *
     * @param authenticated the completed authentication.
     */
    public synchronized Credential join(Authenticator authenticated) throws PeerGroupException {
        
        if( !(authenticated instanceof NullAuthenticator) )
            throw new ClassCastException( "This is not my authenticator!" );
        
        if( !authenticated.isReadyForJoin() )
            throw new PeerGroupException( "Not Ready to join!" );

        Credential newCred = new NullCredential( this, ((NullAuthenticator)authenticated).getAuth1Identity() );
        
        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 NullCredential( this, "nobody" ) );
    }
    
    /**
     *  Given a fragment of a StructuredDocument, reconstruct a Credential object
     *  from that fragment.
     *  
     *  @param  element The StructuredDocument fragment to use for building the
     *  credential.
     *  @return Credential The created credential
     */
    public Credential makeCredential(Element element) throws PeerGroupException, Exception {
        Object rootIs = element.getKey();
        
        // XXX 20010327 bondolo@jxta.org This should use a factory.
        
        if( !"NullCredential".equals(rootIs) )
            throw new PeerGroupException( "Element does not contain a recognized credential format" );
        
        Enumeration children = element.getChildren( "PeerGroupID" );
        if( !children.hasMoreElements() )
            throw new RuntimeException( "Missing PeerGroupID Element" );
        
        PeerGroupID peergroup = (PeerGroupID) IDFactory.fromURL( new URL( (String) ((Element) children.nextElement()).getValue() ) );
        if( children.hasMoreElements() )
            throw new RuntimeException( "Extra PeerGroupID Elements" );
        
        children = element.getChildren( "PeerID" );
        if( !children.hasMoreElements() )
            throw new RuntimeException( "Missing PeerID Element" );
        
        PeerID peer = (PeerID) IDFactory.fromURL( new URL( (String) ((Element) children.nextElement()).getValue() ) );
        if( children.hasMoreElements() )
            throw new RuntimeException( "Extra PeerID Elements" );
        
        children = element.getChildren( "PeerID" );
        if( !children.hasMoreElements() )
            throw new RuntimeException( "Missing PeerID Element" );
        
        String whoami = (String) ((Element) children.nextElement()).getValue();
        if( children.hasMoreElements() )
            throw new RuntimeException( "Extra Identity Elements" );
        
        return new NullCredential( this, peergroup, peer, whoami );
    }
    
}

⌨️ 快捷键说明

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