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

📄 config.java

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

    /** Sets the classname of the initial class that is booted at startup **/
    public void setInitialApp( CodeDescriptor aInitialApp ) {
        
        mInitialApp = aInitialApp;
    }


    /** Adds a rendezvous peer **/
    public void addRendezvousPeer( EndpointAddress address ) {
        mRendezvousPeers.addElement( address );
    }

    /** Returns an enumeration of rendezvous peers **/
    public Enumeration getRendezvousPeers() {
        return mRendezvousPeers.elements();
    }

    /** Clears ou the list of rendezvous peers **/
    public void clearRendezvousPeers() {
        mRendezvousPeers = new Vector();
    }

    /** Tells whether messages should be propagated. **/
    public boolean getShouldPropagate() {
        return mShouldPropagate;
    }

    /** Sets whether messages should be propagated. **/
    public void setShouldPropagate( boolean aShouldPropagate ) {
        mShouldPropagate = aShouldPropagate;
    }


    /** Reads the configuration file in aInStream and creates a new
        Configuration. This is a static method such that the Configuration
        object can be tightly controlled (notice that there are no public
        contructors).
    **/
    public static Config read( InputStream aInStream ) throws ConfigurationException {
                
        /* XXX why does StructuredDocFctry take an inputstream
        rather than a reader?  A writer is used to writing config files. 
        Same method should be used for reading, therefore.  */

        try {
            Config config = new Config();

            StructuredTextDocument doc = (StructuredTextDocument)
                StructuredDocumentFactory.newStructuredDocument(
                    new MimeMediaType ("text/xml"), aInStream);

            config.readConfig( doc );

            return config;
        } catch ( ConfigurationException e ) {
            throw e;
        } catch ( Exception e ) {
            throw new ConfigurationException( "Fatal error reading configuration file", e );
        }
    }

    private void readConfig( StructuredTextDocument doc )
    throws ConfigurationException {
        Enumeration elements = doc.getChildren();

        if ((elements == null) || (!elements.hasMoreElements())) {
            throw new ConfigurationException( "Configuration file is empty or corrupted" );
        }

        while (elements.hasMoreElements()) {
            TextElement element = (TextElement) elements.nextElement();
            readElement( element );
        }
    }

    /** helper function for reading **/
    private void readElement( TextElement e ) throws ConfigurationException {
        String elname = e.getName();
        if ( elname.equals( ConfigConstants.KEY_IS_ROUTER ) )
            mIsRouter = new Boolean( e.getTextValue() ).booleanValue();
        else if ( elname.equals( ConfigConstants.KEY_IS_RENDEZVOUS_SERVER ) )
            mIsRendezvous = Boolean.valueOf( e.getTextValue() ).booleanValue();
        else if ( elname.equals( ConfigConstants.KEY_RENDEZVOUS_PEERS ) )
            readRendezvousPeers( e );
        else if ( elname.equals( ConfigConstants.KEY_SERVICES ) )
            readServices( e );
        else if ( elname.equals( ConfigConstants.KEY_INITIAL_PLATFORM ) )
            readInitPlatform( e );
        else if ( elname.equals( ConfigConstants.KEY_INITIAL_APP ) )
            readInitApp( e );
        else if ( elname.equals( ConfigConstants.KEY_TCP_TRANSPORT ) )
            readTcp( e );
        else if ( elname.equals( ConfigConstants.KEY_HTTP_TRANSPORT ) )
            readHttp( e );
        else if ( elname.equals( ConfigConstants.KEY_PROPAGATE ) )
            mShouldPropagate = Boolean.valueOf( e.getTextValue() ).booleanValue();
        else
            System.err.println( "Unknown element seen during config read: " + elname );
    }

    /** helper function for reading **/
    private void readServices( TextElement e ) throws ConfigurationException {
        Enumeration services = e.getChildren();
        if ( services!=null ) {
            while ( services.hasMoreElements() ) {
                TextElement service = (TextElement) services.nextElement();
                if ( service!=null ) {
                    String servicename = service.getName();
                    CodeDescriptor cd = CodeDescriptor.newFromXML( service );
                    if ( cd != null ) {
                        setServiceImplDescriptor( servicename, cd );
                    }
                }
            }
        }
    }

    /** helper function for reading **/
    private void readRendezvousPeers( TextElement e ) throws ConfigurationException {
        Enumeration peers = e.getChildren();
        if ( peers != null ) {
            while ( peers.hasMoreElements() ) {
                TextElement peer = (TextElement) peers.nextElement();
                addRendezvousPeer( new Address( peer.getTextValue() ) );
            }
        }
    }

    /** helper function for reading **/
    private void readInitPlatform( TextElement e ) {
        mInitialPlatform = CodeDescriptor.newFromXML( e );
    }
    
    /** helper function for reading **/
    private void readInitApp( TextElement e ) {
        mInitialApp = CodeDescriptor.newFromXML( e );
    }
    
    /** helper function for reading **/
    private void readHttp( TextElement e ) {
        mHttpConfig = HttpConfig.newFromXML( e );
    }
    
    /** helper function for reading **/
    private void readTcp( TextElement e ) {
        mTcpConfig = TcpConfig.newFromXML( e );
    }

    /** Writes configuration to a writer in XML format. **/
    public void write( Writer aWriter )
    throws ConfigurationException {
        try {
            // create doc
            StructuredTextDocument doc = null;
            doc = (StructuredTextDocument) StructuredDocumentFactory.newStructuredDocument(
                new MimeMediaType("text/xml"), "jxta:config");
            
            // populate the doc
            writeConfig( doc );

            // write the doc
            doc.sendToWriter( aWriter );
        } catch ( ConfigurationException e ) {
            // same as what I'm throwing: just rethrow.
            throw e;
        } catch ( Exception e ) {
            e.printStackTrace();
            throw new ConfigurationException( "Fatal error during configuration write.", e );
        }
    }

    /** writes all the configuration **/ 
    private void writeConfig( StructuredTextDocument doc )
    throws ConfigurationException {
        if ( mHttpConfig != null )
            mHttpConfig.writeToXML( doc );

        if ( mTcpConfig != null )
            mTcpConfig.writeToXML( doc );

        Element e=null;

        e = doc.createElement( ConfigConstants.KEY_IS_ROUTER, new Boolean( mIsRouter ).toString() );
        doc.appendChild( e );

        writeRendezvousServer( doc );
        
        doc.appendChild( doc.createElement(
            ConfigConstants.KEY_PROPAGATE, String.valueOf( mShouldPropagate ) ) );
        
        writeServices( doc, mServiceCodeDescriptors );

        e = doc.createElement( ConfigConstants.KEY_INITIAL_APP );
        doc.appendChild( e );
        getInitialApp().writeToXML( doc, e );

        e = doc.createElement( ConfigConstants.KEY_INITIAL_PLATFORM );
        doc.appendChild( e );
        getInitialPlatform().writeToXML( doc, e );
        
    }

    /** helper function for writing **/
    private void writeRendezvousServer( StructuredTextDocument doc ) {
        writeRendezvousPeers( doc, getRendezvousPeers() );

        doc.appendChild( doc.createElement(
            ConfigConstants.KEY_IS_RENDEZVOUS_SERVER,
            new Boolean( isRendezvous() ).toString() ) );
    }

    /** writes the rendezvous peers **/
    private void writeRendezvousPeers( StructuredTextDocument doc, Enumeration peers ) {
        Element peersElement = doc.createElement( ConfigConstants.KEY_RENDEZVOUS_PEERS );
        doc.appendChild( peersElement );

        while ( peers.hasMoreElements() ) {
            EndpointAddress addr = (EndpointAddress) peers.nextElement();
            ConfigUtil.writeAddress( doc,
                                     peersElement,
                                     addr,
                                     ConfigConstants.KEY_RENDEZVOUS_PEER );
        }
    }

    /** writes the services **/
    private void writeServices( StructuredTextDocument doc, Hashtable aServiceCodeDescriptors )
    throws ConfigurationException {
        Element services = doc.createElement( ConfigConstants.KEY_SERVICES );
        doc.appendChild( services ); 
        for ( int i=0; i<SERVICE_KEYS.length; i++ ) {
            CodeDescriptor desc = (CodeDescriptor) aServiceCodeDescriptors.get( SERVICE_KEYS[i] );
            if ( desc == null )
                throw new ConfigurationException( "Service " + SERVICE_KEYS[i] + " not configured" );

            Element service = doc.createElement( SERVICE_KEYS[i] );
            services.appendChild( service );
            desc.writeToXML( doc, service );
        }
    }


    /** Parameterized test driver.  Here's the options:
        1 - create default config
        2 - read config file
        3 - write config file
        
        If no parms:
            - creates default config file
            - writes it
            - reads it
            - writes what was just read
    **/
    public static void main(String[] args) throws Exception {
        Config c = Config.createDefaultConfig();
        File firstfile = new File( "first.xml" );
        File secondfile = new File( "second.xml" );

        FileWriter firstwriter = new FileWriter( firstfile );
        FileWriter secondwriter = new FileWriter( secondfile );
        try {
            c.write( firstwriter );
            firstwriter.close();
            Config c2 = Config.read( new FileInputStream( firstfile ) );
            c2.write( secondwriter );
            secondwriter.close();
        } catch ( ConfigurationException e ) {
            System.err.println( e );
            e.getNestedException().printStackTrace();
            e.printStackTrace();
        }
    }
}

⌨️ 快捷键说明

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