📄 networkconfigurator.java
字号:
/* * Copyright (c) 2006-2007 Sun Microsystems, Inc. All rights reserved. * * The Sun Project JXTA(TM) Software License * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * 3. The end-user documentation included with the redistribution, if any, must * include the following acknowledgment: "This product includes software * developed by Sun Microsystems, Inc. for JXTA(TM) technology." * Alternately, this acknowledgment may appear in the software itself, if * and wherever such third-party acknowledgments normally appear. * * 4. The names "Sun", "Sun Microsystems, Inc.", "JXTA" and "Project JXTA" must * not be used to endorse or promote products derived from this software * without prior written permission. For written permission, please contact * Project JXTA at http://www.jxta.org. * * 5. Products derived from this software may not be called "JXTA", nor may * "JXTA" appear in their name, without prior written permission of Sun. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SUN * MICROSYSTEMS OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * JXTA is a registered trademark of Sun Microsystems, Inc. in the United * States and other countries. * * Please see the license information page at : * <http://www.jxta.org/project/www/license.html> for instructions on use of * the license in source files. * * ==================================================================== * * This software consists of voluntary contributions made by many individuals * on behalf of Project JXTA. For more information on Project JXTA, please see * http://www.jxta.org. * * This license is based on the BSD license adopted by the Apache Foundation. */package net.jxta.platform;import net.jxta.document.Advertisement;import net.jxta.document.AdvertisementFactory;import net.jxta.document.MimeMediaType;import net.jxta.document.StructuredDocumentFactory;import net.jxta.document.StructuredDocumentUtils;import net.jxta.document.XMLDocument;import net.jxta.document.XMLElement;import net.jxta.endpoint.EndpointAddress;import net.jxta.id.ID;import net.jxta.id.IDFactory;import net.jxta.impl.membership.pse.PSEUtils;import net.jxta.impl.membership.pse.PSEUtils.IssuerInfo;import net.jxta.impl.protocol.HTTPAdv;import net.jxta.impl.protocol.PSEConfigAdv;import net.jxta.impl.protocol.PeerGroupConfigAdv;import net.jxta.impl.protocol.PlatformConfig;import net.jxta.impl.protocol.RdvConfigAdv;import net.jxta.impl.protocol.RdvConfigAdv.RendezVousConfiguration;import net.jxta.impl.protocol.RelayConfigAdv;import net.jxta.impl.protocol.TCPAdv;import net.jxta.logging.Logging;import net.jxta.peer.PeerID;import net.jxta.peergroup.PeerGroup;import net.jxta.peergroup.PeerGroupID;import net.jxta.protocol.ConfigParams;import net.jxta.protocol.TransportAdvertisement;import javax.security.cert.CertificateException;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.io.OutputStreamWriter;import java.net.MalformedURLException;import java.net.URI;import java.net.URL;import java.security.PrivateKey;import java.security.cert.X509Certificate;import java.util.Enumeration;import java.util.List;import java.util.MissingResourceException;import java.util.NoSuchElementException;import java.util.PropertyResourceBundle;import java.util.ResourceBundle;import java.util.Set;import java.util.logging.Level;import java.util.logging.Logger;/** * NetworkConfigurator provides a simple programmatic interface for JXTA configuration. * <p/> * By default, it defines an edge configuration with TCP in auto mode w/port * range 9701-9799, multicast enabled on group "224.0.1.85", and port 1234, * HTTP transport with only outgoing enabled. * <p/> * By default a new PeerID is always generated. This can be overridden via * {@link NetworkConfigurator#setPeerID} method or loading a PlatformConfig via * {@link NetworkConfigurator#load}. * <p/> * A facility is provided to initialize a configuration by loading from an * existing configuration. This provides limited platform configuration lifecycle * management as well as configuration change management. * <p/> * Also by default, this class sets the default platform configurator to * {@link net.jxta.impl.peergroup.NullConfigurator}. <code>NullConfigurator<code> * is a no operation configurator intended to prevent any other configurators from * being invoked, including the AWT ConfigDialog. * <p/> * NetworkConfigurator makes use of classes from the {@code net.jxta.impl.*} * packages. Applications are very strongly encouraged to avoid importing these * classes as their interfaces may change without notice in future JXTA releases. * The NetworkConfigurator API abstracts the configuration implementation details * and will provide continuity and stability i.e. the NetworkConfigurator API * won't change and it will automatically accommodate changes to service * configuration. * <p/> * <em> Configuration example :</em> * <pre> * NetworkConfigurator config = new NetworkConfigurator(); * if (!config.exists()) { * // Create a new configuration with a new name, principal, and pass * config.setName("New Name"); * config.setPrincipal("username"); * config.setPassword("password"); * try { * //persist it * config.save(); * } catch (IOException io) { * // deal with the io error * } * } else { * // Load the pre-existing configuration * File pc = new File(config.getHome(), "PlatformConfig"); * try { * config.load(pc.toURI()); * // make changes if so desired * .. * .. * // store the PlatformConfig under the default home * config.save(); * } catch (CertificateException ce) { * // In case the root cert is invalid, this creates a new one * try { * //principal * config.setPrincipal("principal"); * //password to encrypt private key with * config.setPassword("password"); * config.save(); * } catch (Exception e) { * e.printStackTrace(); * } * } * <p/> * </pre> * * @since JXTA JSE 2.4 */public class NetworkConfigurator { /** * Logger */ private final static transient Logger LOG = Logger.getLogger(NetworkConfigurator.class.getName()); // begin configuration modes /** * Relay off Mode */ public final static int RELAY_OFF = 1 << 2; /** * Relay client Mode */ public final static int RELAY_CLIENT = 1 << 3; /** * Relay Server Mode */ public final static int RELAY_SERVER = 1 << 4; /** * Proxy Server Mode */ public final static int PROXY_SERVER = 1 << 5; /** * TCP transport client Mode */ public final static int TCP_CLIENT = 1 << 6; /** * TCP transport Server Mode */ public final static int TCP_SERVER = 1 << 7; /** * HTTP transport client Mode */ public final static int HTTP_CLIENT = 1 << 8; /** * HTTP transport server Mode */ public final static int HTTP_SERVER = 1 << 9; /** * IP multicast transport Mode */ public final static int IP_MULTICAST = 1 << 10; /** * RendezVousService Mode */ public final static int RDV_SERVER = 1 << 11; /** * RendezVousService Client */ public final static int RDV_CLIENT = 1 << 12; /** * RendezVousService Ad-Hoc mode */ public final static int RDV_AD_HOC = 1 << 13; /** * Default AD-HOC configuration */ public final static int ADHOC_NODE = TCP_CLIENT | TCP_SERVER | IP_MULTICAST | RDV_AD_HOC | RELAY_OFF; /** * Default Edge configuration */ public final static int EDGE_NODE = TCP_CLIENT | TCP_SERVER | HTTP_CLIENT | IP_MULTICAST | RDV_CLIENT | RELAY_CLIENT; /** * Default Rendezvous configuration */ public final static int RDV_NODE = RDV_SERVER | TCP_CLIENT | TCP_SERVER | HTTP_SERVER; /** * Default Relay configuration */ public final static int RELAY_NODE = RELAY_SERVER | TCP_CLIENT | TCP_SERVER | HTTP_SERVER; /** * Default Proxy configuration */ public final static int PROXY_NODE = PROXY_SERVER | RELAY_NODE; /** * Default Rendezvous/Relay/Proxy configuration */ public final static int RDV_RELAY_PROXY_NODE = RDV_NODE | PROXY_NODE; // end configuration modes /** * Default mode */ protected transient int mode = EDGE_NODE; /** * Default PlatformConfig Peer Description */ protected transient String description = "Platform Config Advertisement created by : " + NetworkConfigurator.class.getName(); /** * The location which will serve as the parent for all stored items used * by JXTA. */ private transient URI storeHome = null; /** * Default peer name */ protected transient String name = "unknown"; /** * Password value used to generate root Certificate and to protect the * Certificate's PrivateKey. */ protected transient String password = null; /** * Default PeerID */ protected transient PeerID peerid = IDFactory.newPeerID(PeerGroupID.defaultNetPeerGroupID); /** * Principal value used to generate root certificate */ protected transient String principal = null; /** * Public Certificate chain */ protected transient X509Certificate[] cert = null; /** * Subject private key */ protected transient PrivateKey subjectPkey = null; /** * Freestanding keystore location */ protected transient URI keyStoreLocation = null; /** * Proxy Service Document */ protected transient XMLElement proxyConfig; /** * Personal Security Environment Config Advertisement * * @see net.jxta.impl.membership.pse.PSEConfig */ protected transient PSEConfigAdv pseConf; /** * Rendezvous Config Advertisement */ protected transient RdvConfigAdv rdvConfig; /** * Default Rendezvous Seeding URI */ protected URI rdvSeedingURI = null; /** * Relay Config Advertisement */ protected transient RelayConfigAdv relayConfig; /** * Default Relay Seeding URI */ protected transient URI relaySeedingURI = null; /** * TCP Config Advertisement */ protected transient TCPAdv tcpConfig; /** * Default TCP transport state */ protected transient boolean tcpEnabled = true; /**
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -