📄 genericpeergroup.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.util.concurrent.BlockingQueue;import java.util.concurrent.Executor;import java.util.concurrent.ThreadPoolExecutor;import net.jxta.access.AccessService;import net.jxta.discovery.DiscoveryService;import net.jxta.document.Advertisement;import net.jxta.document.AdvertisementFactory;import net.jxta.document.Element;import net.jxta.document.XMLElement;import net.jxta.endpoint.EndpointService;import net.jxta.exception.PeerGroupException;import net.jxta.exception.ProtocolNotSupportedException;import net.jxta.exception.ServiceNotFoundException;import net.jxta.id.ID;import net.jxta.id.IDFactory;import net.jxta.impl.loader.RefJxtaLoader;import net.jxta.impl.protocol.PSEConfigAdv;import net.jxta.impl.protocol.PlatformConfig;import net.jxta.impl.util.TimeUtils;import net.jxta.logging.Logging;import net.jxta.membership.MembershipService;import net.jxta.peer.PeerID;import net.jxta.peer.PeerInfoService;import net.jxta.peergroup.PeerGroup;import net.jxta.peergroup.PeerGroupID;import net.jxta.pipe.PipeService;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.protocol.PeerAdvertisement;import net.jxta.protocol.PeerGroupAdvertisement;import net.jxta.rendezvous.RendezVousService;import net.jxta.resolver.ResolverService;import net.jxta.service.Service;import java.io.IOException;import java.lang.reflect.Method;import java.lang.reflect.UndeclaredThrowableException;import java.net.URI;import java.net.URL;import java.security.cert.Certificate;import java.util.ArrayList;import java.util.Collection;import java.util.Collections;import java.util.Enumeration;import java.util.HashMap;import java.util.Hashtable;import java.util.Iterator;import java.util.List;import java.util.Map;import java.util.NoSuchElementException;import java.util.concurrent.ArrayBlockingQueue;import java.util.concurrent.RejectedExecutionException;import java.util.concurrent.RejectedExecutionHandler;import java.util.concurrent.ScheduledExecutorService;import java.util.concurrent.ScheduledThreadPoolExecutor;import java.util.concurrent.ThreadFactory;import java.util.concurrent.TimeUnit;import java.util.concurrent.atomic.AtomicInteger;import java.util.logging.Level;import java.util.logging.Logger;/** * Provides common services for most peer group implementations. */public abstract class GenericPeerGroup implements PeerGroup { /** * Logger */ private final static transient Logger LOG = Logger.getLogger(GenericPeerGroup.class.getName()); /** * Holder for configuration parameters for groups in the process of being created. */ private final static Map<ID, ConfigParams> group_configs = Collections.synchronizedMap(new HashMap<ID, ConfigParams>()); /** * The loader - use the getter and setter for modifying the ClassLoader for * a security manager. * <p/> * This should eventually be group scoped rather than implementation * scoped. We are currently allowing classes to loaded into contexts which * they should not be known. */ private final static JxtaLoader loader = new RefJxtaLoader(new URL[0], new CompatibilityEquater() { public boolean compatible(Element test) { return StdPeerGroup.isCompatible(test); } }); /* * Shortcuts to well known services. */ private EndpointService endpoint; private ResolverService resolver; private DiscoveryService discovery; private PipeService pipe; private MembershipService membership; private RendezVousService rendezvous; private PeerInfoService peerinfo; private AccessService access; /** * This peer's advertisement in this group. */ private final PeerAdvertisement peerAdvertisement; /** * This group's advertisement. */ private PeerGroupAdvertisement peerGroupAdvertisement = null; /** * This group's implAdvertisement. */ protected ModuleImplAdvertisement implAdvertisement = null; /** * This peer's config advertisement. */ protected ConfigParams configAdvertisement = null; /** * This service implements a group but, being a Service, it runs inside of * some group. That's its home group. * <p/> * Exception: The platform peer group does not have a parent group. It * has to be entirely self sufficient. */ protected PeerGroup parentGroup = null; /** * The location of our store */ protected URI jxtaHome = null; /** * The services that do the real work of the Peer Group. */ private final Map<ModuleClassID, Service> services = new HashMap<ModuleClassID, Service>(); /** * {@code true} when we have decided to stop this group. */ private volatile boolean stopping = false; /** * {@code true} when the PG adv has been published. */ private boolean published = false; // assume it hasn't /** * Counts the number of times an interface to this group has been given out. * This is decremented every time an interface object is GCed or * its owner calls unref(). * * <p/>When it reaches zero, if it is time to tear-down the group instance; * nomatter what the GC thinks. There are threads that need to be stopped * before the group instance object ever becomes un-reachable. */ private int masterRefCount = 0; /** * Is {@code true} when at least one interface object has been generated AFTER * initComplete has become true. If true, the group stops when its ref * count reaches zero. */ private boolean stopWhenUnreferenced = false; /** * Is set to {@code true} when {@code init()} is completed enough that it * makes sense to perform ref-counting. */ protected volatile boolean initComplete = false; /** * The thread group in which threads created by this group or services of * this group should live. The thread group is used primarily for debugging * and classification purposes--we don't try to use any of the fancy (and * mostly useless) ThreadGroup features. */ private ThreadGroup threadGroup = null; /** * The minimum number of Threads our Executor will reserve. Once started * these Threads will remain. * * todo convert these hardcoded settings into group config params. */ private final int COREPOOLSIZE = 5; /** * The number of seconds that Threads above {@code COREPOOLSIZE} will * remain idle before terminating. * * todo convert these hardcoded settings into group config params. */ private final long KEEPALIVETIME = 15; /** * The intended upper bound on the number of threads we will allow our * Executor to create. We will allow the pool to grow to twice this size if * we run out of threads. * * todo convert these hardcoded settings into group config params. */ private final int MAXPOOLSIZE = 100; /** * Queue for tasks waiting to be run by our {@code Executor}. */ private BlockingQueue<Runnable> taskQueue; /** * The PeerGroup ThreadPool */ private ThreadPoolExecutor threadPool; /** * The PeerGroup ScheduledExecutor */ private ScheduledThreadPoolExecutor scheduledExecutor; /** * {@inheritDoc} * <p/> * We do not want to count on the invoker to properly unreference the group * object that we return; this call is often used in a loop and it is silly * to increment and decrement ref-counts for references that are sure to * live shorter than the referee. * <p/> * On the other hand it is dangerous for us to share our reference object to * the parent group. That's where weak interface objects come in handy. We * can safely make one and give it away. */ public PeerGroup getParentGroup() { if (parentGroup == null) { return null; } return parentGroup.getWeakInterface(); } /** * {@inheritDoc} */ public URI getStoreHome() { return jxtaHome; } /** * Sets the root location for the store to be used by this peergroup. * <p/> * This should be set early in the peer group's life and then never * changed. * * @param newHome The new store location. */ protected void setStoreHome(URI newHome) { jxtaHome = newHome; } /** * {@inheritDoc} */ public static JxtaLoader getJxtaLoader() { return loader; } public GenericPeerGroup() { // Start building our peer adv. peerAdvertisement = (PeerAdvertisement) AdvertisementFactory.newAdvertisement(PeerAdvertisement.getAdvertisementType()); } /** * {@inheritDoc} */ @Override public boolean equals(Object target) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -