1 22 package org.jboss.kernel; 23 24 import java.io.IOException ; 25 import java.io.ObjectInputStream ; 26 import java.security.BasicPermission ; 27 import java.security.Permission ; 28 import java.security.PermissionCollection ; 29 import java.util.Enumeration ; 30 import java.util.HashSet ; 31 import java.util.Iterator ; 32 33 46 public class KernelPermission extends BasicPermission 47 { 48 49 private static final long serialVersionUID = 5661980843569388590L; 50 51 52 private transient boolean allNames; 53 54 61 public KernelPermission(String name) 62 { 63 this(name, null); 64 } 65 66 74 public KernelPermission(String name, String actions) 75 { 76 super(name, actions); 77 init(name, actions); 78 } 79 80 83 public String toString() 84 { 85 StringBuffer buffer = new StringBuffer (100); 86 buffer.append(getClass().getName()).append(":"); 87 buffer.append(" name=").append(getName()); 88 buffer.append(" actions=").append(getActions()); 89 return buffer.toString(); 90 } 91 92 103 public boolean implies(Permission p) 104 { 105 if( (p instanceof KernelPermission) == false) 106 return false; 107 108 boolean implies = (allNames == true); 109 if( implies == false ) 110 { 111 String n0 = getName(); 112 String n1 = p.getName(); 113 implies = n0.equals(n1); 114 if( implies == false ) 115 { 116 implies = (n0.equals("configure") && n1.equals("access")); 118 } 119 } 120 return implies; 121 } 122 123 128 public PermissionCollection newPermissionCollection() 129 { 130 return new KernelPermissionCollection(); 131 } 132 133 private void readObject(ObjectInputStream ois) throws IOException , ClassNotFoundException 134 { 135 ois.defaultReadObject(); 136 init(getName(), getActions()); 137 } 138 139 149 private void init(String name, String actions) 150 { 151 if( name == null ) 152 throw new NullPointerException ("name cannot be null"); 153 154 if( actions != null && actions.length() > 0 ) 155 throw new IllegalArgumentException ("actions must be null or empty"); 156 157 if (name.equals("*") == false && 158 name.equals("access") == false && 159 name.equals("configure") == false) 160 throw new IllegalArgumentException ("Unknown name: " + name); 161 allNames = name.equals("*"); 162 } 163 164 167 class KernelPermissionCollection extends PermissionCollection 168 { 169 170 private static final long serialVersionUID = 3256442516797665329L; 171 172 173 private HashSet <Permission > permissions = new HashSet <Permission >(); 174 175 176 private boolean hasAll; 177 178 public void add(Permission p) 179 { 180 if (isReadOnly()) 181 throw new SecurityException ("Collection is read-only"); 182 if (p instanceof KernelPermission) 183 permissions.add(p); 184 if (p.getName().equals("configure")) 185 permissions.add(new KernelPermission("access")); 186 else if (p.getName().equals("*")) 187 hasAll = true; 188 } 189 190 public boolean implies(Permission p) 191 { 192 boolean implies = false; 193 if (p instanceof KernelPermission) 194 { 195 implies = hasAll; 196 if (implies == false) 197 implies = permissions.contains(p); 198 } 199 return implies; 200 } 201 202 public Enumeration <Permission > elements() 203 { 204 final Iterator <Permission > iter = permissions.iterator(); 205 return new Enumeration <Permission >() 206 { 207 public boolean hasMoreElements() 208 { 209 return iter.hasNext(); 210 } 211 212 public Permission nextElement() 213 { 214 return iter.next(); 215 } 216 }; 217 } 218 } 219 } 220 | Popular Tags |