001: /******************************************************************************
002: * JBoss, a division of Red Hat *
003: * Copyright 2006, Red Hat Middleware, LLC, and individual *
004: * contributors as indicated by the @authors tag. See the *
005: * copyright.txt in the distribution for a full listing of *
006: * individual contributors. *
007: * *
008: * This is free software; you can redistribute it and/or modify it *
009: * under the terms of the GNU Lesser General Public License as *
010: * published by the Free Software Foundation; either version 2.1 of *
011: * the License, or (at your option) any later version. *
012: * *
013: * This software is distributed in the hope that it will be useful, *
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of *
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
016: * Lesser General Public License for more details. *
017: * *
018: * You should have received a copy of the GNU Lesser General Public *
019: * License along with this software; if not, write to the Free *
020: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
021: * 02110-1301 USA, or see the FSF site: http://www.fsf.org. *
022: ******************************************************************************/package org.jboss.portal.security;
023:
024: import java.io.Serializable;
025: import java.util.Collections;
026: import java.util.HashSet;
027: import java.util.Iterator;
028: import java.util.Set;
029: import java.util.StringTokenizer;
030:
031: /**
032: * Binds a role and a set of actions together. This object is immutable. <p>A portal resource (portal, page, window,
033: * instance, portlet...) is secured via a set of security constraints. each security constraint holds the information
034: * about what roles are allowed what actions.</p>
035: *
036: * @author <a href="mailto:mholzner@novell.com">Martin Holzner</a>
037: * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
038: * @version $Revision: 8784 $
039: */
040: public class RoleSecurityBinding implements Serializable {
041:
042: /** The serialVersionUID */
043: private static final long serialVersionUID = 2723720035191741138L;
044:
045: /** The role name of this contraint. */
046: private final String roleName;
047:
048: /** The set of actions of this constraint. */
049: private final Set actions;
050:
051: /** The cached toString value. */
052: private transient String toString;
053:
054: /** The cached hash code. */
055: private transient int hashCode;
056:
057: /** The cached actions as a string. */
058: private transient String actionsAsString;
059:
060: /**
061: * Create a new constraint with the provided actions for the specified role.
062: *
063: * @param actions a comma separated list of allowed actions
064: * @param role the role name
065: */
066: public RoleSecurityBinding(String actions, String role) {
067: if (role == null) {
068: throw new IllegalArgumentException("Role cannot be null");
069: }
070: if (actions == null) {
071: throw new IllegalArgumentException("Actions cannot be null");
072: }
073:
074: //
075: StringTokenizer tokens = new StringTokenizer(actions, ",");
076: Set set = new HashSet();
077: while (tokens.hasMoreTokens()) {
078: set.add(tokens.nextToken().trim());
079: }
080:
081: //
082: this .roleName = role;
083: this .actions = Collections.unmodifiableSet(set);
084: }
085:
086: /**
087: * Create a new constraint with the provided actions and the specified role.
088: *
089: * @param actions the set of actions
090: * @param role the role name
091: */
092: public RoleSecurityBinding(Set actions, String role) {
093: if (role == null) {
094: throw new IllegalArgumentException("Role cannot be null");
095: }
096: if (actions == null) {
097: throw new IllegalArgumentException("Actions cannot be null");
098: }
099:
100: //
101: this .roleName = role;
102: this .actions = Collections
103: .unmodifiableSet(new HashSet(actions));
104: }
105:
106: /** Copy constructor. */
107: public RoleSecurityBinding(RoleSecurityBinding other) {
108: if (other == null) {
109: throw new IllegalArgumentException(
110: "The constraint to clone cannot be null");
111: }
112:
113: //
114: this .roleName = other.roleName;
115: this .actions = other.actions;
116: }
117:
118: /**
119: * Return a <code>java.util.Set<String></code> of allowed actions.
120: *
121: * @return the action set
122: */
123: public Set getActions() {
124: return actions;
125: }
126:
127: /**
128: * Return the role of this constraint
129: *
130: * @return the role
131: */
132: public String getRoleName() {
133: return roleName;
134: }
135:
136: /**
137: * Return a comma separated list of actions.
138: *
139: * @return the action string representation
140: */
141: public String getActionsAsString() {
142: if (actionsAsString == null) {
143: StringBuffer tmp = new StringBuffer();
144: for (Iterator i = actions.iterator(); i.hasNext();) {
145: String action = (String) i.next();
146: if (i.hasNext()) {
147: tmp.append(", ");
148: }
149: tmp.append(action);
150: }
151: actionsAsString = tmp.toString();
152: }
153: return actionsAsString;
154: }
155:
156: /** @see Object#toString */
157: public String toString() {
158: if (toString == null) {
159: StringBuffer tmp = new StringBuffer(
160: "SecurityConstraint[actions=(");
161: for (Iterator i = actions.iterator();;) {
162: String action = (String) i.next();
163: tmp.append(action);
164: if (i.hasNext()) {
165: tmp.append(", ");
166: } else {
167: break;
168: }
169: }
170: tmp.append("),role=").append(roleName).append("]");
171: toString = tmp.toString();
172: }
173: return toString;
174: }
175:
176: public boolean equals(Object o) {
177: if (this == o) {
178: return true;
179: }
180: if (o instanceof RoleSecurityBinding) {
181: RoleSecurityBinding that = (RoleSecurityBinding) o;
182: return actions.equals(that.actions)
183: && roleName.equals(that.roleName);
184: }
185: return false;
186: }
187:
188: public int hashCode() {
189: if (hashCode == 0) {
190: int hashCode;
191: hashCode = actions.hashCode();
192: hashCode = 29 * hashCode + roleName.hashCode();
193: this.hashCode = hashCode;
194: }
195: return hashCode;
196: }
197: }
|