📄 networkconfigurator.java
字号:
/* * Copyright (c) 2006 Sun Microsystems, Inc. All rights reserved. * * 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 the * Sun Microsystems, Inc. for Project JXTA." * 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. * ==================================================================== * * 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. * * $Id: NetworkConfigurator.java,v 1.4 2006/09/27 01:34:24 hamada Exp $ */package net.jxta.platform;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStreamWriter;import java.net.MalformedURLException;import java.net.URI;import java.net.URL;import java.security.PrivateKey;import java.security.cert.Certificate;import java.security.cert.X509Certificate;import java.util.Enumeration;import java.util.List;import java.util.NoSuchElementException;import java.util.Properties;import java.security.cert.X509Certificate;import javax.security.cert.CertificateException;import net.jxta.document.Advertisement;import net.jxta.document.AdvertisementFactory;import net.jxta.document.MimeMediaType;import net.jxta.document.StructuredDocument;import net.jxta.document.StructuredDocumentFactory;import net.jxta.document.StructuredDocumentUtils;import net.jxta.document.XMLDocument;import net.jxta.document.XMLElement;import net.jxta.id.ID;import net.jxta.id.IDFactory;/* * These imports are required in order to generate configurations for the core JXTA * services. It's strongly suggested that applications avoid importing classes * from the "net.jxta.impl.*" packages. */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.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.peer.PeerID;import net.jxta.peergroup.PeerGroup;import net.jxta.peergroup.PeerGroupFactory;import net.jxta.peergroup.PeerGroupID;import net.jxta.protocol.ConfigParams;import net.jxta.protocol.TransportAdvertisement;import org.apache.log4j.Level;import org.apache.log4j.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 going 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 a * 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 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 configruation implementation details * and will provide continuity and stability i.e. the NetworkConfigurator API * won't change and it will automatically accomodate changes to service * configuration. * * <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(); * } * } * * </pre> */public class NetworkConfigurator { // begin configuration modes /** * Relay client Mode */ public final static int RELAY_OFF = 1 << 2; /** * Relay client Mode */ public final static int RELAY_CLIENT = 1 << 3; /** * Relay client Mode */ public final static int RELAY_SERVER = 1 << 4; /** * Proxy Server Mode */ public final static int PROXY_SERVER = 1 << 5; /** * TCP transport client state */ public final static int TCP_CLIENT = 1 << 6; /** * TCP transport state */ public final static int TCP_SERVER = 1 << 7; /** * HTTP transport client state */ public final static int HTTP_CLIENT = 1 << 8; /** * HTTP transport server state */ public final static int HTTP_SERVER = 1 << 9; /** * IP multicast transport state */ public final static int IP_MULTICAST = 1 << 10; /** * Rendezvous 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 /** * config.properties */ protected transient Properties configProps = null; /** * Default mode */ protected transient int mode = EDGE_NODE; /** * Default PlatformConfig Peer Description */ protected transient String description = "Platform Config Advertisement created by : " + NetworkConfigurator.class.getName(); /** * default debug level. Logging has been deprecated within the PlatformConfig. See {@link net.jxta.logging.Logging}. */ private transient String dftDebugLevel = "OFF"; /** * Default JXTA_HOME set ".jxta" */ protected transient File home = new File(".jxta"); /** * the location which will serve as the parent for all stored items used * by JXTA. */ private static URI storeHome = null; /** * HTTP Config Advertisement */ protected transient HTTPAdv httpConfig; /** * default HTTP transport state */ protected transient boolean httpEnabled = true; /** * Default peer name */ protected transient String name = "unknown"; /** * Password value used to generate root centificate */ 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 */ protected transient X509Certificate cert = null; /** * Subject private key */ protected transient PrivateKey subjectPkey = null; /** * Proxy Service Document */ protected transient XMLElement proxyConfig; /** * Personal Security Enviroment 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; /** * Platform Configuration file */ protected transient File configFile = new File(home, "PlatformConfig"); private final static Logger LOG = Logger.getLogger(NetworkConfigurator.class.getName()); /** * Creates NetworkConfigurator instance with default AD-HOC configuration * * @param storeHome the URI to persistent store * @return NetworkConfigurator instance with default AD-HOC configuration */ public static NetworkConfigurator newAdHocConfiguration(URI storeHome) { return new NetworkConfigurator(ADHOC_NODE, storeHome); } /** * Creates NetworkConfigurator instance with default Edge configuration * * @param storeHome the URI to persistent store * @return NetworkConfigurator instance with default AD-HOC configuration */ public static NetworkConfigurator newEdgeConfiguration(URI storeHome) { return new NetworkConfigurator(EDGE_NODE, storeHome); } /** * Creates NetworkConfigurator instance with default Rendezvous configuration * * @param storeHome the URI to persistent store * @return NetworkConfigurator instance with default Rendezvous configuration */ public static NetworkConfigurator newRdvConfiguration(URI storeHome) { return new NetworkConfigurator(RDV_NODE, storeHome); } /** * Creates NetworkConfigurator instance with default Relay configuration * * @param storeHome the URI to persistent store * @return NetworkConfigurator instance with default Relay configuration */ public static NetworkConfigurator newRelayConfiguration(URI storeHome) { return new NetworkConfigurator(RELAY_NODE, storeHome); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -