⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 worldpeergroupfactory.java

📁 jxta_src_2.41b jxta 2.41b 最新版源码 from www.jxta.org
💻 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: WorldPeerGroupFactory.java,v 1.1 2006/06/14 16:02:27 bondolo Exp $ */package net.jxta.peergroup;import java.io.File;import java.lang.reflect.Constructor;import java.net.URI;import java.util.ResourceBundle;import java.lang.reflect.InvocationTargetException;import org.apache.log4j.Logger;import org.apache.log4j.Level;import net.jxta.peergroup.Configurator;import net.jxta.protocol.ConfigParams;import net.jxta.exception.ConfiguratorException;import net.jxta.exception.PeerGroupException;/** *  A factory for instantiating the World Peer Group. Every peer starts by *  instantiating the World Peer Group and then other Peer Groups are *  instantiated as needed. * *  <p/>The World Peer Group provides the minimum core services needed to find *  and instantiate other Peer Groups on a peer. The World Peer Group is the  *  primordial peer group upon which all other peer groups are instantiated. The *  World Peer Group is primarily responsible for management of physical network *  connections, physical network discovery (generally broadcast) and physical  *  network topology management. * *  <p/>Applications generally do not interact directly with the World Peer *  Group. The World Peer Group includes only limited endpoint, resolver,  *  discovery and rendezvous services. * *  <p/>When the <strong>World Peer Group</strong> starts it may optionally  *  search for <em>The Networkd Peer Group</em> on the local network and, if  *  found, instantiate it. Otherwise a default built-in configuration of <em>The *  Net Peer Group</em> is instantiated. * *  @see net.jxta.peergroup.PeerGroup *  @see net.jxta.peergroup.NetPeerGroupFactory **/public final class WorldPeerGroupFactory {        /**     *  Log4J Logger     **/    private final static transient Logger LOG = Logger.getLogger(WorldPeerGroupFactory.class.getName());        /**     *  Our strong reference to the World Peer Group.     **/    private final PeerGroup world;        /**     *  Provided for backwards compatibility, this constructor instantiates the     *  World Peer Group using the PlatformConfig file 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/>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 construction the World     *  Peer Group.     **/    public WorldPeerGroupFactory( ) throws PeerGroupException {        // Establish the default store location via long established hackery.        String jxta_home = System.getProperty( "JXTA_HOME", ".jxta/" );                if( !jxta_home.endsWith( File.separator ) ) {            jxta_home += File.separator;        }                URI storeHome = new File( jxta_home ).toURI();                // Instantiate the default configurator. Don't do this in your own code.        try {            Configurator configurator = new net.jxta.impl.peergroup.DefaultConfigurator( storeHome );                        // Get (and possibly generate) the platform configuration.            ConfigParams config = configurator.getConfigParams();                        world = newWorldPeerGroup( getDefaultWorldPeerGroupClass(), config, storeHome );                        // persist any changes which were made to the platform config by             // service initialization.            configurator.setConfigParams( config );            configurator.save();        } catch( ConfiguratorException configFailure ) {            LOG.fatal( "Failure while managing World Peer Group configuration" );                        throw new PeerGroupException( "Failure while managing World Peer Group configuration", configFailure );        }    }        /**     *  Constructs the World Peer Group using the specified configuration and      *  using the specified storeHome location for persistence.     *     *  @param config The configuration to use for the World Peer Group.     *  @param storeHome The optional location that the World Peer Group and its'     *  services should use for storing persistent and transient information.     *  May be <tt>null</tt> if the World Peer Group is not provided a     *  perstistent store (though this not currently supported).     *  @throws PeerGroupException Thrown for problems constructing the World     *  Peer Group.     **/    public WorldPeerGroupFactory( ConfigParams config, URI storeHome ) throws PeerGroupException {                world = newWorldPeerGroup( getDefaultWorldPeerGroupClass(), config, storeHome );    }        /**     *  Constructs the World Peer Group using the specified configuration and      *  using the specified storeHome location for persistence.     *     *  @param worldPeerGroupClass The class which will be instantiated for the      *  World Peer Group instance.     *  @param config The configuration to use for the World Peer Group.     *  @param storeHome The optional location that the World Peer Group and its'     *  services should use for storing persistent and transient information.     *  May be <tt>null</tt> if the World Peer Group is not provided a     *  perstistent store (though this not currently supported).     *  @throws PeerGroupException Thrown for problems constructing the World     *  Peer Group.     **/    public WorldPeerGroupFactory( Class worldPeerGroupClass, ConfigParams config, URI storeHome ) throws PeerGroupException {                world = newWorldPeerGroup( worldPeerGroupClass, config, storeHome );    }        /**     *  Returns a strong (reference counted) interface object for the World Peer     *  Group. This reference should be explicitly unreferenced when it is no     *  longer needed.     *     *  @see PeerGroup#unref()     *     *  @return A strong (reference counted) interface object for the World Peer Group.     **/    public PeerGroup getInterface() {        return (PeerGroup) world.getInterface();    }        /**     *  Returns a weak (non-reference counted) interface object for the World      *  Peer Group.     *     *  @see PeerGroup#getWeakInterface()     *     *  @return A weak (non-reference counted) interface object for the World      *  Peer Group.     **/    public PeerGroup getWeakInterface() {        return world.getWeakInterface();    }        /**     *  Determine the class to use from "net.jxta.impl.config". This should be     *  located somewhere on the JXTA class path. Normally it is located in     *  jxta.jar     *     *  @return The Class which has been configured to be used for      *  World Peer Group instances.     *  @throws PeerGroupException Thrown for problems determinging the class to     *  be used for the World Peer Group.     **/    private static Class getDefaultWorldPeerGroupClass( ) throws PeerGroupException {        try {            ResourceBundle rsrcs = ResourceBundle.getBundle("net.jxta.impl.config");                        String worldPeerGroupClassName = rsrcs.getString( "PlatformPeerGroupClassName" ).trim();            Class worldPeerGroupClass = Class.forName(worldPeerGroupClassName);                        return worldPeerGroupClass;        } catch(RuntimeException failed) {            throw new PeerGroupException( "Could not load world peer group class.", failed );        } catch(ClassNotFoundException failed ) {            throw new PeerGroupException( "Could not load world peer group class.", failed );        }    }        /**     *  Constructs the World Peer Group instance.     *     *  @param worldPeerGroupClass The class which will be instantiated for the      *  World Peer Group instance.     *  @param config The configuration to use for the World Peer Group.     *  @param storeHome The optional location the World Peer Group and its'     *  services may use for storing persistent and transient information.     *  May be <tt>null</tt> if the World Peer Group is not provided a     *  perstistent store (though this not currently supported).     *  @throws PeerGroupException Thrown for problems constructing the World     *  Peer Group.     **/    private PeerGroup newWorldPeerGroup( Class worldPeerGroupClass, ConfigParams config, URI storeHome ) throws PeerGroupException {        synchronized( PeerGroup.globalRegistry ) {            PeerGroup result = PeerGroup.globalRegistry.lookupInstance( PeerGroupID.worldPeerGroupID );                        if( null != result ) {                throw new PeerGroupException( "Only a single instance of the World Peer Group may be instantiated at a single time." );            }                        if( !storeHome.isAbsolute() ) {                LOG.fatal( "storeHome must be an absolute URI." );                                throw new PeerGroupException( "storeHome must be an absolute URI." );            }                                    if( storeHome.isOpaque() ) {                LOG.fatal( "Opaque storeHome is not currently supported." );                throw new PeerGroupException( "Opaque storeHome is not currently supported." );            }            try {                if( LOG.isEnabledFor(Level.INFO) ) {                    LOG.info( "Making a new World Peer Group instance using : " + worldPeerGroupClass.getName() );                }                                Constructor<PeerGroup> twoParams = (Constructor<PeerGroup>) worldPeerGroupClass.getConstructor( ConfigParams.class, URI.class  );                                try {                    result = twoParams.newInstance( config, storeHome );                } catch( InvocationTargetException failure ) {                    // unwrap the real exception.                    Throwable cause = failure.getCause();                                        if( cause instanceof Exception) {                        throw (Exception) cause;                    } else if( cause instanceof Error) {                        throw (Error) cause;                    } else {                        // just rethrow what we already had. sigh.                        throw failure;                    }                }                                result.init(null, PeerGroupID.worldPeerGroupID, null);                                return result;            } catch(RuntimeException e) {                // should be all other checked exceptions                LOG.fatal("World Peer Group could not be instantiated.", e);                                // cleanup broken instance                if( null != result ) {                    result.unref();                }                                // just rethrow.                throw e;            } catch(Exception e) {                // should be all other checked exceptions                LOG.fatal("World Peer Group could not be instantiated.", e);                                // cleanup broken instance                if( null != result ) {                    result.unref();                }                                // Simplify exception scheme for caller: any sort of problem wrapped                // in a PeerGroupException.                throw new PeerGroupException("World Peer Group could not be instantiated.", e);            }        }    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -