View Javadoc

1   // ========================================================================
2   // $Id: JAASGroup.java 305 2006-03-07 10:32:14Z janb $
3   // Copyright 2002-2004 Mort Bay Consulting Pty. Ltd.
4   // ------------------------------------------------------------------------
5   // Licensed under the Apache License, Version 2.0 (the "License");
6   // you may not use this file except in compliance with the License.
7   // You may obtain a copy of the License at 
8   // http://www.apache.org/licenses/LICENSE-2.0
9   // Unless required by applicable law or agreed to in writing, software
10  // distributed under the License is distributed on an "AS IS" BASIS,
11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  // See the License for the specific language governing permissions and
13  // limitations under the License.
14  // ========================================================================
15  
16  package org.mortbay.jetty.plus.jaas;
17  
18  import java.security.Principal;
19  import java.security.acl.Group;
20  import java.util.Enumeration;
21  import java.util.HashSet;
22  import java.util.Iterator;
23  
24  
25  public class JAASGroup implements Group 
26  {
27      public static final String ROLES = "__roles__";
28      
29      private String name = null;
30      private HashSet members = null;
31      
32      
33     
34      public JAASGroup(String n)
35      {
36          this.name = n;
37          this.members = new HashSet();
38      }
39     
40      /* ------------------------------------------------------------ */
41      /**
42       *
43       * @param principal <description>
44       * @return <description>
45       */
46      public synchronized boolean addMember(Principal principal)
47      {
48          return members.add(principal);
49      }
50  
51      /**
52       *
53       * @param principal <description>
54       * @return <description>
55       */
56      public synchronized boolean removeMember(Principal principal)
57      {
58          return members.remove(principal);
59      }
60  
61      /**
62       *
63       * @param principal <description>
64       * @return <description>
65       */
66      public boolean isMember(Principal principal)
67      {
68          return members.contains(principal);
69      }
70  
71  
72      
73      /**
74       *
75       * @return <description>
76       */
77      public Enumeration members()
78      {
79  
80          class MembersEnumeration implements Enumeration
81          {
82              private Iterator itor;
83              
84              public MembersEnumeration (Iterator itor)
85              {
86                  this.itor = itor;
87              }
88              
89              public boolean hasMoreElements ()
90              {
91                  return this.itor.hasNext();
92              }
93  
94  
95              public Object nextElement ()
96              {
97                  return this.itor.next();
98              }
99              
100         }
101 
102         return new MembersEnumeration (members.iterator());
103     }
104 
105 
106     /**
107      *
108      * @return <description>
109      */
110     public int hashCode()
111     {
112         return getName().hashCode();
113     }
114 
115 
116     
117     /**
118      *
119      * @param object <description>
120           * @return <description>
121      */
122     public boolean equals(Object object)
123     {
124         if (! (object instanceof JAASGroup))
125             return false;
126 
127         return ((JAASGroup)object).getName().equals(getName());
128     }
129 
130     /**
131      *
132      * @return <description>
133      */
134     public String toString()
135     {
136         return getName();
137     }
138 
139     /**
140      *
141      * @return <description>
142      */
143     public String getName()
144     {
145         
146         return name;
147     }
148 
149 }