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

📄 configurator.java

📁 jxme的一些相关程序,主要是手机上程序开发以及手机和计算机通信的一些程序资料,程序编译需要Ant支持
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
            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) {

        // 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();
        } catch (Exception e) {
        }
    }

    // in the event win launcher sets a proxy parse it, and use it
    // returns http://proxyserver:####
    private String parseHttpProxyServer( String proxy ) {


        if ( proxy.indexOf(";") >= 0 ) {

            StringTokenizer tokenizer = new StringTokenizer(proxy,";");
            while (tokenizer.hasMoreElements() ) {
                String token = (String) tokenizer.nextElement();
                if (token.startsWith("http=") )
                    return token;
            }
        }
        return proxy;
    }
    /**
     * Makes sure the PeerAdvertisement is sane. If it is not, fix it.
     * If it does not exist at all, create a default one.
     *
     * @param  adv  Description of Parameter
     * @return      boolean true: manual reconf advised.
     * @since 1.0
     */
    private boolean fixAdvertisement() {

        try {

            boolean reconf = isReconf();

            if (advertisement == null) {
                reconf = true;

                if( LOG.isEnabledFor(Priority.INFO) )

⌨️ 快捷键说明

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