2 * Copyright (c) 2003-2007 Sun Microsystems, Inc. All rights reserved.
4 * The Sun Project JXTA(TM) Software License
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
9 * 1. Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright notice,
13 * this list of conditions and the following disclaimer in the documentation
14 * and/or other materials provided with the distribution.
16 * 3. The end-user documentation included with the redistribution, if any, must
17 * include the following acknowledgment: "This product includes software
18 * developed by Sun Microsystems, Inc. for JXTA(TM) technology."
19 * Alternately, this acknowledgment may appear in the software itself, if
20 * and wherever such third-party acknowledgments normally appear.
22 * 4. The names "Sun", "Sun Microsystems, Inc.", "JXTA" and "Project JXTA" must
23 * not be used to endorse or promote products derived from this software
24 * without prior written permission. For written permission, please contact
25 * Project JXTA at http://www.jxta.org.
27 * 5. Products derived from this software may not be called "JXTA", nor may
28 * "JXTA" appear in their name, without prior written permission of Sun.
30 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
31 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
32 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SUN
33 * MICROSYSTEMS OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
34 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
35 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
36 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
37 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
38 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
39 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 * JXTA is a registered trademark of Sun Microsystems, Inc. in the United
42 * States and other countries.
44 * Please see the license information page at :
45 * <http://www.jxta.org/project/www/license.html> for instructions on use of
46 * the license in source files.
48 * ====================================================================
50 * This software consists of voluntary contributions made by many individuals
51 * on behalf of Project JXTA. For more information on Project JXTA, please see
52 * http://www.jxta.org.
54 * This license is based on the BSD license adopted by the Apache Foundation.
57 package net.jxta.peergroup;
60 import net.jxta.access.AccessService;
61 import net.jxta.discovery.DiscoveryService;
62 import net.jxta.document.Advertisement;
63 import net.jxta.document.Element;
64 import net.jxta.endpoint.EndpointService;
65 import net.jxta.exception.PeerGroupException;
66 import net.jxta.exception.ProtocolNotSupportedException;
67 import net.jxta.exception.ServiceNotFoundException;
68 import net.jxta.id.ID;
69 import net.jxta.membership.MembershipService;
70 import net.jxta.peer.PeerID;
71 import net.jxta.peer.PeerInfoService;
72 import net.jxta.pipe.PipeService;
73 import net.jxta.platform.JxtaLoader;
74 import net.jxta.platform.Module;
75 import net.jxta.platform.ModuleSpecID;
76 import net.jxta.protocol.ConfigParams;
77 import net.jxta.protocol.ModuleImplAdvertisement;
78 import net.jxta.protocol.PeerAdvertisement;
79 import net.jxta.protocol.PeerGroupAdvertisement;
80 import net.jxta.rendezvous.RendezVousService;
81 import net.jxta.resolver.ResolverService;
82 import net.jxta.service.Service;
83 import java.util.logging.Level;
84 import net.jxta.logging.Logging;
85 import java.util.logging.Logger;
87 import java.io.IOException;
89 import java.util.Collections;
90 import java.util.Iterator;
94 * LightWeightPeerGroup is a class intended to help
95 * building PeerGroup that can inherit one or more
96 * services from a parent PeerGroup.
98 * An LightWeightPeerGroup implements PeerGroup and is to
99 * be used like a PeerGroup by applications.
101 * This class is intended to be extended/implemented.
103 * Note: unlike implementations of peer groups that existed until JXTA 2.2,
104 * LightweightPeergroup permits to implement groups that borrow all or
105 * part of their services to a parent group. One needs to remember
106 * that peers in various such subgroups of a given parent groups may
107 * implicitly all share the same services if that is what the PeerGroup
108 * implementing LightWeightPeerGroup is doing. Please refer to the documentation
109 * of PeerGroups extending LightWeigthPeerGroup do understand which
110 * services are shared, and which are not.
113 public class LightWeightPeerGroup implements PeerGroup {
118 private static final Logger LOG = Logger.getLogger(LightWeightPeerGroup.class.getName());
120 private PeerGroup group = null;
121 private ID assignedID = null;
122 private ModuleImplAdvertisement implAdv = null;
123 private final PeerGroupAdvertisement adv;
128 * All classes that extend this class must invoke this
131 * @param adv PeerGroupAdvertisement of this LightWeightPeerGroup.
132 * Note that only the PeerGroupID is used.
134 public LightWeightPeerGroup(PeerGroupAdvertisement adv) {
138 /***********************************************************
140 ***********************************************************/
145 public void init(PeerGroup group, ID assignedID, Advertisement implAdv) {
147 this.assignedID = assignedID;
148 this.implAdv = (ModuleImplAdvertisement) implAdv;
154 public int startApp(String[] args) {
156 if (Logging.SHOW_SEVERE && LOG.isLoggable(Level.SEVERE)) {
157 LOG.severe("No base peer group defined.");
168 public void stopApp() {}
173 public Service getInterface() {
180 public ModuleImplAdvertisement getImplAdvertisement() {
184 /***********************************************************
186 ***********************************************************/
191 public ThreadGroup getHomeThreadGroup() {
193 return group.getHomeThreadGroup();
199 /***********************************************************
201 ***********************************************************/
206 public URI getStoreHome() {
208 return group.getStoreHome();
217 public JxtaLoader getLoader() {
219 return group.getLoader();
228 public PeerGroup getParentGroup() {
232 } catch (Exception ex) {
233 if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
234 LOG.fine("LightWeightPeerGroup is a base PeerGroup: no parent");
236 throw new RuntimeException("LightWeightPeerGroup is a base PeerGroup: no parent");
243 public boolean isRendezvous() {
245 return group != null && group.isRendezvous();
251 public PeerGroupAdvertisement getPeerGroupAdvertisement() {
255 } else if (group != null) {
256 return group.getPeerGroupAdvertisement();
265 public PeerAdvertisement getPeerAdvertisement() {
268 return group.getPeerAdvertisement();
277 public Service lookupService(ID name) throws ServiceNotFoundException {
280 return group.lookupService(name);
282 throw new ServiceNotFoundException("Not implemented");
289 public Service lookupService(ID name, int ignoredForNow) throws ServiceNotFoundException {
292 return group.lookupService(name);
294 throw new ServiceNotFoundException("Not implemented");
301 public Iterator getRoleMap(ID name) {
304 return group.getRoleMap(name);
306 // No translation; use the given name in a singleton.
307 return Collections.singletonList(name).iterator();
314 public boolean compatible(Element compat) {
316 return group != null && group.compatible(compat);
322 public Module loadModule(ID assignedID, Advertisement impl) throws ProtocolNotSupportedException, PeerGroupException {
325 return group.loadModule(assignedID, impl);
327 throw new ProtocolNotSupportedException("LightWeightPeerGroup does not implement this operation");
334 public Module loadModule(ID assignedID, ModuleSpecID specID, int where) {
337 return group.loadModule(assignedID, specID, where);
346 public void publishGroup(String name, String description) throws IOException {
349 group.publishGroup(name, description);
351 throw new IOException("Not implemented");
358 public PeerGroup newGroup(Advertisement pgAdv) throws PeerGroupException {
361 return group.newGroup(pgAdv);
363 throw new PeerGroupException("Not implemented");
370 public PeerGroup newGroup(PeerGroupID gid, Advertisement impl, String name, String description) throws PeerGroupException {
373 return group.newGroup(gid, impl, name, description);
375 throw new PeerGroupException("Not implemented");
382 public PeerGroup newGroup(PeerGroupID gid) throws PeerGroupException {
385 return group.newGroup(gid);
387 throw new PeerGroupException("Not implemented");
392 * shortcuts to the well-known services, in order to avoid calls to lookup.
398 public RendezVousService getRendezVousService() {
401 return group.getRendezVousService();
410 public EndpointService getEndpointService() {
413 return group.getEndpointService();
422 public ResolverService getResolverService() {
424 return group.getResolverService();
433 public DiscoveryService getDiscoveryService() {
435 return group.getDiscoveryService();
444 public PeerInfoService getPeerInfoService() {
446 return group.getPeerInfoService();
455 public MembershipService getMembershipService() {
457 return group.getMembershipService();
466 public PipeService getPipeService() {
468 return group.getPipeService();
477 public AccessService getAccessService() {
479 return group.getAccessService();
486 * A few convenience methods. This information is available from
487 * the peer and peergroup advertisement.
493 public PeerGroupID getPeerGroupID() {
496 return (PeerGroupID) adv.getID();
497 } else if (group != null) {
498 return group.getPeerGroupID();
500 throw new RuntimeException("No PeerGroupID");
507 public PeerID getPeerID() {
510 return group.getPeerID();
512 throw new RuntimeException("No PeerID");
519 public String getPeerGroupName() {
522 return adv.getName();
523 } else if (group != null) {
524 return group.getPeerGroupName();
526 throw new RuntimeException("No name");
533 public String getPeerName() {
536 return group.getPeerName();
538 throw new RuntimeException("No name");
545 public ConfigParams getConfigAdvertisement() {
548 return group.getConfigAdvertisement();
550 throw new RuntimeException("No ConfigAdvertisement");
557 public ModuleImplAdvertisement getAllPurposePeerGroupImplAdvertisement() throws Exception {
560 return group.getAllPurposePeerGroupImplAdvertisement();
562 throw new RuntimeException("Not implemented");
569 public void unref() {}
574 public PeerGroup getWeakInterface() {
577 * A LightWeightPeerGroup is already a weak reference
578 * that is not shareable, therefore, return self as