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

📄 configurator.java

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

	    System.setProperty("net.jxta.tls.principal", principal);
	    System.setProperty("net.jxta.tls.password", password);
	    canceled = false;
	    dispose();
        }
	    private boolean canceled = true;
    }
    
    private void routerRdvMatch() {
        
        try {
            
            // Make sure that the rendez-vous and router lists
            // are randomly ordered and reduce http rendez-vous and routers to their
            // intersection, in order to accomodate a current limitation.
            // This accounts for most of the junk below.
            // Note that if http routers are not required then all rdvs are good.
            Enumeration rdvElements = null;
            
            Vector rdvs = new Vector();
            try {
                TextElement param = (TextElement)
                advertisement.getServiceParam(PeerGroup.rendezvousClassID);
                if (param != null) {
                    // First, get the Rdv part of the service param.
                    rdvElements = param.getChildren("Rdv");
                    
                    // Get the rendezvous EndpointService Addresses
                    Enumeration rdvEnum = param.getChildren("Addr");
                    while (rdvEnum.hasMoreElements()) {
                        rdvs.addElement( ( (TextElement)
                        rdvEnum.nextElement() ).getTextValue() );
                    }
                }
            } catch (Exception nobigdeal) {
            }
            
            
            
            HTTPAdv httpAdv = null;
            boolean httpDisabled = false;
            Vector routers;
            try {
                Element param =
                advertisement.getServiceParam(PeerGroup.httpProtoClassID);
                if (param.getChildren("isOff").hasMoreElements()) {
                    httpDisabled = true;
                }
                
                // FIXME 20011220 bondolo@jxta.org Temporarily accept both nodes of
                // type HTTPAdv and TransportAdvertisement.
                Enumeration httpChilds = param.getChildren(
                TransportAdvertisement.getAdvertisementType());
                
                // get the TransportAdv from either TransportAdv or HttpAdv
                if( httpChilds.hasMoreElements() ) {
                    param = (Element) httpChilds.nextElement();
                } else {
                    httpChilds = param.getChildren(
                    HTTPAdv.getAdvertisementType());
                    
                    if( httpChilds.hasMoreElements() ) {
                        param = (Element) httpChilds.nextElement();
                    }
                }
                
                httpAdv = (HTTPAdv)
                AdvertisementFactory.newAdvertisement((TextElement) param);
            } catch (Exception nevermind) {
                // Very weird, though. We could not get the HttpAdv while we know
                // that at the very least it was created by fixAdv...
                httpAdv = new HTTPAdv( );
                httpAdv.setProtocol( "http" );
                httpAdv.setPort( "9700" );
                httpAdv.setRouterEnabled(true);
                httpAdv.setProxyEnabled(false);
                httpAdv.setServerEnabled(false);
                if (LOG.isEnabledFor(Priority.WARN)) LOG.warn("httpAdv has vanished");
            }
            
            routers = httpAdv.getRouters();
            if (routers == null) routers = new Vector();
            
            Vector intRouters = new Vector();
            Vector intRdvs = new Vector();
            int intersectSz = 0;
            PosRandom random = new PosRandom();
            
            Enumeration eRdvs = rdvs.elements();
            
            while (eRdvs.hasMoreElements()) {
                
                String rdv = (String) eRdvs.nextElement();
                
                // This elimination process is only for HTTP rdvs.
                // Tcp rdvs are letf alone; they're added at the end
                if (rdv.startsWith("tcp://")) {
                    continue;
                }
                rdv = rdv.substring(7);
                Enumeration eRouters = routers.elements();
                
                // If routing is not enabled, we do not have to look.
                // all rdvs are good (although probably not needed, but that's
                // not our decision to make).
                boolean found = true;
                if (httpAdv.getRouterEnabled()) {
                    found = false;
                    while (eRouters.hasMoreElements()) {
                        String router = (String) eRouters.nextElement();
                        if (rdv.equals(router)) {
                            found = true;
                            break;
                        }
                    }
                }
                if (!found) {
                    continue;
                }
                
                ++intersectSz;
                int here = random.nextPos() % intersectSz;
                
                intRouters.insertElementAt(rdv, here);
                intRdvs.insertElementAt("http://" + rdv, here);
            }
            
            // Add tcp rdvs at the begining or the end, dependent on weither
            // http is actually in use or not.
            int front;
            int range = 0;
            if (! httpDisabled) {
                front = intersectSz;
            } else {
                front = 0;
            }
            eRdvs = rdvs.elements();
            while (eRdvs.hasMoreElements()) {
                
                String rdv = (String) eRdvs.nextElement();
                if (rdv.startsWith("http://")) {
                    continue;
                } else {
                    ++range;
                    intRdvs.insertElementAt(rdv, random.nextPos() % range + front);
                }
            }
            
            // Replace the two vectors in the adv.
            if (intRouters.size() == 0) {
                intRouters = null;
            }
            
            httpAdv.setRouters(intRouters);
            StructuredTextDocument newRdvs = (StructuredTextDocument)
            StructuredDocumentFactory.newStructuredDocument(
            new MimeMediaType("text", "xml"),
            "Parm");
            
            // Copy back into the new document the Rendezvous flag
            if ((rdvElements != null) || rdvElements.hasMoreElements()) {
                StructuredDocumentUtils.copyElements(newRdvs,
                newRdvs,
                (Element) rdvElements.nextElement());
            }
            
            Enumeration rdvEnum = intRdvs.elements();
            while (rdvEnum.hasMoreElements()) {
                Element e =
                newRdvs.createElement("Addr", (String) rdvEnum.nextElement());
                newRdvs.appendChild(e);
            }
            
            StructuredDocument http =
            StructuredDocumentFactory.newStructuredDocument(
            new MimeMediaType("text", "xml"), "Parm");
            
            StructuredDocumentUtils.copyElements(
            http, http, (StructuredDocument)
            httpAdv.getDocument(new MimeMediaType("text", "xml")));
            
            if (httpDisabled) {
                http.appendChild(http.createElement("isOff"));
            }
            
            advertisement.putServiceParam(PeerGroup.httpProtoClassID, http);
            advertisement.putServiceParam(PeerGroup.rendezvousClassID,
            newRdvs);
        } catch(Exception keepGoing) {
        }
    }
    
    
    /**
     * Returns the advertisement.
     *
     * @return PAdvertisment the advertisement.
     * @since 1.0
     */
    public PeerAdvertisement get() {
        return advertisement;
    }
    
    
    /**
     * Save the advertisement in the standard config file.
     *
     * @since 1.0
     */
    public void save() {
        saveTo(configName);
    }
    
    /**
     * Load the advertisement from the standard config file.
     *
     * @since 1.0
     */
    public void load() {
        loadFrom(configName);
    }
    
    
    /**
     * Forces a manual reconf the next time the configurator is invoked.
     *
     * @since 1.0
     */
    public void setReconf() {
        try {
            File file = new File("reconf");
            new FileOutputStream(file).close();
        } catch (Exception ex1) {
            if (LOG.isEnabledFor(Priority.ERROR)) {
                LOG.error("Could not create reconfig flag.");
                LOG.error("Create the file reconfig by hand before retrying.");
            }
        }
    }
    
    /**
     * No manual reconf the next time the configurator is invoked.
     *
     * @since 1.0
     */
    public void clearReconf() {
        try {
            File file = new File("reconf");
            file.delete();
        } catch (Exception ex1) {
            if (LOG.isEnabledFor(Priority.ERROR)) {
                LOG.error("Could not remove reconfig flag.");
                LOG.error("Delete the file reconfig by hand before retrying.");
            }
        }
    }
    
    /**
     * Check if a manual reconf has been forced.
     *
     * @return boolean true if a manual reconf is being forced.
     * @since 1.0
     */
    public boolean isReconf() {
        try {
            boolean forceReconfig;
            
            File file = new File("reconf");
            forceReconfig = file.exists();
            
            if( forceReconfig && LOG.isEnabledFor(Priority.INFO) )
                LOG.info( "Reconfig requested - 'reconf' file found" );
            
            return forceReconfig;
        } catch (Exception ex1) {
            if (LOG.isEnabledFor(Priority.ERROR)){
                LOG.error("Could not check reconfig flag.");
                LOG.error("Assuming it exists.");
            }
            return true;
        }
    }
    
    
    /**
     * Loads the Peer Advertisement from the named file.
     *
     * @param  fileName  Name of the file where the adv is stored.
     * @since
     */
    private void loadFrom(String fileName) {
        
        FileInputStream advStream = null;
        File file = null;
        
        try {
            file = new File(fileName);
            advStream = new FileInputStream(file);
            
            advertisement = (PeerAdvertisement)
            AdvertisementFactory.newAdvertisement(
            new MimeMediaType("text/xml"), advStream);
            
            if (LOG.isEnabledFor(Priority.DEBUG)){
                LOG.debug("Recovered " + fileName);
            }
        } catch (FileNotFoundException e) {
            if (LOG.isEnabledFor(Priority.DEBUG)){
                LOG.debug("No Existing " + fileName);
            }
        } catch (Exception e) {
            if (LOG.isEnabledFor(Priority.WARN)) LOG.warn("Failed to Recover " + fileName);
            e.printStackTrace();
            try {
                // delete that bad file.
                file.delete();
            } catch (Exception ex1) {
                if (LOG.isEnabledFor(Priority.FATAL)) LOG.fatal("Could not remove " + fileName + ". Removed it by hand before retrying");
                throw new JxtaError("Could not remove " + fileName + ". Removed it by hand before retrying");
            }
            
            advertisement = null;
        }
        try {
            if (advStream != null) {
                advStream.close();
            }
        } catch (Exception e) {
        }
    }
    
    
    /**
     * Saves the peer advertisement.
     *
     * @param  fileName  The name of the file where to save it.
     * @since 1.0
     */
    private void saveTo(String fileName) {

        //make sure the parent directory exists...  This could be cleaner but
        //wouldn't be portable to JDK 1.x
        String parent = new File( fileName ).getParent();
        new File( parent ).mkdirs();

        // Save the adv as input for future reconfiguration
        FileOutputStream out = null;
        try {
            out = new FileOutputStream(fileName);
            Document aDoc =
            advertisement.getDocument(new MimeMediaType("text/xml"));
            aDoc.sendToStream(out);
        } catch (Exception e) {
            e.printStackTrace();
            if (LOG.isEnabledFor(Priority.WARN)) LOG.warn("Could not save to " + fileName);
        }
        try {
            out.close();

⌨️ 快捷键说明

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