KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gcc > security > User


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

19 package gcc.security;
20
21 import gcc.properties.*;
22 import java.util.*;
23
24 public class User
25     implements java.security.Principal
26 {
27     public static User getInstance(String username)
28     {
29         User user = (User)_userMap.get(username);
30         if (user == null)
31         {
32             synchronized (_userMap)
33             {
34                 user = (User)_userMap.get(username);
35                 if (user == null)
36                 {
37                     user = new User();
38                     user.init(username);
39                     _userMap.put(username, user);
40                 }
41             }
42         }
43         return user;
44     }
45
46     // properties
47

48     // public constants
49

50     public static final String GUEST = "guest";
51     public static final String NOBODY = "[nobody]";
52     public static final String USER_INFO = "gcc.security.UserInfo";
53
54     // private data
55

56     private static ThreadLocal _current = new ThreadLocal();
57     private static HashMap _userMap = new HashMap();
58     private String _username;
59     private String _lastValidPassword;
60
61     // internal methods
62

63     protected void init(String username)
64     {
65         _username = username;
66     }
67
68     // public methods
69

70     public int hashCode()
71     {
72         return _username.hashCode();
73     }
74
75     public boolean equals(Object thatObject)
76     {
77         if (thatObject == this)
78         {
79             return true;
80         }
81         if (thatObject == null || ! (thatObject instanceof User))
82         {
83             return false;
84         }
85         User that = (User)thatObject;
86         //return this._domain == that._domain
87
// && this._username.equals(that._username);
88
return true;
89     }
90
91     public static User getCurrent()
92     {
93         return (User)_current.get();
94     }
95
96     public static User getCurrentNotNull()
97     {
98         User user = (User)_current.get();
99         if (user == null)
100         {
101             throw new SecurityException("Error: No Current User");
102         }
103         return user;
104     }
105
106     public static User getUnauthenticated()
107     {
108         return User.getInstance("unauthenticated");
109     }
110
111     public static void setCurrent(User user)
112     {
113         _current.set(user);
114     }
115
116     public String getName()
117     {
118         return _username;
119     }
120
121     public String getPassword()
122     {
123         return _lastValidPassword == null ? "" : _lastValidPassword;
124     }
125
126     public String toString()
127     {
128         return User.class.getName() + ":" + _username + "@domain";
129     }
130
131     /**
132      ** Check password for login. Use cached result if available.
133      **/

134     public void login(String password)
135     {
136         System.out.println( "User.login(): username = " + _username + ", password = " + password );
137         boolean ok = true;
138         if (ok)
139         {
140             SimpleSubject.setCurrent(new SimpleSubject(_username, password));
141         }
142         else
143         {
144             SimpleSubject.setCurrent(null);
145         }
146         if (! ok)
147         {
148             throw new SecurityException("Warn: Login Failed. Username: " + _username );
149         }
150     }
151
152     public boolean hasRole(Role role)
153     {
154         return true;
155     }
156
157     public boolean hasRole(String rolename)
158     {
159         boolean hasRole = true;;
160         return hasRole;
161     }
162
163     // protected methods
164

165     protected synchronized boolean hasLocalRole(String rolename)
166     {
167         return true;
168     }
169 }
170
Popular Tags