📄 networkconfigurator.java
字号:
if (httpChilds.hasMoreElements()) { param = (XMLElement) httpChilds.nextElement(); } else { throw new IllegalStateException("Missing HTTP Advertisment"); } // Read-in the adv as it is now. httpConfig = (HTTPAdv) AdvertisementFactory.newAdvertisement(param); /*if (httpConfig.isClientEnabled()) { mode = mode | HTTP_CLIENT; } if (httpConfig.isServerEnabled()) { mode = mode | HTTP_SERVER; } */ } catch (Exception failure) { IOException ioe = new IOException("error processing the HTTP config advertisement"); ioe.initCause(failure); throw ioe; } // Logging settings dftDebugLevel = platformConfig.getDebugLevel(); //ProxyService try { param = (XMLElement) platformConfig.getServiceParam(PeerGroup.proxyClassID); if ((param != null && !param.getChildren("isOff").hasMoreElements())) { mode = mode | PROXY_SERVER; } } catch (Exception failure) { IOException ioe = new IOException("error processing the pse config advertisement"); ioe.initCause(failure); throw ioe; } // Rendezvous try { param = (XMLElement) platformConfig.getServiceParam(PeerGroup.rendezvousClassID); //backwards compatibility param.addAttribute("type", RdvConfigAdv.getAdvertisementType()); rdvConfig = (RdvConfigAdv) AdvertisementFactory.newAdvertisement(param); if (rdvConfig.getConfiguration() == RendezVousConfiguration.AD_HOC) { mode = mode | RDV_AD_HOC; } else if (rdvConfig.getConfiguration() == RendezVousConfiguration.EDGE) { mode = mode | RDV_CLIENT; } else if (rdvConfig.getConfiguration() == RendezVousConfiguration.RENDEZVOUS) { mode = mode | RDV_SERVER; } } catch (Exception failure) { IOException ioe = new IOException("error processing the rendezvous config advertisement"); ioe.initCause(failure); throw ioe; } // Relay try { param = (XMLElement) platformConfig.getServiceParam(PeerGroup.relayProtoClassID); if (param != null && !param.getChildren("isOff").hasMoreElements()) { mode = mode | RELAY_OFF; } //backwards compatibility param.addAttribute("type", RelayConfigAdv.getAdvertisementType()); relayConfig = (RelayConfigAdv) AdvertisementFactory.newAdvertisement(param); /*if (relayConfig.isClientEnabled()) { mode = mode | RELAY_CLIENT; } if (relayConfig.isServerEnabled()) { mode = mode | RELAY_SERVER; } */ } 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; } else { throw new CertificateException("Unexpected membership advertisement " + adv.getAdvertisementType()); } } } /** * Persists a PlatformConfig advertisement under getHome()+"/PlaformConfig" * <p/> * Home may be overridden by a call to setHome() * @see #load * @exception IOException if an i/o error occurs */ public void save() throws IOException { httpEnabled = (httpConfig.isClientEnabled() || httpConfig.isServerEnabled()); tcpEnabled = (tcpConfig.isClientEnabled() || tcpConfig.isServerEnabled()); ConfigParams advertisement = getPlatformConfig(); FileOutputStream out = null; home.mkdirs(); File saveFile = new File(home, "PlatformConfig"); out = new FileOutputStream(saveFile); XMLDocument aDoc = (XMLDocument) advertisement.getDocument(MimeMediaType.XMLUTF8); OutputStreamWriter os = new OutputStreamWriter(out, "UTF-8"); aDoc.sendToWriter(os); os.flush(); if (null != out) { out.close(); } if (configProps != null) { File propfile = new File(home, "config.properties"); FileOutputStream fos = new FileOutputStream(propfile); configProps.store(fos, "# Infrastructure group"); fos.close(); } PeerGroupFactory.setConfiguratorClass(net.jxta.impl.peergroup.NullConfigurator.class); } /** * Returns a StructuredDocument 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 StructuredDocument getParmDoc(boolean enabled, Advertisement adv) { StructuredDocument parmDoc = StructuredDocumentFactory.newStructuredDocument(MimeMediaType.XMLUTF8, "Parm"); StructuredDocument doc = (StructuredDocument) 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 Enviroment 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 prviate key before creating the PSE keystore. The newly created * PSE keystore will be "seeded" with the certificate chain and the private key. * * @see net.jxta.impl.protocol.PSEConfigAdv * @param principal principal * @param password the password used to sign the private key of the root certificate * @return PSEConfigAdv an PSE config advertisement */ 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 Enviroment 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 prviate key before creating the PSE keystore. The newly created * PSE keystore will be "seeded" with the certificate chain and the private key. * * @see net.jxta.impl.protocol.PSEConfigAdv * @param cert X509Certificate * @return PSEConfigAdv an PSE config advertisement */ 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 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; } /** * 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); } advertisement.setDebugLevel(dftDebugLevel); if (tcpConfig != null) { advertisement.putServiceParam(PeerGroup.tcpProtoClassID, getParmDoc(tcpEnabled, tcpConfig)); } if (httpConfig != null) { advertisement.putServiceParam(PeerGroup.httpProtoClassID, getParmDoc(httpEnabled, 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) { 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); } return advertisement; } /** * the main program for the NetworkConfigurator class * intended for illustration purposes only * * @param args the command line arguments * public static void main(String[] args) { File home = new File(System.getProperty("JXTA_HOME", ".jxta")); File instanceHome = new File(home, "main"); instanceHome.mkdirs(); NetworkConfigurator config = new NetworkConfigurator(EDGE_NODE, instanceHome.toURI()); if (config.exists()) { File pc = new File(config.getHome(), "PlatformConfig"); try { config.load(pc.toURI()); config.setName("new name"); config.save(); } catch (CertificateException ce) { ce.printStackTrace(); try { config.setPrincipal("username"); config.setPassword("password"); config.setName("new name/pse recovered"); config.save(); } catch (Exception e) { e.printStackTrace(); } } catch (IOException io) { io.printStackTrace(); } } else { // the following line generates a new peerid, use a predefined pid instead // config.setPeerID(IDFactory.newPeerID(PeerGroupID.defaultNetPeerGroupID)); config.setName("peer"); config.setMode(RDV_RELAY_PROXY_NODE); config.setPrincipal("username"); config.setPassword("password"); config.setDescription("Configuration tutorial"); config.setTcpStartPort(9841); config.setTcpEndPort(9999); config.addSeedRelay(URI.create("tcp://192.18.37.37:9701")); config.addSeedRendezvous(URI.create("tcp://192.18.37.37:9701")); config.addRdvSeedingURI(URI.create("http://rdv.jxtahosts.net/cgi-bin/rendezvous.cgi?2")); config.addRelaySeedingURI(URI.create("http://rdv.jxtahosts.net/cgi-bin/relays.cgi?2")); try { // this overwrites any existing configuration config.save(); } catch (IOException io) { io.printStackTrace(); } } try { PeerGroup netPeerGroup = PeerGroupFactory.newNetPeerGroup(); } catch (net.jxta.exception.PeerGroupException pge) { pge.printStackTrace(); } } */}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -