KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > catalina > cluster > session > SerializablePrincipal


1 /*
2  * Copyright 1999,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17
18 package org.apache.catalina.cluster.session;
19
20
21 import java.util.Arrays JavaDoc;
22 import java.util.List JavaDoc;
23 import org.apache.catalina.Realm;
24
25
26 /**
27  * Generic implementation of <strong>java.security.Principal</strong> that
28  * is available for use by <code>Realm</code> implementations.
29  * The GenericPrincipal does NOT implement serializable and I didn't want to change that implementation
30  * hence I implemented this one instead.
31  * @author Filip Hanik
32  * @version $Revision: 1.5 $ $Date: 2004/12/09 14:36:43 $
33  */

34 import org.apache.catalina.realm.GenericPrincipal;
35 public class SerializablePrincipal implements java.io.Serializable JavaDoc {
36
37
38     // ----------------------------------------------------------- Constructors
39

40     public SerializablePrincipal()
41     {
42         super();
43     }
44     /**
45      * Construct a new Principal, associated with the specified Realm, for the
46      * specified username and password.
47      *
48      * @param realm The Realm that owns this Principal
49      * @param name The username of the user represented by this Principal
50      * @param password Credentials used to authenticate this user
51      */

52     public SerializablePrincipal(Realm realm, String JavaDoc name, String JavaDoc password) {
53
54         this(realm, name, password, null);
55
56     }
57
58
59     /**
60      * Construct a new Principal, associated with the specified Realm, for the
61      * specified username and password, with the specified role names
62      * (as Strings).
63      *
64      * @param realm The Realm that owns this principal
65      * @param name The username of the user represented by this Principal
66      * @param password Credentials used to authenticate this user
67      * @param roles List of roles (must be Strings) possessed by this user
68      */

69     public SerializablePrincipal(Realm realm, String JavaDoc name, String JavaDoc password,
70                             List JavaDoc roles) {
71
72         super();
73         this.realm = realm;
74         this.name = name;
75         this.password = password;
76         if (roles != null) {
77             this.roles = new String JavaDoc[roles.size()];
78             this.roles = (String JavaDoc[]) roles.toArray(this.roles);
79             if (this.roles.length > 0)
80                 Arrays.sort(this.roles);
81         }
82
83     }
84
85
86     // ------------------------------------------------------------- Properties
87

88
89     /**
90      * The username of the user represented by this Principal.
91      */

92     protected String JavaDoc name = null;
93
94     public String JavaDoc getName() {
95         return (this.name);
96     }
97
98
99     /**
100      * The authentication credentials for the user represented by
101      * this Principal.
102      */

103     protected String JavaDoc password = null;
104
105     public String JavaDoc getPassword() {
106         return (this.password);
107     }
108
109
110     /**
111      * The Realm with which this Principal is associated.
112      */

113     protected transient Realm realm = null;
114
115     public Realm getRealm() {
116         return (this.realm);
117     }
118
119     public void setRealm(Realm realm) {
120         this.realm = realm;
121     }
122
123
124
125
126     /**
127      * The set of roles associated with this user.
128      */

129     protected String JavaDoc roles[] = new String JavaDoc[0];
130
131     public String JavaDoc[] getRoles() {
132         return (this.roles);
133     }
134
135
136     // --------------------------------------------------------- Public Methods
137

138
139
140
141     /**
142      * Return a String representation of this object, which exposes only
143      * information that should be public.
144      */

145     public String JavaDoc toString() {
146
147         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("SerializablePrincipal[");
148         sb.append(this.name);
149         sb.append("]");
150         return (sb.toString());
151
152     }
153
154     public static SerializablePrincipal createPrincipal(GenericPrincipal principal)
155     {
156         if ( principal==null) return null;
157         return new SerializablePrincipal(principal.getRealm(),
158                                          principal.getName(),
159                                          principal.getPassword(),
160                                          principal.getRoles()!=null?Arrays.asList(principal.getRoles()):null);
161     }
162
163     public GenericPrincipal getPrincipal( Realm realm )
164     {
165         return new GenericPrincipal(realm,name,password,getRoles()!=null?Arrays.asList(getRoles()):null);
166     }
167     
168     public static GenericPrincipal readPrincipal(java.io.ObjectInputStream JavaDoc in, Realm realm) throws java.io.IOException JavaDoc{
169         String JavaDoc name = in.readUTF();
170         boolean hasPwd = in.readBoolean();
171         String JavaDoc pwd = null;
172         if ( hasPwd ) pwd = in.readUTF();
173         int size = in.readInt();
174         String JavaDoc[] roles = new String JavaDoc[size];
175         for ( int i=0; i<size; i++ ) roles[i] = in.readUTF();
176         return new GenericPrincipal(realm,name,pwd,Arrays.asList(roles));
177     }
178     
179     public static void writePrincipal(GenericPrincipal p, java.io.ObjectOutputStream JavaDoc out) throws java.io.IOException JavaDoc {
180         out.writeUTF(p.getName());
181         out.writeBoolean(p.getPassword()!=null);
182         if ( p.getPassword()!= null ) out.writeUTF(p.getPassword());
183         String JavaDoc[] roles = p.getRoles();
184         if ( roles == null ) roles = new String JavaDoc[0];
185         out.writeInt(roles.length);
186         for ( int i=0; i<roles.length; i++ ) out.writeUTF(roles[i]);
187     }
188
189
190 }
191
Popular Tags