KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > portal > tools > helper > MultipleRoleMatcher


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

16 package org.apache.cocoon.portal.tools.helper;
17
18 import java.util.StringTokenizer JavaDoc;
19
20 import org.apache.cocoon.portal.profile.PortalUser;
21
22 /**
23  * A role matcher matching against several role.
24  *
25  * @version CVS $Id: MultipleRoleMatcher.java 158752 2005-03-23 10:23:17Z cziegeler $
26  */

27 public class MultipleRoleMatcher
28 implements RoleMatcher {
29     
30     /**
31      * The character used to seperate multiple roles.
32      */

33     public static final String JavaDoc ROLE_SEPARATOR = "+";
34
35     /**
36      * The role.
37      */

38     private String JavaDoc[] roles;
39
40     /**
41      * Creates a new MultipleRoleMatcher.
42      */

43     public MultipleRoleMatcher(String JavaDoc roles) {
44         StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(
45             roles,
46             ROLE_SEPARATOR,
47             false);
48
49         this.roles = new String JavaDoc[tokenizer.countTokens()];
50
51         String JavaDoc token;
52         int i = 0;
53         while (tokenizer.hasMoreTokens()) {
54             token = tokenizer.nextToken();
55             this.roles[i] = token;
56             i++;
57         }
58     }
59
60     /**
61      * Overridden from superclass.
62      *
63      * @see RoleMatcher#matches(PortalUser)
64      */

65     public boolean matches(PortalUser user) {
66         // The user must have all roles
67
int length = this.roles.length;
68         for (int i = 0; i < length; i++) {
69             if (!user.isUserInRole(this.roles[i])) {
70                 return false;
71             }
72         }
73         return true;
74     }
75 }
Popular Tags