KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jgroups.auth;
23
24 import org.jgroups.Message;
25 import org.jgroups.util.Util;
26
27 import java.util.Properties JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.StringTokenizer JavaDoc;
31 import java.io.DataOutputStream JavaDoc;
32 import java.io.IOException JavaDoc;
33 import java.io.DataInputStream JavaDoc;
34
35 /**
36  * <p>
37  * The FixedMemberShipToken object predefines a list of IP addresses and Ports that can join the group.
38  *</p>
39  * <p>
40  * Configuration parameters for this example are shown below:
41  * </p>
42  * <ul>
43  * <li>fixed_members_value (required) = List of IP addresses & ports (optionally) - ports must be seperated by a '/' e.g. 127.0.0.1/1010*127.0.0.1/4567</li>
44  * <li>fixed_members_seperator (required) = The seperator used between IP addresses - e.g. *</li>
45  * </ul>
46  * @author Chris Mills (millsy@jboss.com)
47  */

48 public class FixedMembershipToken extends AuthToken {
49     private static final String JavaDoc FIXED_MEMBERS_ATTR = "fixed_members_value";
50     private static final String JavaDoc FIXED_MEMBERS_SEPERATOR_ATTR = "fixed_members_seperator";
51
52     private List JavaDoc memberList = null;
53     private String JavaDoc token = "emptyToken";
54
55     public FixedMembershipToken(){
56     }
57
58     public String JavaDoc getName(){
59         return "org.jgroups.auth.FixedMembershipToken";
60     }
61
62     public boolean authenticate(AuthToken token, Message msg){
63         if((token != null) && (token instanceof FixedMembershipToken) && (this.memberList != null)){
64             //Found a valid Token to authenticate against
65
FixedMembershipToken serverToken = (FixedMembershipToken) token;
66
67             String JavaDoc sourceAddressWithPort = msg.getSrc().toString();
68             String JavaDoc sourceAddressWithoutPort = sourceAddressWithPort.substring(0, sourceAddressWithPort.indexOf(":"));
69
70             if(log.isDebugEnabled()){
71                 log.debug("AUTHToken received from " + sourceAddressWithPort);
72             }
73
74             if((this.memberList.contains(sourceAddressWithPort)) || (this.memberList.contains(sourceAddressWithoutPort))){
75                 //validated
76
if(log.isDebugEnabled()){
77                     log.debug("FixedMembershipToken match");
78                 }
79                 return true;
80             }else{
81                 if(log.isWarnEnabled()){
82                     log.warn("Authentication failed on FixedMembershipToken");
83                 }
84                 return false;
85             }
86         }
87
88         if(log.isWarnEnabled()){
89             log.warn("Invalid AuthToken instance - wrong type or null");
90         }
91         return false;
92     }
93
94     public void setValue(Properties JavaDoc properties){
95         memberList = new ArrayList JavaDoc();
96         StringTokenizer JavaDoc memberListTokenizer = new StringTokenizer JavaDoc((String JavaDoc)properties.get(FixedMembershipToken.FIXED_MEMBERS_ATTR),
97                 (String JavaDoc)properties.get(FixedMembershipToken.FIXED_MEMBERS_SEPERATOR_ATTR));
98         while(memberListTokenizer.hasMoreTokens()){
99             memberList.add(memberListTokenizer.nextToken().replace('/', ':'));
100         }
101         properties.remove(FixedMembershipToken.FIXED_MEMBERS_ATTR);
102         properties.remove(FixedMembershipToken.FIXED_MEMBERS_SEPERATOR_ATTR);
103     }
104     /**
105      * Required to serialize the object to pass across the wire
106      * @param out
107      * @throws java.io.IOException
108      */

109     public void writeTo(DataOutputStream JavaDoc out) throws IOException JavaDoc {
110         if(log.isDebugEnabled()){
111             log.debug("SimpleToken writeTo()");
112         }
113         Util.writeString(this.token, out);
114     }
115     /**
116      * Required to deserialize the object when read in from the wire
117      * @param in
118      * @throws IOException
119      * @throws IllegalAccessException
120      * @throws InstantiationException
121      */

122     public void readFrom(DataInputStream JavaDoc in) throws IOException JavaDoc, IllegalAccessException JavaDoc, InstantiationException JavaDoc {
123         if(log.isDebugEnabled()){
124             log.debug("SimpleToken readFrom()");
125         }
126         this.token = Util.readString(in);
127     }
128 }
129
Popular Tags