KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > unixauth > UNIXRole


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.unixauth;
21
22 import java.io.Serializable JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.StringTokenizer JavaDoc;
26
27 import com.sslexplorer.realms.Realm;
28 import com.sslexplorer.security.Role;
29
30 /**
31  * Implementation of a {@link com.sslexplorer.security.Role}
32  * for <i>Unix Roles</i>.
33  *
34  * @author James D Robinson <a HREF="mailto:james@3sp.com">&lt;james@3sp.com&gt;</a>
35  *
36  */

37 public class UNIXRole implements Role<UNIXRole>, Serializable JavaDoc {
38
39     private String JavaDoc name;
40     private int gid;
41     private String JavaDoc[] members;
42     private final Realm realm;
43     
44     /**
45      * @param realm
46      * @param etcGroupEntry
47      */

48     public UNIXRole(Realm realm, String JavaDoc etcGroupEntry) {
49         this.realm = realm;
50         String JavaDoc[] elements = etcGroupEntry.split(":");
51         name = elements[0];
52         if (elements.length > 2 && !name.equals("+")) {
53             gid = Integer.parseInt(elements[2]);
54             List JavaDoc<String JavaDoc> m = new ArrayList JavaDoc<String JavaDoc>();
55             if (elements.length > 3) {
56                 StringTokenizer JavaDoc z = new StringTokenizer JavaDoc(elements[3], ",");
57                 while (z.hasMoreTokens()) {
58                     m.add(z.nextToken());
59                 }
60             }
61             members = new String JavaDoc[m.size()];
62             m.toArray(members);
63         }
64         else {
65             throw new IllegalArgumentException JavaDoc("Invalid format.");
66         }
67     }
68
69     /**
70      * @return int
71      */

72     public int getGid() {
73         return gid;
74     }
75
76     /**
77      * @param username
78      * @return boolean
79      */

80     public boolean containsMember(String JavaDoc username) {
81         for (int i = 0; i < members.length; i++) {
82             if (members[i].equals(username)) {
83                 return true;
84             }
85         }
86         return false;
87     }
88
89     /*
90      * (non-Javadoc)
91      *
92      * @see com.sslexplorer.permissions.Principal#getPrincipalName()
93      */

94     public String JavaDoc getPrincipalName() {
95         return name;
96     }
97     
98     public String JavaDoc toString() {
99         return getPrincipalName();
100     }
101
102     public int compareTo(UNIXRole o) {
103         return getPrincipalName().compareTo(o.getPrincipalName());
104     }
105
106     /* (non-Javadoc)
107      * @see com.sslexplorer.policyframework.Principal#getRealm()
108      */

109     public Realm getRealm() {
110         return realm;
111     }
112 }
Popular Tags