View Javadoc

1   // ========================================================================
2   // Copyright (c) 2002-2009 Mort Bay Consulting Pty. Ltd.
3   // ------------------------------------------------------------------------
4   // All rights reserved. This program and the accompanying materials
5   // are made available under the terms of the Eclipse Public License v1.0
6   // and Apache License v2.0 which accompanies this distribution.
7   // The Eclipse Public License is available at 
8   // http://www.eclipse.org/legal/epl-v10.html
9   // The Apache License v2.0 is available at
10  // http://www.opensource.org/licenses/apache2.0.php
11  // You may elect to redistribute this code under either of these licenses. 
12  // ========================================================================
13  
14  package org.eclipse.jetty.plus.jaas;
15  
16  import java.security.Principal;
17  import java.security.acl.Group;
18  import java.util.Enumeration;
19  import java.util.HashSet;
20  import java.util.Iterator;
21  
22  
23  public class JAASGroup implements Group 
24  {
25      public static final String ROLES = "__roles__";
26      
27      private String _name = null;
28      private HashSet _members = null;
29      
30      
31     
32      public JAASGroup(String n)
33      {
34          this._name = n;
35          this._members = new HashSet();
36      }
37     
38      /* ------------------------------------------------------------ */
39      /**
40       *
41       * @param principal <description>
42       * @return <description>
43       */
44      public synchronized boolean addMember(Principal principal)
45      {
46          return _members.add(principal);
47      }
48  
49      /**
50       *
51       * @param principal <description>
52       * @return <description>
53       */
54      public synchronized boolean removeMember(Principal principal)
55      {
56          return _members.remove(principal);
57      }
58  
59      /**
60       *
61       * @param principal <description>
62       * @return <description>
63       */
64      public boolean isMember(Principal principal)
65      {
66          return _members.contains(principal);
67      }
68  
69  
70      
71      /**
72       *
73       * @return <description>
74       */
75      public Enumeration members()
76      {
77  
78          class MembersEnumeration implements Enumeration
79          {
80              private Iterator itor;
81              
82              public MembersEnumeration (Iterator itor)
83              {
84                  this.itor = itor;
85              }
86              
87              public boolean hasMoreElements ()
88              {
89                  return this.itor.hasNext();
90              }
91  
92  
93              public Object nextElement ()
94              {
95                  return this.itor.next();
96              }
97              
98          }
99  
100         return new MembersEnumeration (_members.iterator());
101     }
102 
103 
104     /**
105      *
106      * @return <description>
107      */
108     public int hashCode()
109     {
110         return getName().hashCode();
111     }
112 
113 
114     
115     /**
116      *
117      * @param object <description>
118           * @return <description>
119      */
120     public boolean equals(Object object)
121     {
122         if (! (object instanceof JAASGroup))
123             return false;
124 
125         return ((JAASGroup)object).getName().equals(getName());
126     }
127 
128     /**
129      *
130      * @return <description>
131      */
132     public String toString()
133     {
134         return getName();
135     }
136 
137     /**
138      *
139      * @return <description>
140      */
141     public String getName()
142     {
143         
144         return _name;
145     }
146 
147 }