1 9 package org.jboss.portal.core.security; 10 11 import org.jboss.portal.common.FQN; 12 import org.jboss.portal.common.util.SetMap; 13 14 import java.util.Set ; 15 import java.util.Iterator ; 16 import java.util.Map ; 17 import java.util.HashMap ; 18 import java.util.HashSet ; 19 import java.util.Collections ; 20 import java.io.Serializable ; 21 22 26 public class Scheme implements Serializable 27 { 28 29 30 static final long serialVersionUID = 6459318089372864576L; 31 32 33 private Map paths; 34 35 38 public Scheme() 39 { 40 paths = new HashMap (); 41 } 42 43 48 public Scheme(Scheme other) throws IllegalArgumentException 49 { 50 if (other == null) 51 { 52 throw new IllegalArgumentException ("Cannot copy null"); 53 } 54 55 paths = new HashMap (); 57 for (Iterator i = other.paths.entrySet().iterator(); i.hasNext();) 58 { 59 Map.Entry entry = (Map.Entry)i.next(); 60 FQN key = (FQN)entry.getKey(); 61 SetMap value = (SetMap)entry.getValue(); 62 paths.put(key, value); 63 } 64 } 65 66 69 public Set getChildrenNames(String [] path) 70 { 71 FQN parent = new FQN(path); 72 Set names = new HashSet (); 73 for (Iterator i = paths.keySet().iterator(); i.hasNext();) 74 { 75 FQN child = (FQN)i.next(); 76 if (child.isChildOf(parent)) 77 { 78 names.add(child.getName(child.size() - 1)); 79 } 80 } 81 return names; 82 } 83 84 87 public Set getRoles(String [] path) 88 { 89 FQN fqn = new FQN(path); 90 SetMap sm = (SetMap)paths.get(fqn); 91 if (sm != null) 92 { 93 return new HashSet (sm.keySet()); 94 } 95 else 96 { 97 return Collections.EMPTY_SET; 98 } 99 } 100 101 104 public Set get(String [] path, String role) 105 { 106 Set permissions = null; 107 SetMap sm = (SetMap)paths.get(new FQN(path)); 108 if (sm != null) 109 { 110 permissions = sm.get(role); 111 } 112 if (permissions == null) 113 { 114 permissions = Collections.EMPTY_SET; 115 } 116 return permissions; 117 } 118 119 122 public Set get(String [] path, Set roles) 123 { 124 Set union = null; 125 for (Iterator i = roles.iterator(); i.hasNext();) 126 { 127 String role = (String )i.next(); 128 Set permissions = get(path, role); 129 if (permissions.size() > 0) 130 { 131 if (union == null) 132 { 133 union = new HashSet (permissions); 134 } 135 else 136 { 137 union.addAll(permissions); 138 } 139 } 140 } 141 if (union == null) 142 { 143 union = Collections.EMPTY_SET; 144 } 145 return union; 146 } 147 148 153 public synchronized void add(String [] path, String role, String permission) 154 { 155 FQN key = new FQN(path); 156 157 SetMap sm = (SetMap)paths.get(key); 159 if (sm == null) 160 { 161 sm = new SetMap(); 162 } 163 else 164 { 165 sm = new SetMap(sm); 166 } 167 sm.put(role, permission); 168 169 HashMap copy = new HashMap (paths); 171 copy.put(key, sm); 172 paths = copy; 173 } 174 175 180 public synchronized void remove(String [] path, String role, String permission) 181 { 182 FQN key = new FQN(path); 183 184 SetMap sm = (SetMap)paths.get(key); 186 if (sm != null) 187 { 188 sm = new SetMap(sm); 189 sm.remove(role, permission); 190 HashMap copy = new HashMap (paths); 191 copy.put(key, sm); 192 paths = copy; 193 } 194 } 195 196 201 public synchronized void remove(String [] path) 202 { 203 FQN key = new FQN(path); 204 Map copy = new HashMap (paths); 205 copy.remove(key); 206 paths = copy; 207 } 208 209 214 public synchronized void clear() 215 { 216 paths = new HashMap (); 217 } 218 } 219 | Popular Tags |