KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgroups > auth > SimpleToken


1 package org.jgroups.auth;
2
3 import org.jgroups.util.Util;
4 import org.jgroups.Message;
5
6 import java.io.DataOutputStream JavaDoc;
7 import java.io.IOException JavaDoc;
8 import java.io.DataInputStream JavaDoc;
9 import java.util.Properties JavaDoc;
10 /**
11  * <p>
12  * This is an example of using a preshared token for authentication purposes. All members of the group have to have the same string value in the JGroups config.
13  * </p>
14  * <p>JGroups config parameters:</p>
15  *<ul>
16  * <li>auth_value (required) = the string to encrypt</li>
17  * </ul>
18  * @see org.jgroups.auth.AuthToken
19  * @author Chris Mills
20  */

21 public class SimpleToken extends AuthToken {
22
23     public static final String JavaDoc TOKEN_ATTR = "auth_value";
24     private String JavaDoc token = null;
25
26     public SimpleToken(){
27         //need an empty constructor
28
}
29
30     public SimpleToken(String JavaDoc token){
31         this.token = token;
32     }
33
34     public void setValue(Properties JavaDoc properties){
35         this.token = (String JavaDoc)properties.get(SimpleToken.TOKEN_ATTR);
36         properties.remove(SimpleToken.TOKEN_ATTR);
37     }
38
39     public String JavaDoc getName(){
40         return "org.jgroups.auth.SimpleToken";
41     }
42
43     public boolean authenticate(AuthToken token, Message msg){
44         if((token != null) && (token instanceof SimpleToken)){
45             //Found a valid Token to authenticate against
46
SimpleToken serverToken = (SimpleToken) token;
47
48             if((this.token != null) && (serverToken.token != null) && (this.token.equalsIgnoreCase(serverToken.token))){
49                 //validated
50
if(log.isDebugEnabled()){
51                     log.debug("SimpleToken match");
52                 }
53                 return true;
54             }else{
55                 if(log.isWarnEnabled()){
56                     log.warn("Authentication failed on SimpleToken");
57                 }
58                 return false;
59             }
60         }
61
62         if(log.isWarnEnabled()){
63             log.warn("Invalid AuthToken instance - wrong type or null");
64         }
65         return false;
66     }
67     /**
68      * Required to serialize the object to pass across the wire
69      * @param out
70      * @throws IOException
71      */

72     public void writeTo(DataOutputStream JavaDoc out) throws IOException JavaDoc {
73         if(log.isDebugEnabled()){
74             log.debug("SimpleToken writeTo()");
75         }
76         Util.writeString(this.token, out);
77     }
78     /**
79      * Required to deserialize the object when read in from the wire
80      * @param in
81      * @throws IOException
82      * @throws IllegalAccessException
83      * @throws InstantiationException
84      */

85     public void readFrom(DataInputStream JavaDoc in) throws IOException JavaDoc, IllegalAccessException JavaDoc, InstantiationException JavaDoc {
86         if(log.isDebugEnabled()){
87             log.debug("SimpleToken readFrom()");
88         }
89         this.token = Util.readString(in);
90     }
91 }
92
Popular Tags