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

📄 idfactory.java

📁 jxme的一些相关程序,主要是手机上程序开发以及手机和计算机通信的一些程序资料,程序编译需要Ant支持
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    }

    /**
     *  Register a class with the factory from its class name. We override the
     *  standard implementation to get the id type from the class and
     *  use that as the key to register the class with the factory.
     *
     *  @since JXTA 1.0
     *
     * @param className The class name which will be regiestered.
     * @return boolean true if the class was registered otherwise false.
     * @Exception Exception   when an error occurs.
     */
    protected boolean registerAssoc( String className ) {
        boolean registeredSomething = false;

        try {
            Class idClass = Class.forName( className );

            Instantiator instantiator = (Instantiator)
                (idClass.getField("INSTANTIATOR").get(null));

            String idType = instantiator.getSupportedIDFormat( );

            registeredSomething = registerAssoc( idType, instantiator );
        }
        catch( Exception ignored ) {
            ignored.printStackTrace();
        }

        return registeredSomething;
    }

    /**
     *    Register a constructor for an ID type to enable IDs of that type to be
     *    constructed.
     *
     *    @since JXTA 1.0
     *
     *    @param  type    the identifying value for this id format
     *    @param  instanceClass  the Instantiator to use in constructing objects
     *    of this ID format.
     *    @return true   if the ID format is registered. If there is already a
     *    constructor for this format then false will be returned.
     */
    public static boolean registerIDType( int type, Class instanceClass ) {
        boolean result = factory.registerAssoc( new Integer( type ), instanceClass );

        return result;
    }

    /**
     *  Construct a new ID instance from a JXTA ID contained in a URI.
     *
     *  @see net.jxta.id.ID
     *  @see java.net.URL
     *
     *  @since JXTA 1.0
     *
     *  @param source  URI which will be decoded to create a new ID instance.
     *  @return  ID containing the new ID instance initialized from the URI.
     *  @throws UnknownServiceException Is thrown if the URI provided is of a
     *  format unrecognized by this JXTA implementation.
     *  @throws MalformedURLException Is thrown if the URI provided is not
     *  a valid, recognized JXTA URI.
     **/
    public static ID fromURL( URL source ) throws
    MalformedURLException, UnknownServiceException {

        ID result = null;

        // check the protocol
        if ( !ID.URIEncodingName.equalsIgnoreCase(source.getProtocol()) )
            throw new UnknownServiceException( "URI protocol type was not as expected." );

        String encoded = source.getFile();

        //PDA requirements 04.03.2002
        //URL.getFile returns String with '/' character in JDK1.1.8.
        //If there is a better idea can be fixed another way
        if (encoded.startsWith("/")) encoded = encoded.substring(1);
        //PDA requirements 04.03.2002

        // Decode the URN to convert any % encodings and convert it from UTF8.

        String decoded = sun.net.www.protocol.urn.Handler.decodeURN( encoded );

        int colonAt = decoded.indexOf( ':' );

        // There's a colon right?
        if( -1 == colonAt )
            throw new UnknownServiceException( "URN namespace was missing." );
        // check the namespace
        if ( !net.jxta.id.ID.URNNamespace.equalsIgnoreCase(decoded.substring( 0, colonAt )) ) {

            throw new UnknownServiceException( "URN namespace was not as expected. (" +
            net.jxta.id.ID.URNNamespace + "!=" + decoded.substring( 0, colonAt ) + ")" );
        }

        // skip the namespace portion and the colon
        decoded = decoded.substring( colonAt + 1 );

        int dashAt = decoded.indexOf( '-' );

        // there's a dash, right?
        if( -1 == dashAt )
            throw new UnknownServiceException( "URN Encodingtype was missing." );

        // get the encoding used for this id
        decoded = decoded.substring( 0, dashAt );

        if( ! factory.loadedString )
            factory.loadedString = factory.doRegisterIDTypes();

        Instantiator instantiator;
        try {
            instantiator = (Instantiator) factory.getInstantiator( decoded );
        } catch ( NoSuchElementException itsUnknown ) {
            instantiator = (Instantiator) factory.getInstantiator( "unknown" );
        }

        result = instantiator.fromURL( source );

        return result;
    }

    /**
     *  Creates a new CodatID Instance. A new random Codata ID is created for
     *  the provided Peer Group. This type of CodatID can be used as a
     *  canonical reference for dynamic content.
     *
     *  @since JXTA 1.0
     *  @see net.jxta.codat.CodatID
     *
     *  @param groupID    the group to which this content will belong.
     *  @return the newly created ID.
     **/
    public static CodatID newCodatID( PeerGroupID groupID ) {
        if( ! factory.loadedString )
            factory.loadedString = factory.doRegisterIDTypes();

        Instantiator instantiator =
        (Instantiator) factory.getInstantiator( factory.idNewInstances );

        return instantiator.newCodatID( groupID );
    }

    /**
     *  Creates a new CodatID Instance. A new random Codata ID is created for
     *  the provided Peer Group and contains a hash value for the Codat data.
     *  This type of Codat ID is most appropriate for static content. By
     *  including a hash value this form of Codat ID provides greater assurance
     *  of the canonical property of IDs. It also allows the document content
     *  returned when this ID is used to be verified to ensure it has not been
     *   altered.
     *
     *  @param  groupID The group to which this ID will belong.
     *  @param  in  The InputStream from which the content hash is calculated.
     *  The stream is read until EOF and then closed.
     *
     *  @since JXTA 1.0
     *  @see net.jxta.codat.CodatID
     *
     *  @return the newly created ID.
     *  @throws IOException I/O Error reading document
     **/
    public static CodatID newCodatID( PeerGroupID groupID, InputStream in )
    throws  IOException {
        if( ! factory.loadedString )
            factory.loadedString = factory.doRegisterIDTypes();

        Instantiator instantiator =
        (Instantiator) factory.getInstantiator( factory.idNewInstances );

        return instantiator.newCodatID( groupID, in );
    }

    /**
     *  Creates a new PeerID Instance. A new random peer id will be generated.
     *  The PeerID will be a member of the provided group.
     *
     *  @since       JXTA  1.0
     *  @see net.jxta.peer.PeerID
     *
     *  @param groupID    the group to which this PeerID will belong.
     *  @return the newly created ID.
     **/
    public static PeerID newPeerID( PeerGroupID groupID ) {
        if( ! factory.loadedString )
            factory.loadedString = factory.doRegisterIDTypes();

        Instantiator instantiator =
        (Instantiator) factory.getInstantiator( factory.idNewInstances );

        return instantiator.newPeerID( groupID );
    }

    /**
     *  Creates a new PeerGroupID Instance. A new random peer group id will be
     *  generated.
     *
     *  @since JXTA 1.0
     *  @see net.jxta.peergroup.PeerGroupID
     *
     *  @return the newly created ID.
     **/
    public static PeerGroupID newPeerGroupID( ) {
        if( ! factory.loadedString )
            factory.loadedString = factory.doRegisterIDTypes();

        Instantiator instantiator =
        (Instantiator) factory.getInstantiator( factory.idNewInstances );

        return instantiator.newPeerGroupID( );
    }

    /**
     *  Creates a new PipeID Instance. A new random pipe id will be generated.
     *  The Pipe ID will be a member of the provided group.
     *
     *  @since       JXTA  1.0
     *  @see net.jxta.pipe.PipeID
     *
     *  @param groupID    the group to which this Pipe ID will belong.
     *  @return the newly created ID.
     **/
    public static PipeID newPipeID( PeerGroupID groupID ) {
        if( ! factory.loadedString )
            factory.loadedString = factory.doRegisterIDTypes();

        Instantiator instantiator =
        (Instantiator) factory.getInstantiator( factory.idNewInstances );

        return instantiator.newPipeID( groupID );
    }

    /**
     *  Creates a new PipeID Instance. A new pipe id will be generated with the
     *  provided seed information. The Pipe ID will be a member of the provided
     *  group. The seed information should be at least four bytes in length,
     *  though longer values are better. This varient of Pipe ID allows you to
     *  create "Well-known" pipes within the context of diverse groups. This can
     *  be useful for common services that need to do discovery without
     *  advertisements or for network organization services. Because of the
     *  potential for ID collisions and the difficulties with maintaining
     *  common service interfaces this form of Pipe ID should be used sparingly.
     *
     *  @since       JXTA  1.0
     *  @see net.jxta.pipe.PipeID
     *
     *  @param groupID  the group to which this Pipe ID will belong.
     *  @param seed The seed information which will be used in creating the
     *  pipeID.
     *  @return the newly created ID.
     **/
    public static PipeID newPipeID( PeerGroupID groupID, byte [] seed ) {
        if( ! factory.loadedString )
            factory.loadedString = factory.doRegisterIDTypes();

        Instantiator instantiator =
        (Instantiator) factory.getInstantiator( factory.idNewInstances );

        return instantiator.newPipeID( groupID, seed );
    }

    /**
     *  Creates a new ModuleClassID Instance. A new random Module Class Id will
     *  be generated with a zero value role identifier. This form of Module
     *  Class Id is appropriate for cases where the module does not need to be
     *  distinguished from other instances of the same Module.
     *
     *  @since JXTA 1.0
     *  @see net.jxta.platform.ModuleClassID
     *
     *  @return the newly created ID.
     **/
    public static ModuleClassID newModuleClassID( ) {
        if( ! factory.loadedString )
            factory.loadedString = factory.doRegisterIDTypes();

        Instantiator instantiator =
        (Instantiator) factory.getInstantiator( factory.idNewInstances );

        return instantiator.newModuleClassID( );
    }

    /**
     *  Creates a new ModuleClassID Instance. A new random Module Class Id will
     *  be generated with a a random value role identifier and a base class of
     *  the provided Module Class ID. This form of Module Class Id is
     *  appropriate for cases where it is necessary to distinguish instances
     *  of the same service interface.
     *
     *  @since JXTA 1.0
     *  @see net.jxta.platform.ModuleClassID
     *
     *  @param  baseClass   The module class ID which will be used as a base
     *  class for this new role value instance.
     *  @return the newly created ID.
     **/
    public static ModuleClassID newModuleClassID( ModuleClassID baseClass ) {
        if( ! factory.loadedString )
            factory.loadedString = factory.doRegisterIDTypes();

        Instantiator instantiator =
        (Instantiator) factory.getInstantiator( factory.idNewInstances );

        return instantiator.newModuleClassID( baseClass );
    }

    /**
     *  Creates a new ModuleClassID Instance. A new random Module Spec Id will
     *  be generated.
     *
     *  @since JXTA 1.0
     *  @see net.jxta.platform.ModuleSpecID
     *
     *  @param  baseClass   The module class ID which will be used as a base
     *  class for this new Spec ID.
     *  @return the newly created ID.
     **/
    public static ModuleSpecID newModuleSpecID( ModuleClassID baseClass ) {
        if( ! factory.loadedString )
            factory.loadedString = factory.doRegisterIDTypes();

        Instantiator instantiator =
        (Instantiator) factory.getInstantiator( factory.idNewInstances );

        return instantiator.newModuleSpecID( baseClass );

    }
}

⌨️ 快捷键说明

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