]> sjero.net Git - linphone/blob - p2pproxy/dependencies-src/jxse-src-2.5/api/src/net/jxta/peergroup/LightWeightPeerGroup.java
remove mediastreamer2 and add it as a submodule instead.
[linphone] / p2pproxy / dependencies-src / jxse-src-2.5 / api / src / net / jxta / peergroup / LightWeightPeerGroup.java
1 /*
2  * Copyright (c) 2003-2007 Sun Microsystems, Inc.  All rights reserved.
3  *  
4  *  The Sun Project JXTA(TM) Software License
5  *  
6  *  Redistribution and use in source and binary forms, with or without 
7  *  modification, are permitted provided that the following conditions are met:
8  *  
9  *  1. Redistributions of source code must retain the above copyright notice,
10  *     this list of conditions and the following disclaimer.
11  *  
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.
15  *  
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.
21  *  
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.
26  *  
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.
29  *  
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.
40  *  
41  *  JXTA is a registered trademark of Sun Microsystems, Inc. in the United 
42  *  States and other countries.
43  *  
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.
47  *  
48  *  ====================================================================
49  *  
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.
53  *  
54  *  This license is based on the BSD license adopted by the Apache Foundation. 
55  */
56
57 package net.jxta.peergroup;
58
59
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;
86
87 import java.io.IOException;
88 import java.net.URI;
89 import java.util.Collections;
90 import java.util.Iterator;
91
92
93 /**
94  * LightWeightPeerGroup is a class intended to help
95  * building PeerGroup that can inherit one or more
96  * services from a parent PeerGroup.
97  * <p/>
98  * An LightWeightPeerGroup implements PeerGroup and is to
99  * be used like a PeerGroup by applications.
100  * <p/>
101  * This class is intended to be extended/implemented.
102  * <p/>
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.
111  */
112
113 public class LightWeightPeerGroup implements PeerGroup {
114
115     /**
116      * Log4J Logger
117      */
118     private static final Logger LOG = Logger.getLogger(LightWeightPeerGroup.class.getName());
119
120     private PeerGroup group = null;
121     private ID assignedID = null;
122     private ModuleImplAdvertisement implAdv = null;
123     private final PeerGroupAdvertisement adv;
124
125     /**
126      * Constructor
127      * <p/>
128      * All classes that extend this class must invoke this
129      * constructor.
130      *
131      * @param adv PeerGroupAdvertisement of this LightWeightPeerGroup.
132      *            Note that only the PeerGroupID is used.
133      */
134     public LightWeightPeerGroup(PeerGroupAdvertisement adv) {
135         this.adv = adv;
136     }
137
138     /***********************************************************
139      ** Module API
140      ***********************************************************/
141     
142     /**
143      * {@inheritDoc}
144      */
145     public void init(PeerGroup group, ID assignedID, Advertisement implAdv) {
146         this.group = group;
147         this.assignedID = assignedID;
148         this.implAdv = (ModuleImplAdvertisement) implAdv;
149     }
150
151     /**
152      * {@inheritDoc}
153      */
154     public int startApp(String[] args) {
155         if (null == group) {
156             if (Logging.SHOW_SEVERE && LOG.isLoggable(Level.SEVERE)) {
157                 LOG.severe("No base peer group defined.");
158             }
159             return -1;
160         }
161
162         return START_OK;
163     }
164
165     /**
166      * {@inheritDoc}
167      */
168     public void stopApp() {}
169
170     /**
171      * {@inheritDoc}
172      */
173     public Service getInterface() {
174         return this;
175     }
176
177     /**
178      * {@inheritDoc}
179      */
180     public ModuleImplAdvertisement getImplAdvertisement() {
181         return implAdv;
182     }
183
184     /***********************************************************
185      ** PeerGroup API
186      ***********************************************************/
187
188     /**
189      * {@inheritDoc}
190      */
191     public ThreadGroup getHomeThreadGroup() {
192         if (group != null) {
193             return group.getHomeThreadGroup();
194         } else {
195             return null;
196         }
197     }
198
199     /***********************************************************
200      ** PeerGroup API
201      ***********************************************************/
202
203     /**
204      * {@inheritDoc}
205      */
206     public URI getStoreHome() {
207         if (group != null) {
208             return group.getStoreHome();
209         } else {
210             return null;
211         }
212     }
213
214     /**
215      * {@inheritDoc}
216      */
217     public JxtaLoader getLoader() {
218         if (group != null) {
219             return group.getLoader();
220         } else {
221             return null;
222         }
223     }
224
225     /**
226      * {@inheritDoc}
227      */
228     public PeerGroup getParentGroup() {
229
230         try {
231             return group;
232         } catch (Exception ex) {
233             if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
234                 LOG.fine("LightWeightPeerGroup is a base PeerGroup: no parent");
235             }
236             throw new RuntimeException("LightWeightPeerGroup is a base PeerGroup: no parent");
237         }
238     }
239
240     /**
241      * {@inheritDoc}
242      */
243     public boolean isRendezvous() {
244
245         return group != null && group.isRendezvous();
246     }
247
248     /**
249      * {@inheritDoc}
250      */
251     public PeerGroupAdvertisement getPeerGroupAdvertisement() {
252
253         if (adv != null) {
254             return adv;
255         } else if (group != null) {
256             return group.getPeerGroupAdvertisement();
257         } else {
258             return null;
259         }
260     }
261
262     /**
263      * {@inheritDoc}
264      */
265     public PeerAdvertisement getPeerAdvertisement() {
266
267         if (group != null) {
268             return group.getPeerAdvertisement();
269         } else {
270             return null;
271         }
272     }
273
274     /**
275      * {@inheritDoc}
276      */
277     public Service lookupService(ID name) throws ServiceNotFoundException {
278
279         if (group != null) {
280             return group.lookupService(name);
281         } else {
282             throw new ServiceNotFoundException("Not implemented");
283         }
284     }
285
286     /**
287      * {@inheritDoc}
288      */
289     public Service lookupService(ID name, int ignoredForNow) throws ServiceNotFoundException {
290
291         if (group != null) {
292             return group.lookupService(name);
293         } else {
294             throw new ServiceNotFoundException("Not implemented");
295         }
296     }
297
298     /**
299      * {@inheritDoc}
300      */
301     public Iterator getRoleMap(ID name) {
302
303         if (group != null) {
304             return group.getRoleMap(name);
305         } else {
306             // No translation; use the given name in a singleton.
307             return Collections.singletonList(name).iterator();
308         }
309     }
310
311     /**
312      * {@inheritDoc}
313      */
314     public boolean compatible(Element compat) {
315
316         return group != null && group.compatible(compat);
317     }
318
319     /**
320      * {@inheritDoc}
321      */
322     public Module loadModule(ID assignedID, Advertisement impl) throws ProtocolNotSupportedException, PeerGroupException {
323
324         if (group != null) {
325             return group.loadModule(assignedID, impl);
326         } else {
327             throw new ProtocolNotSupportedException("LightWeightPeerGroup does not implement this operation");
328         }
329     }
330
331     /**
332      * {@inheritDoc}
333      */
334     public Module loadModule(ID assignedID, ModuleSpecID specID, int where) {
335
336         if (group != null) {
337             return group.loadModule(assignedID, specID, where);
338         } else {
339             return null;
340         }
341     }
342
343     /**
344      * {@inheritDoc}
345      */
346     public void publishGroup(String name, String description) throws IOException {
347
348         if (group != null) {
349             group.publishGroup(name, description);
350         } else {
351             throw new IOException("Not implemented");
352         }
353     }
354
355     /**
356      * {@inheritDoc}
357      */
358     public PeerGroup newGroup(Advertisement pgAdv) throws PeerGroupException {
359
360         if (group != null) {
361             return group.newGroup(pgAdv);
362         } else {
363             throw new PeerGroupException("Not implemented");
364         }
365     }
366
367     /**
368      * {@inheritDoc}
369      */
370     public PeerGroup newGroup(PeerGroupID gid, Advertisement impl, String name, String description) throws PeerGroupException {
371
372         if (group != null) {
373             return group.newGroup(gid, impl, name, description);
374         } else {
375             throw new PeerGroupException("Not implemented");
376         }
377     }
378
379     /**
380      * {@inheritDoc}
381      */
382     public PeerGroup newGroup(PeerGroupID gid) throws PeerGroupException {
383
384         if (group != null) {
385             return group.newGroup(gid);
386         } else {
387             throw new PeerGroupException("Not implemented");
388         }
389     }
390
391     /*
392      * shortcuts to the well-known services, in order to avoid calls to lookup.
393      */
394
395     /**
396      * {@inheritDoc}
397      */
398     public RendezVousService getRendezVousService() {
399
400         if (group != null) {
401             return group.getRendezVousService();
402         } else {
403             return null;
404         }
405     }
406
407     /**
408      * {@inheritDoc}
409      */
410     public EndpointService getEndpointService() {
411
412         if (group != null) {
413             return group.getEndpointService();
414         } else {
415             return null;
416         }
417     }
418
419     /**
420      * {@inheritDoc}
421      */
422     public ResolverService getResolverService() {
423         if (group != null) {
424             return group.getResolverService();
425         } else {
426             return null;
427         }
428     }
429
430     /**
431      * {@inheritDoc}
432      */
433     public DiscoveryService getDiscoveryService() {
434         if (group != null) {
435             return group.getDiscoveryService();
436         } else {
437             return null;
438         }
439     }
440
441     /**
442      * {@inheritDoc}
443      */
444     public PeerInfoService getPeerInfoService() {
445         if (group != null) {
446             return group.getPeerInfoService();
447         } else {
448             return null;
449         }
450     }
451
452     /**
453      * {@inheritDoc}
454      */
455     public MembershipService getMembershipService() {
456         if (group != null) {
457             return group.getMembershipService();
458         } else {
459             return null;
460         }
461     }
462
463     /**
464      * {@inheritDoc}
465      */
466     public PipeService getPipeService() {
467         if (group != null) {
468             return group.getPipeService();
469         } else {
470             return null;
471         }
472     }
473
474     /**
475      * {@inheritDoc}
476      */
477     public AccessService getAccessService() {
478         if (group != null) {
479             return group.getAccessService();
480         } else {
481             return null;
482         }
483     }
484
485     /*
486      * A few convenience methods. This information is available from
487      * the peer and peergroup advertisement.
488      */
489
490     /**
491      * {@inheritDoc}
492      */
493     public PeerGroupID getPeerGroupID() {
494
495         if (adv != null) {
496             return (PeerGroupID) adv.getID();
497         } else if (group != null) {
498             return group.getPeerGroupID();
499         } else {
500             throw new RuntimeException("No PeerGroupID");
501         }
502     }
503
504     /**
505      * {@inheritDoc}
506      */
507     public PeerID getPeerID() {
508
509         if (group != null) {
510             return group.getPeerID();
511         } else {
512             throw new RuntimeException("No PeerID");
513         }
514     }
515
516     /**
517      * {@inheritDoc}
518      */
519     public String getPeerGroupName() {
520
521         if (adv != null) {
522             return adv.getName();
523         } else if (group != null) {
524             return group.getPeerGroupName();
525         } else {
526             throw new RuntimeException("No name");
527         }
528     }
529
530     /**
531      * {@inheritDoc}
532      */
533     public String getPeerName() {
534
535         if (group != null) {
536             return group.getPeerName();
537         } else {
538             throw new RuntimeException("No name");
539         }
540     }
541
542     /**
543      * {@inheritDoc}
544      */
545     public ConfigParams getConfigAdvertisement() {
546
547         if (group != null) {
548             return group.getConfigAdvertisement();
549         } else {
550             throw new RuntimeException("No ConfigAdvertisement");
551         }
552     }
553
554     /**
555      * {@inheritDoc}
556      */
557     public ModuleImplAdvertisement getAllPurposePeerGroupImplAdvertisement() throws Exception {
558
559         if (group != null) {
560             return group.getAllPurposePeerGroupImplAdvertisement();
561         } else {
562             throw new RuntimeException("Not implemented");
563         }
564     }
565
566     /**
567      * {@inheritDoc}
568      */
569     public void unref() {}
570
571     /**
572      * {@inheritDoc}
573      */
574     public PeerGroup getWeakInterface() {
575
576         /**
577          * A LightWeightPeerGroup is already a weak reference
578          * that is not shareable, therefore, return self as
579          * a weak reference.
580          **/
581
582         return this;
583     }
584 }