1 17 18 19 20 package org.apache.lenya.ac; 21 22 import java.util.Arrays ; 23 import java.util.HashSet ; 24 import java.util.Set ; 25 26 import org.apache.cocoon.environment.Session; 27 import org.apache.log4j.Category; 28 29 32 public class Identity implements Identifiable, java.io.Serializable { 33 private Set identifiables = new HashSet (); 34 35 private static final Category log = Category.getInstance(Identity.class); 36 37 40 public Identity() { 41 addIdentifiable(World.getInstance()); 42 } 43 44 47 private void writeObject(java.io.ObjectOutputStream out) throws java.io.IOException { 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 58 private void readObject(java.io.ObjectInputStream in) throws java.io.IOException , ClassNotFoundException { 59 in.defaultReadObject(); 60 identifiables = (Set ) in.readObject(); 61 62 if (log.isDebugEnabled()) { 63 log.debug("Identity loaded from serialized object: " + this.toString()); 64 } 65 } 66 67 71 public Identifiable[] getIdentifiables() { 72 return (Identifiable[]) identifiables.toArray(new Identifiable[identifiables.size()]); 73 } 74 75 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 94 public Accreditable[] getAccreditables() { 95 Set accreditables = new HashSet (); 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 109 public String toString() { 110 String accrString = ""; 111 Accreditable[] accreditables = getAccreditables(); 112 113 for (int i = 0; i < accreditables.length; i++) { 114 accrString += (" " + accreditables[i]); 115 } 116 117 String string = "[identity:" + accrString + "]"; 118 119 return string; 120 } 121 122 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 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 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 186 public boolean contains(Identifiable identifiable) { 187 return identifiables.contains(identifiable); 188 } 189 190 195 public static Identity getIdentity(Session session) { 196 Identity identity = (Identity) session.getAttribute(Identity.class.getName()); 197 return identity; 198 } 199 200 204 public void removeIdentifiable(Identifiable identifiable) { 205 assert identifiables.contains(identifiable); 206 identifiables.remove(identifiable); 207 } 208 209 } 210 | Popular Tags |