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 public class JxtaGroupUtil {
14
15 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 logger.info("Joining peer group: "+peerGroup.getPeerGroupName()+"...");
27 if(!JxtaAuthenticationUtil.authenticate(peerGroup,keyStorePassword,identityPassword,true)){
28 logger.error("Can't join "+peerGroup.getPeerGroupName()+".");
29 System.exit(-1);
30 }else {
31 logger.info("Successfully joined "+peerGroup.getPeerGroupName()+"...");
32 }
33 }
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 PeerGroup resultPeerGroup = null;
49 PeerGroupAdvertisement peerGroupAdvertisement = null;
50 logger.info("Creating a new group advertisement called: "+groupName);
51 try {
52
53 ModuleImplAdvertisement implAdv = parentGroup.getAllPurposePeerGroupImplAdvertisement();
54
55 resultPeerGroup = parentGroup.newGroup(null,
56 implAdv,
57 groupName,
58 groupDescription);
59
60
61 peerGroupAdvertisement = resultPeerGroup.getPeerGroupAdvertisement();
62 PeerGroupID GID = peerGroupAdvertisement.getPeerGroupID();
63 logger.info(" Group = " +peerGroupAdvertisement.getName() + "\n Group ID = " + GID.toString());
64 } catch (Exception ex) {
65 logger.fatal("Group "+groupName+" creation failed.");
66 ex.printStackTrace();
67 System.exit(-1);
68 }
69
70
71 if(autoRdv){
72 logger.info("Enabling auto rendevous for :"+groupName+".");
73 resultPeerGroup.getRendezVousService().setAutoStart(autoRdv, rendezVousPeriod);
74 resultPeerGroup.getRendezVousService().addListener(rendezvousListener);
75 }
76
77 if(publish){
78 logger.info("Remote publishing "+groupName+".");
79 try {
80
81 parentGroup.getDiscoveryService().publish(resultPeerGroup.getPeerGroupAdvertisement());
82 parentGroup.getDiscoveryService().publish(resultPeerGroup.getPeerAdvertisement());
83
84 parentGroup.getDiscoveryService().remotePublish(resultPeerGroup.getPeerGroupAdvertisement(), DiscoveryService.GROUP);
85 parentGroup.getDiscoveryService().remotePublish(resultPeerGroup.getPeerAdvertisement(), DiscoveryService.PEER);
86
87 logger.info("Group published successfully.");
88 } catch (Exception ex) {
89 logger.fatal("Can't remotelly publish the group "+groupName+" .");
90 ex.printStackTrace();
91 System.exit(-1);
92 }
93 }
94 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 logger.info("Searching "+peerGroupName + " in the local cache...");
110 PeerGroup resultPeerGroup = null;
111 PeerGroupAdvertisement[] localCachePGAdv = JxtaUtil.groupsInLocalCache(parentGroup.getDiscoveryService());
112 for(int i=0;i<localCachePGAdv.length;i++){
113 if(localCachePGAdv[i].getName().equals(peerGroupName)){
114 try {
115 logger.info("The specified "+peerGroupName + " has been found in the local cache. Instantiating it...");
116
117 resultPeerGroup = parentGroup.newGroup(localCachePGAdv[i]);
118 break;
119 } catch (PeerGroupException ex) {
120 logger.error("can't create "+peerGroupName+" from local cache.");
121 ex.printStackTrace();
122 }
123 }
124 }
125
126 if(autoRdv && resultPeerGroup!=null){
127 logger.info("Enabling auto rendevous for :"+peerGroupName+".");
128 resultPeerGroup.getRendezVousService().setAutoStart(autoRdv, rendezVousPeriod);
129 resultPeerGroup.getRendezVousService().addListener(rendezvousListener);
130 }
131
132 if(publish && resultPeerGroup!=null){
133 logger.info("Remote publishing "+peerGroupName+".");
134 try {
135
136 parentGroup.getDiscoveryService().publish(resultPeerGroup.getPeerGroupAdvertisement());
137 parentGroup.getDiscoveryService().publish(resultPeerGroup.getPeerAdvertisement());
138
139 parentGroup.getDiscoveryService().remotePublish(resultPeerGroup.getPeerGroupAdvertisement(), DiscoveryService.GROUP);
140 parentGroup.getDiscoveryService().remotePublish(resultPeerGroup.getPeerAdvertisement(), DiscoveryService.PEER);
141
142 logger.info("Group published successfully.");
143 } catch (Exception ex) {
144 logger.fatal("Can't remotelly publish the group "+peerGroupName+" .");
145 ex.printStackTrace();
146 System.exit(-1);
147 }
148 }
149 return resultPeerGroup;
150 }
151 }