import java.io.Serializable; import java.security.Principal; import java.util.Collection; /** * Principal - a simple, serializable Principal. * * @author Chris Schultz (chris@christopherschultz.net) */ public class SimplePrincipal implements Principal, Serializable { private String _name; private Collection _roles; // TODO: Should this be public or not? public SimplePrincipal(String name, Collection roles) { _name = name; _roles = roles; } /** * Returns the name of this principal. * * @return the name of this principal. */ public String getName() { return _name; } public boolean isInRole(String rolename) { return (_roles != null && _roles.contains(rolename)); } /** * Compares this principal to the specified object. * * @param obj object to compare with. * * @return true if the object passed in is a SimplePrincipal with the * same name. */ public boolean equals(Object o) { return (o instanceof Principal) && _name.equals(((Principal)o).getName()); } /** * Returns a string representation of this principal. * * @return a string representation of this principal. */ public String toString() { return "SimplePrincipal[name = \'" + _name + "\'" + ", roles=" + _roles + "]"; } /** * Returns a hashcode for this principal. * * @return a hashcode for this principal. */ public int hashCode() { return _name.hashCode(); } }