📄 stdpeergroup.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.impl.peergroup;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.lang.reflect.Method;import java.net.URI;import java.net.URL;import net.jxta.discovery.DiscoveryService;import net.jxta.document.Advertisement;import net.jxta.document.AdvertisementFactory;import net.jxta.document.Element;import net.jxta.document.MimeMediaType;import net.jxta.document.StructuredDocumentFactory;import net.jxta.document.XMLDocument;import net.jxta.document.XMLElement;import net.jxta.endpoint.MessageTransport;import net.jxta.exception.PeerGroupException;import net.jxta.exception.ServiceNotFoundException;import net.jxta.id.ID;import net.jxta.impl.cm.Cm;import net.jxta.impl.cm.SrdiIndex;import net.jxta.logging.Logging;import net.jxta.peergroup.PeerGroup;import net.jxta.platform.JxtaLoader;import net.jxta.platform.Module;import net.jxta.platform.ModuleClassID;import net.jxta.platform.ModuleSpecID;import net.jxta.protocol.ConfigParams;import net.jxta.protocol.ModuleImplAdvertisement;import net.jxta.service.Service;import java.text.MessageFormat;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.logging.Level;import java.util.logging.Logger;import net.jxta.document.TextElement;/** * A subclass of GenericPeerGroup that makes a peer group out of independent * plugin services listed in its impl advertisement. */public class StdPeerGroup extends GenericPeerGroup { /** * Logger */ private final static transient Logger LOG = Logger.getLogger(StdPeerGroup.class.getName()); // A few things common to all ImplAdv for built-in things. public static final XMLDocument STD_COMPAT = mkCS(); public static final String MODULE_IMPL_STD_URI = "http://jxta-jxse.dev.java.net/download/jxta.jar"; public static final String MODULE_IMPL_STD_PROVIDER = "sun.com"; protected static final String STD_COMPAT_FORMAT = "Efmt"; // FIXME 20061206 bondolo Update this to "JRE1.5" after 2.5 release. 2.4.1 and earlier don't do version comparison correctly. /** * The Specification title and Specification version we require. */ protected static final String STD_COMPAT_FORMAT_VALUE = "JDK1.4.1"; protected static final String STD_COMPAT_BINDING = "Bind"; protected static final String STD_COMPAT_BINDING_VALUE = "V2.0 Ref Impl"; static { // Initialize the JXTA class loader with the standard modules. try { Enumeration<URL> allProviderLists = GenericPeerGroup.class.getClassLoader().getResources("META-INF/services/net.jxta.platform.Module"); for (URL providers : Collections.list(allProviderLists)) { registerFromFile(providers); } } catch (IOException ex) { if (Logging.SHOW_WARNING && LOG.isLoggable(Level.WARNING)) { LOG.log(Level.WARNING, "Failed to locate provider lists", ex); } } // XXX Force redefinition of StdPeerGroup implAdvertisement. getJxtaLoader().defineClass(getDefaultModuleImplAdvertisement()); } /** * Register instance classes given a URL to a file containing modules which * must be found on the current class path. Each line of the file contains a * module spec ID, the class name and the Module description. The fields are * separated by whitespace. Comments are marked with a '#', the pound sign. * Any text following # on any line in the file is ignored. * * @param providerList the URL to a file containing a list of modules * @return {@code true} if at least one of the instance classes could be * registered otherwise {@code false}. */ private static boolean registerFromFile(URL providers) { boolean registeredSomething = false; InputStream urlStream = null; try { urlStream = providers.openStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(urlStream, "UTF-8")); String provider; while ((provider = reader.readLine()) != null) { int comment = provider.indexOf('#'); if (comment != -1) { provider = provider.substring(0, comment); } provider = provider.trim(); if (0 == provider.length()) { continue; } try { String[] parts = provider.split("\\s", 3); if (3 == parts.length) { ModuleSpecID msid = ModuleSpecID.create(URI.create(parts[0])); String code = parts[1]; String description = parts[2]; ModuleImplAdvertisement moduleImplAdv; try { Class<Module> moduleClass = (Class<Module>) Class.forName(code); Method getImplAdvMethod = moduleClass.getMethod("getDefaultModuleImplAdvertisement"); moduleImplAdv = (ModuleImplAdvertisement) getImplAdvMethod.invoke(null); } catch(Exception failed) { // Use default ModuleImplAdvertisement. moduleImplAdv = StdPeerGroup.mkImplAdvBuiltin(msid, code, description); } getJxtaLoader().defineClass(moduleImplAdv); if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) { LOG.fine("Registered Module " + msid + " : " + parts[1]); } } else { if (Logging.SHOW_WARNING && LOG.isLoggable(Level.WARNING)) { LOG.log(Level.WARNING, "Failed to register \'" + provider + "\'"); } } } catch (Exception allElse) { if (Logging.SHOW_WARNING && LOG.isLoggable(Level.WARNING)) { LOG.log(Level.WARNING, "Failed to register \'" + provider + "\'", allElse); } } } } catch (IOException ex) { if (Logging.SHOW_WARNING && LOG.isLoggable(Level.WARNING)) { LOG.log(Level.WARNING, "Failed to read provider list " + providers, ex); } } finally { if (null != urlStream) { try { urlStream.close(); } catch (IOException ignored) { } } } return registeredSomething; } /** * If {@code true} then the PeerGroup has been started. */ private volatile boolean started = false; /** * The order in which we started the services. */ private final List<ModuleClassID> moduleStartOrder = new ArrayList<ModuleClassID>(); /** * A map of the Message Transports for this group. * <p/> * <ul> * <li>keys are {@link net.jxta.platform.ModuleClassID}</li> * <li>values are {@link net.jxta.platform.Module}, but should also be * {@link net.jxta.endpoint.MessageTransport}</li> * </ul> */ private final Map<ModuleClassID, Object> messageTransports = new HashMap<ModuleClassID, Object>(); /** * A map of the applications for this group. * <p/> * <ul> * <li>keys are {@link net.jxta.platform.ModuleClassID}</li> * <li>values are {@link net.jxta.platform.Module} or * {@link net.jxta.protocol.ModuleImplAdvertisement} or * {@link net.jxta.platform.ModuleSpecID}</li> * </ul> */ private final Map<ModuleClassID, Object> applications = new HashMap<ModuleClassID, Object>(); /** * Cache for this group. */ private Cm cm = null; private static XMLDocument mkCS() { XMLDocument doc = (XMLDocument) StructuredDocumentFactory.newStructuredDocument(MimeMediaType.XMLUTF8, "Comp"); XMLElement e = doc.createElement(STD_COMPAT_FORMAT, STD_COMPAT_FORMAT_VALUE); doc.appendChild(e); e = doc.createElement(STD_COMPAT_BINDING, STD_COMPAT_BINDING_VALUE); doc.appendChild(e); return doc; } /** * An internal convenience method essentially for bootstrapping. * Make a standard ModuleImplAdv for any service that comes builtin this * reference implementation. * In most cases there are no params, so we do not take that argument. * The invoker may add params afterwards. * * @param specID spec ID * @param code code uri * @param descr description * @return a ModuleImplAdvertisement */ static ModuleImplAdvertisement mkImplAdvBuiltin(ModuleSpecID specID, String code, String descr) { ModuleImplAdvertisement implAdv = (ModuleImplAdvertisement) AdvertisementFactory.newAdvertisement(ModuleImplAdvertisement.getAdvertisementType()); implAdv.setModuleSpecID(specID); implAdv.setCompat(STD_COMPAT); implAdv.setCode(code); implAdv.setUri(MODULE_IMPL_STD_URI); implAdv.setProvider(MODULE_IMPL_STD_PROVIDER); implAdv.setDescription(descr); return implAdv; } /** * Create and populate the default module impl Advertisement for this class. * * @return The default module impl advertisement for this class. */ private static ModuleImplAdvertisement getDefaultModuleImplAdvertisement() { ModuleImplAdvertisement implAdv = mkImplAdvBuiltin(PeerGroup.allPurposePeerGroupSpecID, StdPeerGroup.class.getName(), "General Purpose Peer Group Implementation"); // Create the service list for the group. StdPeerGroupParamAdv paramAdv = new StdPeerGroupParamAdv(); ModuleImplAdvertisement moduleAdv; // set the services // core services JxtaLoader loader = getJxtaLoader(); moduleAdv = loader.findModuleImplAdvertisement(PeerGroup.refEndpointSpecID); paramAdv.addService(PeerGroup.endpointClassID, moduleAdv); moduleAdv = loader.findModuleImplAdvertisement(PeerGroup.refResolverSpecID); paramAdv.addService(PeerGroup.resolverClassID, moduleAdv);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -