📄 stdpeergroup.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: StdPeerGroup.java,v 1.74 2006/02/22 01:29:43 bondolo Exp $ */package net.jxta.impl.peergroup;import java.net.URI;import java.util.ArrayList;import java.util.Arrays;import java.util.Enumeration;import java.util.Hashtable;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.Map;import java.util.Map.Entry;import org.apache.log4j.Level;import org.apache.log4j.Logger;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.TextElement;import net.jxta.document.XMLDocument;import net.jxta.endpoint.MessageTransport;import net.jxta.id.ID;import net.jxta.peergroup.PeerGroup;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 net.jxta.exception.PeerGroupException;import net.jxta.exception.ServiceNotFoundException;import net.jxta.exception.ViolationException;import net.jxta.impl.cm.Cm;import net.jxta.impl.cm.SrdiIndex;/** * A subclass of GenericPeerGroup that makes a peer group out of independent * plugin services listed in its impl advertisement. */public class StdPeerGroup extends GenericPeerGroup implements CompatibilityEquater { private static final 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://www.jxta.org/download/jxta.jar"; public static final String MODULE_IMPL_STD_PROVIDER = "sun.com"; static final String STD_COMPAT_FORMAT = "Efmt"; static final String STD_COMPAT_FORMAT_VALUE = "JDK1.4.1"; static final String STD_COMPAT_BINDING = "Bind"; static final String STD_COMPAT_BINDING_VALUE = "V2.0 Ref Impl"; private volatile boolean initialized = false; private volatile boolean started = false; /** * A map of the applications for this group. * * <ul> * <li>keys are {@see net.jxta.platform.ModuleClassID}</li> * <li>values are {@see net.jxta.platform.Module} or * {@link net.jxta.protocol.ModuleImplAdvertisement}</li> * </ul> **/ private Map applications = new HashMap(); /** * A map of the protocols for this group. * * <ul> * <li>keys are {@see net.jxta.platform.ModuleClassID}</li> * <li>values are {@see net.jxta.platform.Module}, but should also be * {@see net.jxta.endpoint.MessageTransport}</li> * </ul> * **/ private Map protocols = new HashMap(); /** * Cache for this group. **/ private Cm cm = null; private List disabledModules = new ArrayList(); private ModuleImplAdvertisement allPurposeImplAdv = null; private static XMLDocument mkCS() { XMLDocument doc = (XMLDocument) StructuredDocumentFactory.newStructuredDocument( MimeMediaType.XMLUTF8, "Comp"); Element 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; } /** * constructor **/ public StdPeerGroup() { } /** * 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. **/ protected 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; } /** * {@inheritDoc} **/ public boolean compatible(Element compat) { return isCompatible( compat ); } /** * Evaluates if the given compatibility statement makes the module that * bears it is loadable by this group. * * @return boolean True if the given statement is compatible. */ public static boolean isCompatible(Element compat) { // Our criterion of compatibility is equality. However, we // must do the comparision on a logical level. This is // easy because the reference compat statement is manufactured // by this class as well. So we can make assumptions as // to its structure: two elements each with a specific value. boolean formatOk = false; boolean bindingOk = false; try { Enumeration hisChildren = compat.getChildren(); int i = 0; while (hisChildren.hasMoreElements()) { // Stop after 2 elements; there shall not be more. if (++i > 2) return false; Element e = (Element) hisChildren.nextElement(); String key = (String) e.getKey(); String val = (String) e.getValue(); if (STD_COMPAT_FORMAT.equals(key) && STD_COMPAT_FORMAT_VALUE.equals(val)) { formatOk = true; } else if (STD_COMPAT_BINDING.equals(key) && STD_COMPAT_BINDING_VALUE.equals(val)) { bindingOk = true; } else { return false; // Might as well stop right now. } } } catch (Exception any) { } return formatOk && bindingOk; } /** * Builds a table of modules indexed by their class ID. * The values are the loaded modules, the keys are their classId. * This routine interprets the parameter list in the advertisement. * * @param modules The modules to load * @param thisClassOnly load only the module specified from the map * @param privileged if true then modules will get a real reference to * the group loading them, otherwise its an interface object. **/ protected void loadAllModules(Map modules, ModuleClassID thisClassOnly, boolean privileged) { Iterator allKeys = Arrays.asList( modules.keySet().toArray() ).iterator(); while (allKeys.hasNext()) { ModuleClassID classID = (ModuleClassID) allKeys.next(); Object value = modules.get(classID); // Class was filtered. if (thisClassOnly != null && ! thisClassOnly.equals(classID)) { continue; } // If it is disabled, strip it. if (disabledModules.contains(classID)) { if (value instanceof ModuleClassID) { if (LOG.isEnabledFor(Level.INFO)) { LOG.info("Module disabled by configuration : " + classID ); } } else { if (LOG.isEnabledFor(Level.INFO)) { LOG.info("Module disabled by configuration : " + ((ModuleImplAdvertisement) value).getDescription()); } } modules.remove(classID); if (thisClassOnly != null) { break; } else { continue; } } // Already loaded. if (value instanceof Module) { if (thisClassOnly != null) { break; } else { continue; } } // Try and load it. try { Module theModule = null; if (value instanceof ModuleImplAdvertisement) { // Load module will republish localy but not in the // parent since that adv does not come from there. theModule = loadModule(classID, (ModuleImplAdvertisement) value, privileged); } else if (value instanceof ModuleSpecID) { // loadModule will republish both localy and in the parent // Where the module was fetched. theModule = loadModule(classID, (ModuleSpecID) value, FromParent, privileged); } else {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -