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