📄 netpeergroupfactory.java
字号:
/* * Copyright (c) 2001-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.peergroup;import net.jxta.document.MimeMediaType;import net.jxta.document.StructuredDocumentFactory;import net.jxta.document.XMLElement;import net.jxta.exception.PeerGroupException;import net.jxta.id.ID;import net.jxta.id.IDFactory;import net.jxta.logging.Logging;import net.jxta.protocol.ConfigParams;import net.jxta.protocol.ModuleImplAdvertisement;import net.jxta.impl.protocol.PeerGroupConfigAdv;import net.jxta.impl.peergroup.GenericPeerGroup;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.net.URI;import java.util.MissingResourceException;import java.util.PropertyResourceBundle;import java.util.ResourceBundle;import java.util.logging.Level;import java.util.logging.Logger;/** * A factory for instantiating a Network Peer Group instances. The Network Peer * Group is the base peer group for applications and services within the JXTA * network. Most applications and services will instantiate their own peer * groups using the Network Peer Group as a base. * <p/> * A non-default configuration of <em>The Network Peer Group</em> may be * set-up by the administrator in charge of the network domain inside which the * peer is starting. <em>The Network Peer Group</em> may be discovered via the * JXTA Discovery protocol. Many such groups may be configured by an * administrator. * * @since JXTA JSE 2.4 * * @see net.jxta.peergroup.PeerGroup * @see net.jxta.peergroup.WorldPeerGroupFactory */public final class NetPeerGroupFactory { /** * Logger */ private final static transient Logger LOG = Logger.getLogger(NetPeerGroupFactory.class.getName()); /** * Our strong reference to the net peer group. */ private final PeerGroup net; /** * Instantiates the Net Peer Group using the ConfigParams found in the * directory specified by the {@code JXTA_HOME} system property or the * "{@code .jxta/}" directory if {@code JXTA_HOME} is not defined. * <p/> * This constructor is provided primarily for backwards compatibility. * Though not deprecated this method should be considered as sample code * only and the other constructors should be used whenever possible. * * @throws PeerGroupException Thrown for problems constructing the Net Peer * Group. */ public NetPeerGroupFactory() throws PeerGroupException { WorldPeerGroupFactory world = new WorldPeerGroupFactory(); PeerGroup worldGroup = world.getInterface(); NetGroupTunables tunables; try { ConfigParams cp = worldGroup.getConfigAdvertisement(); PeerGroupConfigAdv netGroupConfig = (PeerGroupConfigAdv) cp.getSvcConfigAdvertisement(PeerGroup.peerGroupClassID); if (null == netGroupConfig) { tunables = new NetGroupTunables(ResourceBundle.getBundle("net.jxta.impl.config"), new NetGroupTunables()); // load overides from "${JXTA_HOME}config.properties". URI storeHome = worldGroup.getStoreHome(); if (null != storeHome) { try { File configProperties = new File(new File(storeHome), "config.properties"); ResourceBundle rsrcs = new PropertyResourceBundle(new FileInputStream(configProperties)); tunables = new NetGroupTunables(rsrcs, tunables); if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) { LOG.fine("Loaded defaults from " + rsrcs); } } catch (MissingResourceException ignored) { // ingnored } catch (IOException ignored) { // ingnored } } } else { tunables = new NetGroupTunables(netGroupConfig.getPeerGroupID(), netGroupConfig.getName(), netGroupConfig.getDesc()); } net = newNetPeerGroup(worldGroup, null, tunables.id, tunables.name, tunables.desc, null); } finally { worldGroup.unref(); } } /** * Constructs a Net Peer Group using the specified parent peer group. This * is the preferred constructor for constructing a Net Peer Group using the * default configuration. The resulting Net Peer Group instance will use * the default ID, Name and Description. * * @param parentGroup The Peer Group which will be the parent of the newly * created net peer group. This should normally be the World Peer Group. * @throws PeerGroupException Thrown for problems constructing the Net Peer * Group. */ public NetPeerGroupFactory(PeerGroup parentGroup) throws PeerGroupException { ConfigParams cp = parentGroup.getConfigAdvertisement(); PeerGroupConfigAdv netGroupConfig = (PeerGroupConfigAdv) cp.getSvcConfigAdvertisement(PeerGroup.peerGroupClassID); NetGroupTunables tunables; if (null == netGroupConfig) { tunables = new NetGroupTunables(ResourceBundle.getBundle("net.jxta.impl.config"), new NetGroupTunables()); } else { tunables = new NetGroupTunables(netGroupConfig.getPeerGroupID(), netGroupConfig.getName(), netGroupConfig.getDesc()); } net = newNetPeerGroup(parentGroup, null, tunables.id, tunables.name, tunables.desc, null); } /** * Constructs a Net Peer Group and the World Peer Group using the * configuration specified by the provided ConfigParams and using the * specified storeHome location for persistence. The resulting Net Peer * Group instance will use the default ID, Name and Description. * * @param config The configuration to use for the newly created World Peer * Group and Net Peer Groups. * @param storeHome The optional location that the World Peer Group, the * Net Peer Group and its' services should use for storing persistent and * transient information. May be {@code null} if the World Peer Group is * not provided a persistent store (though this not currently supported). * @throws PeerGroupException Thrown for problems constructing the Net Peer * Group. */ public NetPeerGroupFactory(ConfigParams config, URI storeHome) throws PeerGroupException { WorldPeerGroupFactory world = new WorldPeerGroupFactory(config, storeHome); PeerGroup worldGroup = world.getInterface(); try { PeerGroupConfigAdv netGroupConfig = (PeerGroupConfigAdv) config.getSvcConfigAdvertisement(PeerGroup.peerGroupClassID); NetGroupTunables tunables; if (null == netGroupConfig) { tunables = new NetGroupTunables(ResourceBundle.getBundle("net.jxta.impl.config"), new NetGroupTunables()); } else { tunables = new NetGroupTunables(netGroupConfig.getPeerGroupID(), netGroupConfig.getName(), netGroupConfig.getDesc()); } net = newNetPeerGroup(worldGroup, config, tunables.id, tunables.name, tunables.desc, null); } finally { worldGroup.unref(); } } /** * Constructs a Net Peer Group and the World Peer Group using the * configuration specified by the provided ConfigParams and using the * specified storeHome location for persistence. The resulting Net Peer * Group instance will use the group information provided in the * <p/> * This constructor is provided in anticipation of other improvements * to the peer group instantiation process. Currently it has some * unreasonable limitations which keep it from being very useful. In a * future release it will be improved. * * @param config The configuration to use for the newly created World Peer * Group and Net Peer Groups. * @param storeHome The optional location that the World Peer Group, the * Net Peer Group and its' services should use for storing persistent and * transient information. May be {@code null} if the World Peer Group is * not provided a persistent store (though this not currently supported). * @throws PeerGroupException Thrown for problems constructing the Net Peer * Group. * @param parentGroup the parent peer group */ public NetPeerGroupFactory(PeerGroup parentGroup, ConfigParams config, URI storeHome) throws PeerGroupException { if (config != parentGroup.getConfigAdvertisement()) { throw new IllegalArgumentException("This constructor cannot currently accept group parameters different than the parent group"); } if (null == storeHome) { if (null != parentGroup.getStoreHome()) { throw new IllegalArgumentException("This constructor cannot currently accept a different store location than the parent group"); } } else { if (!storeHome.equals(parentGroup.getStoreHome())) { throw new IllegalArgumentException("This constructor cannot currently accept a different store location than the parent group"); } } ConfigParams cp = parentGroup.getConfigAdvertisement(); PeerGroupConfigAdv netGroupConfig = (PeerGroupConfigAdv) cp.getSvcConfigAdvertisement(PeerGroup.peerGroupClassID); NetGroupTunables tunables; if (null == netGroupConfig) { tunables = new NetGroupTunables(ResourceBundle.getBundle("net.jxta.impl.config"), new NetGroupTunables()); } else { tunables = new NetGroupTunables(netGroupConfig.getPeerGroupID(), netGroupConfig.getName(), netGroupConfig.getDesc()); } net = newNetPeerGroup(parentGroup, config, tunables.id, tunables.name, tunables.desc, null); } /** * Constructs a Net Peer Group and the World Peer Group using the * configuration specified by the provided ConfigParams and using the * specified storeHome location for persistence. * * @deprecated With the addition of support for {@code PeerGroupConfigAdv} * this constructor is being deprecated as the precedence of settings is * ambiguous. * * @param config The configuration to use for the newly created World Peer * Group and Net Peer Groups. * @param storeHome The optional location that the World Peer Group, the * Net Peer Group and its' services should use for storing persistent and * transient information. May be {@code null} if the World Peer Group is * not provided a persistent store (though this not currently supported). * @param id The PeerGroupID which will be used for the new Net Peer Group * instance. * @param name The name which will be used for the new Net Peer Group
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -