KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > jaas > principal > PrincipalCollectionImpl


1 package info.magnolia.jaas.principal;
2
3 import info.magnolia.cms.security.auth.PrincipalCollection;
4
5 import java.security.Principal JavaDoc;
6 import java.util.ArrayList JavaDoc;
7 import java.util.Collection JavaDoc;
8 import java.util.Iterator JavaDoc;
9
10 import org.apache.commons.lang.StringUtils;
11 import org.apache.commons.lang.builder.ToStringBuilder;
12 import org.apache.commons.lang.builder.ToStringStyle;
13
14
15 /**
16  * Date: Jun 29, 2005
17  * @author Sameer Charles $Id: PrincipalCollectionImpl.java 6341 2006-09-12 09:18:27Z philipp $
18  */

19 public class PrincipalCollectionImpl implements PrincipalCollection {
20
21     /**
22      * Stable serialVersionUID.
23      */

24     private static final long serialVersionUID = 222L;
25
26     private static final String JavaDoc NAME = "PrincipalCollection";
27
28     /**
29      * collection on principal objects
30      */

31     private Collection collection = new ArrayList JavaDoc();
32
33     private String JavaDoc name;
34
35     /**
36      * Get name given to this principal
37      * @return name
38      */

39     public String JavaDoc getName() {
40         if (StringUtils.isEmpty(this.name)) {
41             return NAME;
42         }
43         return this.name;
44     }
45
46     /**
47      * Set this principal name
48      */

49     public void setName(String JavaDoc name) {
50         this.name = name;
51     }
52
53     /**
54      * Set collection
55      * @param collection
56      */

57     public void set(Collection collection) {
58         this.collection = collection;
59     }
60
61     /**
62      * Add to collection
63      * @param principal to be added to the collection
64      */

65     public void add(Principal JavaDoc principal) {
66         this.collection.add(principal);
67     }
68
69     /**
70      * Remove from the collection
71      * @param principal to be removed from the collection
72      */

73     public void remove(Principal JavaDoc principal) {
74         this.collection.remove(principal);
75     }
76
77     /**
78      * Clear collection
79      */

80     public void clearAll() {
81         this.collection.clear();
82     }
83
84     /**
85      * Check if this collection contains specified object
86      * @param principal
87      * @return true if the specified object exist in the collection
88      */

89     public boolean contains(Principal JavaDoc principal) {
90         return this.collection.contains(principal);
91     }
92
93     /**
94      * Checks if this collection contains object with the specified name
95      * @param name
96      * @return true if the collection contains the principal by the specified name
97      */

98     public boolean contains(String JavaDoc name) {
99         return this.get(name) != null;
100     }
101
102     /**
103      * Get principal associated to the specified name from the collection
104      * @param name
105      * @return principal object associated to the specified name
106      */

107     public Principal JavaDoc get(String JavaDoc name) {
108         Iterator JavaDoc principalIterator = this.collection.iterator();
109         while (principalIterator.hasNext()) {
110             Principal JavaDoc principal = (Principal JavaDoc) principalIterator.next();
111             if (StringUtils.equalsIgnoreCase(name, principal.getName())) {
112                 return principal;
113             }
114         }
115         return null;
116     }
117
118     /**
119      * @see java.lang.Object#toString()
120      */

121     public String JavaDoc toString() {
122         return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE).append("name", this.name).toString();
123     }
124 }
125
Popular Tags