KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lenya > ac > Identity


1 /*
2  * Copyright 1999-2004 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  */

17
18 /* $Id: Identity.java 111679 2004-12-13 00:28:01Z gregor $ */
19
20 package org.apache.lenya.ac;
21
22 import java.util.Arrays JavaDoc;
23 import java.util.HashSet JavaDoc;
24 import java.util.Set JavaDoc;
25
26 import org.apache.cocoon.environment.Session;
27 import org.apache.log4j.Category;
28
29 /**
30  * Identity object. Used to store the authenticated accreditables in the session.
31  */

32 public class Identity implements Identifiable, java.io.Serializable JavaDoc {
33     private Set JavaDoc identifiables = new HashSet JavaDoc();
34     
35     private static final Category log = Category.getInstance(Identity.class);
36     
37     /**
38      * Ctor.
39      */

40     public Identity() {
41         addIdentifiable(World.getInstance());
42     }
43
44     /**
45      * In the case of Tomcat the object will be serialized to TOMCAT/work/Standalone/localhost/lenya/SESSIONS.ser
46      */

47     private void writeObject(java.io.ObjectOutputStream JavaDoc out) throws java.io.IOException JavaDoc {
48         if (log.isDebugEnabled()) {
49             log.debug("Serializing identity which is attached to session: " + this.toString());
50         }
51         out.defaultWriteObject();
52         out.writeObject(identifiables);
53     }
54
55     /**
56      * In case of Tomcat the object will be restored from TOMCAT/work/Standalone/localhost/lenya/SESSIONS.ser
57      */

58     private void readObject(java.io.ObjectInputStream JavaDoc in) throws java.io.IOException JavaDoc, ClassNotFoundException JavaDoc {
59         in.defaultReadObject();
60         identifiables = (Set JavaDoc) in.readObject();
61
62         if (log.isDebugEnabled()) {
63             log.debug("Identity loaded from serialized object: " + this.toString());
64         }
65     }
66
67     /**
68      * Returns the identifiables of this identity.
69      * @return An array of identifiables.
70      */

71     public Identifiable[] getIdentifiables() {
72         return (Identifiable[]) identifiables.toArray(new Identifiable[identifiables.size()]);
73     }
74
75     /**
76      * Adds a new identifiable to this identity.
77      * @param identifiable The identifiable to add.
78      */

79     public void addIdentifiable(Identifiable identifiable) {
80         assert identifiable != null;
81         assert identifiable != this;
82         assert !identifiables.contains(identifiable);
83         
84         if (log.isDebugEnabled()) {
85             log.debug("Adding identifiable: [" + identifiable + "]");
86         }
87         
88         identifiables.add(identifiable);
89     }
90
91     /**
92      * @see Accreditable#getAccreditables()
93      */

94     public Accreditable[] getAccreditables() {
95         Set JavaDoc accreditables = new HashSet JavaDoc();
96         Identifiable[] identifiables = getIdentifiables();
97
98         for (int i = 0; i < identifiables.length; i++) {
99             Accreditable[] groupAccreditables = identifiables[i].getAccreditables();
100             accreditables.addAll(Arrays.asList(groupAccreditables));
101         }
102
103         return (Accreditable[]) accreditables.toArray(new Accreditable[accreditables.size()]);
104     }
105
106     /**
107      * @see java.lang.Object#toString()
108      */

109     public String JavaDoc toString() {
110         String JavaDoc accrString = "";
111         Accreditable[] accreditables = getAccreditables();
112
113         for (int i = 0; i < accreditables.length; i++) {
114             accrString += (" " + accreditables[i]);
115         }
116
117         String JavaDoc string = "[identity:" + accrString + "]";
118
119         return string;
120     }
121     
122     /**
123      * Checks if this identity belongs to a certain accreditable manager.
124      * @param manager The accreditable manager to check for.
125      * @return A boolean value.
126      *
127      * @throws AccessControlException if an error occurs
128      */

129     public boolean belongsTo(AccreditableManager manager) throws AccessControlException {
130         
131         boolean belongs = true;
132         
133         Identifiable identifiables[] = getIdentifiables();
134         int i = 0;
135         while (belongs && i < identifiables.length) {
136             if (identifiables[i] instanceof User) {
137                 User user = (User) identifiables[i];
138                 User otherUser = manager.getUserManager().getUser(user.getId());
139                 belongs = belongs && user == otherUser;
140             }
141             i++;
142         }
143         
144         return belongs;
145     }
146
147     /**
148      * Returns the user of this identity.
149      * @return A user.
150      */

151     public User getUser() {
152         User user = null;
153         Identifiable[] identifiables = getIdentifiables();
154         int i = 0;
155         while (user == null && i < identifiables.length) {
156             if (identifiables[i] instanceof User) {
157                 user = (User) identifiables[i];
158             }
159             i++;
160         }
161         return user;
162      }
163
164     /**
165      * Returns the machine of this identity.
166      * @return A machine.
167      */

168     public Machine getMachine() {
169         Machine machine = null;
170         Identifiable[] identifiables = getIdentifiables();
171         int i = 0;
172         while (machine == null && i < identifiables.length) {
173             if (identifiables[i] instanceof Machine) {
174                 machine = (Machine) identifiables[i];
175             }
176             i++;
177         }
178         return machine;
179      }
180      
181      /**
182       * Checks if this identity contains a certain identifiable.
183       * @param identifiable The identifiable to look for.
184       * @return A boolean value.
185       */

186      public boolean contains(Identifiable identifiable) {
187          return identifiables.contains(identifiable);
188      }
189      
190      /**
191       * Fetches the identity from a session.
192       * @param session The session.
193       * @return An identity.
194       */

195      public static Identity getIdentity(Session session) {
196          Identity identity = (Identity) session.getAttribute(Identity.class.getName());
197          return identity;
198      }
199      
200      /**
201       * Removes a certain identifiable from the idenity.
202       * @param identifiable An identifiable.
203       */

204      public void removeIdentifiable(Identifiable identifiable) {
205          assert identifiables.contains(identifiable);
206          identifiables.remove(identifiable);
207      }
208
209 }
210
Popular Tags