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

LineHitsSource
1 package base.jdbs.network.util;
2  
3 import net.jxta.credential.AuthenticationCredential;
4 import net.jxta.exception.PeerGroupException;
5 import net.jxta.exception.ProtocolNotSupportedException;
6 import net.jxta.impl.membership.pse.StringAuthenticator;
7 import net.jxta.membership.MembershipService;
8 import net.jxta.peergroup.PeerGroup;
9  
10 import org.apache.log4j.Logger;
11  
12 import base.InternetCafe;
13  
140public class JxtaAuthenticationUtil {
15     
160    private static final transient Logger logger = Logger.getLogger(JxtaAuthenticationUtil.class.getName());
17     
18     private static final String STRING_AUTHENTICATOR = "StringAuthentication";
19     private static final String AUTHENTICATION = STRING_AUTHENTICATOR;
20  
21     public static boolean authenticate(PeerGroup peerGroup, String keyStorePassword, String identityPassword, boolean join) {
220        InternetCafe.setStatusBarMessage("Authenticating "+peerGroup.getPeerName()+" in group "+peerGroup.getPeerGroupName()+"...");
230        boolean isAuthenticated = isAuthenticated(peerGroup);
24         // xxx: assumption that ks pwd == id pwd
25         //String pwd = v.getConfig() != null ? v.getConfig().getPassword() : null;
26         
270        String pwd = keyStorePassword;
28         
290        if (pwd != null) {
300            if (keyStorePassword == null ||
310                    keyStorePassword.trim().length() == 0) {
320                keyStorePassword = pwd;
33             }
34             
350            if (identityPassword == null ||
360                    identityPassword.trim().length() == 0) {
370                identityPassword = pwd;
38             }
39         }
40         
410        if (! isAuthenticated) {
420            if (keyStorePassword != null && identityPassword != null) {
430                MembershipService ms = peerGroup.getMembershipService();
440                AuthenticationCredential ac = new AuthenticationCredential(peerGroup, AUTHENTICATION, null);
450                StringAuthenticator sa = null;
46                 
47                 try {
480                    sa = (StringAuthenticator)ms.apply(ac);
490                } catch (ProtocolNotSupportedException pnse) {
500                    logger.fatal(pnse.getMessage());
510                    pnse.printStackTrace();
520                    System.exit(-1);
53                     
540                } catch (PeerGroupException pge) {
550                    logger.fatal(pge.getMessage());
560                    pge.printStackTrace();
570                    System.exit(-1);
580                }
59                 
600                sa.setAuth1_KeyStorePassword(keyStorePassword);
610                sa.setAuth2Identity(peerGroup.getPeerID());
620                sa.setAuth3_IdentityPassword(identityPassword);
63                 
640                isAuthenticated = sa.isReadyForJoin();
650                if(isAuthenticated){
660                    InternetCafe.setStatusBarMessage("Successfully Authenticated...");
67                 }
68                 
690                if (isAuthenticated && join) {
70                     try {
710                        ms.join(sa);
720                    } catch (PeerGroupException pge) {
73                         
740                        logger.fatal("can't join "+peerGroup.getPeerGroupName());
750                        pge.printStackTrace();
760                        System.exit(-1);
770                    }
78                 }
790            }
800        }else logger.info("Peer "+peerGroup.getPeerName()+ " was already authenticated in group "+peerGroup.getPeerGroupName()+"...");
81         
820        return isAuthenticated;
83     }
84     
85     /**
86      * This method is helpful to check if we are authenticated in a PeerGroup.
87      * @param peerGroup The PeerGroup where our authentication must be ckecked.
88      * @return true if we are authenticated in the PeerGroup passed as formal
89      * parameter, false otherwise.
90      */
91     private static boolean isAuthenticated(PeerGroup peerGroup) {
920        boolean result = false;
930        logger.info("Checking authentication in : "+peerGroup.getPeerGroupName());
94         try {
950            result = (peerGroup.getMembershipService().getDefaultCredential() != null);
960        } catch (PeerGroupException pge) {
970            pge.printStackTrace();
980            logger.info("no default credential");
990        }
1000        logger.info("is authenticated: " + peerGroup.getPeerGroupName() +" " + result);
1010        return result;
102     }
103 }

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.