1 18 19 package org.osgi.service.application; 20 21 import java.security.Permission ; 22 import java.util.*; 23 24 import org.osgi.framework.*; 25 26 38 public class ApplicationAdminPermission extends Permission { 39 private static final long serialVersionUID = 1L; 40 41 44 public static final String LIFECYCLE_ACTION = "lifecycle"; 45 46 52 public static final String SCHEDULE_ACTION = "schedule"; 53 54 57 public static final String LOCK_ACTION = "lock"; 58 59 private ApplicationDescriptor applicationDescriptor; 60 61 95 public ApplicationAdminPermission(String filter, String actions) throws InvalidSyntaxException { 96 super(filter == null ? "*" : filter); 97 98 if( filter == null ) 99 filter = "*"; 100 101 if( actions == null ) 102 throw new NullPointerException ( "Action string cannot be null!" ); 103 104 this.applicationDescriptor = null; 105 this.filter = (filter == null ? "*" : filter); 106 this.actions = actions; 107 108 if( !filter.equals( "*" ) && !filter.equals( "<<SELF>>" ) ) 109 FrameworkUtil.createFilter( this.filter ); init(); 111 } 112 113 120 public ApplicationAdminPermission(ApplicationDescriptor application, String actions) { 121 super(application.getApplicationId()); 122 123 if( application == null || actions == null ) 124 throw new NullPointerException ( "ApplicationDescriptor and action string cannot be null!" ); 125 126 this.filter = application.getApplicationId(); 127 this.applicationDescriptor = application; 128 this.actions = actions; 129 130 init(); 131 } 132 133 142 public ApplicationAdminPermission setCurrentApplicationId(String applicationId) { 143 ApplicationAdminPermission newPerm = null; 144 145 if( this.applicationDescriptor == null ) { 146 try { 147 newPerm = new ApplicationAdminPermission( this.filter, this.actions ); 148 }catch( InvalidSyntaxException e ) { 149 throw new RuntimeException ( "Internal error" ); 150 } 151 } 152 else 153 newPerm = new ApplicationAdminPermission( this.applicationDescriptor, this.actions ); 154 155 newPerm.applicationID = applicationId; 156 157 return newPerm; 158 } 159 160 179 public boolean implies(Permission otherPermission) { 180 if( otherPermission == null ) 181 return false; 182 183 if(!(otherPermission instanceof ApplicationAdminPermission)) 184 return false; 185 186 ApplicationAdminPermission other = (ApplicationAdminPermission) otherPermission; 187 188 if( !filter.equals("*") ) { 189 if( other.applicationDescriptor == null ) 190 return false; 191 192 if( filter.equals( "<<SELF>>") ) { 193 if( other.applicationID == null ) 194 return false; 195 196 if( !other.applicationID.equals( other.applicationDescriptor.getApplicationId() ) ) 197 return false; 198 } 199 else { 200 Hashtable props = new Hashtable(); 201 props.put( "pid", other.applicationDescriptor.getApplicationId() ); 202 props.put( "signer", new SignerWrapper( other.applicationDescriptor ) ); 203 204 Filter flt = getFilter(); 205 if( flt == null ) 206 return false; 207 208 if( !flt.match( props ) ) 209 return false; 210 } 211 } 212 213 if( !actionsVector.containsAll( other.actionsVector ) ) 214 return false; 215 216 return true; 217 } 218 219 public boolean equals(Object with) { 220 if( with == null || !(with instanceof ApplicationAdminPermission) ) 221 return false; 222 223 ApplicationAdminPermission other = (ApplicationAdminPermission)with; 224 225 if( other.actionsVector.size() != actionsVector.size() ) 227 return false; 228 229 for( int i=0; i != actionsVector.size(); i++ ) 230 if( !other.actionsVector.contains( actionsVector.get( i ) ) ) 231 return false; 232 233 234 return equal(this.filter, other.filter ) && equal(this.applicationDescriptor, other.applicationDescriptor) 235 && equal(this.applicationID, other.applicationID); 236 } 237 238 245 private static boolean equal(Object a, Object b) { 246 if( a == b ) { 249 return true; 250 } 251 252 return a.equals(b); 253 } 254 255 public int hashCode() { 256 int hc = 0; 257 for( int i=0; i != actionsVector.size(); i++ ) 258 hc ^= ((String )actionsVector.get( i )).hashCode(); 259 hc ^= (null == this.filter )? 0 : this.filter.hashCode(); 260 hc ^= (null == this.applicationDescriptor) ? 0 : this.applicationDescriptor.hashCode(); 261 hc ^= (null == this.applicationID) ? 0 : this.applicationID.hashCode(); 262 return hc; 263 } 264 265 269 public String getActions() { 270 return actions; 271 } 272 273 private String applicationID; 274 275 private static final Vector ACTIONS = new Vector(); 276 private Vector actionsVector; 277 private final String filter; 278 private final String actions; 279 private Filter appliedFilter = null; 280 281 static { 282 ACTIONS.add(LIFECYCLE_ACTION); 283 ACTIONS.add(SCHEDULE_ACTION); 284 ACTIONS.add(LOCK_ACTION); 285 } 286 287 private static Vector actionsVector(String actions) { 288 Vector v = new Vector(); 289 StringTokenizer t = new StringTokenizer(actions.toUpperCase(), ","); 290 while (t.hasMoreTokens()) { 291 String action = t.nextToken().trim(); 292 v.add(action.toLowerCase()); 293 } 294 295 if( v.contains( SCHEDULE_ACTION ) && !v.contains( LIFECYCLE_ACTION ) ) 296 v.add( LIFECYCLE_ACTION ); 297 298 return v; 299 } 300 301 302 private static class SignerWrapper extends Object { 303 private String pattern; 304 private ApplicationDescriptor appDesc; 305 306 public SignerWrapper(String pattern) { 307 this.pattern = pattern; 308 } 309 310 SignerWrapper(ApplicationDescriptor appDesc) { 311 this.appDesc = appDesc; 312 } 313 314 public boolean equals(Object o) { 315 if (!(o instanceof SignerWrapper)) 316 return false; 317 SignerWrapper other = (SignerWrapper) o; 318 ApplicationDescriptor matchAppDesc = (ApplicationDescriptor) (appDesc != null ? appDesc : other.appDesc); 319 String matchPattern = appDesc != null ? other.pattern : pattern; 320 return matchAppDesc.matchDNChain(matchPattern); 321 } 322 } 323 324 private void init() { 325 actionsVector = actionsVector( actions ); 326 327 if ( actions.equals("*") ) 328 actionsVector = actionsVector( LIFECYCLE_ACTION + "," + SCHEDULE_ACTION + "," + LOCK_ACTION ); 329 else if (!ACTIONS.containsAll(actionsVector)) 330 throw new IllegalArgumentException ("Illegal action!"); 331 332 applicationID = null; 333 } 334 335 private Filter getFilter() { 336 String transformedFilter = filter; 337 338 if (appliedFilter == null) { 339 try { 340 int pos = filter.indexOf("signer"); if (pos != -1){ 342 343 StringBuffer filterBuf = new StringBuffer (filter); 345 int numAsteriskFound = 0; 347 int walkbackPos; 349 while (pos != -1) { 351 352 walkbackPos = pos-1; 354 355 while(walkbackPos >= 0 && Character.isWhitespace(filter.charAt(walkbackPos))) { 357 walkbackPos--; 358 } 359 if (walkbackPos <0) { 360 break; 362 } 363 364 if (filter.charAt(walkbackPos) != '(' || (walkbackPos > 0 && filter.charAt(walkbackPos-1) == '\\')) { 366 pos = filter.indexOf("signer",pos+6); continue; 369 } 370 pos+=6; 372 while (Character.isWhitespace(filter.charAt(pos))) { 374 pos++; 375 } 376 377 if (filter.charAt(pos) != '=') { 379 pos = filter.indexOf("signer",pos); continue; 382 } 383 pos++; 385 while (!(filter.charAt(pos) == ')' && filter.charAt(pos-1) != '\\')) { 387 if (filter.charAt(pos) == '*') { 388 filterBuf.insert(pos+numAsteriskFound,'\\'); 389 numAsteriskFound++; 390 } 391 pos++; 392 } 393 394 pos = filter.indexOf("signer",pos); } transformedFilter = filterBuf.toString(); 398 } 400 appliedFilter = FrameworkUtil.createFilter( transformedFilter ); 401 } catch (InvalidSyntaxException e) { 402 } 404 } 405 return appliedFilter; 406 } 407 } 408 | Popular Tags |