View Javadoc

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