1 18 package org.apache.geronimo.interop.security; 19 20 import java.util.HashMap ; 21 22 23 public class User 24 implements java.security.Principal { 25 public static User getInstance(String 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 42 44 public static final String GUEST = "guest"; 45 public static final String NOBODY = "[nobody]"; 46 public static final String USER_INFO = "org.apache.geronimo.interop.security.UserInfo"; 47 48 50 private static ThreadLocal _current = new ThreadLocal (); 51 private static HashMap _userMap = new HashMap (); 52 private String _username; 53 private String _lastValidPassword; 54 55 57 protected void init(String username) { 58 _username = username; 59 } 60 61 63 public int hashCode() { 64 return _username.hashCode(); 65 } 66 67 public boolean equals(Object 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 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 ("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 getName() { 101 return _username; 102 } 103 104 public String getPassword() { 105 return _lastValidPassword == null ? "" : _lastValidPassword; 106 } 107 108 public String toString() { 109 return User.class.getName() + ":" + _username + "@domain"; 110 } 111 112 115 public void login(String 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 ("Warn: Login Failed. Username: " + _username); 125 } 126 } 127 128 public boolean hasRole(Role role) { 129 return true; 130 } 131 132 public boolean hasRole(String rolename) { 133 boolean hasRole = true; 134 ; 135 return hasRole; 136 } 137 138 140 protected synchronized boolean hasLocalRole(String rolename) { 141 return true; 142 } 143 } 144 | Popular Tags |