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

📄 networkconfigurator.java

📁 JXTA&#8482 is a set of open, generalized peer-to-peer (P2P) protocols that allow any networked devi
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
            }            // backwards compatibility            param.addAttribute("type", RelayConfigAdv.getAdvertisementType());            relayConfig = (RelayConfigAdv) AdvertisementFactory.newAdvertisement(param);        } catch (Exception failure) {            IOException ioe = new IOException("error processing the relay config advertisement");            ioe.initCause(failure);            throw ioe;        }                // PSE        param = (XMLElement) platformConfig.getServiceParam(PeerGroup.membershipClassID);        if (param != null) {            Advertisement adv = null;            try {                adv = AdvertisementFactory.newAdvertisement(param);            } catch (NoSuchElementException notAnAdv) {                CertificateException cnfe = new CertificateException("No membership advertisement found");                cnfe.initCause(notAnAdv);            } catch (IllegalArgumentException invalidAdv) {                CertificateException cnfe = new CertificateException("Invalid membership advertisement");                cnfe.initCause(invalidAdv);            }                        if (adv instanceof PSEConfigAdv) {                pseConf = (PSEConfigAdv) adv;                cert = pseConf.getCertificateChain();            } else {                throw new CertificateException("Error processing the Membership config advertisement. Unexpected membership advertisement "                        + adv.getAdvertisementType());            }        }                // Infra Group        infraPeerGroupConfig = (PeerGroupConfigAdv) platformConfig.getSvcConfigAdvertisement(PeerGroup.peerGroupClassID);        if (null == infraPeerGroupConfig) {            infraPeerGroupConfig = createInfraConfigAdv();            try {                URI configPropsURI = storeHome.resolve("config.properties");                InputStream configPropsIS = configPropsURI.toURL().openStream();                ResourceBundle rsrcs = new PropertyResourceBundle(configPropsIS);                configPropsIS.close();                NetGroupTunables tunables = new NetGroupTunables(rsrcs, new NetGroupTunables());                infraPeerGroupConfig.setPeerGroupID(tunables.id);                infraPeerGroupConfig.setName(tunables.name);                infraPeerGroupConfig.setDesc(tunables.desc);            } catch (IOException ignored) {                //ignored            } catch (MissingResourceException ignored) {                //ignored            }        }        return platformConfig;    }        /**     * Persists a PlatformConfig advertisement under getStoreHome()+"/PlaformConfig"     * <p/>     * Home may be overridden by a call to setHome()     *      * @see #load     * @throws IOException If there is a failure saving the PlatformConfig.     */    public void save() throws IOException {        httpEnabled = (httpConfig.isClientEnabled() || httpConfig.isServerEnabled());        tcpEnabled = (tcpConfig.isClientEnabled() || tcpConfig.isServerEnabled());        ConfigParams advertisement = getPlatformConfig();        OutputStream out = null;                try {            if ("file".equalsIgnoreCase(storeHome.getScheme())) {                File saveDir = new File(storeHome);                saveDir.mkdirs();                // Sadly we can't use URL.openConnection() to create the                // OutputStream for file:// URLs. bogus.                out = new FileOutputStream(new File(saveDir, "PlatformConfig"));            } else {                out = storeHome.resolve("PlatformConfig").toURL().openConnection().getOutputStream();            }            XMLDocument aDoc = (XMLDocument) advertisement.getDocument(MimeMediaType.XMLUTF8);            OutputStreamWriter os = new OutputStreamWriter(out, "UTF-8");            aDoc.sendToWriter(os);            os.flush();        } finally {            if (null != out) {                out.close();            }        }    }        /**     * Returns a XMLDocument representation of an Advertisement     *     * @param enabled whether the param doc is enabled, adds a "isOff"     *                element if disabled     * @param adv     the Advertisement to retrieve the param doc from     * @return the parmDoc value     */    protected XMLDocument getParmDoc(boolean enabled, Advertisement adv) {        XMLDocument parmDoc = (XMLDocument) StructuredDocumentFactory.newStructuredDocument(MimeMediaType.XMLUTF8, "Parm");        XMLDocument doc = (XMLDocument) adv.getDocument(MimeMediaType.XMLUTF8);                StructuredDocumentUtils.copyElements(parmDoc, parmDoc, doc);        if (!enabled) {            parmDoc.appendChild(parmDoc.createElement("isOff"));        }        return parmDoc;    }        /**     * Creates an HTTP transport advertisement     *     * @return an HTTP transport advertisement     */    protected HTTPAdv createHttpAdv() {        httpConfig = (HTTPAdv) AdvertisementFactory.newAdvertisement(HTTPAdv.getAdvertisementType());        httpConfig.setProtocol("http");        httpConfig.setPort(9700);        httpConfig.setClientEnabled((mode & HTTP_CLIENT) == HTTP_CLIENT);        httpConfig.setServerEnabled((mode & HTTP_SERVER) == HTTP_SERVER);        return httpConfig;    }        /**     * Creates Personal Security Environment Config Advertisement     * <p/>The configuration advertisement can include an optional seed certificate     * chain and encrypted private key. If this seed information is present the PSE     * Membership Service will require an initial authentication to unlock the     * encrypted private key before creating the PSE keystore. The newly created     * PSE keystore will be "seeded" with the certificate chain and the private key.     *     * @param principal principal     * @param password  the password used to sign the private key of the root certificate     * @return PSEConfigAdv an PSE config advertisement     * @see net.jxta.impl.protocol.PSEConfigAdv     */    protected PSEConfigAdv createPSEAdv(String principal, String password) {        pseConf = (PSEConfigAdv) AdvertisementFactory.newAdvertisement(PSEConfigAdv.getAdvertisementType());        if (principal != null && password != null) {            IssuerInfo info = PSEUtils.genCert(principal, null);                        pseConf.setCertificate(info.cert);            pseConf.setPrivateKey(info.subjectPkey, password.toCharArray());        }        return pseConf;    }    /**     * Creates Personal Security Environment Config Advertisement     * <p/>The configuration advertisement can include an optional seed certificate     * chain and encrypted private key. If this seed information is present the PSE     * Membership Service will require an initial authentication to unlock the     * encrypted private key before creating the PSE keystore. The newly created     * PSE keystore will be "seeded" with the certificate chain and the private key.     *     * @param cert X509Certificate     * @return PSEConfigAdv an PSE config advertisement     * @see net.jxta.impl.protocol.PSEConfigAdv     */    protected PSEConfigAdv createPSEAdv(X509Certificate cert) {        pseConf = (PSEConfigAdv) AdvertisementFactory.newAdvertisement(PSEConfigAdv.getAdvertisementType());        if (subjectPkey != null && password != null) {            pseConf.setCertificate(cert);            pseConf.setPrivateKey(subjectPkey, password.toCharArray());        }        return pseConf;    }    /**     * Creates Personal Security Environment Config Advertisement     * <p/>The configuration advertisement can include an optional seed certificate     * chain and encrypted private key. If this seed information is present the PSE     * Membership Service will require an initial authentication to unlock the     * encrypted private key before creating the PSE keystore. The newly created     * PSE keystore will be "seeded" with the certificate chain and the private key.     *     * @param certificateChain X509Certificate[]     * @return PSEConfigAdv an PSE config advertisement     * @see net.jxta.impl.protocol.PSEConfigAdv     */    protected PSEConfigAdv createPSEAdv(X509Certificate[] certificateChain) {        pseConf = (PSEConfigAdv) AdvertisementFactory.newAdvertisement(PSEConfigAdv.getAdvertisementType());        if (subjectPkey != null && password != null) {            pseConf.setCertificateChain(certificateChain);            pseConf.setPrivateKey(subjectPkey, password.toCharArray());        }        return pseConf;    }        /**     * Creates a ProxyService configuration advertisement     *     * @return ProxyService configuration advertisement     */    protected XMLDocument createProxyAdv() {        return (XMLDocument) StructuredDocumentFactory.newStructuredDocument(MimeMediaType.XMLUTF8, "Parm");    }        /**     * Creates a RendezVousService configuration advertisement with default values (EDGE)     *     * @return a RdvConfigAdv     */    protected RdvConfigAdv createRdvConfigAdv() {        rdvConfig = (RdvConfigAdv) AdvertisementFactory.newAdvertisement(RdvConfigAdv.getAdvertisementType());        if (mode == RDV_AD_HOC) {            rdvConfig.setConfiguration(RendezVousConfiguration.AD_HOC);        } else if ((mode & RDV_CLIENT) == RDV_CLIENT) {            rdvConfig.setConfiguration(RendezVousConfiguration.EDGE);        } else if ((mode & RDV_SERVER) == RDV_SERVER) {            rdvConfig.setConfiguration(RendezVousConfiguration.RENDEZVOUS);        }        // A better alternative is to reference rdv service defaults (currently private)        // rdvConfig.setMaxClients(200);        return rdvConfig;    }        /**     * Creates a RelayService configuration advertisement with default values (EDGE)     *     * @return a RelayConfigAdv     */    protected RelayConfigAdv createRelayConfigAdv() {        relayConfig = (RelayConfigAdv) AdvertisementFactory.newAdvertisement(RelayConfigAdv.getAdvertisementType());        relayConfig.setUseOnlySeeds(false);        relayConfig.setClientEnabled((mode & RELAY_CLIENT) == RELAY_CLIENT || mode == EDGE_NODE);        relayConfig.setServerEnabled((mode & RELAY_SERVER) == RELAY_SERVER);        return relayConfig;    }        /**     * Creates an TCP transport advertisement with the platform default values.     * multicast on, 224.0.1.85:1234, with a max packet size of 16K     *     * @return a TCP transport advertisement     */    protected TCPAdv createTcpAdv() {        tcpConfig = (TCPAdv) AdvertisementFactory.newAdvertisement(TCPAdv.getAdvertisementType());        tcpConfig.setProtocol("tcp");        tcpConfig.setInterfaceAddress(null);        tcpConfig.setPort(9701);        tcpConfig.setStartPort(9701);        tcpConfig.setEndPort(9799);        tcpConfig.setMulticastAddr("224.0.1.85");        tcpConfig.setMulticastPort(1234);        tcpConfig.setMulticastSize(16384);        tcpConfig.setMulticastState((mode & IP_MULTICAST) == IP_MULTICAST);        tcpConfig.setServer(null);        tcpConfig.setClientEnabled((mode & TCP_CLIENT) == TCP_CLIENT);        tcpConfig.setServerEnabled((mode & TCP_SERVER) == TCP_SERVER);        return tcpConfig;    }        protected PeerGroupConfigAdv createInfraConfigAdv() {        infraPeerGroupConfig = (PeerGroupConfigAdv) AdvertisementFactory.newAdvertisement(                PeerGroupConfigAdv.getAdvertisementType());                NetGroupTunables tunables = new NetGroupTunables(ResourceBundle.getBundle("net.jxta.impl.config"), new NetGroupTunables());                infraPeerGroupConfig.setPeerGroupID(tunables.id);        infraPeerGroupConfig.setName(tunables.name);        infraPeerGroupConfig.setDesc(tunables.desc);                return infraPeerGroupConfig;    }        /**     * Returns a PlatformConfig which represents a platform configuration.     * <p/>Fine tuning is achieved through accessing each configured advertisement     * and achieved through accessing each configured advertisement and modifying     * each object directly.     *     * @return the PeerPlatformConfig Advertisement     */    public ConfigParams getPlatformConfig() {        PlatformConfig advertisement = (PlatformConfig) AdvertisementFactory.newAdvertisement(                PlatformConfig.getAdvertisementType());                advertisement.setName(name);        advertisement.setDescription(description);        if (peerid != null) {            advertisement.setPeerID(peerid);        }                if (tcpConfig != null) {            boolean enabled = tcpEnabled && (tcpConfig.isServerEnabled() || tcpConfig.isClientEnabled());            advertisement.putServiceParam(PeerGroup.tcpProtoClassID, getParmDoc(enabled, tcpConfig));        }                if (httpConfig != null) {            boolean enabled = httpEnabled && (httpConfig.isServerEnabled() || httpConfig.isClientEnabled());            advertisement.putServiceParam(PeerGroup.httpProtoClassID, getParmDoc(enabled, httpConfig));        }                if (relayConfig != null) {            boolean isOff = ((mode & RELAY_OFF) == RELAY_OFF) || (relayConfig.isServerEnabled() && relayConfig.isClientEnabled());            XMLDocument relayDoc = (XMLDocument) relayConfig.getDocument(MimeMediaType.XMLUTF8);                        if (isOff) {                relayDoc.appendChild(relayDoc.createElement("isOff"));            }            advertisement.putServiceParam(PeerGroup.relayProtoClassID, relayDoc);        }                if (rdvConfig != null) {            XMLDocument rdvDoc = (XMLDocument) rdvConfig.getDocument(MimeMediaType.XMLUTF8);            advertisement.putServiceParam(PeerGroup.rendezvousClassID, rdvDoc);        }                if (cert != null) {            pseConf = createPSEAdv(cert);        } else {            pseConf = createPSEAdv(principal, password);        }                if (pseConf != null) {            if (keyStoreLocation != null) {                if (keyStoreLocation.isAbsolute()) {                    pseConf.setKeyStoreLocation(keyStoreLocation);                } else {                    if (Logging.SHOW_WARNING && LOG.isLoggable(Level.WARNING)) {                        LOG.warning("Keystore location set, but is not absolute: " + keyStoreLocation);                    }                }            }            XMLDocument pseDoc = (XMLDocument) pseConf.getDocument(MimeMediaType.XMLUTF8);            advertisement.putServiceParam(PeerGroup.membershipClassID, pseDoc);        }                if (proxyConfig != null && ((mode & PROXY_SERVER) == PROXY_SERVER)) {            advertisement.putServiceParam(PeerGroup.proxyClassID, proxyConfig);        }        if ((null != infraPeerGroupConfig) && (null != infraPeerGroupConfig.getPeerGroupID())                && (ID.nullID != infraPeerGroupConfig.getPeerGroupID())                && (PeerGroupID.defaultNetPeerGroupID != infraPeerGroupConfig.getPeerGroupID())) {            advertisement.setSvcConfigAdvertisement(PeerGroup.peerGroupClassID, infraPeerGroupConfig);        }        return advertisement;    }        /**     *  @param location The location of the platform config.     *  @return The platformConfig     *  @throws IOException Thrown for failures reading the PlatformConfig.     */    private PlatformConfig read(URI location) throws IOException {        URL url;                try {            url = location.toURL();        } catch (MalformedURLException mue) {            IllegalArgumentException failure = new IllegalArgumentException("Failed to convert URI to URL");            failure.initCause(mue);            throw failure;        }                InputStream input = url.openStream();        try {            XMLDocument document = (XMLDocument) StructuredDocumentFactory.newStructuredDocument(MimeMediaType.XMLUTF8, input);            PlatformConfig platformConfig = (PlatformConfig) AdvertisementFactory.newAdvertisement(document);            return platformConfig;        } finally {            input.close();        }    }        /**     * Holds the construction tuna

⌨️ 快捷键说明

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