001: /*
002: * Copyright 2002,2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.catalina.users;
018:
019: import java.util.ArrayList;
020: import java.util.Iterator;
021:
022: import org.apache.catalina.Role;
023: import org.apache.catalina.UserDatabase;
024:
025: /**
026: * <p>Concrete implementation of {@link Group} for the
027: * {@link MemoryUserDatabase} implementation of {@link UserDatabase}.</p>
028: *
029: * @author Craig R. McClanahan
030: * @version $Revision: 1.3 $ $Date: 2004/02/27 14:58:50 $
031: * @since 4.1
032: */
033:
034: public class MemoryGroup extends AbstractGroup {
035:
036: // ----------------------------------------------------------- Constructors
037:
038: /**
039: * Package-private constructor used by the factory method in
040: * {@link MemoryUserDatabase}.
041: *
042: * @param database The {@link MemoryUserDatabase} that owns this group
043: * @param groupname Group name of this group
044: * @param description Description of this group
045: */
046: MemoryGroup(MemoryUserDatabase database, String groupname,
047: String description) {
048:
049: super ();
050: this .database = database;
051: setGroupname(groupname);
052: setDescription(description);
053:
054: }
055:
056: // ----------------------------------------------------- Instance Variables
057:
058: /**
059: * The {@link MemoryUserDatabase} that owns this group.
060: */
061: protected MemoryUserDatabase database = null;
062:
063: /**
064: * The set of {@link Role}s associated with this group.
065: */
066: protected ArrayList roles = new ArrayList();
067:
068: // ------------------------------------------------------------- Properties
069:
070: /**
071: * Return the set of {@link Role}s assigned specifically to this group.
072: */
073: public Iterator getRoles() {
074:
075: synchronized (roles) {
076: return (roles.iterator());
077: }
078:
079: }
080:
081: /**
082: * Return the {@link UserDatabase} within which this Group is defined.
083: */
084: public UserDatabase getUserDatabase() {
085:
086: return (this .database);
087:
088: }
089:
090: /**
091: * Return the set of {@link User}s that are members of this group.
092: */
093: public Iterator getUsers() {
094:
095: ArrayList results = new ArrayList();
096: Iterator users = database.getUsers();
097: while (users.hasNext()) {
098: MemoryUser user = (MemoryUser) users.next();
099: if (user.isInGroup(this )) {
100: results.add(user);
101: }
102: }
103: return (results.iterator());
104:
105: }
106:
107: // --------------------------------------------------------- Public Methods
108:
109: /**
110: * Add a new {@link Role} to those assigned specifically to this group.
111: *
112: * @param role The new role
113: */
114: public void addRole(Role role) {
115:
116: synchronized (roles) {
117: if (!roles.contains(role)) {
118: roles.add(role);
119: }
120: }
121:
122: }
123:
124: /**
125: * Is this group specifically assigned the specified {@link Role}?
126: *
127: * @param role The role to check
128: */
129: public boolean isInRole(Role role) {
130:
131: synchronized (roles) {
132: return (roles.contains(role));
133: }
134:
135: }
136:
137: /**
138: * Remove a {@link Role} from those assigned to this group.
139: *
140: * @param role The old role
141: */
142: public void removeRole(Role role) {
143:
144: synchronized (roles) {
145: roles.remove(role);
146: }
147:
148: }
149:
150: /**
151: * Remove all {@link Role}s from those assigned to this group.
152: */
153: public void removeRoles() {
154:
155: synchronized (roles) {
156: roles.clear();
157: }
158:
159: }
160:
161: /**
162: * <p>Return a String representation of this group in XML format.</p>
163: */
164: public String toString() {
165:
166: StringBuffer sb = new StringBuffer("<group groupname=\"");
167: sb.append(groupname);
168: sb.append("\"");
169: if (description != null) {
170: sb.append(" description=\"");
171: sb.append(description);
172: sb.append("\"");
173: }
174: synchronized (roles) {
175: if (roles.size() > 0) {
176: sb.append(" roles=\"");
177: int n = 0;
178: Iterator values = roles.iterator();
179: while (values.hasNext()) {
180: if (n > 0) {
181: sb.append(',');
182: }
183: n++;
184: sb.append((String) ((Role) values.next())
185: .getRolename());
186: }
187: sb.append("\"");
188: }
189: }
190: sb.append("/>");
191: return (sb.toString());
192:
193: }
194:
195: }
|