KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > catalina > users > MemoryGroup


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18
19 package org.apache.catalina.users;
20
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.Iterator JavaDoc;
24
25 import org.apache.catalina.Role;
26 import org.apache.catalina.UserDatabase;
27
28
29 /**
30  * <p>Concrete implementation of {@link org.apache.catalina.Group} for the
31  * {@link MemoryUserDatabase} implementation of {@link UserDatabase}.</p>
32  *
33  * @author Craig R. McClanahan
34  * @version $Revision: 467222 $ $Date: 2006-10-24 05:17:11 +0200 (mar., 24 oct. 2006) $
35  * @since 4.1
36  */

37
38 public class MemoryGroup extends AbstractGroup {
39
40
41     // ----------------------------------------------------------- Constructors
42

43
44     /**
45      * Package-private constructor used by the factory method in
46      * {@link MemoryUserDatabase}.
47      *
48      * @param database The {@link MemoryUserDatabase} that owns this group
49      * @param groupname Group name of this group
50      * @param description Description of this group
51      */

52     MemoryGroup(MemoryUserDatabase database,
53                 String JavaDoc groupname, String JavaDoc description) {
54
55         super();
56         this.database = database;
57         setGroupname(groupname);
58         setDescription(description);
59
60     }
61
62
63     // ----------------------------------------------------- Instance Variables
64

65
66     /**
67      * The {@link MemoryUserDatabase} that owns this group.
68      */

69     protected MemoryUserDatabase database = null;
70
71
72     /**
73      * The set of {@link Role}s associated with this group.
74      */

75     protected ArrayList JavaDoc roles = new ArrayList JavaDoc();
76
77
78     // ------------------------------------------------------------- Properties
79

80
81     /**
82      * Return the set of {@link Role}s assigned specifically to this group.
83      */

84     public Iterator JavaDoc getRoles() {
85
86         synchronized (roles) {
87             return (roles.iterator());
88         }
89
90     }
91
92
93     /**
94      * Return the {@link UserDatabase} within which this Group is defined.
95      */

96     public UserDatabase getUserDatabase() {
97
98         return (this.database);
99
100     }
101
102
103     /**
104      * Return the set of {@link org.apache.catalina.User}s that are members of this group.
105      */

106     public Iterator JavaDoc getUsers() {
107
108         ArrayList JavaDoc results = new ArrayList JavaDoc();
109         Iterator JavaDoc users = database.getUsers();
110         while (users.hasNext()) {
111             MemoryUser user = (MemoryUser) users.next();
112             if (user.isInGroup(this)) {
113                 results.add(user);
114             }
115         }
116         return (results.iterator());
117
118     }
119
120
121     // --------------------------------------------------------- Public Methods
122

123
124     /**
125      * Add a new {@link Role} to those assigned specifically to this group.
126      *
127      * @param role The new role
128      */

129     public void addRole(Role role) {
130
131         synchronized (roles) {
132             if (!roles.contains(role)) {
133                 roles.add(role);
134             }
135         }
136
137     }
138
139
140     /**
141      * Is this group specifically assigned the specified {@link Role}?
142      *
143      * @param role The role to check
144      */

145     public boolean isInRole(Role role) {
146
147         synchronized (roles) {
148             return (roles.contains(role));
149         }
150
151     }
152
153
154     /**
155      * Remove a {@link Role} from those assigned to this group.
156      *
157      * @param role The old role
158      */

159     public void removeRole(Role role) {
160
161         synchronized (roles) {
162             roles.remove(role);
163         }
164
165     }
166
167
168     /**
169      * Remove all {@link Role}s from those assigned to this group.
170      */

171     public void removeRoles() {
172
173         synchronized (roles) {
174             roles.clear();
175         }
176
177     }
178
179
180     /**
181      * <p>Return a String representation of this group in XML format.</p>
182      */

183     public String JavaDoc toString() {
184
185         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("<group groupname=\"");
186         sb.append(groupname);
187         sb.append("\"");
188         if (description != null) {
189             sb.append(" description=\"");
190             sb.append(description);
191             sb.append("\"");
192         }
193         synchronized (roles) {
194             if (roles.size() > 0) {
195                 sb.append(" roles=\"");
196                 int n = 0;
197                 Iterator JavaDoc values = roles.iterator();
198                 while (values.hasNext()) {
199                     if (n > 0) {
200                         sb.append(',');
201                     }
202                     n++;
203                     sb.append((String JavaDoc) ((Role) values.next()).getRolename());
204                 }
205                 sb.append("\"");
206             }
207         }
208         sb.append("/>");
209         return (sb.toString());
210
211     }
212
213
214 }
215
Popular Tags