Line | Hits | Source |
---|---|---|
1 | package base.jdbs.network.util; | |
2 | ||
3 | import net.jxta.discovery.DiscoveryService; | |
4 | import net.jxta.exception.PeerGroupException; | |
5 | import net.jxta.peergroup.PeerGroup; | |
6 | import net.jxta.peergroup.PeerGroupID; | |
7 | import net.jxta.protocol.ModuleImplAdvertisement; | |
8 | import net.jxta.protocol.PeerGroupAdvertisement; | |
9 | import net.jxta.rendezvous.RendezvousListener; | |
10 | ||
11 | import org.apache.log4j.Logger; | |
12 | ||
13 | 0 | public class JxtaGroupUtil { |
14 | ||
15 | 0 | private static final transient Logger logger = Logger.getLogger(JxtaGroupUtil.class.getName()); |
16 | ||
17 | /** | |
18 | * This method is used to join a PeerGroup authenticating our peer | |
19 | * on the bases of provided key store and identity passwords. It | |
20 | * assumes that our passwords are equals (keyStorePassword == identityPassword). | |
21 | * @param peerGroup The PeerGroup where we want to join. | |
22 | * @param keyStorePassword Our key store password. | |
23 | * @param identityPassword Our identity password. | |
24 | */ | |
25 | public static void joinGroup(PeerGroup peerGroup, String keyStorePassword, String identityPassword) { | |
26 | 0 | logger.info("Joining peer group: "+peerGroup.getPeerGroupName()+"..."); |
27 | 0 | if(!JxtaAuthenticationUtil.authenticate(peerGroup,keyStorePassword,identityPassword,true)){ |
28 | 0 | logger.error("Can't join "+peerGroup.getPeerGroupName()+"."); |
29 | 0 | System.exit(-1); |
30 | 0 | }else { |
31 | 0 | logger.info("Successfully joined "+peerGroup.getPeerGroupName()+"..."); |
32 | } | |
33 | 0 | } |
34 | ||
35 | /** | |
36 | * This method is used to create a new PeerGroup from its parent group. | |
37 | * @param parentGroup The parent PeerGroup from wich the new PeerGroup must be created. | |
38 | * @param groupName A name for the new PeerGroup to create. | |
39 | * @param groupDescription A description for the new PeerGroup to create. | |
40 | * @param autoRdv States if an auto rendezvous must be setted for the group to be created. | |
41 | * @param rendezVousPeriod The auto rendezvous period for the new group's rendezvous service. | |
42 | * @param rendezvousListener A rendezvous listener for the new peer group. | |
43 | * @param publish States if the created new group must be published locally and remotelly, | |
44 | * note that should be grant a rendezvous connection before trying to publish the new Group remotelly. | |
45 | * @return A new PeerGroup. | |
46 | */ | |
47 | public static PeerGroup createGroup(PeerGroup parentGroup, String groupName, String groupDescription, boolean autoRdv, int rendezVousPeriod, RendezvousListener rendezvousListener, boolean publish) { | |
48 | 0 | PeerGroup resultPeerGroup = null; |
49 | 0 | PeerGroupAdvertisement peerGroupAdvertisement = null; |
50 | 0 | logger.info("Creating a new group advertisement called: "+groupName); |
51 | try { | |
52 | // create a new all purpose peergroup. | |
53 | 0 | ModuleImplAdvertisement implAdv = parentGroup.getAllPurposePeerGroupImplAdvertisement(); |
54 | ||
55 | 0 | resultPeerGroup = parentGroup.newGroup(null,// Assign new group ID |
56 | 0 | implAdv, // The implem. adv |
57 | 0 | groupName, // The name |
58 | 0 | groupDescription); // Helpful descr. |
59 | ||
60 | // print the name of the group and the peer group ID | |
61 | 0 | peerGroupAdvertisement = resultPeerGroup.getPeerGroupAdvertisement(); |
62 | 0 | PeerGroupID GID = peerGroupAdvertisement.getPeerGroupID(); |
63 | 0 | logger.info(" Group = " +peerGroupAdvertisement.getName() + "\n Group ID = " + GID.toString()); |
64 | 0 | } catch (Exception ex) { |
65 | 0 | logger.fatal("Group "+groupName+" creation failed."); |
66 | 0 | ex.printStackTrace(); |
67 | 0 | System.exit(-1); |
68 | 0 | } |
69 | ||
70 | ||
71 | 0 | if(autoRdv){ |
72 | 0 | logger.info("Enabling auto rendevous for :"+groupName+"."); |
73 | 0 | resultPeerGroup.getRendezVousService().setAutoStart(autoRdv, rendezVousPeriod); |
74 | 0 | resultPeerGroup.getRendezVousService().addListener(rendezvousListener); |
75 | } | |
76 | ||
77 | 0 | if(publish){ |
78 | 0 | logger.info("Remote publishing "+groupName+"."); |
79 | try { | |
80 | // publish this advertisement (send out to other peers and rendezvous peer) | |
81 | 0 | parentGroup.getDiscoveryService().publish(resultPeerGroup.getPeerGroupAdvertisement()); |
82 | 0 | parentGroup.getDiscoveryService().publish(resultPeerGroup.getPeerAdvertisement()); |
83 | ||
84 | 0 | parentGroup.getDiscoveryService().remotePublish(resultPeerGroup.getPeerGroupAdvertisement(), DiscoveryService.GROUP); |
85 | 0 | parentGroup.getDiscoveryService().remotePublish(resultPeerGroup.getPeerAdvertisement(), DiscoveryService.PEER); |
86 | ||
87 | 0 | logger.info("Group published successfully."); |
88 | 0 | } catch (Exception ex) { |
89 | 0 | logger.fatal("Can't remotelly publish the group "+groupName+" ."); |
90 | 0 | ex.printStackTrace(); |
91 | 0 | System.exit(-1); |
92 | 0 | } |
93 | } | |
94 | 0 | return resultPeerGroup; |
95 | } | |
96 | ||
97 | /** | |
98 | * This method is used to create a PeerGroup whose name is peerGroupName from the local peer's cache.. | |
99 | * @param parentGroup The parent PeerGroup from wich the local cache PeerGroup must be instantiated. | |
100 | * @param peerGroupName The Peer Group's name to be instantiated from the local peer's cache. | |
101 | * @param autoRdv States if an auto rendezvous must be setted for the group to be created. | |
102 | * @param rendezVousPeriod The auto rendezvous period for the new group's rendezvous service. | |
103 | * @param rendezvousListener A rendezvous listener for the new peer group. | |
104 | * @param publish States if the created new group must be published locally and remotelly, | |
105 | * note that should be grant a rendezvous connection before trying to publish the new Group remotelly. | |
106 | * @return A new PeerGroup whose name is peerGroupName from the local cache, null if the local cache doesn't contains such PeerGroup. | |
107 | */ | |
108 | public static PeerGroup createGroupFromLocalCache(PeerGroup parentGroup, String peerGroupName, boolean autoRdv, int rendezVousPeriod, RendezvousListener rendezvousListener, boolean publish){ | |
109 | 0 | logger.info("Searching "+peerGroupName + " in the local cache..."); |
110 | 0 | PeerGroup resultPeerGroup = null; |
111 | 0 | PeerGroupAdvertisement[] localCachePGAdv = JxtaUtil.groupsInLocalCache(parentGroup.getDiscoveryService()); |
112 | 0 | for(int i=0;i<localCachePGAdv.length;i++){ |
113 | 0 | if(localCachePGAdv[i].getName().equals(peerGroupName)){ |
114 | try { | |
115 | 0 | logger.info("The specified "+peerGroupName + " has been found in the local cache. Instantiating it..."); |
116 | //The peer group is in our cache, lets try to instantiate it from the found advertisement. | |
117 | 0 | resultPeerGroup = parentGroup.newGroup(localCachePGAdv[i]); |
118 | 0 | break; |
119 | 0 | } catch (PeerGroupException ex) { |
120 | 0 | logger.error("can't create "+peerGroupName+" from local cache."); |
121 | 0 | ex.printStackTrace(); |
122 | } | |
123 | } | |
124 | } | |
125 | ||
126 | 0 | if(autoRdv && resultPeerGroup!=null){ |
127 | 0 | logger.info("Enabling auto rendevous for :"+peerGroupName+"."); |
128 | 0 | resultPeerGroup.getRendezVousService().setAutoStart(autoRdv, rendezVousPeriod); |
129 | 0 | resultPeerGroup.getRendezVousService().addListener(rendezvousListener); |
130 | } | |
131 | ||
132 | 0 | if(publish && resultPeerGroup!=null){ |
133 | 0 | logger.info("Remote publishing "+peerGroupName+"."); |
134 | try { | |
135 | // publish this advertisement (send out to other peers and rendezvous peer) | |
136 | 0 | parentGroup.getDiscoveryService().publish(resultPeerGroup.getPeerGroupAdvertisement()); |
137 | 0 | parentGroup.getDiscoveryService().publish(resultPeerGroup.getPeerAdvertisement()); |
138 | ||
139 | 0 | parentGroup.getDiscoveryService().remotePublish(resultPeerGroup.getPeerGroupAdvertisement(), DiscoveryService.GROUP); |
140 | 0 | parentGroup.getDiscoveryService().remotePublish(resultPeerGroup.getPeerAdvertisement(), DiscoveryService.PEER); |
141 | ||
142 | 0 | logger.info("Group published successfully."); |
143 | 0 | } catch (Exception ex) { |
144 | 0 | logger.fatal("Can't remotelly publish the group "+peerGroupName+" ."); |
145 | 0 | ex.printStackTrace(); |
146 | 0 | System.exit(-1); |
147 | 0 | } |
148 | } | |
149 | 0 | return resultPeerGroup; |
150 | } | |
151 | } |
this report was generated by version 1.0.5 of jcoverage. |
copyright © 2003, jcoverage ltd. all rights reserved. |