Coverage details for base.jdbs.network.util.JxtaGroupUtil

LineHitsSource
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  
130public class JxtaGroupUtil {
14  
150    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) {
260        logger.info("Joining peer group: "+peerGroup.getPeerGroupName()+"...");
270        if(!JxtaAuthenticationUtil.authenticate(peerGroup,keyStorePassword,identityPassword,true)){
280            logger.error("Can't join "+peerGroup.getPeerGroupName()+".");
290            System.exit(-1);
300        }else {
310            logger.info("Successfully joined "+peerGroup.getPeerGroupName()+"...");
32         }
330    }
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) {
480        PeerGroup resultPeerGroup = null;
490        PeerGroupAdvertisement peerGroupAdvertisement = null;
500        logger.info("Creating a new group advertisement called: "+groupName);
51         try {
52             // create a new all purpose peergroup.
530            ModuleImplAdvertisement implAdv = parentGroup.getAllPurposePeerGroupImplAdvertisement();
54             
550            resultPeerGroup = parentGroup.newGroup(null,// Assign new group ID
560                    implAdv, // The implem. adv
570                    groupName, // The name
580                    groupDescription); // Helpful descr.
59             
60             // print the name of the group and the peer group ID
610            peerGroupAdvertisement = resultPeerGroup.getPeerGroupAdvertisement();
620            PeerGroupID GID = peerGroupAdvertisement.getPeerGroupID();
630            logger.info(" Group = " +peerGroupAdvertisement.getName() + "\n Group ID = " + GID.toString());
640        } catch (Exception ex) {
650            logger.fatal("Group "+groupName+" creation failed.");
660            ex.printStackTrace();
670            System.exit(-1);
680        }
69         
70         
710        if(autoRdv){
720            logger.info("Enabling auto rendevous for :"+groupName+".");
730            resultPeerGroup.getRendezVousService().setAutoStart(autoRdv, rendezVousPeriod);
740            resultPeerGroup.getRendezVousService().addListener(rendezvousListener);
75         }
76         
770        if(publish){
780            logger.info("Remote publishing "+groupName+".");
79             try {
80                 // publish this advertisement (send out to other peers and rendezvous peer)
810                parentGroup.getDiscoveryService().publish(resultPeerGroup.getPeerGroupAdvertisement());
820                parentGroup.getDiscoveryService().publish(resultPeerGroup.getPeerAdvertisement());
83                 
840                parentGroup.getDiscoveryService().remotePublish(resultPeerGroup.getPeerGroupAdvertisement(), DiscoveryService.GROUP);
850                parentGroup.getDiscoveryService().remotePublish(resultPeerGroup.getPeerAdvertisement(), DiscoveryService.PEER);
86                 
870                logger.info("Group published successfully.");
880            } catch (Exception ex) {
890                logger.fatal("Can't remotelly publish the group "+groupName+" .");
900                ex.printStackTrace();
910                System.exit(-1);
920            }
93         }
940        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){
1090        logger.info("Searching "+peerGroupName + " in the local cache...");
1100        PeerGroup resultPeerGroup = null;
1110        PeerGroupAdvertisement[] localCachePGAdv = JxtaUtil.groupsInLocalCache(parentGroup.getDiscoveryService());
1120        for(int i=0;i<localCachePGAdv.length;i++){
1130            if(localCachePGAdv[i].getName().equals(peerGroupName)){
114                 try {
1150                    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.
1170                    resultPeerGroup = parentGroup.newGroup(localCachePGAdv[i]);
1180                    break;
1190                } catch (PeerGroupException ex) {
1200                    logger.error("can't create "+peerGroupName+" from local cache.");
1210                    ex.printStackTrace();
122                 }
123             }
124         }
125         
1260        if(autoRdv && resultPeerGroup!=null){
1270            logger.info("Enabling auto rendevous for :"+peerGroupName+".");
1280            resultPeerGroup.getRendezVousService().setAutoStart(autoRdv, rendezVousPeriod);
1290            resultPeerGroup.getRendezVousService().addListener(rendezvousListener);
130         }
131         
1320        if(publish && resultPeerGroup!=null){
1330            logger.info("Remote publishing "+peerGroupName+".");
134             try {
135                 // publish this advertisement (send out to other peers and rendezvous peer)
1360                parentGroup.getDiscoveryService().publish(resultPeerGroup.getPeerGroupAdvertisement());
1370                parentGroup.getDiscoveryService().publish(resultPeerGroup.getPeerAdvertisement());
138                 
1390                parentGroup.getDiscoveryService().remotePublish(resultPeerGroup.getPeerGroupAdvertisement(), DiscoveryService.GROUP);
1400                parentGroup.getDiscoveryService().remotePublish(resultPeerGroup.getPeerAdvertisement(), DiscoveryService.PEER);
141                 
1420                logger.info("Group published successfully.");
1430            } catch (Exception ex) {
1440                logger.fatal("Can't remotelly publish the group "+peerGroupName+" .");
1450                ex.printStackTrace();
1460                System.exit(-1);
1470            }
148         }
1490        return resultPeerGroup;
150     }
151 }

this report was generated by version 1.0.5 of jcoverage.
visit www.jcoverage.com for updates.

copyright © 2003, jcoverage ltd. all rights reserved.
Java is a trademark of Sun Microsystems, Inc. in the United States and other countries.