| 1 11 package org.eclipse.osgi.framework.internal.core; 12 13 import java.io.*; 14 import java.lang.reflect.*; 15 import java.net.*; 16 import java.security.*; 17 import java.util.*; 18 import org.eclipse.core.runtime.internal.adaptor.ContextFinder; 19 import org.eclipse.osgi.framework.adaptor.*; 20 import org.eclipse.osgi.framework.debug.Debug; 21 import org.eclipse.osgi.framework.eventmgr.*; 22 import org.eclipse.osgi.framework.internal.protocol.ContentHandlerFactory; 23 import org.eclipse.osgi.framework.internal.protocol.StreamHandlerFactory; 24 import org.eclipse.osgi.framework.log.FrameworkLog; 25 import org.eclipse.osgi.framework.log.FrameworkLogEntry; 26 import org.eclipse.osgi.framework.util.SecureAction; 27 import org.eclipse.osgi.internal.profile.Profile; 28 import org.eclipse.osgi.util.ManifestElement; 29 import org.eclipse.osgi.util.NLS; 30 import org.osgi.framework.*; 31 32 35 public class Framework implements EventDispatcher, EventPublisher { 36 private static final String PROP_CONTEXTCLASSLOADER_PARENT = "osgi.contextClassLoaderParent"; private static final String CONTEXTCLASSLOADER_PARENT_APP = "app"; private static final String CONTEXTCLASSLOADER_PARENT_EXT = "ext"; private static final String CONTEXTCLASSLOADER_PARENT_BOOT = "boot"; private static final String CONTEXTCLASSLOADER_PARENT_FWK = "fwk"; 43 private static String J2SE = "J2SE-"; private static String JAVASE = "JavaSE-"; private static String PROFILE_EXT = ".profile"; 47 protected FrameworkAdaptor adaptor; 48 52 protected Properties properties; 53 54 protected boolean active; 55 56 protected BundleRepository bundles; 57 58 protected PackageAdminImpl packageAdmin; 59 60 protected PermissionAdminImpl permissionAdmin; 61 65 protected StartLevelManager startLevelManager; 66 67 protected ServiceRegistry serviceRegistry; 69 protected long serviceid; 70 71 78 79 protected EventListeners bundleEvent; 80 protected static final int BUNDLEEVENT = 1; 81 82 protected EventListeners bundleEventSync; 83 protected static final int BUNDLEEVENTSYNC = 2; 84 85 protected EventListeners serviceEvent; 86 protected static final int SERVICEEVENT = 3; 87 88 protected EventListeners frameworkEvent; 89 protected static final int FRAMEWORKEVENT = 4; 90 protected static final int BATCHEVENT_BEGIN = Integer.MIN_VALUE + 1; 91 protected static final int BATCHEVENT_END = Integer.MIN_VALUE; 92 93 protected EventManager eventManager; 94 95 protected Hashtable installLock; 96 97 protected SystemBundle systemBundle; 98 String [] bootDelegation; 99 String [] bootDelegationStems; 100 boolean bootDelegateAll = false; 101 boolean contextBootDelegation = "true".equals(FrameworkProperties.getProperty("osgi.context.bootdelegation", "true")); boolean compatibiltyBootDelegation = false; 103 104 107 protected static AliasMapper aliasMapper = new AliasMapper(); 108 protected ConditionalPermissionAdminImpl condPermAdmin; 109 SecureAction secureAction = (SecureAction) AccessController.doPrivileged(SecureAction.createSecureAction()); 110 private HashMap adminPermissions = new HashMap(); 112 113 private StreamHandlerFactory streamHandlerFactory; 115 private ContentHandlerFactory contentHandlerFactory; 116 117 121 static { 122 Class c; 123 c = GetDataFileAction.class; 124 c.getName(); } 126 127 static class GetDataFileAction implements PrivilegedAction { 128 private AbstractBundle bundle; 129 private String filename; 130 131 public GetDataFileAction(AbstractBundle bundle, String filename) { 132 this.bundle = bundle; 133 this.filename= filename; 134 } 135 136 public Object run() { 137 return bundle.getBundleData().getDataFile(filename); 138 } 139 } 140 141 146 public Framework(FrameworkAdaptor adaptor) { 147 initialize(adaptor); 148 } 149 150 155 protected void initialize(FrameworkAdaptor adaptor) { 156 if (Profile.PROFILE && Profile.STARTUP) 157 Profile.logEnter("Framework.initialze()", null); long start = System.currentTimeMillis(); 159 this.adaptor = adaptor; 160 active = false; 161 installSecurityManager(); 162 if (Debug.DEBUG && Debug.DEBUG_SECURITY) { 163 Debug.println("SecurityManager: " + System.getSecurityManager()); Debug.println("ProtectionDomain of Framework.class: \n" + this.getClass().getProtectionDomain()); } 166 setNLSFrameworkLog(); 167 initializeContextFinder(); 169 170 adaptor.initialize(this); 171 if (Profile.PROFILE && Profile.STARTUP) 172 Profile.logTime("Framework.initialze()", "adapter initialized"); try { 174 adaptor.initializeStorage(); 175 } catch (IOException e) { 176 e.printStackTrace(); 177 throw new RuntimeException (e.getMessage()); 178 } 179 if (Profile.PROFILE && Profile.STARTUP) 180 Profile.logTime("Framework.initialze()", "adapter storage initialized"); 185 initializeProperties(adaptor.getProperties()); 186 187 packageAdmin = new PackageAdminImpl(this); 188 SecurityManager sm = System.getSecurityManager(); 189 if (sm != null) { 190 try { 191 permissionAdmin = new PermissionAdminImpl(this, adaptor.getPermissionStorage()); 192 } catch (IOException e) { 193 e.printStackTrace(); 194 throw new RuntimeException (e.getMessage()); 195 } 196 try { 197 condPermAdmin = new ConditionalPermissionAdminImpl(this, adaptor.getPermissionStorage()); 198 } catch (IOException e) { 199 e.printStackTrace(); 200 throw new RuntimeException (e.getMessage()); 201 } 202 } 203 if (Profile.PROFILE && Profile.STARTUP) 204 Profile.logTime("Framework.initialze()", "done init props & new PermissionAdminImpl"); startLevelManager = new StartLevelManager(this); 206 207 eventManager = new EventManager("Framework Event Dispatcher"); bundleEvent = new EventListeners(); 209 bundleEventSync = new EventListeners(); 210 serviceEvent = new EventListeners(); 211 frameworkEvent = new EventListeners(); 212 if (Profile.PROFILE && Profile.STARTUP) 213 Profile.logTime("Framework.initialze()", "done new EventManager"); 215 serviceid = 1; 216 serviceRegistry = adaptor.getServiceRegistry(); 217 installLock = new Hashtable(10); 221 222 createSystemBundle(); 223 loadVMProfile(); setBootDelegation(); if (Profile.PROFILE && Profile.STARTUP) 226 Profile.logTime("Framework.initialze()", "done createSystemBundle"); 228 installURLStreamHandlerFactory(systemBundle.context, adaptor); 229 230 installContentHandlerFactory(systemBundle.context, adaptor); 231 if (Profile.PROFILE && Profile.STARTUP) 232 Profile.logTime("Framework.initialze()", "done new URLStream/Content HandlerFactory"); 234 BundleData[] bundleDatas = adaptor.getInstalledBundles(); 235 bundles = new BundleRepository(bundleDatas == null ? 10 : bundleDatas.length + 1); 236 237 bundles.add(systemBundle); 238 if (bundleDatas != null) { 239 for (int i = 0; i < bundleDatas.length; i++) { 240 try { 241 AbstractBundle bundle = AbstractBundle.createBundle(bundleDatas[i], this); 242 bundles.add(bundle); 243 } catch (BundleException be) { 244 publishFrameworkEvent(FrameworkEvent.ERROR, systemBundle, be); 246 } 247 } 248 } 249 if (Debug.DEBUG && Debug.DEBUG_GENERAL) 250 System.out.println("Initialize the framework: " + (System.currentTimeMillis() - start)); if (Profile.PROFILE && Profile.STARTUP) 252 Profile.logExit("Framework.initialize()"); } 254 255 private void setNLSFrameworkLog() { 256 try { 257 Field frameworkLogField = NLS.class.getDeclaredField("frameworkLog"); frameworkLogField.setAccessible(true); 259 frameworkLogField.set(null, adaptor.getFrameworkLog()); 260 } catch (Exception e) { 261 adaptor.getFrameworkLog().log(new FrameworkLogEntry(FrameworkAdaptor.FRAMEWORK_SYMBOLICNAME, FrameworkLogEntry.ERROR, 0, e.getMessage(), 0, e, null)); 262 } 263 } 264 265 private void createSystemBundle() { 266 try { 267 systemBundle = new SystemBundle(this); 268 } catch (BundleException e) { e.printStackTrace(); 270 throw new RuntimeException (NLS.bind(Msg.OSGI_SYSTEMBUNDLE_CREATE_EXCEPTION, e.getMessage())); 271 } 272 } 273 274 279 protected void initializeProperties(Properties adaptorProperties) { 280 properties = FrameworkProperties.getProperties(); 281 Enumeration enumKeys = adaptorProperties.propertyNames(); 282 while (enumKeys.hasMoreElements()) { 283 String key = (String ) enumKeys.nextElement(); 284 if (properties.getProperty(key) == null) { 285 properties.put(key, adaptorProperties.getProperty(key)); 286 } 287 } 288 properties.put(Constants.FRAMEWORK_VENDOR, Constants.OSGI_FRAMEWORK_VENDOR); 289 properties.put(Constants.FRAMEWORK_VERSION, Constants.OSGI_FRAMEWORK_VERSION); 290 String value = properties.getProperty(Constants.FRAMEWORK_PROCESSOR); 291 if (value == null) { 292 value = properties.getProperty(Constants.JVM_OS_ARCH); 293 if (value != null) { 294 properties.put(Constants.FRAMEWORK_PROCESSOR, value); 295 } 296 } 297 value = properties.getProperty(Constants.FRAMEWORK_OS_NAME); 298 if (value == null) { 299 value = properties.getProperty(Constants.JVM_OS_NAME); 300 try { 301 String canonicalValue = (String ) aliasMapper.aliasOSName(value); 302 if (canonicalValue != null) { 303 value = canonicalValue; 304 } 305 } catch (ClassCastException ex) { 306 } 310 if (value != null) { 311 properties.put(Constants.FRAMEWORK_OS_NAME, value); 312 } 313 } 314 value = properties.getProperty(Constants.FRAMEWORK_OS_VERSION); 315 if (value == null) { 316 value = properties.getProperty(Constants.JVM_OS_VERSION); 317 if (value != null) { 318 int space = value.indexOf(' '); 319 if (space > 0) { 320 value = value.substring(0, space); 321 } 322 properties.put(Constants.FRAMEWORK_OS_VERSION, value); 323 } 324 } 325 value = properties.getProperty(Constants.FRAMEWORK_LANGUAGE); 326 if (value == null) 327 properties.put(Constants.FRAMEWORK_LANGUAGE, Locale.getDefault().getLanguage()); 329 properties.put(Constants.SUPPORTS_FRAMEWORK_FRAGMENT, "true"); 331 properties.put(Constants.SUPPORTS_FRAMEWORK_REQUIREBUNDLE, "true"); 332 } 333 334 private void setBootDelegation() { 335 compatibiltyBootDelegation = "true".equals(FrameworkProperties.getProperty(Constants.OSGI_COMPATIBILITY_BOOTDELEGATION)); String bootDelegationProp = properties.getProperty(Constants.OSGI_BOOTDELEGATION); 339 if (bootDelegationProp == null) 340 return; 341 if (bootDelegationProp.trim().length() == 0) 342 return; 343 String [] bootPackages = ManifestElement.getArrayFromList(bootDelegationProp); 344 ArrayList exactMatch = new ArrayList(bootPackages.length); 345 ArrayList stemMatch = new ArrayList(bootPackages.length); 346 for (int i = 0; i < bootPackages.length; i++) { 347 if (bootPackages[i].equals("*")) { bootDelegateAll = true; 349 return; 350 } else if (bootPackages[i].endsWith("*")) { if (bootPackages[i].length() > 2 && bootPackages[i].endsWith(".*")) stemMatch.add(bootPackages[i].substring(0, bootPackages[i].length() - 1)); 353 } else { 354 exactMatch.add(bootPackages[i]); 355 } 356 } 357 if (exactMatch.size() > 0) 358 bootDelegation = (String []) exactMatch.toArray(new String [exactMatch.size()]); 359 if (stemMatch.size() > 0) 360 bootDelegationStems = (String []) stemMatch.toArray(new String [stemMatch.size()]); 361 } 362 363 private void loadVMProfile() { 364 Properties profileProps = findVMProfile(); 365 String systemExports = properties.getProperty(Constants.OSGI_FRAMEWORK_SYSTEM_PACKAGES); 366 if (systemExports == null) { 368 systemExports = profileProps.getProperty(Constants.OSGI_FRAMEWORK_SYSTEM_PACKAGES); 369 if (systemExports != null) 370 properties.put(Constants.OSGI_FRAMEWORK_SYSTEM_PACKAGES, systemExports); 371 } 372 String type = properties.getProperty(Constants.OSGI_JAVA_PROFILE_BOOTDELEGATION); String profileBootDelegation = profileProps.getProperty(Constants.OSGI_BOOTDELEGATION); 375 if (Constants.OSGI_BOOTDELEGATION_OVERRIDE.equals(type)) { 376 if (profileBootDelegation == null) 377 properties.remove(Constants.OSGI_BOOTDELEGATION); else 379 properties.put(Constants.OSGI_BOOTDELEGATION, profileBootDelegation); } else if (Constants.OSGI_BOOTDELEGATION_NONE.equals(type)) 381 properties.remove(Constants.OSGI_BOOTDELEGATION); if (properties.getProperty(Constants.FRAMEWORK_EXECUTIONENVIRONMENT) == null) { 384 String ee = profileProps.getProperty(Constants.FRAMEWORK_EXECUTIONENVIRONMENT, profileProps.getProperty(Constants.OSGI_JAVA_PROFILE_NAME)); 386 if (ee != null) 387 properties.put(Constants.FRAMEWORK_EXECUTIONENVIRONMENT, ee); 388 } 389 } 390 391 private Properties findVMProfile() { 392 Properties result = new Properties(); 393 String j2meConfig = properties.getProperty(Constants.J2ME_MICROEDITION_CONFIGURATION); 395 String j2meProfiles = properties.getProperty(Constants.J2ME_MICROEDITION_PROFILES); 396 String vmProfile = null; 397 String javaEdition = null; 398 Version javaVersion = null; 399 if (j2meConfig != null && j2meConfig.length() > 0 && j2meProfiles != null && j2meProfiles.length() > 0) { 400 String [] j2meProfileList = ManifestElement.getArrayFromList(j2meProfiles, " "); if (j2meProfileList != null && j2meProfileList.length > 0) 404 vmProfile = j2meConfig + '_' + j2meProfileList[j2meProfileList.length - 1]; 405 } else { 406 String javaSpecVersion = properties.getProperty("java.specification.version"); if (javaSpecVersion != null) { 414 StringTokenizer st = new StringTokenizer(javaSpecVersion, " _-"); javaSpecVersion = st.nextToken(); 416 String javaSpecName = properties.getProperty("java.specification.name"); if ("J2ME Foundation Specification".equals(javaSpecName)) vmProfile = "CDC-" + javaSpecVersion + "_Foundation-" + javaSpecVersion; else { 420 Version v16 = new Version("1.6"); javaEdition = J2SE; 423 try { 424 javaVersion = new Version(javaSpecVersion); 425 if (v16.compareTo(javaVersion) <= 0) 426 javaEdition = JAVASE; 427 } catch (IllegalArgumentException e) { 428 } 430 vmProfile = javaEdition + javaSpecVersion; 431 } 432 } 433 } 434 URL url = null; 435 String propJavaProfile = FrameworkProperties.getProperty(Constants.OSGI_JAVA_PROFILE); 437 if (propJavaProfile != null) 438 try { 439 url = new URL(propJavaProfile); 441 } catch (MalformedURLException e1) { 442 url = findInSystemBundle(propJavaProfile); 444 } 445 if (url == null && vmProfile != null) { 446 String javaProfile = vmProfile + PROFILE_EXT; 448 url = findInSystemBundle(javaProfile); 449 if (url == null) 450 url = getNextBestProfile(javaEdition, javaVersion); 451 } 452 if (url == null) 453 url = findInSystemBundle("OSGi_Minimum-1.1.profile"); if (url != null) { 456 InputStream in = null; 457 try { 458 in = url.openStream(); 459 result.load(new BufferedInputStream(in)); 460 } catch (IOException e) { 461 } finally { 463 if (in != null) 464 try { 465 in.close(); 466 } catch (IOException ee) { 467 } 469 } 470 } 471 if (result.getProperty(Constants.OSGI_JAVA_PROFILE_NAME) == null) 473 if (vmProfile != null) 474 result.put(Constants.OSGI_JAVA_PROFILE_NAME, vmProfile.replace('_', '/')); 475 else 476 result.put(Constants.OSGI_JAVA_PROFILE_NAME, "OSGi/Minimum-1.1"); return result; 479 } 480 481 private URL getNextBestProfile(String javaEdition, Version javaVersion) { 482 if (javaVersion == null || (javaEdition != J2SE && javaEdition != JAVASE)) 483 return null; URL bestProfile = findNextBestProfile(javaEdition, javaVersion); 485 if (bestProfile == null && javaEdition == JAVASE) 486 bestProfile = findNextBestProfile(J2SE, javaVersion); 488 return bestProfile; 489 } 490 491 private URL findNextBestProfile(String javaEdition, Version javaVersion) { 492 URL result = null; 493 int minor = javaVersion.getMinor(); 494 do { 495 result = findInSystemBundle(javaEdition + javaVersion.getMajor() + "." + minor + PROFILE_EXT); minor = minor - 1; 497 } while (result == null && minor > 0); 498 return result; 499 } 500 501 private URL findInSystemBundle(String entry) { 502 URL result = systemBundle.getEntry(entry); 503 if (result == null) { 504 ClassLoader loader=getClass().getClassLoader(); 506 result = loader==null ? ClassLoader.getSystemResource(entry) : loader.getResource(entry); 507 } 508 return result; 509 } 510 511 515 protected boolean isActive() { 516 return (active); 517 } 518 519 523 public synchronized void close() { 524 if (active) { 525 shutdown(); 526 } 527 synchronized (bundles) { 528 List allBundles = bundles.getBundles(); 529 int size = allBundles.size(); 530 for (int i = 0; i < size; i++) { 531 AbstractBundle bundle = (AbstractBundle) allBundles.get(i); 532 bundle.close(); 533 } 534 bundles.removeAllBundles(); 535 } 536 serviceRegistry = null; 537 if (bundleEvent != null) { 538 bundleEvent.removeAllListeners(); 539 bundleEvent = null; 540 } 541 if (bundleEventSync != null) { 542 bundleEventSync.removeAllListeners(); 543 bundleEventSync = null; 544 } 545 if (serviceEvent != null) { 546 serviceEvent.removeAllListeners(); 547 serviceEvent = null; 548 } 549 if (frameworkEvent != null) { 550 frameworkEvent.removeAllListeners(); 551 frameworkEvent = null; 552 } 553 if (eventManager != null) { 554 eventManager.close(); 555 eventManager = null; 556 } 557 permissionAdmin = null; 558 condPermAdmin = null; 559 packageAdmin = null; 560 adaptor = null; 561 uninstallURLStreamHandlerFactory(); 562 uninstallContentHandlerFactory(); 563 } 564 565 577 public synchronized void launch() { 578 579 if (active) { 580 return; 581 } 582 583 active = true; 584 585 if (Debug.DEBUG && Debug.DEBUG_GENERAL) { 586 Debug.println("Trying to launch framework"); } 588 systemBundle.resume(); 589 } 590 591 601 public synchronized void shutdown() { 602 603 if (!active) { 604 return; 605 } 606 610 systemBundle.state = AbstractBundle.STOPPING; 611 publishBundleEvent(BundleEvent.STOPPING, systemBundle); 613 try { 614 adaptor.frameworkStopping(systemBundle.getContext()); 615 } catch (Throwable t) { 616 publishFrameworkEvent(FrameworkEvent.ERROR, systemBundle, t); 617 } 618 619 if (Debug.DEBUG && Debug.DEBUG_GENERAL) { 620 Debug.println("Trying to shutdown Framework"); } 622 systemBundle.suspend(); 623 try { 624 adaptor.compactStorage(); 625 } catch (IOException e) { 626 publishFrameworkEvent(FrameworkEvent.ERROR, systemBundle, e); 627 } 628 629 active = false; 630 } 631 632 637 AbstractBundle createAndVerifyBundle(BundleData bundledata) throws BundleException { 638 if (bundledata.getSymbolicName() != null) { 640 AbstractBundle installedBundle = getBundleBySymbolicName(bundledata.getSymbolicName(), bundledata.getVersion()); 641 if (installedBundle != null && installedBundle.getBundleId() != bundledata.getBundleID()) { 642 throw new BundleException(NLS.bind(Msg.BUNDLE_INSTALL_SAME_UNIQUEID, new Object [] {installedBundle.getSymbolicName(), installedBundle.getVersion().toString(), installedBundle.getLocation()})); 643 } 644 } 645 verifyExecutionEnvironment(bundledata.getManifest()); 646 return AbstractBundle.createBundle(bundledata, this); 647 } 648 649 661 protected boolean verifyExecutionEnvironment(Dictionary manifest) throws BundleException { 662 if (!Boolean.valueOf(FrameworkProperties.getProperty(Constants.ECLIPSE_EE_INSTALL_VERIFY, Boolean.TRUE.toString())).booleanValue()) return true; 664 String headerValue = (String ) manifest.get(Constants.BUNDLE_REQUIREDEXECUTIONENVIRONMENT); 665 666 if (headerValue == null) { 667 return true; 668 } 669 ManifestElement[] bundleRequiredEE = ManifestElement.parseHeader(Constants.BUNDLE_REQUIREDEXECUTIONENVIRONMENT, headerValue); 670 if (bundleRequiredEE.length == 0) { 671 return true; 672 } 673 String systemEE = FrameworkProperties.getProperty(Constants.FRAMEWORK_EXECUTIONENVIRONMENT); 674 if (systemEE != null && !systemEE.equals("")) { ManifestElement[] systemEEs = ManifestElement.parseHeader(Constants.BUNDLE_REQUIREDEXECUTIONENVIRONMENT, systemEE); 676 for (int i = 0; i < systemEEs.length; i++) { 677 for (int j = 0; j < bundleRequiredEE.length; j++) { 678 if (systemEEs[i].getValue().equals(bundleRequiredEE[j].getValue())) { 679 return true; 680 } 681 } 682 } 683 } 684 685 StringBuffer bundleEE = new StringBuffer (25); 686 for (int i = 0; i < bundleRequiredEE.length; i++) { 687 if (i > 0) { 688 bundleEE.append(","); } 690 bundleEE.append(bundleRequiredEE[i].getValue()); 691 } 692 throw new BundleException(NLS.bind(Msg.BUNDLE_INSTALL_REQUIRED_EE_EXCEPTION, bundleEE.toString())); 693 } 694 695 722 public String getProperty(String key) { 723 return properties.getProperty(key); 724 } 725 726 |