1 17 18 package org.sape.carbon.services.security.management; 19 20 import java.io.Serializable ; 21 import java.security.AccessControlException ; 22 import java.security.Principal ; 23 import java.security.acl.Group ; 24 import java.util.Collections ; 25 import java.util.Enumeration ; 26 import java.util.HashSet ; 27 import java.util.Set ; 28 29 import org.sape.carbon.core.component.Lookup; 30 import org.sape.carbon.core.exception.ExceptionUtility; 31 32 55 public class DefaultGroupImpl implements LazyLoadGroup, Serializable { 56 57 protected transient String userManagerServiceLocation; 58 59 60 protected Object membersLock = new Object (); 61 62 63 protected Set members; 64 65 66 protected String name; 67 68 75 private ThreadLocal checkedGroupsLocal = new ThreadLocal (); 76 77 88 public DefaultGroupImpl( 89 String name, Set members, String userManagerServiceLocation) { 90 this.name = name; 91 this.members = members; 92 93 this.userManagerServiceLocation = userManagerServiceLocation; 94 } 95 96 104 public synchronized void setMembers(Set members) { 105 if (this.members != null) { 106 throw new IllegalStateException ( 107 "Object was initialized without " 108 + "any members and no members have been added."); 109 } 110 111 this.members = members; 112 } 113 114 123 public boolean isLoaded() { 124 return !(this.members == null); 125 } 126 127 146 public synchronized boolean addMember(Principal principal) { 147 boolean result = false; 148 149 if (this.members == null) { 150 throw new IllegalStateException ( 151 "Object was initialized without " 152 + "any members and no members have been added."); 153 } 154 155 if (this.userManagerServiceLocation == null) { 156 throw new UnsupportedOperationException ( 157 "Cannot add users to group. " 158 + "Try using the UserManager.addUserToGroup() method instead."); 159 } else { 160 UserManager userManager = 161 (UserManager) Lookup.getInstance().fetchComponent( 162 this.userManagerServiceLocation); 163 164 try { 165 result = userManager.addPrincipalToGroup(principal, this); 166 members.add(principal); 167 } catch (SecurityManagementException sme) { 168 throw new AccessControlException ( 169 "Caught SecurityManagementException: " 170 + ExceptionUtility.printStackTracesToString(sme)); 171 } 172 } 173 174 return result; 175 } 176 177 196 public synchronized boolean removeMember(Principal principal) { 197 boolean result = false; 198 199 if (this.members == null) { 200 throw new IllegalStateException ( 201 "Object was initialized without " 202 + "any members and no members have been added."); 203 } 204 205 if (this.userManagerServiceLocation == null) { 206 throw new UnsupportedOperationException ( 207 "Cannot remove users from group. " 208 + "Try using the UserManager.addUserToGroup() method instead."); 209 } else { 210 UserManager userManager = 211 (UserManager) Lookup.getInstance().fetchComponent( 212 this.userManagerServiceLocation); 213 214 try { 215 result = 216 userManager.removePrincipalFromGroup(principal, this); 217 members.remove(principal); 218 } catch (SecurityManagementException sme) { 219 throw new AccessControlException ( 220 "Caught SecurityManagementException: " 221 + ExceptionUtility.printStackTracesToString(sme)); 222 } 223 } 224 225 return result; 226 } 227 228 239 protected boolean isDirectMember(Principal principal) { 240 boolean isDirectMember = this.members.contains(principal); 241 242 if (!isDirectMember) { 243 Enumeration membersEnumeration = this.members(); 244 while ( 245 membersEnumeration.hasMoreElements() && (!isDirectMember)) { 246 Principal currentMember = 247 (Principal ) membersEnumeration.nextElement(); 248 249 if (principal != null && currentMember != null 250 && principal.getName().equals(currentMember.getName())) { 251 isDirectMember = true; 252 } 253 } 254 } 255 256 return isDirectMember; 257 } 258 259 293 public synchronized boolean isMember(Principal principal) { 294 boolean isMember = false; 298 299 if (this.members == null) { 300 throw new IllegalStateException ( 301 "Object was initialized without " 302 + "any members and no members have been added."); 303 } 304 305 if (isDirectMember(principal)) { 306 isMember = true; 307 } else { 308 boolean isFirstCycle = false; 309 Set checkedGroups = (Set ) checkedGroupsLocal.get(); 310 311 if (checkedGroups == null) { 312 isFirstCycle = true; 316 checkedGroups = new HashSet (); 317 checkedGroupsLocal.set(checkedGroups); 318 } 319 320 checkedGroups.add(this); 321 322 Enumeration membersEnumeration = this.members(); 323 324 while ( 325 membersEnumeration.hasMoreElements() && (!isMember)) { 326 Object currentMember = membersEnumeration.nextElement(); 327 328 if ( 329 currentMember instanceof Group 330 && !checkedGroups.contains(currentMember)) { 331 checkedGroups.add(currentMember); 335 336 if (((Group ) currentMember).isMember(principal)) { 337 isMember = true; 338 } 339 } 340 } 341 342 if (isFirstCycle) { 345 checkedGroupsLocal.set(null); 346 } 347 } 348 349 return isMember; 350 } 351 352 369 public synchronized Enumeration members() { 370 if (this.members == null) { 371 throw new IllegalStateException ( 372 "Object was initialized without " 373 + "any members and no members have been added."); 374 } 375 376 HashSet membersCopy = new HashSet (members); 380 381 return Collections.enumeration(membersCopy); 382 } 383 384 389 public String getName() { 390 return this.name; 391 } 392 393 400 public boolean equals(Object obj) { 401 boolean result = false; 402 403 if (obj instanceof Group ) { 404 if (this.getName().equals(((Group ) obj).getName())) { 405 result = true; 406 } 407 } 408 409 return result; 410 } 411 412 417 public int hashCode() { 418 return getName().hashCode(); 419 } 420 421 431 public String toString() { 432 StringBuffer sb = new StringBuffer (32); 433 sb.append(this.getClass().getName()); 434 sb.append(" ["); 435 sb.append("name="); 436 sb.append(getName()); 437 sb.append(", members.size()="); 438 sb.append(members.size()); 439 sb.append("]"); 440 441 return sb.toString(); 442 } 443 } 444 | Popular Tags |