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

📄 peergroupfactory.java

📁 jxme的一些相关程序,主要是手机上程序开发以及手机和计算机通信的一些程序资料,程序编译需要Ant支持
💻 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: PeerGroupFactory.java,v 1.29 2002/05/13 19:31:30 burtonator Exp $
 */

package net.jxta.peergroup;

import java.lang.Class;

import java.io.FileInputStream;
import java.util.Hashtable;
import java.util.Properties;

import net.jxta.document.Advertisement;
import net.jxta.document.AdvertisementFactory;
import net.jxta.document.StructuredDocument;
import net.jxta.document.MimeMediaType;
import net.jxta.protocol.ModuleImplAdvertisement;
import net.jxta.protocol.PeerAdvertisement;
import net.jxta.id.ID;
import net.jxta.id.IDFactory;
import net.jxta.peergroup.PeerGroup;
import net.jxta.exception.PeerGroupException;
import net.jxta.exception.JxtaError;

import java.util.Hashtable;

import org.apache.log4j.Category;
import org.apache.log4j.Priority;


// Here we refer to the implementation, which is normal since we are explicitly
// chosing to use a particular peergroup implementation for the default net
// peer group

import net.jxta.impl.peergroup.StdPeerGroupParamAdv;

/**
 * Factory Classes to create a new Peer Platform and New PeerGroups. This
 * class is used by Jxta applications to create new groups
 *
 * <p> JXTA comes with two subclasses of peer groups:
 *
 * <ul type-disc>
 *
 * <li> <strong>Platform</strong>: this is used to represent the <em>World</em>
 * group. Every peer, when booting, becomes part of this group. This group
 * provides the minimum core services needed to find and instanciate other
 * groups on a peer. <strong>Platform</strong> has the privilege of assigning
 * a new ID to the peer, if it does not already have one. The <em>World</em>
 * group's ID is invariant.</li>
 *
 * <li> <strong>StdPeergroup</strong>: this is currently used to implement
 * all other kinds of peer groups.
 * The first such group that it is instanciated after booting is known as
 * <em>The Net Peer Group</em>. When <strong>Platform</strong> starts it may
 * optionaly search for <em>The Net Peer Group</em> on the local network and,
 * if found, instanciate it. Otherwise a default configuration of
 * <em>The Net Peer Group</em> is instanciated.
 *
 * A non-default configuration of <em>The Net Peer Group</em> may be set-up
 * by the administrator in charge of the network domain inside which the peer
 * is booting. <em>The Net Peer Group</em> is discovered via the DiscoveryService
 * protocol. Many such groups may be configured by an administrator.<br>
 *
 * <strong>StdPeergroup</strong> may also be used to implement User-defined
 * peer groups: Users can create new peer groups which use their own set of
 * customized services.
 *</li>
 *</ul>
 *
 * <p>The Platform group is created via the <b>newPlatform</b> method. The Net
 * Peer Group and user Peer Groups are created via the <b>newPeerGroup</b>
 * method.
 *
 *  @see net.jxta.peergroup.PeerGroup
 *
 *  @version $Revision: 1.29 $
 *  @since JXTA 1.0 */

public class PeerGroupFactory {
  
  private final static Category LOG = Category.getInstance(PeerGroupFactory.class.getName());
  
  private static Class stdPeerGroupClass;
  private static Class platformClass;
  
  /**
   * Static Method to initialize the std peer group class
   *
   *  @param Class name to be initialized in the factory
   *  @version $Revision: 1.29 $
   *
   *  @since JXTA 1.0
   */
  
  public static void setStdPeerGroupClass(Class c) {
    stdPeerGroupClass = c;
  }
  
  /**
   * Static Method to initialize the Platform peergroup class
   *
   *  @param Class name of the class implementing platform
   *  @version $Revision: 1.29 $
   *
   *  @since JXTA 1.0
   */
  
  public static void setPlatformClass(Class c) {
    platformClass = c;
  }
  
  /**
   * Static Method to create a new peer group instance.
   *
   * After beeing created the init method needs to be called, and
   * the startApp() method may be called, at the invoker's discretion.
   *
   *  @return PeerGroup instance of a new PeerGroup
   *  @version $Revision: 1.29 $
   *
   */
  
  
  public static PeerGroup newPeerGroup() {
    try {
      return (PeerGroup) stdPeerGroupClass.newInstance();
    } catch (Exception e) {
      if (LOG.isEnabledFor(Priority.WARN))
        LOG.warn( "newInstace failed : ", e);
      
      throw new JxtaError("No valid Standard PeerGroup class" + e.getMessage() );
    }
  }
  
  
  
  /**
   * Static Method to create a new peer Plaform
   *
   * The init() method is called automatically. The startApp() method
   * is left for the invoker to call if appropriate.
   *
   * Invoking this method amounts to creating an instance of JXTA.
   *
   * Since JXTA stores its persistent state in the local filesystem
   * relative to the initial current directory, it is unadvisable to
   * start more than one instance with the same current directory.
   *
   *  @return PeerGroup instance of a new Platform
   *
   *  @since JXTA 1.0
   */
  
  public static PeerGroup newPlatform() {
    
    PeerGroup plat = null;
    try {
      plat = (PeerGroup) platformClass.newInstance();
    } catch (Exception e) {
      if (LOG.isEnabledFor(Priority.ERROR))
        LOG.error( "newInstace failed : ", e);
      
      throw new JxtaError("No valid Platform class : " + e.getMessage() );
    }
    
    try {
      plat.init(null, null, null);
      
      return (PeerGroup) plat.getInterface();
    } catch (Exception e) {
      if (LOG.isEnabledFor(Priority.ERROR))
        LOG.error( "Platform.init failed : ", e);
      
      throw new JxtaError("Platform.init failed : " + e.getMessage() );
    }
  }
  
  /**
   * Until the concept of netPeerGroup is better integrated, we need
   * something straightforward so that applications that whish to control
   * the start-up process can easily bring up a netPeerGroup like
   * startNetPeerGroup does.
   *
   * This method does this.
   *
   * @param pg The platform group.
   * @return PeerGroup The default netPeerGroup
   */
  
  public static PeerGroup newNetPeerGroup(PeerGroup pg)
  throws PeerGroupException {
    try {
      // Use the all purpose group impl as a basis.
      ModuleImplAdvertisement netPgImplAdv =
      pg.getAllPurposePeerGroupImplAdvertisement();
      
      // For now, still use the well know NPG naming, it is not
      // identical to the allPurpose PG because we use the class
      // ShadowPeerGroup which initializes the peer config from its
      // parent.
      netPgImplAdv.setModuleSpecID(PeerGroup.refNetPeerGroupSpecID);
      netPgImplAdv.setCode("net.jxta.impl.peergroup.ShadowPeerGroup");
      netPgImplAdv.setDescription("Default NetPeerGroup reference"
      + " implementation.");
      
      // Use the well known default NPG ID.
      PeerGroupID pgId = PeerGroupID.defaultNetPeerGroupID;
      
      // We've got a gid and a complete ModuleImplAdv so we can build
      // the group now.
      PeerGroup newPg = pg.newGroup(pgId,
      netPgImplAdv,
      "NetPeerGroup",
      "NetPeerGroup by default");
      return newPg;
    } catch (Exception e) {
      if (LOG.isEnabledFor(Priority.ERROR))
        LOG.error( "newNetPeerGroup failed : ", e);
      
      // Simplify exception scheme for caller: any sort of trbl is
      // a PeerGroupException.
      throw new PeerGroupException( "newNetPeerGroup failed : " + e.getMessage());
    }
  }
  
  
  /**
   *  Creates a default platform peergroup, from which a new netpeergroug is
   *  created.  This simplifies the method by which applications can start jxta.
   *
   *  @return The newly created net peer group.
   */
  public static PeerGroup newNetPeerGroup( ) throws PeerGroupException {
    try {
        setPlatformClass( net.jxta.impl.peergroup.Platform.class );
        setStdPeerGroupClass( net.jxta.impl.peergroup.StdPeerGroup.class );

    } catch ( Throwable t ) {
        if ( LOG.isEnabledFor( Priority.ERROR ) )
            LOG.error( "newNetPeerGroup failed : ", t);
        
        throw new PeerGroupException( "Unable to find class :" + t.getMessage() );
    }
    
    // create the  default Platform Group.
    PeerGroup platformGroup = newPlatform();
    PeerGroup npg = newNetPeerGroup( platformGroup );
    return npg;
  }
  
  /**
   * private constructor. this class cannot be instantiated.
   **/
  private PeerGroupFactory() {}
}

⌨️ 快捷键说明

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