📄 netpeergroupfactory.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 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: NetPeerGroupFactory.java,v 1.1 2006/06/14 16:02:26 bondolo Exp $ */package net.jxta.peergroup;import java.io.File;import java.io.FileInputStream;import java.net.URI;import java.util.ResourceBundle;import java.util.PropertyResourceBundle;import java.io.IOException;import java.util.MissingResourceException;import org.apache.log4j.Logger;import org.apache.log4j.Level;import net.jxta.document.XMLElement;import net.jxta.document.MimeMediaType;import net.jxta.document.StructuredDocumentFactory;import net.jxta.id.ID;import net.jxta.id.IDFactory;import net.jxta.peergroup.WorldPeerGroupFactory;import net.jxta.protocol.ModuleImplAdvertisement;import net.jxta.protocol.ConfigParams;import net.jxta.exception.PeerGroupException;/** * 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. * * @see net.jxta.peergroup.PeerGroup * @see net.jxta.peergroup.WorldPeerGroupFactory **/public final class NetPeerGroupFactory { /** * Log4J 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(); try { NetGroupTunables 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 (LOG.isEnabledFor(Level.DEBUG)) { LOG.debug( "Loaded defaults from " + rsrcs); } } catch(MissingResourceException ignored) { ; } catch(IOException ignored ) { ; } } net = newNetPeerGroup( worldGroup, 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 { NetGroupTunables tunables = new NetGroupTunables( ResourceBundle.getBundle("net.jxta.impl.config"), new NetGroupTunables() ); net = newNetPeerGroup( parentGroup, 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(); NetGroupTunables tunables = new NetGroupTunables( ResourceBundle.getBundle("net.jxta.impl.config"), new NetGroupTunables() ); try { net = newNetPeerGroup( worldGroup, 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. **/ 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" ); } } NetGroupTunables tunables = new NetGroupTunables( ResourceBundle.getBundle("net.jxta.impl.config"), new NetGroupTunables() ); net = newNetPeerGroup( parentGroup, 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. * * @param config The configuration to use for the newly created World Peer * Group and Net Peer Groups.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -