?? configurator.java
字號:
/* * Copyright (c) 2001 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 materialsset 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 THE APACHE SOFTWARE FOUNDATION 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: Configurator.java,v 1.213 2006/06/19 18:27:36 bondolo Exp $ */package net.jxta.ext.config;import net.jxta.ext.config.optimizers.RelayOptimizer;import java.io.File;import java.io.FileNotFoundException;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.FileReader;import java.io.InputStreamReader;import java.io.Reader;import java.io.IOException;import java.io.ObjectInput;import java.io.ObjectOutput;import java.io.StringWriter;import java.lang.reflect.UndeclaredThrowableException;import java.net.MalformedURLException;import java.net.URI;import java.net.URISyntaxException;import java.net.URL;import java.security.cert.Certificate;import java.security.cert.X509Certificate;import java.text.MessageFormat;import java.util.Arrays;import java.util.ArrayList;import java.util.Collections;import java.util.Enumeration;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.Map;import java.util.Map.Entry;import java.util.NoSuchElementException;import java.util.Properties;import net.jxta.document.AdvertisementFactory;import net.jxta.document.Attributable;import net.jxta.document.Attribute;import net.jxta.document.Element;import net.jxta.document.MimeMediaType;import net.jxta.document.StructuredDocument;import net.jxta.document.StructuredDocumentFactory;import net.jxta.document.StructuredDocumentUtils;import net.jxta.document.StructuredTextDocument;import net.jxta.document.TextElement;import net.jxta.document.XMLDocument;import net.jxta.exception.ConfiguratorException;import net.jxta.id.ID;import net.jxta.id.IDFactory;import net.jxta.impl.endpoint.IPUtils;import net.jxta.impl.membership.pse.PSEUtils;import net.jxta.impl.peergroup.PlatformConfigurator;import net.jxta.impl.protocol.HTTPAdv;import net.jxta.impl.protocol.PlatformConfig;import net.jxta.impl.protocol.PSEConfigAdv;import net.jxta.impl.protocol.RdvConfigAdv;import net.jxta.impl.protocol.RelayConfigAdv;import net.jxta.impl.protocol.TCPAdv;import net.jxta.protocol.ConfigParams;import net.jxta.peer.PeerID;import net.jxta.peergroup.PeerGroupFactory;import net.jxta.peergroup.PeerGroupID;import net.jxta.protocol.TransportAdvertisement;import org.apache.log4j.Level;import org.apache.log4j.Logger;/** * A Configurator that is capable of instantiating and persisting JXTA configuration * state based upon declarative {@link net.jxta.ext.config.Profile} and {@link net.jxta.impl.protocol.PlatformConfig} * information. * * <p>A Configurator serves primarily as a JXTA Configuration Bean, or property * sheet, and implements very little attribute association logic beyond that of * what is required to perform fundamental configuration integrity validation. * * <p>The principal constituents of a JXTA Configurator are: * * <ul> * <li>Peer Information</li> * <li>Peer Transports</li> * <li>Peer Services</li> * <li>JXTA Network</li> * <li>Configuration Extensions</li> * </ul> * * <p>Peer Information describes the local JXTA instance that a specified * Configurator instance represents. The bulk of the Peer Information * configuration data is optional and includes: * * <ul> * <li>name</li> * <li>ID</li> * <li>descriptor</li> * <li>JXTA Home</li> * <li>Log4J trace level</li> * <li>security</li> * <li>root certificate</li> * <li>HTTP Proxy</li> * </ul> * * <p>Peer Transports describe the physical {@link net.jxta.ext.config.Address} with which a Peer * connects with the overall JXTA Network. Transports can be specified as * incoming (aka server), outgoing (aka client) and both. Transport * implementations include: * * <ul> * <li>TCP</li> * <li>HTTP</li> * </ul> * * <p>Peer Services represent the JXTA Services that a specified Configurator * instance will provision, which include: * * <ul> * <li>{@link net.jxta.rendezvous.RendezVousService RendezVous}</li> * <li>Relay</li> * <li>{@link net.jxta.impl.proxy.ProxyService Proxy}</li> * </ul> * * <p>The JXTA Network information specifies JXTA Network Services upon which a * specified Configurator instance will rely upon, including: * * <ul> * <li>{@link net.jxta.rendezvous.RendezVousService RendezVous}</li> * <li>Relays</li> * </ul> * * <p>The Configuration Extensions information includes extensible configuration * features that includes end user provided {@link net.jxta.ext.config.Optimizer}. * * <p>Configuration information can be derived from declarative {@link net.jxta.ext.config.Profile}, * {@link net.jxta.impl.protocol.PlatformConfig}, or programatically via provided APIs. * Further, a combination of all of the above is also possible and often the most * practical. * * <p>The Configurator API which is predominately comprised of a series of property * sheet getter/setter calls in addition to PlatformConfig creation and persistence. * * <p>A Configurator can be optionally be instantiated by from a {@link net.jxta.ext.config.Profile}, * which is, by and large, a toolable and person-parsible serialized construct in * the form of XML. A {@link net.jxta.ext.config.Profile} basically drives a series of Configurator * API invocations. * * <p>A {@link net.jxta.ext.config.ui.Configurator Configurator UI} is also available, * that provides a series of reusable Configuration UI components that, in turn, * leverage the afore mentioned Profile constructs. * * <p>Prior to {@link net.jxta.impl.protocol.PlatformConfig} generation, the current * Configurator state is exercised via normalization, optimization and validation * processes, the later of which can throw a {@link net.jxta.exception.ConfiguratorException}. * The normalization process massages the configuration state by filling in missing * or incomplete information necessary for further processing. The optimization * process performs a series of pluggable optimizer exercises, as implemented * via the {@link net.jxta.ext.config.Optimizer} interface, that tune and adjust the normalized * configuration state based on the provided optimization heuristics. Lastly, the * validation process performs integrity checks against the current configuration * state just prior to {@link net.jxta.impl.protocol.PlatformConfig} generation. * * <p>The derived {@link net.jxta.impl.protocol.PlatformConfig} instance is then * returned to the Configurator invoker. The {@link net.jxta.ext.config.Configurator#save()} methods will perist * the {@link net.jxta.impl.protocol.PlatformConfig} to the file system. * * @author james todd [gonzo at jxta dot org] */public class Configuratorimplements PlatformConfigurator { private final static String COLON = ":"; private final static String SLASH = "/";// private final static String BRACKET_OPEN = "[";// private final static String BRACKET_CLOSE = "]"; private final static String PARENTHESIS_OPEN = "("; private final static String PARENTHESIS_CLOSE = ")"; private final static String EMPTY_STRING = ""; private final static String NEW_LINE = "\n"; private final static String SPACE = " "; private final static String EMPTY_SPACE = ""; private final static String SPACE_REGEX = "\\s"; private final static String TAB = SPACE + SPACE; private final static int MILLISECONDS_PER_SECOND = 1000; private final static char NULL_CHAR = '\0'; private static final String JXTA_URI_PREFIX = ID.URIEncodingName + COLON + ID.URNNamespace + COLON; private final static Logger LOG = Logger.getLogger(Configurator.class.getName()); private URI home = Env.JXTA_HOME.toURI(); private String descriptor = Default.PEER_DESCRIPTOR; private String peerName = null; private String peerDescription = null; private Trace trace = Trace.DEFAULT; private PeerID peerId = null; private boolean isSecurity = Default.SECURITY_IS_ENABLED; private String principal = null; private String password = null; private URI rootCertificateAddress = null; private PSEConfigAdv pse = null; private URI peerProxy = null; private PeerGroupID infrastructureGroupId = null; private String infrastructureGroupName = null; private String infrastructureGroupDescription = null; private URI rendezVousBootstrap = Default.RENDEZVOUS_BOOTSTRAP_ADDRESS; private URI relaysBootstrap = Default.RELAYS_BOOTSTRAP_ADDRESS; private boolean isRelaysDiscovery = Default.RELAYS_DISCOVERY_IS_ENABLED; private List transports = null; private RelayConfigAdv relayConfig = null; private int endpointQueueSize = -1; private boolean isProxyEnabled = Default.PROXY_SERVICE_IS_ENABLED; private RdvConfigAdv rdvConfig = null; private List optimizers = null; private List validators = null; private PlatformConfig platformConfig = null; private final Map customParams = new HashMap(); /** * @deprecated will be removed when {@link net.jxta.peergroup.PeerGroupFactory} * defaults to the {@link net.jxta.impl.peergroup.NullConfigurator} */ public static void setConfigurator(Class configurator) { if (configurator != null && PlatformConfigurator.class.isAssignableFrom(configurator)) { try { PeerGroupFactory.setConfiguratorClass(configurator); } catch (Exception e) { throw new IllegalArgumentException("invalid configurator", e); } } else { throw new IllegalArgumentException("invalid configurator: " + (configurator != null ? configurator.getName() : null)); } } /** * Convenience method which constructs a {@link java.net.URI} that represents * the local ANY/ALL interface(s). * * @param base provided address * @return ANY/ALL interface */ public static URI toAllAddresses(URI base) { URI u = null; URI b = base; String a = Env.ALL_ADDRESSES.getHostAddress(); if (b != null) { try { u = new URI(b.getScheme(), EMPTY_STRING, a, b.getPort(), b.getPath(), b.getPath(), b.getFragment()); } catch (URISyntaxException use) { if (LOG.isEnabledFor(Level.ERROR)) { LOG.error("invalid transformation", use); } } } else { try { u = new URI(Default.ANY_TCP_ADDRESS.getScheme(), EMPTY_STRING, a, Default.TCP_PORT, EMPTY_STRING, EMPTY_STRING, EMPTY_STRING); } catch (URISyntaxException use) { if (LOG.isEnabledFor(Level.ERROR)) { LOG.error("invalid transformation", use); } } } return u; } // xxx: we will likely continue to need a no-arg constructor /** * Default constructor. * * <p>The default JXTA persistence directory will be set to the value of: * * <pre> * System.getProperty("user.home", "/.jxta"); * </pre> * * <p>Existing {@link net.jxta.ext.config.Profile} and {@link net.jxta.impl.protocol.PlatformConfig} * files will be parsed in succession in order to contextually instantiate * the newly create Configurator, enabling configuration update processing. * In the event a "profile.xml" file does not exist in the JXTA persistence * directory, the default {@link net.jxta.ext.config.Profile#DEFAULT} profile will be used to * establish a baseline configuration. * * @deprecated It's recommended to use the four params constructor */ public Configurator() { this(Env.JXTA_HOME.toURI()); } /** * Constructor whereby one can specify a non-default JXTA persistence directory * value as a {@link java.net.URI}, typically of scheme "file" with which to * establish the initial context. * * @param home JXTA persistence directory destination. */ public Configurator(URI home) { this(home, null, null, null, null); } // xxx: ?deprecation candidate? /** * Constructor whereby one can specify a peer name and password. * * @param name peer name * @param password peer password */ public Configurator(String name, String password) { this(Env.JXTA_HOME.toURI(), name, password); } // xxx: ?deprecation candidate? /** * Constructor whereby one can specify an alternative JXTA persistence directory * along with the peer name and password with which to establish the inital * context. * * @param home JXTA persistence directory destination * @param name peer name * @param password peer password */ public Configurator(URI home, String name, String password) { this(home, name, null, name, password); } /** * Constructor whereby one can specify a peer name, principal and password * with which to establish the initial context. * * @deprecated Constructors specifying 'principal' are deprecated. * * @param name peer name * @param principal peer principal * @param password peer password */ public Configurator(String name, String principal, String password) { this(Env.JXTA_HOME.toURI(), name, principal, password); } /** * Constructor whereby one can specify a peer name, description, principal * and password with which to establish the initial context. *
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -