1 18 19 package org.osgi.service.application; 20 21 import java.util.Iterator ; 22 import java.util.Map ; 23 import org.eclipse.equinox.internal.app.AppPersistence; 24 import org.osgi.framework.Constants; 25 import org.osgi.framework.InvalidSyntaxException; 26 27 32 33 public abstract class ApplicationDescriptor { 34 40 41 44 public static final String APPLICATION_NAME = "application.name"; 45 46 49 public static final String APPLICATION_ICON = "application.icon"; 50 51 54 public static final String APPLICATION_PID = Constants.SERVICE_PID; 55 56 59 public static final String APPLICATION_VERSION = "application.version"; 60 61 64 public static final String APPLICATION_VENDOR = Constants.SERVICE_VENDOR; 65 66 67 70 public static final String APPLICATION_VISIBLE = "application.visible"; 71 72 75 public static final String APPLICATION_LAUNCHABLE = "application.launchable"; 76 77 80 public static final String APPLICATION_LOCKED = "application.locked"; 81 82 85 public static final String APPLICATION_DESCRIPTION = "application.description"; 86 87 90 public static final String APPLICATION_DOCUMENTATION = "application.documentation"; 91 92 95 public static final String APPLICATION_COPYRIGHT = "application.copyright"; 96 97 100 public static final String APPLICATION_LICENSE = "application.license"; 101 102 105 public static final String APPLICATION_CONTAINER = "application.container"; 106 107 110 public static final String APPLICATION_LOCATION = "application.location"; 111 112 113 private final String pid; 114 115 private boolean[] locked = {false}; 116 117 127 protected ApplicationDescriptor(String applicationId) { 128 if(null == applicationId ) { 129 throw new NullPointerException ("Application ID must not be null!"); 130 } 131 132 this.pid = applicationId; 133 locked[0] = isLocked(); 134 } 135 136 141 public final String getApplicationId() { 142 return pid; 143 } 144 145 164 public abstract boolean matchDNChain( String pattern ); 165 166 197 public final Map getProperties(String locale) { 198 Map props = getPropertiesSpecific( locale ); 199 Boolean containerLocked = (Boolean ) props.remove( APPLICATION_LOCKED ); 200 synchronized (locked) { 201 if (containerLocked != null && containerLocked.booleanValue() != locked[0]) { 202 if (locked[0]) 203 lockSpecific(); 204 else 205 unlockSpecific(); 206 } 207 } 208 props.put( APPLICATION_LOCKED, new Boolean ( locked[0] ) ); 209 return props; 210 } 211 212 242 protected abstract Map getPropertiesSpecific(String locale); 243 244 302 public final ApplicationHandle launch(Map arguments) 303 throws ApplicationException { 304 SecurityManager sm = System.getSecurityManager(); 305 if (sm!= null) 306 sm.checkPermission(new ApplicationAdminPermission(this, ApplicationAdminPermission.LIFECYCLE_ACTION)); 307 synchronized (locked) { 308 if (locked[0]) 309 throw new ApplicationException(ApplicationException.APPLICATION_LOCKED, "Application is locked, can't launch!"); 310 } 311 if( !isLaunchableSpecific() ) 312 throw new ApplicationException(ApplicationException.APPLICATION_NOT_LAUNCHABLE, 313 "Cannot launch the application!"); 314 checkArgs(arguments); 315 try { 316 return launchSpecific(arguments); 317 } catch(IllegalStateException ise) { 318 throw ise; 319 } catch(SecurityException se) { 320 throw se; 321 } catch( ApplicationException ae) { 322 throw ae; 323 } catch(Exception t) { 324 throw new ApplicationException(ApplicationException.APPLICATION_INTERNAL_ERROR, t); 325 } 326 } 327 328 351 protected abstract ApplicationHandle launchSpecific(Map arguments) 352 throws Exception ; 353 354 364 protected abstract boolean isLaunchableSpecific(); 365 366 428 public final ScheduledApplication schedule(String scheduleId, Map arguments, String topic, 429 String eventFilter, boolean recurring) throws InvalidSyntaxException, 430 ApplicationException { 431 SecurityManager sm = System.getSecurityManager(); 432 if (sm != null) 433 sm.checkPermission(new ApplicationAdminPermission(this, ApplicationAdminPermission.SCHEDULE_ACTION)); 434 checkArgs(arguments); 435 isLaunchableSpecific(); return AppPersistence.addScheduledApp(this, scheduleId, arguments, topic, eventFilter, recurring); 437 } 438 439 450 public final void lock() { 451 SecurityManager sm = System.getSecurityManager(); 452 if (sm != null) 453 sm.checkPermission(new ApplicationAdminPermission(this, ApplicationAdminPermission.LOCK_ACTION)); 454 synchronized (locked) { 455 if (locked[0]) 456 return; 457 locked[0] = true; 458 lockSpecific(); 459 saveLock(true); 460 } 461 } 462 463 470 protected abstract void lockSpecific(); 471 472 481 public final void unlock() { 482 SecurityManager sm = System.getSecurityManager(); 483 if (sm != null) 484 sm.checkPermission(new ApplicationAdminPermission(this, ApplicationAdminPermission.LOCK_ACTION)); 485 synchronized (locked) { 486 if (!locked[0]) 487 return; 488 locked[0] = false; 489 unlockSpecific(); 490 saveLock(false); 491 } 492 } 493 494 502 protected abstract void unlockSpecific(); 503 504 private void saveLock(boolean locked) { 505 AppPersistence.saveLock(this, locked); 506 } 507 508 private boolean isLocked() { 509 return AppPersistence.isLocked(this); 510 } 511 512 private void checkArgs(Map arguments) { 513 if (arguments == null) 514 return; 515 for (Iterator keys = arguments.keySet().iterator(); keys.hasNext();) { 516 Object key = keys.next(); 517 if (!(key instanceof String )) 518 throw new IllegalArgumentException ("Invalid key type: " + key == null ? "<null>" : key.getClass().getName()); 519 if ("".equals(key)) throw new IllegalArgumentException ("Empty string is an invalid key"); 521 } 522 } 523 524 525 } | Popular Tags |