001: package org.osbl.authorization;
002:
003: import org.osbl.ItemRegistry;
004:
005: import java.security.PermissionCollection;
006: import java.security.Permission;
007: import java.util.*;
008:
009: public class PatternPermissionCollection extends PermissionCollection {
010: SortedSet<Permission> permissions = new TreeSet<Permission>();
011: private Map<String, SortedSet<String>> filteredPermissionsByAction;
012:
013: public void add(Permission permission) {
014: if (!(permission instanceof Pattern))
015: throw new IllegalArgumentException(
016: "Pattern Permission expected");
017:
018: permissions.add(permission);
019: filteredPermissionsByAction = null;
020: }
021:
022: public void include(String pattern, String action) {
023: permissions.add(new Pattern(PatternType.INCLUDE, action,
024: pattern));
025: filteredPermissionsByAction = null;
026: }
027:
028: public void exclude(String pattern, String action) {
029: permissions.add(new Pattern(PatternType.EXCLUDE, action,
030: pattern));
031: filteredPermissionsByAction = null;
032: }
033:
034: public void clearPermissions() {
035: permissions.clear();
036: filteredPermissionsByAction = null;
037: }
038:
039: public boolean implies(Permission permission) {
040: Map<String, SortedSet<String>> map = matchPermissions();
041: SortedSet<String> permissions = map
042: .get(permission.getActions());
043: return permissions.contains(permission.getName());
044: }
045:
046: public Enumeration<Permission> elements() {
047: return Collections.enumeration(permissions);
048: }
049:
050: public Map<String, SortedSet<String>> matchPermissions() {
051: if (filteredPermissionsByAction == null) {
052: List<Pattern> patterns = new ArrayList(permissions);
053: Collections.sort(patterns);
054: filteredPermissionsByAction = new HashMap<String, SortedSet<String>>();
055:
056: Map<String, SortedSet<String>> permissionsByAction = getPermissionsByAction();
057:
058: for (Pattern pattern : patterns) {
059: String actions = pattern.getAction();
060:
061: if (pattern.getType() == PatternType.INCLUDE) {
062: for (int i = 0; i < actions.length(); i++) {
063: String action = actions.substring(i, i + 1);
064: SortedSet<String> filteredPermissions = filteredPermissionsByAction
065: .get(action);
066:
067: if (filteredPermissions == null) {
068: filteredPermissions = new TreeSet<String>();
069: filteredPermissionsByAction.put(action,
070: filteredPermissions);
071: }
072: include(permissionsByAction.get(action),
073: filteredPermissions, pattern
074: .getPattern());
075: }
076: } else {
077: for (int i = 0; i < actions.length(); i++) {
078: String action = actions.substring(i, i + 1);
079: SortedSet<String> filteredPermissions = filteredPermissionsByAction
080: .get(action);
081:
082: if (filteredPermissions == null) {
083: filteredPermissions = new TreeSet<String>(
084: permissionsByAction.get(action));
085: filteredPermissionsByAction.put(action,
086: filteredPermissions);
087: }
088: exclude(filteredPermissions, pattern
089: .getPattern());
090: }
091: }
092: }
093: for (Map.Entry<String, SortedSet<String>> entry : permissionsByAction
094: .entrySet()) {
095: SortedSet<String> filteredPermissions = filteredPermissionsByAction
096: .get(entry.getKey());
097: if (filteredPermissions == null) {
098: filteredPermissionsByAction.put(entry.getKey(),
099: new TreeSet<String>());
100: System.out.println("No permissions for action = "
101: + entry.getKey());
102: }
103: }
104: }
105: return filteredPermissionsByAction;
106: }
107:
108: private void include(SortedSet<String> permissions,
109: SortedSet<String> filteredPermissions, String patternString) {
110: java.util.regex.Pattern pattern = java.util.regex.Pattern
111: .compile(patternString);
112: for (String permission : permissions) {
113: if (pattern.matcher(permission).matches())
114: filteredPermissions.add(permission);
115: }
116: }
117:
118: private void exclude(SortedSet<String> filteredPermissions,
119: String patternString) {
120: java.util.regex.Pattern pattern = java.util.regex.Pattern
121: .compile(patternString);
122: for (Iterator<String> iterator = filteredPermissions.iterator(); iterator
123: .hasNext();) {
124: String permission = iterator.next();
125: if (pattern.matcher(permission).matches())
126: iterator.remove();
127: }
128: }
129:
130: protected Map<String, SortedSet<String>> getPermissionsByAction() {
131: return PermissionCache.getInstance().getPermissionsByAction();
132: }
133: }
|