001: package org.osbl.authorization;
002:
003: import org.hibernate.util.EmptyIterator;
004:
005: import java.security.PermissionCollection;
006: import java.security.Permission;
007: import java.util.*;
008:
009: public class UnionPermissionCollection extends PermissionCollection {
010: List<PermissionCollection> permissionCollections = new ArrayList<PermissionCollection>();
011:
012: public void add(Permission permission) {
013: throw new UnsupportedOperationException();
014: }
015:
016: public boolean implies(Permission permission) {
017: for (PermissionCollection permissionCollection : permissionCollections) {
018: if (permissionCollection.implies(permission))
019: return true;
020: }
021: return false;
022: }
023:
024: public Enumeration<Permission> elements() {
025: List<Enumeration> iterators = new ArrayList<Enumeration>(
026: permissionCollections.size());
027: for (PermissionCollection permissionCollection : permissionCollections) {
028: iterators.add(permissionCollection.elements());
029: }
030: return new JoinedIterator(iterators);
031: }
032:
033: public void addPermissionCollection(
034: PermissionCollection permissionCollection) {
035: permissionCollections.add(permissionCollection);
036: }
037:
038: public static class JoinedIterator implements Iterator, Enumeration {
039:
040: // wrapped iterators
041: private Iterator[] iterators;
042:
043: // index of current iterator in the wrapped iterators array
044: private int currentIteratorIndex;
045:
046: // the current iterator
047: private Iterator currentIterator;
048:
049: // the last used iterator
050: private Iterator lastUsedIterator;
051:
052: public JoinedIterator(List iterators) {
053: this ((Iterator[]) iterators.toArray(new Iterator[iterators
054: .size()]));
055: }
056:
057: public JoinedIterator(Iterator[] iterators) {
058: if (iterators == null)
059: throw new NullPointerException(
060: "Unexpected NULL iterators argument");
061: this .iterators = iterators;
062: }
063:
064: public JoinedIterator(Iterator first, Iterator second) {
065: this (new Iterator[] { first, second });
066: }
067:
068: public boolean hasNext() {
069: updateCurrentIterator();
070: return currentIterator.hasNext();
071: }
072:
073: public Object next() {
074: updateCurrentIterator();
075: return currentIterator.next();
076: }
077:
078: public void remove() {
079: updateCurrentIterator();
080: lastUsedIterator.remove();
081: }
082:
083: // call this before any Iterator method to make sure that the current Iterator
084: // is not exhausted
085: protected void updateCurrentIterator() {
086:
087: if (currentIterator == null) {
088: if (iterators.length == 0) {
089: currentIterator = EmptyIterator.INSTANCE;
090: } else {
091: currentIterator = iterators[0];
092: }
093: // set last used iterator here, in case the user calls remove
094: // before calling hasNext() or next() (although they shouldn't)
095: lastUsedIterator = currentIterator;
096: }
097:
098: while (!currentIterator.hasNext()
099: && currentIteratorIndex < iterators.length - 1) {
100: currentIteratorIndex++;
101: currentIterator = iterators[currentIteratorIndex];
102: }
103: }
104:
105: public boolean hasMoreElements() {
106: return hasNext();
107: }
108:
109: public Object nextElement() {
110: return next();
111: }
112: }
113:
114: public static class IteratorEnumeration<T> implements
115: Enumeration<T> {
116: private Iterator<T> iterator;
117:
118: public IteratorEnumeration(Iterator<T> iterator) {
119: this .iterator = iterator;
120: }
121:
122: public boolean hasMoreElements() {
123: return iterator.hasNext();
124: }
125:
126: public T nextElement() throws NoSuchElementException {
127: return iterator.next();
128: }
129: }
130: }
|