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.core.impl.model.instance.persistent;
023:
024: import org.jboss.portal.core.model.instance.Instance;
025:
026: import java.io.Serializable;
027: import java.util.Collections;
028: import java.util.HashSet;
029: import java.util.Iterator;
030: import java.util.Set;
031: import java.util.StringTokenizer;
032:
033: /**
034: * Security Constraint for an instance
035: *
036: * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
037: * @version $Revision: 8786 $
038: */
039: final class PersistentRoleSecurityBinding implements Serializable {
040:
041: /** The serialVersionUID */
042: private static final long serialVersionUID = -2832148715381794267L;
043:
044: /** The primary key. */
045: private Long key;
046:
047: /** The role of this contraint. */
048: private String role;
049:
050: /** The set of actions of this constraint. */
051: private Set actions;
052:
053: /** The cached toString value. */
054: private transient String toString;
055:
056: /** The cached hash code. */
057: private transient int hashCode;
058:
059: /** The cached actions as a string. */
060: private transient String actionsAsString;
061:
062: private Instance instance;
063:
064: public PersistentRoleSecurityBinding() {
065: super ();
066: }
067:
068: /**
069: * Create a new constraint with the provided actions for the specified role.
070: *
071: * @param actions a comma separated list of allowed actions
072: * @param role the role name
073: */
074: public PersistentRoleSecurityBinding(String actions, String role) {
075: if (role == null) {
076: throw new IllegalArgumentException("Role cannot be null");
077: }
078: if (actions == null) {
079: throw new IllegalArgumentException("Actions cannot be null");
080: }
081:
082: //
083: StringTokenizer tokens = new StringTokenizer(actions, ",");
084: Set set = new HashSet();
085: while (tokens.hasMoreTokens()) {
086: set.add(tokens.nextToken().trim());
087: }
088:
089: //
090: this .role = role;
091: this .actions = Collections.unmodifiableSet(set);
092: }
093:
094: /**
095: * Create a new constraint with the provided actions and the specified role.
096: *
097: * @param actions the set of actions
098: * @param role the role name
099: */
100: public PersistentRoleSecurityBinding(Set actions, String role) {
101: if (role == null) {
102: throw new IllegalArgumentException("Role cannot be null");
103: }
104: if (actions == null) {
105: throw new IllegalArgumentException("Actions cannot be null");
106: }
107:
108: //
109: this .role = role;
110: this .actions = Collections
111: .unmodifiableSet(new HashSet(actions));
112: }
113:
114: /** Copy constructor. */
115: public PersistentRoleSecurityBinding(
116: PersistentRoleSecurityBinding other) {
117: if (other == null) {
118: throw new IllegalArgumentException(
119: "The constraint to clone cannot be null");
120: }
121:
122: //
123: this .role = other.role;
124: this .actions = other.actions;
125: }
126:
127: /**
128: * Return a <code>java.util.Set<String></code> of allowed actions.
129: *
130: * @return the action set
131: */
132: public Set getActions() {
133: return actions;
134: }
135:
136: /**
137: * Return the role of this constraint
138: *
139: * @return the role
140: */
141: public String getRole() {
142: return role;
143: }
144:
145: /**
146: * Return a comma separated list of actions.
147: *
148: * @return the action string representation
149: */
150: public String getActionsAsString() {
151: if (actionsAsString == null) {
152: StringBuffer tmp = new StringBuffer();
153: for (Iterator i = actions.iterator(); i.hasNext();) {
154: String action = (String) i.next();
155: if (i.hasNext()) {
156: tmp.append(", ");
157: }
158: tmp.append(action);
159: }
160: actionsAsString = tmp.toString();
161: }
162: return actionsAsString;
163: }
164:
165: /** @see Object#toString */
166: public String toString() {
167: if (toString == null) {
168: StringBuffer tmp = new StringBuffer(
169: "SecurityConstraint: actions [");
170: for (Iterator i = actions.iterator(); i.hasNext();) {
171: String action = (String) i.next();
172: if (i.hasNext()) {
173: tmp.append(", ");
174: }
175: tmp.append(action);
176: }
177: tmp.append("] role [").append(role).append("]");
178: toString = tmp.toString();
179: }
180: return toString;
181: }
182:
183: public boolean equals(Object o) {
184: if (this == o) {
185: return true;
186: }
187: if (o instanceof PersistentRoleSecurityBinding) {
188: PersistentRoleSecurityBinding that = (PersistentRoleSecurityBinding) o;
189: return actions.equals(that.actions)
190: && role.equals(that.role);
191: }
192: return false;
193: }
194:
195: public int hashCode() {
196: if (hashCode == 0) {
197: int hashCode;
198: hashCode = actions.hashCode();
199: hashCode = 29 * hashCode + role.hashCode();
200: this .hashCode = hashCode;
201: }
202: return hashCode;
203: }
204:
205: protected void setKey(Long k) {
206: key = k;
207: }
208:
209: protected Long getKey() {
210: return key;
211: }
212:
213: public void setActions(Set actions) {
214: this .actions = actions;
215: }
216:
217: public void setRole(String role) {
218: this .role = role;
219: }
220:
221: public Instance getInstance() {
222: return instance;
223: }
224:
225: public void setInstance(Instance instance) {
226: this.instance = instance;
227: }
228: }
|