1 18 19 package org.osgi.service.cm; 20 21 import java.security.*; 22 import java.util.Enumeration ; 23 import java.util.NoSuchElementException ; 24 25 33 34 public final class ConfigurationPermission extends BasicPermission { 35 static final long serialVersionUID = 5716868734811965383L; 36 39 public final static String CONFIGURE = "configure"; 40 41 47 48 public ConfigurationPermission(String name, String actions) { 49 super(name); 50 if (!name.equals("*")) { 51 throw new IllegalArgumentException ("name must be *"); 52 } 53 actions = actions.trim(); 54 if (actions.equalsIgnoreCase(CONFIGURE)||actions.equals("*")) 55 return; 56 57 throw new IllegalArgumentException ("actions must be " + CONFIGURE); 58 } 59 60 68 69 public boolean implies(Permission p) { 70 return p instanceof ConfigurationPermission; 71 } 72 73 84 public boolean equals(Object obj) { 85 return obj instanceof ConfigurationPermission; 86 } 87 88 93 94 public int hashCode() { 95 return getName().hashCode() ^ getActions().hashCode(); 96 } 97 98 109 public String getActions() { 110 return CONFIGURE; 111 } 112 113 119 public PermissionCollection newPermissionCollection() { 120 return new ConfigurationPermissionCollection(); 121 } 122 } 123 124 131 final class ConfigurationPermissionCollection extends PermissionCollection { 132 static final long serialVersionUID = -6917638867081695839L; 133 138 private boolean hasElement; 139 140 144 public ConfigurationPermissionCollection() { 145 hasElement = false; 146 } 147 148 161 162 public void add(Permission permission) { 163 if (!(permission instanceof ConfigurationPermission)) { 164 throw new IllegalArgumentException ("invalid permission: " 165 + permission); 166 } 167 168 if (isReadOnly()) 169 throw new SecurityException ("attempt to add a Permission to a " 170 + "readonly PermissionCollection"); 171 172 hasElement = true; 173 } 174 175 184 185 public boolean implies(Permission p) { 186 return hasElement && (p instanceof ConfigurationPermission); 187 } 188 189 194 195 public Enumeration elements() { 196 final boolean nonEmpty = hasElement; 197 return new Enumeration () { 198 private boolean more = nonEmpty; 199 200 public boolean hasMoreElements() { 201 return more; 202 } 203 204 public Object nextElement() { 205 if (more) { 206 more = false; 207 208 return new ConfigurationPermission("*", 209 ConfigurationPermission.CONFIGURE); 210 } 211 else { 212 throw new NoSuchElementException (); 213 } 214 } 215 }; 216 } 217 218 } 219 | Popular Tags |