001: /*
002: jGuard is a security framework based on top of jaas (java authentication and authorization security).
003: it is written for web applications, to resolve simply, access control problems.
004: version $Name$
005: http://sourceforge.net/projects/jguard/
006:
007: Copyright (C) 2004 Charles GAY
008:
009: This library is free software; you can redistribute it and/or
010: modify it under the terms of the GNU Lesser General Public
011: License as published by the Free Software Foundation; either
012: version 2.1 of the License, or (at your option) any later version.
013:
014: This library is distributed in the hope that it will be useful,
015: but WITHOUT ANY WARRANTY; without even the implied warranty of
016: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017: Lesser General Public License for more details.
018:
019: You should have received a copy of the GNU Lesser General Public
020: License along with this library; if not, write to the Free Software
021: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
022:
023:
024: jGuard project home page:
025: http://sourceforge.net/projects/jguard/
026:
027: */
028: package net.sf.jguard.core.authorization.permissions;
029:
030: import java.io.Serializable;
031: import java.security.Permission;
032: import java.security.PermissionCollection;
033: import java.util.HashSet;
034: import java.util.Iterator;
035: import java.util.Set;
036:
037: /**
038: * regroups Permissions (in a 'functional' way) as a JGPositivePermissionCollection
039: * (additive mechanism).it differs from JGPermissionCollection
040: * which regroups Permission in a technical way, by owning a name which
041: * refers to functional resources.
042: * @author <a href="mailto:diabolo512@users.sourceforge.net">Charles Gay</a>
043: *
044: */
045: public class Domain extends JGPermissionCollection implements
046: Comparable, Cloneable, Serializable {
047:
048: private static final long serialVersionUID = 178066544850786962L;
049:
050: private String name;
051:
052: public Domain(String domainName) {
053: super ();
054: name = domainName;
055: }
056:
057: public Domain(String domainName, PermissionCollection pcoll) {
058: name = domainName;
059: permissions = new HashSet();
060: super .addAll(pcoll);
061: }
062:
063: /**
064: * @return Returns the domainName.
065: */
066: public String getName() {
067: return name;
068: }
069:
070: /**
071: * @param domainName The domainName to set.
072: */
073: public void setName(String domainName) {
074:
075: this .name = domainName;
076: }
077:
078: /**
079: * override the <i>equals</i> method inherited from Object.
080: * @param obj an Domain
081: * @return true if equals, false otherwise
082: */
083: public boolean equals(Object obj) {
084: Domain domain = (Domain) obj;
085: if (this .name.equals(domain.getName())) {
086: return true;
087: }
088: return false;
089: }
090:
091: /**
092: * methode used to accelerate the comparation process:
093: * useful when hashcode return different int.
094: * it uses hashcode of the name.
095: * @return hashcode
096: */
097: public int hashCode() {
098: return name.toString().hashCode();
099: }
100:
101: /**
102: * override the java.lang.Object 's <i>clone</i> method.
103: * @return new Domain
104: */
105: public Object clone() throws CloneNotSupportedException {
106: JGPermissionCollection dom = new Domain(new String(this .name));
107: Set perms = new HashSet();
108: Iterator itPermissions = permissions.iterator();
109: while (itPermissions.hasNext()) {
110: Permission perm = (Permission) itPermissions.next();
111: String permName = perm.getName();
112: String permActions = perm.getActions();
113: Class permClass = perm.getClass();
114: Permission newPerm;
115: try {
116: newPerm = PermissionUtils.getPermission(permClass
117: .getName(), permName, permActions);
118: } catch (ClassNotFoundException e) {
119: throw new CloneNotSupportedException(e.getMessage());
120: }
121: perms.add(newPerm);
122: }
123: dom.setPermissions(perms);
124: return dom;
125: }
126:
127: /**
128: * compare name with the name of the specified object for order.
129: * @param o the Domain to compare
130: * @see java.lang.Comparable#compareTo(java.lang.Object)
131: */
132: public int compareTo(Object o) {
133: Domain domain = (Domain) o;
134: return this .name.compareTo(domain.getName());
135: }
136:
137: /**
138: * convert object into a string representation.
139: * @return string representation
140: */
141: public String toString() {
142: StringBuffer sb = new StringBuffer();
143: sb.append("\n");
144: sb.append(" name=" + name);
145: sb.append("\n");
146: sb.append(" permissions=\n" + super .toString());
147: sb.append("\n");
148: return sb.toString();
149: }
150:
151: public boolean implies(Permission permission) {
152: Iterator it = permissions.iterator();
153: Permission p;
154:
155: while (it.hasNext()) {
156: p = (Permission) it.next();
157: if (p.implies(permission)) {
158: return true;
159: }
160: }
161: return false;
162: }
163:
164: }
|