001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: /* $Id: Identity.java 499017 2007-01-23 13:25:22Z andreas $ */
020:
021: package org.apache.lenya.ac;
022:
023: import java.util.Arrays;
024: import java.util.HashSet;
025: import java.util.Set;
026: import java.io.IOException;
027: import java.io.ObjectInputStream;
028: import java.io.ObjectOutputStream;
029: import java.io.Serializable;
030:
031: import org.apache.avalon.framework.container.ContainerUtil;
032: import org.apache.avalon.framework.logger.AbstractLogEnabled;
033: import org.apache.avalon.framework.logger.Logger;
034: import org.apache.cocoon.environment.Session;
035:
036: /**
037: * Identity object. Used to store the authenticated accreditables in the session.
038: */
039: public class Identity extends AbstractLogEnabled implements
040: Identifiable, Serializable {
041: /**
042: *
043: */
044: private static final long serialVersionUID = 1L;
045: private Set identifiables = new HashSet();
046:
047: /**
048: * Ctor.
049: * @param logger The logger.
050: */
051: public Identity(Logger logger) {
052: ContainerUtil.enableLogging(this , logger);
053: }
054:
055: /**
056: * Initializes this identity.
057: */
058: public void initialize() {
059: addIdentifiable(World.getInstance());
060: }
061:
062: /**
063: * In the case of Tomcat the object will be serialized to TOMCAT/work/Standalone/localhost/lenya/SESSIONS.ser
064: * @param out OutputStream to hold the serialized identity
065: * @throws IOException
066: */
067: private void writeObject(ObjectOutputStream out) throws IOException {
068: out.defaultWriteObject();
069: out.writeObject(this .identifiables);
070: }
071:
072: /**
073: * In case of Tomcat the object will be restored from TOMCAT/work/Standalone/localhost/lenya/SESSIONS.ser
074: * @param in InputStream that holds the serialized identity
075: * @throws IOException
076: * @throws ClassNotFoundException
077: */
078: private void readObject(ObjectInputStream in) throws IOException,
079: ClassNotFoundException {
080: in.defaultReadObject();
081: this .identifiables = (Set) in.readObject();
082: }
083:
084: /**
085: * Returns the identifiables of this identity.
086: * @return An array of identifiables.
087: */
088: public Identifiable[] getIdentifiables() {
089: return (Identifiable[]) this .identifiables
090: .toArray(new Identifiable[this .identifiables.size()]);
091: }
092:
093: /**
094: * Adds a new identifiable to this identity.
095: * @param identifiable The identifiable to add.
096: */
097: public void addIdentifiable(Identifiable identifiable) {
098: assert identifiable != null;
099: assert identifiable != this ;
100: assert !this .identifiables.contains(identifiable);
101:
102: if (getLogger().isDebugEnabled()) {
103: getLogger().debug(
104: "Adding identifiable: [" + identifiable + "]");
105: }
106:
107: this .identifiables.add(identifiable);
108: }
109:
110: /**
111: * @see Accreditable#getAccreditables()
112: */
113: public Accreditable[] getAccreditables() {
114: Set accreditables = new HashSet();
115: Identifiable[] _identifiables = getIdentifiables();
116:
117: for (int i = 0; i < _identifiables.length; i++) {
118: Accreditable[] groupAccreditables = _identifiables[i]
119: .getAccreditables();
120: accreditables.addAll(Arrays.asList(groupAccreditables));
121: }
122:
123: return (Accreditable[]) accreditables
124: .toArray(new Accreditable[accreditables.size()]);
125: }
126:
127: /**
128: * @see java.lang.Object#toString()
129: */
130: public String toString() {
131: StringBuffer buf = new StringBuffer();
132: Accreditable[] accreditables = getAccreditables();
133:
134: for (int i = 0; i < accreditables.length; i++) {
135: buf.append(" " + accreditables[i]);
136: }
137:
138: String string = "[identity:" + buf.toString() + "]";
139:
140: return string;
141: }
142:
143: /**
144: * Checks if this identity belongs to a certain accreditable manager.
145: * @param manager The accreditable manager to check for.
146: * @return A boolean value.
147: * @throws AccessControlException if an error occurs
148: */
149: public boolean belongsTo(AccreditableManager manager)
150: throws AccessControlException {
151: User user = getUser();
152: if (user == null) {
153: return true;
154: } else {
155: String this Id = user.getAccreditableManager().getId();
156: String otherId = manager.getId();
157: return this Id.equals(otherId);
158: }
159: }
160:
161: /**
162: * Returns the user of this identity.
163: * @return A user.
164: */
165: public User getUser() {
166: User user = null;
167: Identifiable[] _identifiables = getIdentifiables();
168: int i = 0;
169: while (user == null && i < _identifiables.length) {
170: if (_identifiables[i] instanceof User) {
171: user = (User) _identifiables[i];
172: }
173: i++;
174: }
175: return user;
176: }
177:
178: /**
179: * Returns the machine of this identity.
180: * @return A machine.
181: */
182: public Machine getMachine() {
183: Machine machine = null;
184: Identifiable[] _identifiables = getIdentifiables();
185: int i = 0;
186: while (machine == null && i < _identifiables.length) {
187: if (_identifiables[i] instanceof Machine) {
188: machine = (Machine) _identifiables[i];
189: }
190: i++;
191: }
192: return machine;
193: }
194:
195: /**
196: * Checks if this identity contains a certain identifiable.
197: * @param identifiable The identifiable to look for.
198: * @return A boolean value.
199: */
200: public boolean contains(Identifiable identifiable) {
201: return this .identifiables.contains(identifiable);
202: }
203:
204: /**
205: * Fetches the identity from a session.
206: * @param session The session.
207: * @return An identity.
208: */
209: public static Identity getIdentity(Session session) {
210: Identity identity = (Identity) session
211: .getAttribute(Identity.class.getName());
212: return identity;
213: }
214:
215: /**
216: * Removes a certain identifiable from the idenity.
217: * @param identifiable An identifiable.
218: */
219: public void removeIdentifiable(Identifiable identifiable) {
220: assert this.identifiables.contains(identifiable);
221: this.identifiables.remove(identifiable);
222: }
223:
224: }
|