View Javadoc

1   package base.jdbs.network.util;
2   
3   import java.util.Enumeration;
4   import java.util.Vector;
5   
6   import net.jxta.discovery.DiscoveryService;
7   import net.jxta.protocol.PeerAdvertisement;
8   import net.jxta.protocol.PeerGroupAdvertisement;
9   
10  import org.apache.log4j.Logger;
11  
12  public class JxtaUtil {
13  	
14  	private static final transient Logger logger = Logger.getLogger(JxtaUtil.class.getName());
15  	
16  	
17  	/***
18  	 * This method must be used to retrieve each PeerGroupAdvertisement stored in the local peer's cache.
19  	 * @param discoveryService The discovery service used to check the Peer Group Advertisements stored in the local cache.
20  	 * @return A set of PeerGroupAdvertisement found in the local peer's cache.
21  	 */
22  	public static PeerGroupAdvertisement[] groupsInLocalCache(DiscoveryService discoveryService) {
23  		Vector<PeerGroupAdvertisement> result = new Vector<PeerGroupAdvertisement>();
24  		logger.debug("--- local cache (Peer Groups)  ---");
25  		try {
26  			PeerGroupAdvertisement peerGroupAdvertisement = null;
27  			Enumeration enumeration = discoveryService.getLocalAdvertisements(DiscoveryService.GROUP, null, null);
28  			if (enumeration != null) {
29  				while (enumeration.hasMoreElements()) {
30  					peerGroupAdvertisement = (PeerGroupAdvertisement) enumeration.nextElement();
31  					result.add(peerGroupAdvertisement);
32  					logger.debug( peerGroupAdvertisement.getName() + ", group ID = " +peerGroupAdvertisement.getPeerGroupID().toString());
33  				}
34  			}
35  		} catch (Exception ex) {
36  			ex.printStackTrace();
37  		}
38  		logger.debug("--- end local cache ---");
39  		return result.toArray(new PeerGroupAdvertisement[0]);
40  	}
41  	
42  	/***
43  	 * This method must be used to retrieve each PeerAdvertisement stored in the local peer's cache.
44  	 * @param discoveryService The discovery service used to check the Peer Advertisements stored in the local cache.
45  	 * @return A set of PeerAdvertisement found in the local peer's cache.
46  	 */
47  	public static PeerAdvertisement[] peersInLocalCache(DiscoveryService discoveryService) {
48  		Vector<PeerAdvertisement> result = new Vector<PeerAdvertisement>();
49  		logger.debug("--- local cache (Peers)  ---");
50  		try {
51  			PeerAdvertisement peerAdvertisement = null;
52  			Enumeration enumeration = discoveryService.getLocalAdvertisements(DiscoveryService.PEER, null, null);
53  			if (enumeration != null) {
54  				while (enumeration.hasMoreElements()) {
55  					peerAdvertisement = (PeerAdvertisement) enumeration.nextElement();
56  					result.add(peerAdvertisement);
57  					logger.debug( peerAdvertisement.getName() + ", peer ID = " +peerAdvertisement.getPeerID().toString());
58  				}
59  			}
60  		} catch (Exception ex) {
61  			ex.printStackTrace();
62  		}
63  		logger.debug("--- end local cache ---");
64  		return result.toArray(new PeerAdvertisement[0]);
65  	}
66  	
67  	
68  
69  }