KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > interop > security > User


1 /**
2  *
3  * Copyright 2004-2005 The Apache Software Foundation
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 implied.
14  *
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.geronimo.interop.security;
19
20 import java.util.HashMap JavaDoc;
21
22
23 public class User
24         implements java.security.Principal JavaDoc {
25     public static User getInstance(String JavaDoc username) {
26         User user = (User) _userMap.get(username);
27         if (user == null) {
28             synchronized (_userMap) {
29                 user = (User) _userMap.get(username);
30                 if (user == null) {
31                     user = new User();
32                     user.init(username);
33                     _userMap.put(username, user);
34                 }
35             }
36         }
37         return user;
38     }
39
40     // properties
41

42     // public constants
43

44     public static final String JavaDoc GUEST = "guest";
45     public static final String JavaDoc NOBODY = "[nobody]";
46     public static final String JavaDoc USER_INFO = "org.apache.geronimo.interop.security.UserInfo";
47
48     // private data
49

50     private static ThreadLocal JavaDoc _current = new ThreadLocal JavaDoc();
51     private static HashMap JavaDoc _userMap = new HashMap JavaDoc();
52     private String JavaDoc _username;
53     private String JavaDoc _lastValidPassword;
54
55     // internal methods
56

57     protected void init(String JavaDoc username) {
58         _username = username;
59     }
60
61     // public methods
62

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

115     public void login(String JavaDoc password) {
116         System.out.println("User.login(): username = " + _username + ", password = " + password);
117         boolean ok = true;
118         if (ok) {
119             SimpleSubject.setCurrent(new SimpleSubject(_username, password));
120         } else {
121             SimpleSubject.setCurrent(null);
122         }
123         if (!ok) {
124             throw new SecurityException JavaDoc("Warn: Login Failed. Username: " + _username);
125         }
126     }
127
128     public boolean hasRole(Role role) {
129         return true;
130     }
131
132     public boolean hasRole(String JavaDoc rolename) {
133         boolean hasRole = true;
134         ;
135         return hasRole;
136     }
137
138     // protected methods
139

140     protected synchronized boolean hasLocalRole(String JavaDoc rolename) {
141         return true;
142     }
143 }
144
Popular Tags