1 36 package org.ungoverned.oscar; 37 38 import java.io.*; 39 import java.net.*; 40 import java.security.*; 41 import java.security.cert.Certificate ; 42 import java.util.*; 43 44 import org.osgi.framework.*; 45 import org.osgi.service.packageadmin.ExportedPackage; 46 import org.ungoverned.moduleloader.*; 47 import org.ungoverned.moduleloader.search.ImportSearchPolicy; 48 import org.ungoverned.moduleloader.search.ValidationException; 49 import org.ungoverned.moduleloader.search.ValidationListener; 50 import org.ungoverned.oscar.util.*; 51 52 public class Oscar 53 { 54 private static PrintStream m_debugOut = null; 56 private static PrintStream m_errorOut = null; 58 private static String m_outputLockObj = new String ("output lock"); 60 61 84 private String m_adminLock = new String ("admin lock"); 86 private String m_quickLock = new String ("quick lock"); 88 89 private ModuleManager m_mgr = null; 91 92 private transient Map m_configPropMap = null; 94 private transient long m_nextId = 1L; 96 private transient long m_nextServiceId = 1L; 98 private transient int m_activeStartLevel = 100 OscarConstants.FRAMEWORK_INACTIVE_STARTLEVEL; 101 102 private transient OscarDispatchQueue m_dispatchQueue = null; 104 private Dispatcher m_frameworkDispatcher = null; 106 private Dispatcher m_bundleDispatcher = null; 107 private Dispatcher m_serviceDispatcher = null; 108 109 private transient HashMap m_installedBundleMap = null; 111 private transient BundleImpl[] m_uninstalledBundles = null; 113 114 private transient BundleCache m_cache = null; 116 private transient Map m_frameworkPropMap = null; 118 119 private static AdminPermission m_adminPerm = new AdminPermission(); 122 123 private StartStopPrivileged m_startStopPrivileged = new StartStopPrivileged(this); 126 127 public static final int UNKNOWN_STATUS = -1; 129 public static final int RUNNING_STATUS = 0; 130 public static final int STARTING_STATUS = 1; 131 public static final int STOPPING_STATUS = 2; 132 private transient int m_oscarStatus = UNKNOWN_STATUS; 133 134 139 static 140 { 141 initializeSystemProperties(); 142 } 143 144 160 public Oscar() 161 { 162 this(null, null); 163 } 164 165 262 public Oscar(Properties props) 263 { 264 this(props, null); 265 } 266 267 285 public Oscar(List activatorList) 286 { 287 this(null, activatorList); 288 } 289 290 313 public Oscar(Properties props, List activatorList) 314 { 315 if (props != null) 317 { 318 m_configPropMap = new HashMap(); 319 for (Enumeration e = props.propertyNames(); e.hasMoreElements(); ) 320 { 321 String name = (String ) e.nextElement(); 322 m_configPropMap.put(name, props.getProperty(name)); 323 } 324 } 325 326 String className = getConfigProperty(OscarConstants.CACHE_CLASS_PROP); 329 if (className == null) 330 { 331 className = DefaultBundleCache.class.getName(); 332 } 333 334 try 335 { 336 Class clazz = Class.forName(className); 337 m_cache = (BundleCache) clazz.newInstance(); 338 m_cache.initialize(this); 339 } 340 catch (Exception ex) 341 { 342 System.err.println("Error creating bundle cache:"); 343 ex.printStackTrace(); 344 System.err.println("\nThis may result from the fact that Oscar 1.0 uses a"); 345 System.err.println("different bundle cache format than previous versions"); 346 System.err.println("of Oscar. Please read the bundle cache documentation for"); 347 System.err.println("more details: http://oscar.objectweb.org/cache.html."); 348 349 String embedded = getConfigProperty( 351 OscarConstants.EMBEDDED_EXECUTION_PROP); 352 boolean isEmbedded = (embedded == null) 353 ? false : embedded.equals("true"); 354 if (!isEmbedded) 355 { 356 System.exit(-1); 357 } 358 else 359 { 360 throw new RuntimeException (ex.toString()); 361 } 362 } 363 364 initialize(activatorList); 366 } 367 368 392 public String getConfigProperty(String name) 393 { 394 if (m_configPropMap != null) 395 { 396 return (String ) m_configPropMap.get(name); 397 } 398 return System.getProperty(name); 399 } 400 401 private void setConfigProperty(String name, String value) 402 { 403 if (m_configPropMap != null) 407 { 408 m_configPropMap.put(name, value); 409 } 410 else 411 { 412 System.setProperty(name, value); 413 } 414 } 415 416 419 private void initialize(List activatorList) 420 { 421 ImportSearchPolicy searchPolicy = new OSGiImportSearchPolicy(this); 422 m_mgr = new ModuleManager(searchPolicy, new OSGiURLPolicy(this)); 423 424 m_mgr.addModuleListener((OSGiSelectionPolicy) searchPolicy.getSelectionPolicy()); 428 429 searchPolicy.addValidationListener(new ValidationListener() { 433 public void moduleValidated(ModuleEvent event) 434 { 435 synchronized (m_quickLock) 436 { 437 try 438 { 439 long id = BundleInfo.getBundleIdFromModuleId( 440 event.getModule().getId()); 441 if (id >= 0) 442 { 443 BundleImpl bundle = (BundleImpl) getBundle(id); 449 if (bundle.getInfo().getCurrentModule() == event.getModule()) 450 { 451 bundle.getInfo().setState(Bundle.RESOLVED); 452 } 453 } 454 } 455 catch (NumberFormatException ex) 456 { 457 } 459 } 460 } 461 462 public void moduleInvalidated(ModuleEvent event) 463 { 464 } 471 }); 472 473 m_oscarStatus = STARTING_STATUS; 475 476 m_errorOut = System.err; 478 479 m_dispatchQueue = new OscarDispatchQueue(); 481 m_installedBundleMap = new HashMap(); 482 483 m_frameworkPropMap = new CaseInsensitiveMap(); 485 initializeOsgiProperties(); 486 initializeBundleProperties(); 487 488 SystemBundle systembundle = null; 492 try 493 { 494 BundleInfo info = new BundleInfo( 496 new SystemBundleArchive(), null); 497 systembundle = new SystemBundle(this, info, activatorList); 498 systembundle.getInfo().addModule( 499 m_mgr.addModule( 500 "0", systembundle.getAttributes(), 501 systembundle.getResourceSources(), 502 systembundle.getLibrarySources())); 503 m_installedBundleMap.put( 504 systembundle.getInfo().getLocation(), systembundle); 505 506 try 509 { 510 searchPolicy.validate(systembundle.getInfo().getCurrentModule()); 511 } 512 catch (ValidationException ex) 513 { 514 int[] v = (int[]) ex.getVersion(); 516 throw new BundleException("Unresolved package: " 517 + ex.getIdentifier() + "; specification-version=\"" 518 + v[0] + "." + v[1] + "." + v[2] + "\""); 519 } 520 521 systembundle.start(); 525 } 526 catch (Exception ex) 527 { 528 m_mgr = null; 529 Oscar.error("Unable to start system bundle: " + ex); 530 throw new RuntimeException ("Unable to start system bundle."); 531 } 532 533 BundleArchive[] archives = null; 535 536 try 538 { 539 archives = m_cache.getArchives(); 540 } 541 catch (Exception ex) 542 { 543 Oscar.error("Oscar: Unable to list saved bundles: " + ex); 544 archives = null; 545 } 546 547 BundleImpl bundle = null; 548 549 for (int i = 0; (archives != null) && (i < archives.length); i++) 551 { 552 m_nextId = Math.max(m_nextId, archives[i].getId() + 1); 556 557 try 558 { 559 if (archives[i].getPersistentState() == Bundle.UNINSTALLED) 565 { 566 m_cache.remove(archives[i]); 567 } 568 else 570 { 571 bundle = (BundleImpl) installBundle( 573 archives[i].getId(), archives[i].getLocation(), null); 574 } 575 } 576 catch (BundleException ex) 577 { 578 fireFrameworkEvent(FrameworkEvent.ERROR, bundle, ex); 579 try 580 { 581 Oscar.error("Oscar: Unable to re-install " 582 + archives[i].getLocation()); 583 } 584 catch (Exception ex2) 585 { 586 Oscar.error("Oscar: Unable to re-install bundle " 587 + archives[i].getId()); 588 } 589 Oscar.error("Oscar: " + ex); 590 } 592 catch (Exception ex) 593 { 594 fireFrameworkEvent(FrameworkEvent.ERROR, bundle, ex); 595 try 596 { 597 Oscar.error("Oscar: Exception while re-installing " 598 + archives[i].getLocation()); 599 } 600 catch (Exception ex2) 601 { 602 Oscar.error("Oscar: Exception while re-installing bundle " 603 + archives[i].getId()); 604 } 605 } 607 } 608 609 int startLevel = OscarConstants.FRAMEWORK_DEFAULT_STARTLEVEL; 611 String s = getConfigProperty(OscarConstants.FRAMEWORK_STARTLEVEL_PROP); 612 if (s != null) 613 { 614 try 615 { 616 startLevel = Integer.parseInt(s); 617 } 618 catch (NumberFormatException ex) 619 { 620 startLevel = OscarConstants.FRAMEWORK_DEFAULT_STARTLEVEL; 621 } 622 } 623 624 processAutoProperties(); 626 627 setStartLevelInternal(startLevel); 629 630 m_oscarStatus = RUNNING_STATUS; 632 633 systembundle.getInfo().setState(Bundle.ACTIVE); 635 636 fireBundleEvent(BundleEvent.STARTED, systembundle); 638 639 fireFrameworkEvent(FrameworkEvent.STARTED, getBundle(0), null); 641 } 642 643 647 public void shutdown() 648 { 649 if (System.getSecurityManager() != null) 650 { 651 AccessController.checkPermission(m_adminPerm); 652 } 653 654 synchronized (m_adminLock) 656 { 657 if (m_oscarStatus != RUNNING_STATUS) 659 { 660 return; 661 } 662 663 m_oscarStatus = STOPPING_STATUS; 665 } 666 667 setStartLevelInternal(0); 670 671 try { 675 getBundle(0).stop(); 676 } catch (Exception ex) { 677 fireFrameworkEvent(FrameworkEvent.ERROR, getBundle(0), ex); 678 Oscar.error("Error stopping system bundle.", ex); 679 } 680 681 Bundle[] bundles = getBundles(); 683 for (int i = 0; i < bundles.length; i++) 684 { 685 BundleImpl bundle = (BundleImpl) bundles[i]; 686 if (bundle.getInfo().isRemovalPending()) 687 { 688 try 689 { 690 purgeBundle(bundle); 691 } 692 catch (Exception ex) 693 { 694 fireFrameworkEvent(FrameworkEvent.ERROR, bundle, ex); 695 Oscar.error("Oscar: Unable to purge bundle " 696 + bundle.getInfo().getLocation()); 697 } 698 } 699 } 700 701 for (int i = 0; 703 (m_uninstalledBundles != null) && (i < m_uninstalledBundles.length); 704 i++) 705 { 706 try 707 { 708 removeBundle(m_uninstalledBundles[i]); 709 } 710 catch (Exception ex) 711 { 712 Oscar.error("Oscar: Unable to remove " 713 + m_uninstalledBundles[i].getInfo().getLocation()); 714 } 715 } 716 717 DispatchQueue.shutdown(); 719 720 m_oscarStatus = UNKNOWN_STATUS; 722 } 723 724 729 protected int getStartLevel() 730 { 731 return m_activeStartLevel; 732 } 733 734 743 protected void setStartLevelInternal(int requestedLevel) 744 { 745 boolean lowering = (requestedLevel < m_activeStartLevel); 748 749 m_activeStartLevel = requestedLevel; 751 752 synchronized (m_adminLock) 755 { 756 Bundle[] bundles = getBundles(); 758 759 Comparator comparator = null; 763 if (lowering) 764 { 765 comparator = new Comparator() { 767 public int compare(Object o1, Object o2) 768 { 769 BundleImpl b1 = (BundleImpl) o1; 770 BundleImpl b2 = (BundleImpl) o2; 771 if (b1.getInfo().getStartLevel(getInitialBundleStartLevel()) 772 < b2.getInfo().getStartLevel(getInitialBundleStartLevel())) 773 { 774 return 1; 775 } 776 else if (b1.getInfo().getStartLevel(getInitialBundleStartLevel()) 777 > b2.getInfo().getStartLevel(getInitialBundleStartLevel())) 778 { 779 return -1; 780 } 781 return 0; 782 } 783 }; 784 } 785 else 786 { 787 comparator = new Comparator() { 789 public int compare(Object o1, Object o2) 790 { 791 BundleImpl b1 = (BundleImpl) o1; 792 BundleImpl b2 = (BundleImpl) o2; 793 if (b1.getInfo().getStartLevel(getInitialBundleStartLevel()) 794 > b2.getInfo().getStartLevel(getInitialBundleStartLevel())) 795 { 796 return 1; 797 } 798 else if (b1.getInfo().getStartLevel(getInitialBundleStartLevel()) 799 < b2.getInfo().getStartLevel(getInitialBundleStartLevel())) 800 { 801 return -1; 802 } 803 return 0; 804 } 805 }; 806 } 807 808 Arrays.sort(bundles, comparator); 809 810 for (int i = 0; (bundles != null) && (i < bundles.length); i++) 812 { 813 BundleImpl impl = (BundleImpl) bundles[i]; 814 815 if (impl.getInfo().getBundleId() == 0) 819 { 820 continue; 821 } 822 823 if (impl.getInfo().getStartLevel(getInitialBundleStartLevel()) 825 <= m_activeStartLevel) 826 { 827 try 828 { 829 startBundleWithStartLevel(impl); 830 } 831 catch (Throwable th) 832 { 833 th.printStackTrace(); 834 fireFrameworkEvent(FrameworkEvent.ERROR, impl, th); 835 Oscar.error("Oscar: Error starting " 836 + impl.getInfo().getLocation()); 837 } 838 } 839 else if (impl.getInfo().getStartLevel(getInitialBundleStartLevel()) 840 > m_activeStartLevel) 841 { 842 try 843 { 844 stopBundleWithStartLevel(impl); 845 } 846 catch (Throwable th) 847 { 848 fireFrameworkEvent(FrameworkEvent.ERROR, impl, th); 849 Oscar.error("Oscar: Error stopping " 850 + impl.getInfo().getLocation()); 851 } 852 } 853 } 854 } 855 856 fireFrameworkEvent(FrameworkEvent.STARTLEVEL_CHANGED, getBundle(0), null); 857 } 858 859 865 protected int getInitialBundleStartLevel() 866 { 867 String s = getConfigProperty(OscarConstants.BUNDLE_STARTLEVEL_PROP); 868 869 if (s != null) 870 { 871 try 872 { 873 return Integer.parseInt(s); 874 } 875 catch (NumberFormatException ex) 876 { 877 } 879 } 880 return OscarConstants.BUNDLE_DEFAULT_STARTLEVEL; 881 } 882 883 894 protected void setInitialBundleStartLevel(int startLevel) 895 { 896 if (System.getSecurityManager() != null) 897 { 898 AccessController.checkPermission(m_adminPerm); 899 } 900 901 if (startLevel <= 0) 902 { 903 throw new IllegalArgumentException ( 904 "Initial start level must be greater than zero."); 905 } 906 907 setConfigProperty( 908 OscarConstants.BUNDLE_STARTLEVEL_PROP, 909 Integer.toString(startLevel)); 910 } 911 912 920 protected int getBundleStartLevel(Bundle bundle) 921 { 922 if (bundle.getState() == Bundle.UNINSTALLED) 923 { 924 throw new IllegalArgumentException ("Bundle is uninstalled."); 925 } 926 927 return ((BundleImpl) bundle).getInfo().getStartLevel(getInitialBundleStartLevel()); 928 } 929 930 941 protected void setBundleStartLevel(Bundle bundle, int startLevel) 942 { 943 if (System.getSecurityManager() != null) 944 { 945 AccessController.checkPermission(m_adminPerm); 946 } 947 948 if (bundle.getBundleId() == 0) 950 { 951 throw new IllegalArgumentException ( 952 "Cannot change system bundle start level."); 953 } 954 else if (bundle.getState() == Bundle.UNINSTALLED) 955 { 956 throw new IllegalArgumentException ("Bundle is uninstalled."); 957 } 958 959 if (startLevel >= 1) 960 { 961 BundleImpl impl = (BundleImpl) bundle; 962 impl.getInfo().setStartLevel(startLevel); 963 try 964 { 965 m_cache.getArchive(impl.getBundleId()).setStartLevel(startLevel); 966 } 967 catch (Exception ex) 968 { 969 Oscar.error("Unable to save start level.", ex); 970 } 971 972 try 973 { 974 if (impl.getInfo().getStartLevel(getInitialBundleStartLevel()) 976 <= getStartLevel()) 977 { 978 startBundleWithStartLevel(impl); 979 } 980 else 981 { 982 stopBundleWithStartLevel(impl); 983 } 984 } 985 catch (Throwable th) 986 { 987 fireFrameworkEvent(FrameworkEvent.ERROR, impl, th); 988 Oscar.error("Error starting/stopping bundle.", th); 989 } 990 } 991 } 992 993 1002 protected boolean isBundlePersistentlyStarted(Bundle bundle) 1003 { 1004 if (bundle.getState() == Bundle.UNINSTALLED) 1005 { 1006 throw new IllegalArgumentException ("Bundle is uninstalled."); 1007 } 1008 1009 return 1010 (((BundleImpl) bundle).getInfo().getPersistentState() 1011 == Bundle.ACTIVE); 1012 } 1013 1014 1018 1031 public int getFrameworkStatus() 1032 { 1033 return m_oscarStatus; 1034 } 1035 1036 1044 public boolean isStrictOSGi() 1045 { 1046 String strict = getConfigProperty(OscarConstants.STRICT_OSGI_PROP); 1047 return (strict == null) ? true : strict.equals("true"); 1048 } 1049 1050 1054 1062 private BundleImpl findExportingBundle(String pkgName) 1063 { 1064 BundleImpl bundle = null; 1067 ImportSearchPolicy search = 1068 (ImportSearchPolicy) m_mgr.getSearchPolicy(); 1069 int[] version = { 0, 0, 0 }; 1070 Module exporter = search.resolveImportTarget(pkgName, version); 1071 if (exporter != null) 1072 { 1073 bundle = (BundleImpl) getBundle( 1074 BundleInfo.getBundleIdFromModuleId(exporter.getId())); 1075 } 1076 return bundle; 1077 } 1078 1079 1087 protected ExportedPackage getExportedPackage(String name) 1088 { 1089 BundleImpl bundle = findExportingBundle(name); 1090 if (bundle != null) 1091 { 1092 Module[] modules = bundle.getInfo().getModules(); 1103 for (int modIdx = 0; modIdx < modules.length; modIdx++) 1104 { 1105 Object version = 1106 ImportSearchPolicy.getExportVersion(modules[modIdx], name); 1107 if (version != null) 1108 { 1109 return new ExportedPackageImpl( 1110 this, bundle, name, (int[]) version); 1111 } 1112 } 1113 } 1114 1115 return null; 1116 } 1117 1118 1128 protected ExportedPackage[] getExportedPackages(Bundle b) 1129 { 1130 ExportedPackage[] pkgs = null; 1131 List list = new ArrayList(); 1132 1133 if (b != null) 1136 { 1137 BundleImpl bundle = (BundleImpl) b; 1138 getExportedPackages(bundle, list); 1139 } 1140 else 1142 { 1143 for (int bundleIdx = 0; 1145 (m_uninstalledBundles != null) && (bundleIdx < m_uninstalledBundles.length); 1146 bundleIdx++) 1147 { 1148 BundleImpl bundle = m_uninstalledBundles[bundleIdx]; 1149 getExportedPackages(bundle, list); 1150 } 1151 1152 Bundle[] bundles = getBundles(); 1154 for (int bundleIdx = 0; bundleIdx < bundles.length; bundleIdx++) 1155 { 1156 BundleImpl bundle = (BundleImpl) bundles[bundleIdx]; 1157 getExportedPackages(bundle, list); 1158 } 1159 } 1160 1161 return (ExportedPackage[]) list.toArray(new ExportedPackage[list.size()]); 1162 } 1163 1164 1170 private void getExportedPackages(BundleImpl bundle, List list) 1171 { 1172 Module[] modules = bundle.getInfo().getModules(); 1176 for (int modIdx = 0; modIdx < modules.length; modIdx++) 1177 { 1178 Object [][] exports = 1179 ImportSearchPolicy.getExportsAttribute(modules[modIdx]); 1180 if (exports.length > 0) 1181 { 1182 for (int expIdx = 0; expIdx < exports.length; expIdx++) 1183 { 1184 if (exports[expIdx][ImportSearchPolicy.RESOLVING_MODULE_IDX] 1188 == modules[modIdx]) 1189 { 1190 list.add(new ExportedPackageImpl( 1191 this, bundle, 1192 (String ) exports[expIdx][ImportSearchPolicy.IDENTIFIER_IDX], 1193 (int[]) exports[expIdx][ImportSearchPolicy.VERSION_IDX])); 1194 } 1195 } 1196 } 1197 } 1198 } 1199 1200 protected Bundle[] getImportingBundles(ExportedPackage ep) 1201 { 1202 BundleImpl exporter = (BundleImpl) 1206 ((ExportedPackageImpl) ep).getExportingBundleInternal(); 1207 BundleInfo exporterInfo = exporter.getInfo(); 1208 String exportName = ep.getName(); 1209 1210 List list = new ArrayList(); 1212 Bundle[] bundles = getBundles(); 1213 1214 for (int bundleIdx = 0; bundleIdx < bundles.length; bundleIdx++) 1216 { 1217 BundleImpl bundle = (BundleImpl) bundles[bundleIdx]; 1218 1219 boolean doesImport = false; 1222 1223 String [] searchAttrs = { 1225 ImportSearchPolicy.IMPORTS_ATTR, 1226 ImportSearchPolicy.EXPORTS_ATTR 1227 }; 1228 1229 for (int attrIdx = 0; 1230 (!doesImport) && (attrIdx < searchAttrs.length); 1231 attrIdx++) 1232 { 1233 Module[] modules = bundle.getInfo().getModules(); 1235 for (int modIdx = 0; 1236 (!doesImport) && (modIdx < modules.length); 1237 modIdx++) 1238 { 1239 Object [][] imports = 1240 ImportSearchPolicy.getImportsOrExports( 1241 modules[modIdx], searchAttrs[attrIdx]); 1242 for (int importIdx = 0; 1243 (!doesImport) && (importIdx < imports.length); 1244 importIdx++) 1245 { 1246 String importName = (String ) 1248 imports[importIdx][ImportSearchPolicy.IDENTIFIER_IDX]; 1249 Module resolvingModule = (Module) 1251 imports[importIdx][ImportSearchPolicy.RESOLVING_MODULE_IDX]; 1252 if (exportName.equals(importName) && 1256 exporterInfo.hasModule(resolvingModule)) 1257 { 1258 list.add(bundles[bundleIdx]); 1260 doesImport = true; 1262 } 1263 } 1264 } 1265 } 1266 } 1267 1268 1269 if (list.size() > 0) 1271 { 1272 return (Bundle[]) list.toArray(new Bundle[list.size()]); 1273 } 1274 1275 return null; 1276 } 1277 1278 protected void refreshPackages(Bundle[] targets) 1279 { 1280 if (System.getSecurityManager() != null) 1281 { 1282 AccessController.checkPermission(m_adminPerm); 1283 } 1284 1285 synchronized (m_adminLock) 1286 { 1287 if (targets == null) 1289 { 1290 ArrayList list = new ArrayList(); 1291 1292 for (int i = 0; 1294 (m_uninstalledBundles != null) && (i < m_uninstalledBundles.length); 1295 i++) 1296 { 1297 list.add(m_uninstalledBundles[i]); 1298 } 1299 m_uninstalledBundles = null; 1300 1301 Iterator iter = m_installedBundleMap.values().iterator(); 1303 while (iter.hasNext()) 1304 { 1305 BundleImpl bundle = (BundleImpl) iter.next(); 1306 if (bundle.getInfo().isRemovalPending()) 1307 { 1308 list.add(bundle); 1309 } 1310 } 1311 1312 if (list.size() > 0) 1314 { 1315 targets = (Bundle[]) list.toArray(new Bundle[list.size()]); 1316 } 1317 } 1318 1319 if (targets != null) 1322 { 1323 HashMap map = new HashMap(); 1326 for (int targetIdx = 0; targetIdx < targets.length; targetIdx++) 1327 { 1328 BundleImpl target = (BundleImpl) targets[targetIdx]; 1329 1330 map.put(new Long (target.getBundleId()), 1334 new RefreshHelper(target)); 1335 populateImportGraph(target, map); 1337 } 1338 1339 for (Iterator iter = map.values().iterator(); iter.hasNext(); ) 1343 { 1344 RefreshHelper helper = (RefreshHelper) iter.next(); 1345 helper.stop(); 1346 helper.purgeOrRemove(); 1347 helper.reinitialize(); 1348 } 1349 1350 for (Iterator iter = map.values().iterator(); iter.hasNext(); ) 1352 { 1353 RefreshHelper helper = (RefreshHelper) iter.next(); 1354 helper.restart(); 1355 } 1356 } 1357 } 1358 1359 fireFrameworkEvent(FrameworkEvent.PACKAGES_REFRESHED, getBundle(0), null); 1360 } 1361 1362 private void populateImportGraph(BundleImpl target, HashMap map) 1363 { 1364 ExportedPackage[] pkgs = getExportedPackages(target); 1366 1367 for (int pkgIdx = 0; (pkgs != null) && (pkgIdx < pkgs.length); pkgIdx++) 1368 { 1369 Bundle[] importers = getImportingBundles(pkgs[pkgIdx]); 1371 1372 for (int impIdx = 0; 1374 (importers != null) && (impIdx < importers.length); 1375 impIdx++) 1376 { 1377 Long id = new Long (importers[impIdx].getBundleId()); 1378 if (map.get(id) == null) 1379 { 1380 map.put(id, new RefreshHelper(importers[impIdx])); 1381 populateImportGraph( 1383 (BundleImpl) importers[impIdx], map); 1384 } 1385 } 1386 } 1387 } 1388 1389 1405 protected boolean addImport( 1406 Module module, Object target, Object version, boolean force) 1407 { 1408 synchronized (m_mgr) 1414 { 1415 ImportSearchPolicy search = (ImportSearchPolicy) m_mgr.getSearchPolicy(); 1416 boolean added = false; 1417 Module exporter = null; 1418 1419 boolean valid = ImportSearchPolicy.getValidAttribute(module).booleanValue(); 1421 1422 if (valid) 1425 { 1426 exporter = search.resolveImportTarget(target, version); 1427 } 1428 1429 if (force 1437 || !valid 1438 || (valid && (exporter != null))) 1439 { 1440 Object [][] imports = ImportSearchPolicy.getImportsAttribute(module); 1443 Object [][] newImports = new Object [imports.length + 1][3]; 1444 for (int i = 0; i < imports.length; i++) 1445 { 1446 newImports[i] = imports[i]; 1447 } 1448 1449 newImports[newImports.length - 1] = new Object [] { target, version, exporter }; 1451 module.setAttribute(ImportSearchPolicy.IMPORTS_ATTR, newImports); 1452 added = true; 1453 1454 if ((exporter == null) && valid) 1458 { 1459 search.invalidate( 1460 module, module.getAttributes(), module.getResourceSources(), 1461 module.getLibrarySources()); 1462 } 1463 } 1464 1465 return added; 1466 } 1467 } 1468 1469 1473 1476 protected long getBundleId(BundleImpl bundle) 1477 { 1478 return bundle.getInfo().getBundleId(); 1479 } 1480 1481 1484 protected Dictionary getBundleHeaders(BundleImpl bundle) 1485 { 1486 if (System.getSecurityManager() != null) 1487 { 1488 AccessController.checkPermission(m_adminPerm); 1489 } 1490 return new MapToDictionary(bundle.getInfo().getCurrentHeader()); 1491 } 1492 1493 1496 protected String getBundleLocation(BundleImpl bundle) 1497 { 1498 if (System.getSecurityManager() != null) 1499 { 1500 AccessController.checkPermission(m_adminPerm); 1501 } 1502 return bundle.getInfo().getLocation(); 1503 } 1504 1505 1508 protected URL getBundleResource(BundleImpl bundle, String name) 1509 { 1510 if (bundle.getInfo().getState() == Bundle.UNINSTALLED) 1511 { 1512 throw new IllegalStateException ("The bundle is uninstalled."); 1513 } 1514 else if (System.getSecurityManager() != null) 1515 { 1516 AccessController.checkPermission(m_adminPerm); 1517 } 1518 return bundle.getInfo().getCurrentModule().getClassLoader().getResource(name); 1519 } 1534 1535 1538 protected ServiceReference[] getBundleRegisteredServices(BundleImpl bundle) 1539 { 1540 if (bundle.getInfo().getState() == Bundle.UNINSTALLED) 1541 { 1542 throw new IllegalStateException ("The bundle is uninstalled."); 1543 } 1544 1545 synchronized (m_quickLock) 1546 { 1547 BundleInfo info = bundle.getInfo(); 1548 1549 if (info.getServiceRegistrationCount() > 0) 1550 { 1551 ArrayList list = new ArrayList(); 1553 for (int regIdx = 0; regIdx < info.getServiceRegistrationCount(); regIdx++) 1554 { 1555 ServiceRegistrationImpl reg = (ServiceRegistrationImpl) 1557 info.getServiceRegistration(regIdx); 1558 1559 boolean hasPermission = false; 1564 if (System.getSecurityManager() != null) 1565 { 1566 String [] objectClass = (String []) 1567 reg.getProperty(Constants.OBJECTCLASS); 1568 if (objectClass == null) 1569 { 1570 return null; 1571 } 1572 for (int ifcIdx = 0; 1573 !hasPermission && (ifcIdx < objectClass.length); 1574 ifcIdx++) 1575 { 1576 try 1577 { 1578 ServicePermission perm = 1579 new ServicePermission( 1580 objectClass[ifcIdx], ServicePermission.GET); 1581 AccessController.checkPermission(perm); 1582 hasPermission = true; 1583 } 1584 catch (Exception ex) 1585 { 1586 } 1587 } 1588 } 1589 else 1590 { 1591 hasPermission = true; 1592 } 1593 1594 if (hasPermission) 1595 { 1596 list.add(reg.getReference()); 1597 } 1598 } 1599 1600 if (list.size() > 0) 1601 { 1602 return (ServiceReference[]) 1603 list.toArray(new ServiceReference[list.size()]); 1604 } 1605 } 1606 } 1607 1608 return null; 1609 } 1610 1611 1614 protected ServiceReference[] getBundleServicesInUse(BundleImpl bundle) 1615 { 1616 synchronized (m_quickLock) 1617 { 1618 BundleInfo info = bundle.getInfo(); 1619 Iterator iter = info.getServiceUsageCounters(); 1620 if (iter.hasNext()) 1621 { 1622 ArrayList list = new ArrayList(); 1624 while (iter.hasNext()) 1625 { 1626 ServiceReference ref = (ServiceReference) iter.next(); 1628 1629 boolean hasPermission = false; 1634 if (System.getSecurityManager() != null) 1635 { 1636 String [] objectClass = (String []) 1637 ref.getProperty(Constants.OBJECTCLASS); 1638 if (objectClass == null) 1639 { 1640 return null; 1641 } 1642 for (int i = 0; 1643 !hasPermission && (i < objectClass.length); 1644 i++) 1645 { 1646 try 1647 { 1648 ServicePermission perm = 1649 new ServicePermission( 1650 objectClass[i], ServicePermission.GET); 1651 AccessController.checkPermission(perm); 1652 hasPermission = true; 1653 } 1654 catch (Exception ex) 1655 { 1656 } 1657 } 1658 } 1659 else 1660 { 1661 hasPermission = true; 1662 } 1663 1664 if (hasPermission) 1665 { 1666 list.add(ref); 1667 } 1668 } 1669 1670 if (list.size() > 0) 1671 { 1672 return (ServiceReference[]) 1673 list.toArray(new ServiceReference[list.size()]); 1674 } 1675 } 1676 } 1677 1678 return null; 1679 } 1680 1681 1684 protected int getBundleState(BundleImpl bundle) 1685 { 1686 return bundle.getInfo().getState(); 1687 } 1688 1689 1692 protected boolean bundleHasPermission(BundleImpl bundle, Object obj) 1693 { 1694 if (bundle.getInfo().getState() == Bundle.UNINSTALLED) 1695 { 1696 throw new IllegalStateException ("The bundle is uninstalled."); 1697 } 1698 1699 return true; 1700 } 1701 1702 1705 protected void startBundle(BundleImpl bundle) 1706 throws BundleException 1707 { 1708 if (System.getSecurityManager() != null) 1709 { 1710 AccessController.checkPermission(m_adminPerm); 1711 } 1712 1713 synchronized (m_adminLock) 1714 { 1715 bundle.getInfo().setPersistentStateActive(); 1717 1718 try 1719 { 1720 startBundleWithStartLevel(bundle); 1722 } 1723 catch (Throwable th) 1724 { 1725 bundle.getInfo().setPersistentStateInactive(); 1728 1729th.printStackTrace(); 1730 1731 if (th instanceof BundleException) 1735 { 1736 throw (BundleException) th; 1737 } 1738 else if (th instanceof IllegalStateException ) 1739 { 1740 throw (IllegalStateException ) th; 1741 } 1742 else if (th instanceof SecurityException ) 1743 { 1744 throw (SecurityException ) th; 1745 } 1746 else if (th instanceof PrivilegedActionException) 1749 { 1750 th = ((PrivilegedActionException) th).getException(); 1751 } 1752 1753 throw new BundleException("Activator start error.", th); 1755 } 1756 1757 try 1759 { 1760 m_cache.getArchive(bundle.getInfo().getBundleId()) 1761 .setPersistentState(bundle.getInfo().getPersistentState()); 1762 } 1763 catch (Exception ex) 1764 { 1765 Oscar.error("Oscar: Error saving persistent bundle state."); 1766 Oscar.error("Oscar: " + ex); 1767 } 1768 } 1769 } 1770 1771 1782 private void startBundleWithStartLevel(BundleImpl bundle) 1783 throws Throwable 1784 { 1785 BundleInfo info = bundle.getInfo(); 1786 1787 if ((info.getPersistentState() != Bundle.ACTIVE) 1790 || (info.getStartLevel(getInitialBundleStartLevel()) > getStartLevel())) 1791 { 1792 return; 1793 } 1794 1795 switch (info.getState()) 1796 { 1797 case Bundle.UNINSTALLED: 1798 throw new IllegalStateException ("Cannot start an uninstalled bundle."); 1799 case Bundle.STARTING: 1800 case Bundle.STOPPING: 1801 throw new BundleException("Starting a bundle that is starting or stopping is currently not supported."); 1802 case Bundle.ACTIVE: 1803 return; 1804 case Bundle.INSTALLED: 1805 resolveBundle(bundle); 1806 case Bundle.RESOLVED: 1807 info.setState(Bundle.STARTING); 1808 } 1809 1810 try 1811 { 1812 bundle.getInfo().setActivator(createBundleActivator(bundle.getInfo())); 1814 1815 if (bundle.getInfo().getActivator() != null) 1817 { 1818 if (info.getContext() == null) 1819 { 1820 info.setContext(new BundleContextImpl(this, bundle)); 1821 } 1822 1823 if (System.getSecurityManager() != null) 1824 { 1825 m_startStopPrivileged.setAction(StartStopPrivileged.START_ACTION); 1826 m_startStopPrivileged.setBundle(bundle); 1827 AccessController.doPrivileged(m_startStopPrivileged); 1828 } 1829 else 1830 { 1831 info.getActivator().start(info.getContext()); 1832 } 1833 } 1834 } 1835 catch (Throwable th) 1836 { 1837 info.setState(Bundle.RESOLVED); 1840 1841 unregisterServices(bundle); 1843 1844 ungetServices(bundle); 1846 1847 removeListeners(bundle); 1849 1850 throw th; 1851 } 1852 1853 info.setState(Bundle.ACTIVE); 1854 1855 fireBundleEvent(BundleEvent.STARTED, bundle); 1856 } 1857 1858 1861 protected void updateBundle(BundleImpl bundle, InputStream is) 1862 throws BundleException 1863 { 1864 if (System.getSecurityManager() != null) 1865 { 1866 AccessController.checkPermission(m_adminPerm); 1867 } 1868 1869 1872 try 1873 { 1874 boolean activated = false; 1876 Exception rethrow = null; 1877 1878 synchronized (m_adminLock) 1879 { 1880 BundleInfo info = bundle.getInfo(); 1881 1882 if (info.getState() == Bundle.UNINSTALLED) 1883 { 1884 throw new IllegalStateException ("The bundle is uninstalled."); 1885 } 1886 1887 String updateLocation = (String ) 1889 info.getCurrentHeader().get(Constants.BUNDLE_UPDATELOCATION); 1890 1891 if (updateLocation == null) 1893 { 1894 updateLocation = info.getLocation(); 1895 } 1896 1897 activated = (info.getState() == Bundle.ACTIVE); 1899 1900 if (activated) 1902 { 1903 stopBundle(bundle); 1904 } 1905 1906 try 1907 { 1908 if (is == null) 1910 { 1911 URL url = new URL(updateLocation); 1914 URLConnection conn = url.openConnection(); 1915 1916 String auth = System.getProperty("http.proxyAuth"); 1918 if ((auth != null) && (auth.length() > 0)) 1919 { 1920 if ("http".equals(url.getProtocol()) || 1921 "https".equals(url.getProtocol())) 1922 { 1923 String base64 = Util.base64Encode(auth); 1924 conn.setRequestProperty( 1925 "Proxy-Authorization", "Basic " + base64); 1926 } 1927 } 1928 is = conn.getInputStream(); 1929 } 1930 BundleArchive archive = m_cache.getArchive(info.getBundleId()); 1932 m_cache.update(archive, is); 1935 Module module = createModule( 1939 info.getBundleId(), 1940 archive.getRevisionCount() - 1, 1941 info.getCurrentHeader()); 1942 info.addModule(module); 1944 } 1945 catch (Exception ex) 1946 { 1947 Oscar.error("Unable to update the bundle."); 1948 rethrow = ex; 1949 } 1950 finally 1951 { 1952 try 1953 { 1954 if (is != null) is.close(); 1955 } 1956 catch (Exception ex) 1957 { 1958 Oscar.error("Unable to close input stream: " + ex); 1959 } 1960 } 1961 1962 info.setState(Bundle.INSTALLED); 1963 1964 info.setRemovalPending(); 1966 } 1967 1968 if (rethrow == null) 1970 { 1971 fireBundleEvent(BundleEvent.UPDATED, bundle); 1974 } 1975 1976 if (activated) 1978 { 1979 startBundle(bundle); 1981 } 1982 1983 if (rethrow != null) 1985 { 1986 throw new BundleException("Update failed.", rethrow); 1987 } 1988 } 1989 finally 1990 { 1991 try 1992 { 1993 if (is != null) is.close(); 1994 } 1995 catch (IOException ex) 1996 { 1997 Oscar.error("Oscar: Could not close update stream."); 1998 } 2000 } 2001 } 2002 2003 2006 protected void stopBundle(BundleImpl bundle) 2007 throws BundleException 2008 { 2009 if (System.getSecurityManager() != null) 2010 { 2011 AccessController.checkPermission(m_adminPerm); 2012 } 2013 2014 Throwable rethrow = null; 2015 2016 synchronized (m_adminLock) 2017 { 2018 bundle.getInfo().setPersistentStateInactive(); 2020 2021 try 2022 { 2023 stopBundleWithStartLevel(bundle); 2025 } 2026 catch (Throwable th) 2027 { 2028 Oscar.error("Oscar: Error calling activator.", th); 2029 rethrow = th; 2030 } 2031 2032 try 2034 { 2035 m_cache.getArchive(bundle.getInfo().getBundleId()) 2036 .setPersistentState(bundle.getInfo().getPersistentState()); 2037 } 2038 catch (Exception ex) 2039 { 2040 Oscar.error("Oscar: Error saving persistent bundle state."); 2041 Oscar.error("Oscar: " + ex); 2042 } 2043 } 2044 2045 if (rethrow != null) 2047 { 2048 if (rethrow instanceof BundleException) 2052 { 2053 throw (BundleException) rethrow; 2054 } 2055 else if (rethrow instanceof IllegalStateException ) 2056 { 2057 throw (IllegalStateException ) rethrow; 2058 } 2059 else if (rethrow instanceof SecurityException ) 2060 { 2061 throw (SecurityException ) rethrow; 2062 } 2063 else if (rethrow instanceof PrivilegedActionException) 2064 { 2065 rethrow = ((PrivilegedActionException) rethrow).getException(); 2066 } 2067 2068 throw new BundleException("Activator stop error.", rethrow); 2070 } 2071 } 2072 2073 2082 private void stopBundleWithStartLevel(BundleImpl bundle) 2083 throws Throwable 2084 { 2085 BundleInfo info = bundle.getInfo(); 2086 2087 switch (info.getState()) 2088 { 2089 case Bundle.UNINSTALLED: 2090 throw new IllegalStateException ("Cannot stop an uninstalled bundle."); 2091 case Bundle.STARTING: 2092 case Bundle.STOPPING: 2093 throw new BundleException("Stopping a bundle that is starting or stopping is currently not supported."); 2094 case Bundle.INSTALLED: 2095 case Bundle.RESOLVED: 2096 return; 2097 case Bundle.ACTIVE: 2098 info.setState(Bundle.STOPPING); 2100 } 2101 2102 Throwable rethrow = null; 2103 2104 try 2105 { 2106 if (bundle.getInfo().getActivator() != null) 2107 { 2108 if (System.getSecurityManager() != null) 2109 { 2110 m_startStopPrivileged.setAction(StartStopPrivileged.STOP_ACTION); 2111 m_startStopPrivileged.setBundle(bundle); 2112 AccessController.doPrivileged(m_startStopPrivileged); 2113 } 2114 else 2115 { 2116 info.getActivator().stop(info.getContext()); 2117 } 2118 } 2119 2120 String strict = getConfigProperty(OscarConstants.STRICT_OSGI_PROP); 2124 boolean isStrict = (strict == null) ? true : strict.equals("true"); 2125 if (!isStrict) 2126 { 2127 try 2128 { 2129 m_cache.getArchive(info.getBundleId()) 2130 .setActivator(info.getActivator()); 2131 } 2132 catch (Exception ex) 2133 { 2134 } 2137 } 2138 } 2139 catch (Throwable th) 2140 { 2141 th.printStackTrace(); 2143 rethrow = th; 2144 } 2145 2146 2147 unregisterServices(bundle); 2149 2150 ungetServices(bundle); 2152 2153 removeListeners(bundle); 2156 2157 info.setState(Bundle.RESOLVED); 2158 fireBundleEvent(BundleEvent.STOPPED, bundle); 2159 2160 if (rethrow != null) 2161 { 2162 throw rethrow; 2163 } 2164 } 2165 2166 2169 protected void uninstallBundle(BundleImpl bundle) 2170 throws BundleException 2171 { 2172 if (System.getSecurityManager() != null) 2173 { 2174 AccessController.checkPermission(m_adminPerm); 2175 } 2176 2177 BundleException rethrow = null; 2178 2179 synchronized (m_adminLock) 2180 { 2181 BundleInfo info = bundle.getInfo(); 2182 if (info.getState() == Bundle.UNINSTALLED) 2183 { 2184 throw new IllegalStateException ("The bundle is uninstalled."); 2185 } 2186 2187 try 2191 { 2192 stopBundle(bundle); 2193 } 2194 catch (BundleException ex) 2195 { 2196 rethrow = ex; 2197 } 2198 2199 BundleImpl target = null; 2202 synchronized (m_quickLock) 2203 { 2204 target = (BundleImpl) m_installedBundleMap.remove( 2205 info.getLocation()); 2206 } 2207 2208 if (target != null) 2211 { 2212 target.getInfo().setPersistentStateUninstalled(); 2214 2215 target.getInfo().setRemovalPending(); 2217 2218 BundleImpl[] bundles = null; 2220 if (m_uninstalledBundles == null) 2221 { 2222 bundles = new BundleImpl[1]; 2223 } 2224 else 2225 { 2226 bundles = new BundleImpl[m_uninstalledBundles.length + 1]; 2227 System.arraycopy( 2228 m_uninstalledBundles, 0, bundles, 0, bundles.length - 1); 2229 } 2230 bundles[bundles.length - 1] = target; 2231 m_uninstalledBundles = bundles; 2232 } 2233 else 2234 { 2235 Oscar.error("Unable to remove bundle from installed map!"); 2236 } 2237 2238 info.setState(Bundle.UNINSTALLED); 2240 } 2241 2242 fireBundleEvent(BundleEvent.UNINSTALLED, bundle); 2244 2245 if (rethrow != null) 2246 { 2247 throw rethrow; 2248 } 2249 } 2250 2251 2255 2262 protected String getProperty(String key) 2263 { 2264 if (m_frameworkPropMap == null) 2266 { 2267 return null; 2268 } 2269 String val = (String ) m_frameworkPropMap.get(key); 2270 return substVars(val); 2271 } 2272 2273 2279 protected void setProperty(String key, String value) 2280 { 2281 if (m_frameworkPropMap != null) 2283 { 2284 m_frameworkPropMap.put(key, value); 2285 } 2286 } 2287 2288 2299 protected Bundle installBundle(String location, InputStream is) 2300 throws BundleException 2301 { 2302 return installBundle(-1, location, is); 2303 } 2304 2305 private Bundle installBundle(long id, String location, InputStream is) 2306 throws BundleException 2307 { 2308 if (System.getSecurityManager() != null) 2309 { 2310 AccessController.checkPermission(m_adminPerm); 2311 } 2312 2313 BundleImpl bundle = null; 2314 2315 synchronized (m_adminLock) 2316 { 2317 try 2318 { 2319 if ((getFrameworkStatus() == Oscar.STOPPING_STATUS) || 2321 (getFrameworkStatus() == Oscar.UNKNOWN_STATUS)) 2322 { 2323 throw new BundleException("The framework has been shutdown."); 2324 } 2325 2326 bundle = (BundleImpl) getBundle(location); 2329 if (bundle != null) 2330 { 2331 return bundle; 2332 } 2333 2334 boolean isNew = (id < 0); 2336 2337 if (isNew) 2339 { 2340 id = getNextId(); 2342 2343 try 2344 { 2345 if (is == null) 2347 { 2348 URL url = new URL(location); 2351 URLConnection conn = url.openConnection(); 2352 2353 String auth = System.getProperty("http.proxyAuth"); 2355 if ((auth != null) && (auth.length() > 0)) 2356 { 2357 if ("http".equals(url.getProtocol()) || 2358 "https".equals(url.getProtocol())) 2359 { 2360 String base64 = Util.base64Encode(auth); 2361 conn.setRequestProperty( 2362 "Proxy-Authorization", "Basic " + base64); 2363 } 2364 } 2365 is = conn.getInputStream(); 2366 } 2367 m_cache.create(id, location, is); 2369 } 2370 catch (Exception ex) 2371 { 2372 throw new BundleException("Unable to cache bundle: " + location, ex); 2373 } 2374 finally 2375 { 2376 try 2377 { 2378 if (is != null) is.close(); 2379 } 2380 catch (IOException ex) 2381 { 2382 Oscar.error("Unable to close input stream: " + ex); 2383 } 2384 } 2385 } 2386 else 2387 { 2388 try 2394 { 2395 if (m_cache.getArchive(id).getRevisionCount() > 1) 2396 { 2397 m_cache.purge(m_cache.getArchive(id)); 2398 } 2399 } 2400 catch (Exception ex) 2401 { 2402 ex.printStackTrace(); 2403 Oscar.error("Oscar: Could not purge bundle.", ex); 2404 } 2405 } 2406 2407 try 2408 { 2409 BundleArchive archive = m_cache.getArchive(id); 2410 bundle = new BundleImpl(this, createBundleInfo(archive)); 2411 } 2412 catch (Exception ex) 2413 { 2414 if (isNew) 2418 { 2419 try 2420 { 2421 m_cache.remove(m_cache.getArchive(id)); 2422 } 2423 catch (Exception ex1) 2424 { 2425 Oscar.error("Could not remove from cache.", ex1); 2426 } 2427 } 2428 throw new BundleException("Could not create bundle object: " + ex); 2429 } 2430 2431 if (isNew) 2435 { 2436 setBundleStartLevel(bundle, getInitialBundleStartLevel()); 2437 } 2438 2439 synchronized (m_quickLock) 2440 { 2441 m_installedBundleMap.put(location, bundle); 2442 } 2443 } 2444 finally 2445 { 2446 try 2448 { 2449 if (is != null) is.close(); 2450 } 2451 catch (IOException ex) 2452 { 2453 Oscar.error("Oscar: Unable to close input stream."); 2454 } 2456 } 2457 } 2458 2459 fireBundleEvent(BundleEvent.INSTALLED, bundle); 2461 2462 return bundle; 2464 } 2465 2466 2474 protected Bundle getBundle(long id) 2475 { 2476 synchronized (m_quickLock) 2477 { 2478 BundleImpl bundle = null; 2479 2480 for (Iterator i = m_installedBundleMap.values().iterator(); i.hasNext(); ) 2481 { 2482 bundle = (BundleImpl) i.next(); 2483 if (bundle.getInfo().getBundleId() == id) 2484 { 2485 return bundle; 2486 } 2487 } 2488 } 2489 2490 return null; 2491 } 2492 2493 private Comparator m_comparator = null; 2495 2496 2503 protected Bundle[] getBundles() 2504 { 2505 if (m_comparator == null) 2506 { 2507 m_comparator = new Comparator() { 2508 public int compare(Object o1, Object o2) 2509 { 2510 Bundle b1 = (Bundle) o1; 2511 Bundle b2 = (Bundle) o2; 2512 if (b1.getBundleId() > b2.getBundleId()) 2513 return 1; 2514 else if (b1.getBundleId() < b2.getBundleId()) 2515 return -1; 2516 return 0; 2517 } 2518 }; 2519 } 2520 2521 Bundle[] bundles = null; 2522 2523 synchronized (m_quickLock) 2524 { 2525 if (m_installedBundleMap.size() == 0) 2526 { 2527 return null; 2528 } 2529 2530 bundles = new Bundle[m_installedBundleMap.size()]; 2531 int counter = 0; 2532 for (Iterator i = m_installedBundleMap.values().iterator(); i.hasNext(); ) 2533 { 2534 bundles[counter++] = (Bundle) i.next(); 2535 } 2536 } 2537 2538 Arrays.sort(bundles, m_comparator); 2539 2540 return bundles; 2541 } 2542 2543 2551 protected void addBundleListener(Bundle bundle, BundleListener l) 2552 { 2553 BundleListenerWrapper old = (BundleListenerWrapper) 2556 m_dispatchQueue.getListener(BundleListener.class, l); 2557 if (old == null) 2558 { 2559 l = new BundleListenerWrapper(bundle, l); 2560 m_dispatchQueue.addListener(BundleListener.class, l); 2561 } 2562 } 2563 2564 2570 protected void removeBundleListener(BundleListener l) 2571 { 2572 m_dispatchQueue.removeListener(BundleListener.class, l); 2573 } 2574 2575 2584 protected void addServiceListener(Bundle bundle, ServiceListener l, String f) 2585 throws InvalidSyntaxException 2586 { 2587 ServiceListenerWrapper old = (ServiceListenerWrapper) 2590 m_dispatchQueue.getListener(ServiceListener.class, l); 2591 if (old != null) 2592 { 2593 old.setFilter((f == null) ? null : new FilterImpl(f)); 2594 } 2595 else 2596 { 2597 l = new ServiceListenerWrapper( 2598 bundle, l, (f == null) ? null : new FilterImpl(f)); 2599 m_dispatchQueue.addListener(ServiceListener.class, l); 2600 } 2601 } 2602 2603 2609 protected void removeServiceListener(ServiceListener l) 2610 { 2611 m_dispatchQueue.removeListener(ServiceListener.class, l); 2612 } 2613 2614 2622 protected void addFrameworkListener(Bundle bundle, FrameworkListener l) 2623 { 2624 FrameworkListenerWrapper old = (FrameworkListenerWrapper) 2627 m_dispatchQueue.getListener(FrameworkListener.class, l); 2628 if (old == null) 2629 { 2630 l = new FrameworkListenerWrapper(bundle, l); 2631 m_dispatchQueue.addListener(FrameworkListener.class, l); 2632 } 2633 } 2634 2635 2641 protected void removeFrameworkListener(FrameworkListener l) 2642 { 2643 m_dispatchQueue.removeListener(FrameworkListener.class, l); 2644 } 2645 2646 2657 protected ServiceRegistration registerService( 2658 BundleImpl bundle, String [] classNames, Object svcObj, Dictionary dict) 2659 { 2660 if (classNames == null) 2661 { 2662 throw new NullPointerException ("Service class names cannot be null."); 2663 } 2664 else if (svcObj == null) 2665 { 2666 throw new IllegalArgumentException ("Service object cannot be null."); 2667 } 2668 2669 if (System.getSecurityManager() != null) 2671 { 2672 for (int i = 0; i < classNames.length; i++) 2673 { 2674 ServicePermission perm = new ServicePermission( 2675 classNames[i], ServicePermission.REGISTER); 2676 AccessController.checkPermission(perm); 2677 } 2678 } 2679 2680 ServiceRegistrationImpl reg = null; 2681 2682 synchronized (m_quickLock) 2683 { 2684 BundleInfo info = bundle.getInfo(); 2685 2686 if ((info.getState() & (Bundle.STARTING | Bundle.ACTIVE)) == 0) 2688 { 2689 throw new IllegalStateException ( 2690 "Can only register services while bundle is active or activating."); 2691 } 2692 2693 if (!(svcObj instanceof ServiceFactory)) 2697 { 2698 String pkgName = null; 2699 Class clazz = null; 2700 ClassLoader loader = svcObj.getClass().getClassLoader(); 2702 loader = (loader == null) 2704 ? ClassLoader.getSystemClassLoader() : loader; 2705 for (int i = 0; i < classNames.length; i++) 2706 { 2707 try 2708 { 2709 clazz = loader.loadClass(classNames[i]); 2710 } 2711 catch (ClassNotFoundException ex) 2712 { 2713 throw new IllegalArgumentException ("Class not found: " + ex); 2714 } 2715 if (!clazz.isAssignableFrom(svcObj.getClass())) 2716 { 2717 throw new IllegalArgumentException ( 2718 "Service object is not an instance of \"" 2719 + classNames[i] + "\"."); 2720 } 2721 } 2722 } 2723 2724 reg = new ServiceRegistrationImpl( 2725 this, bundle, classNames, 2726 new Long (getNextServiceId()), svcObj, dict); 2727 2728 info.addServiceRegistration(reg); 2729 } 2730 2731 fireServiceEvent(ServiceEvent.REGISTERED, reg.getReference()); 2733 2734 return reg; 2735 } 2736 2737 2740 protected ServiceReference[] getServiceReferences(String className, String expr) 2741 throws InvalidSyntaxException 2742 { 2743 Oscar.debug("Oscar.getServiceReferences(" + className + ", " + expr + ")"); 2744 2745 if ((className != null) && (System.getSecurityManager() != null)) 2748 { 2749 try 2750 { 2751 ServicePermission perm = 2752 new ServicePermission(className, ServicePermission.GET); 2753 AccessController.checkPermission(perm); 2754 } 2755 catch (Exception ex) 2756 { 2757 Oscar.error(ex.getMessage()); 2761 return null; 2762 } 2763 } 2764 2765 Filter filter = null; 2767 if (expr != null) 2768 { 2769 filter = new FilterImpl(expr); 2770 } 2771 2772 synchronized (m_quickLock) 2773 { 2774 ArrayList list = new ArrayList(); 2776 for (Iterator i = m_installedBundleMap.values().iterator(); i.hasNext(); ) 2778 { 2779 BundleImpl bundle = (BundleImpl) i.next(); 2780 2781 if (bundle.getInfo().getState() != Bundle.STOPPING) 2783 { 2784 ServiceReference[] refs = getBundleRegisteredServices(bundle); 2786 for (int refIdx = 0; 2787 (refs != null) && (refIdx < refs.length); 2788 refIdx++) 2789 { 2790 boolean matched = false; 2793 2794 if ((className == null) && 2796 ((filter == null) || filter.match(refs[refIdx]))) 2797 { 2798 boolean hasPermission = true; 2799 if (System.getSecurityManager() != null) 2802 { 2803 String [] objectClass = (String []) 2804 refs[refIdx].getProperty(OscarConstants.OBJECTCLASS); 2805 for (int classIdx = 0; 2806 classIdx < objectClass.length; 2807 classIdx++) 2808 { 2809 try 2810 { 2811 ServicePermission perm = new ServicePermission( 2812 objectClass[classIdx], ServicePermission.GET); 2813 AccessController.checkPermission(perm); 2814 break; 2818 } 2819 catch (Exception ex) 2820 { 2821 Oscar.error(ex.getMessage()); 2825 hasPermission = false; 2826 break; 2827 } 2828 } 2829 } 2830 matched = hasPermission; 2831 } 2832 else if (className != null) 2836 { 2837 String [] objectClass = (String []) 2838 refs[refIdx].getProperty(OscarConstants.OBJECTCLASS); 2839 for (int classIdx = 0; 2840 classIdx < objectClass.length; 2841 classIdx++) 2842 { 2843 if (objectClass[classIdx].equals(className) && 2844 ((filter == null) || filter.match(refs[refIdx]))) 2845 { 2846 matched = true; 2847 break; 2848 } 2849 } 2850 } 2851 2852 if (matched) 2854 { 2855 list.add(refs[refIdx]); 2856 } 2857 } 2858 } 2859 } 2860 2861 if (list.size() > 0) 2862 { 2863 ServiceReference[] refs = new ServiceReference[list.size()]; 2864 for (int i = 0; i < list.size(); i++) 2865 { 2866 refs[i] = (ServiceReference) list.get(i); 2867 } 2868 return refs; 2869 } 2870 } 2871 2872 return null; 2873 } 2874 2875 2878 protected Object getService(BundleImpl bundle, ServiceReference ref) 2879 { 2880 synchronized (m_quickLock) 2881 { 2882 String [] objectClass = (String []) 2886 ref.getProperty(Constants.OBJECTCLASS); 2887 if (objectClass == null) 2888 { 2889 return null; 2890 } 2891 2892 boolean hasPermission = false; 2893 if (System.getSecurityManager() != null) 2894 { 2895 for (int i = 0; 2896 !hasPermission && (i < objectClass.length); 2897 i++) 2898 { 2899 try 2900 { 2901 ServicePermission perm = 2902 new ServicePermission( 2903 objectClass[i], ServicePermission.GET); 2904 AccessController.checkPermission(perm); 2905 hasPermission = true; 2906 } 2907 catch (Exception ex) 2908 { 2909 } 2910 } 2911 } 2912 else 2913 { 2914 hasPermission = true; 2915 } 2916 2917 if (!hasPermission) 2920 { 2921 return null; 2922 } 2923 2924 ServiceRegistrationImpl reg = 2926 ((ServiceReferenceImpl) ref).getServiceRegistration().isValid() 2927 ? ((ServiceReferenceImpl) ref).getServiceRegistration() 2928 : null; 2929 2930 BundleInfo clientInfo = bundle.getInfo(); 2931 2932 if (reg == null) 2939 { 2940 clientInfo.removeServiceUsageCounter(ref); 2942 2943 2949 return null; 2950 } 2951 2952 BundleInfo.UsageCounter usage = clientInfo.getServiceUsageCounter(ref); 2954 2955 Object svcObj = null; 2958 if (usage != null) 2959 { 2960 usage.m_count++; 2961 svcObj = usage.m_svcObj; 2962 } 2963 else 2964 { 2965 svcObj = reg.getService(bundle); 2967 2968 if (svcObj != null) 2970 { 2971 usage = new BundleInfo.UsageCounter(); 2972 usage.m_svcObj = svcObj; 2973 usage.m_count++; 2974 clientInfo.putServiceUsageCounter(ref, usage); 2975 } 2976 } 2977 2978 return svcObj; 2979 } 2980 } 2981 2982 2985 protected Object ungetService(BundleImpl bundle, ServiceReference ref) 2986 { 2987 synchronized (m_quickLock) 2988 { 2989 BundleInfo clientInfo = bundle.getInfo(); 2990 BundleInfo.UsageCounter usage = null; 2991 2992 usage = clientInfo.getServiceUsageCounter(ref); 2994 2995 if (usage == null) 2997 { 2998 return null; 2999 } 3000 3001 usage.m_count--; 3003 3004 if (usage.m_count == 0) 3008 { 3009 clientInfo.removeServiceUsageCounter(ref); 3010 ServiceRegistrationImpl reg = 3011 ((ServiceReferenceImpl) ref).getServiceRegistration(); 3012 reg.ungetService(bundle, usage.m_svcObj); 3013 usage.m_svcObj = null; 3014 } 3015 3016 return usage.m_svcObj; 3018 } 3019 } 3020 3021 3024 protected File getBundleDataFile(BundleImpl bundle, String name) 3025 throws IllegalStateException 3026 { 3027 if ((bundle.getInfo().getState() != Bundle.ACTIVE) && 3031 (bundle.getInfo().getState() != Bundle.STARTING) && 3032 (bundle.getInfo().getState() != Bundle.STOPPING)) 3033 { 3034 throw new IllegalStateException ("Only active bundles can create files."); 3035 } 3036 try 3037 { 3038 return m_cache.getArchive( 3039 bundle.getInfo().getBundleId()).getDataFile(name); 3040 } 3041 catch (Exception ex) 3042 { 3043 Oscar.error(ex.getMessage()); 3044 return null; 3045 } 3046 } 3047 3048 3052 private BundleInfo createBundleInfo(BundleArchive archive) 3053 throws Exception 3054 { 3055 Map headerMap = null; 3057 try 3058 { 3059 headerMap = archive.getManifestHeader(archive.getRevisionCount() - 1); 3062 } 3063 catch (Exception ex) 3064 { 3065 throw new BundleException("Unable to read JAR manifest.", ex); 3066 } 3067 3068 if (headerMap == null) 3070 { 3071 throw new BundleException("Unable to read JAR manifest header."); 3072 } 3073 3074 Module module = createModule( 3078 archive.getId(), archive.getRevisionCount() - 1, headerMap); 3079 3080 return new BundleInfo(archive, module); 3082 } 3083 3084 3093 private Module createModule(long id, int revision, Map headerMap) 3094 throws Exception 3095 { 3096 ResourceSource[] resSources = null; 3099 try 3100 { 3101 String [] classPath = m_cache.getArchive(id).getClassPath(revision); 3103 3104 resSources = new ResourceSource[classPath.length]; 3106 for (int i = 0; i < classPath.length; i++) 3107 { 3108 resSources[i] = new JarResourceSource(classPath[i]); 3109 } 3110 } 3111 catch (Exception ex) 3112 { 3113 ex.printStackTrace(); 3114 throw new BundleException("Error in class path: " + ex); 3115 } 3116 3117 Object [][] imports = TextUtil.parseImportExportHeader( 3120 (String ) headerMap.get(Constants.IMPORT_PACKAGE)); 3121 3122 Object [][] exports = TextUtil.parseImportExportHeader( 3125 (String ) headerMap.get(Constants.EXPORT_PACKAGE)); 3126 3127 String [] dynamics = TextUtil.parseCommaDelimitedString( 3130 (String ) headerMap.get(Constants.DYNAMICIMPORT_PACKAGE)); 3131 dynamics = (dynamics == null) ? new String [0] : dynamics; 3132 3133 Object [][] attributes = { 3134 new Object [] { ImportSearchPolicy.EXPORTS_ATTR, exports }, 3135 new Object [] { ImportSearchPolicy.IMPORTS_ATTR, imports }, 3136 new Object [] { OSGiImportSearchPolicy.DYNAMIC_IMPORTS_ATTR, dynamics}, 3137 new Object [] { ImportSearchPolicy.PROPAGATES_ATTR, new Object [0] } 3138 }; 3139 3140 LibraryInfo[] libraries = 3142 TextUtil.parseLibraryStrings(TextUtil.parseCommaDelimitedString( 3143 (String ) headerMap.get(Constants.BUNDLE_NATIVECODE))); 3144 LibrarySource[] libSources = { 3145 new OSGiLibrarySource( 3146 m_cache, id, revision, 3147 getProperty(Constants.FRAMEWORK_OS_NAME), 3148 getProperty(Constants.FRAMEWORK_PROCESSOR), 3149 libraries) 3150 }; 3151 3152 Module module = 3153 m_mgr.addModule( 3154 Long.toString(id) + "." + Integer.toString(revision), 3155 attributes, resSources, libSources); 3156 3157 return module; 3158 } 3159 3160 3164 3171 private Bundle getBundle(String location) 3172 { 3173 synchronized (m_quickLock) 3174 { 3175 return (Bundle) m_installedBundleMap.get(location); 3176 } 3177 } 3178 3179 3187 private void purgeBundle(BundleImpl bundle) throws Exception 3188 { 3189 3193 BundleInfo info = bundle.getInfo(); 3195 3196 synchronized (m_quickLock) 3208 { 3209 Module[] modules = info.getModules(); 3210 for (int i = 0; i < modules.length; i++) 3211 { 3212 m_mgr.removeModule(modules[i]); 3213 } 3214 } 3215 3216 m_cache.purge(m_cache.getArchive(info.getBundleId())); 3218 } 3219 3220 3227 private void removeBundle(BundleImpl bundle) throws Exception 3228 { 3229 synchronized (m_adminLock) 3230 { 3231 synchronized (m_quickLock) 3232 { 3233 m_installedBundleMap.remove(bundle.getInfo().getLocation()); 3235 } 3236 3237 Module[] modules = bundle.getInfo().getModules(); 3240 for (int i = 0; i < modules.length; i++) 3241 { 3242 m_mgr.removeModule(modules[i]); 3243 } 3244 3245 m_cache.remove(m_cache.getArchive(bundle.getInfo().getBundleId())); 3247 } 3248 } 3249 3250 private BundleActivator createBundleActivator(BundleInfo info) 3251 throws Exception 3252 { 3253 3258 BundleActivator activator = null; 3259 3260 String strict = getConfigProperty(OscarConstants.STRICT_OSGI_PROP); 3261 boolean isStrict = (strict == null) ? true : strict.equals("true"); 3262 if (!isStrict) 3263 { 3264 try 3265 { 3266 activator = 3267 m_cache.getArchive(info.getBundleId()) 3268 .getActivator(info.getCurrentModule().getClassLoader()); 3269 } 3270 catch (Exception ex) 3271 { 3272 activator = null; 3273 } 3274 } 3275 3276 if (activator == null) 3279 { 3280 BundleArchive ba = m_cache.getArchive(info.getBundleId()); 3282 Map headerMap = ba.getManifestHeader(ba.getRevisionCount() - 1); 3286 String className = (String ) headerMap.get(Constants.BUNDLE_ACTIVATOR); 3288 if (className != null) 3290 { 3291 className = className.trim(); 3292 Class clazz = info.getCurrentModule().getClassLoader().loadClass(className); 3293 if (clazz == null) 3294 { 3295 throw new BundleException("Not found: " 3296 + className); 3297 } 3298 activator = (BundleActivator) clazz.newInstance(); 3299 } 3300 } 3301 3302 return activator; 3303 } 3304 3305 protected void resolveBundle(BundleImpl bundle) 3306 throws BundleException 3307 { 3308 synchronized (m_adminLock) 3309 { 3310 if (System.getSecurityManager() != null) 3313 { 3314 URL url = null; 3315 try 3316 { 3317 url = new URL(bundle.getInfo().getLocation()); 3318 } 3319 catch (MalformedURLException ex) 3320 { 3321 throw new BundleException("Cannot resolve, bad URL " 3322 + bundle.getInfo().getLocation()); 3323 } 3324 3325 try 3326 { 3327 AccessController.doPrivileged(new CheckImportsPrivileged(url, bundle)); 3328 } 3329 catch (PrivilegedActionException ex) 3330 { 3331 Exception thrown = ((PrivilegedActionException) ex).getException(); 3332 if (thrown instanceof AccessControlException) 3333 { 3334 throw (AccessControlException) thrown; 3335 } 3336 else 3337 { 3338 throw new BundleException("Problem resolving: " + ex); 3339 } 3340 } 3341 } 3342 3343 ImportSearchPolicy search = 3346 (ImportSearchPolicy) m_mgr.getSearchPolicy(); 3347 Module module = bundle.getInfo().getCurrentModule(); 3348 try 3349 { 3350 search.validate(module); 3351 } 3352 catch (ValidationException ex) 3353 { 3354 int[] v = (int[]) ex.getVersion(); 3355 throw new BundleException("Unresolved package: " 3356 + ex.getIdentifier() + "; specification-version=\"" 3357 + v[0] + "." + v[1] + "." + v[2] + "\""); 3358 } 3359 3360 bundle.getInfo().setState(Bundle.RESOLVED); 3361 } 3362 } 3363 3364 3368 3371 protected void servicePropertiesModified(ServiceRegistration reg) 3372 { 3373 fireServiceEvent(ServiceEvent.MODIFIED, reg.getReference()); 3374 } 3375 3376 3384 protected Bundle[] getUsingBundles(ServiceReference ref) 3385 { 3386 synchronized (m_quickLock) 3390 { 3391 List list = null; 3392 3393 Iterator iter = m_installedBundleMap.values().iterator(); 3397 while (iter.hasNext()) 3398 { 3399 BundleImpl bundle = (BundleImpl) iter.next(); 3400 BundleInfo info = bundle.getInfo(); 3401 if (info.getServiceUsageCounter(ref) != null) 3402 { 3403 if (list == null) 3404 { 3405 list = new ArrayList(); 3406 } 3407 list.add(bundle); 3408 } 3409 } 3410 3411 if (list != null) 3412 { 3413 Bundle[] bundles = new Bundle[list.size()]; 3414 return (Bundle[]) list.toArray(bundles); 3415 } 3416 } 3417 3418 return null; 3419 } 3420 3421 3426 protected void unregisterService(BundleImpl bundle, ServiceRegistrationImpl reg) 3427 { 3428 synchronized (m_quickLock) 3429 { 3430 BundleInfo info = bundle.getInfo(); 3431 info.removeServiceRegistration(reg); 3432 } 3433 3434 fireServiceEvent(ServiceEvent.UNREGISTERING, reg.getReference()); 3436 } 3437 3438 3441 protected void unregisterServices(BundleImpl bundle) 3442 { 3443 synchronized (m_quickLock) 3444 { 3445 BundleInfo info = bundle.getInfo(); 3446 while (info.getServiceRegistrationCount() != 0) 3447 { 3448 ServiceRegistrationImpl reg = info.getServiceRegistration(0); 3449 unregisterService(bundle, reg); 3450 } 3451 } 3452 } 3453 3454 3459 protected void ungetServices(BundleImpl bundle) 3460 { 3461 synchronized (m_quickLock) 3462 { 3463 BundleInfo info = bundle.getInfo(); 3464 if (info.getState() == Bundle.UNINSTALLED) 3465 { 3466 throw new IllegalStateException ("The bundle is uninstalled."); 3467 } 3468 3469 Iterator iter = info.getServiceUsageCounters(); 3472 if (iter.hasNext()) 3473 { 3474 ArrayList list = new ArrayList(); 3478 while (iter.hasNext()) 3479 { 3480 list.add(iter.next()); 3481 } 3482 3483 for (int i = 0; i < list.size(); i++) 3486 { 3487 ServiceReference ref = (ServiceReference) list.get(i); 3488 while (info.getServiceUsageCounter(ref) != null) 3490 { 3491 ungetService(bundle, ref); 3492 } 3493 } 3494 } 3495 } 3496 } 3497 3498 3502 3505 private void fireFrameworkEvent( 3506 int type, Bundle bundle, Throwable throwable) 3507 { 3508 if (m_frameworkDispatcher == null) 3509 { 3510 m_frameworkDispatcher = new Dispatcher() { 3511 public void dispatch(EventListener l, EventObject eventObj) 3512 { 3513 ((FrameworkListener) l) 3514 .frameworkEvent((FrameworkEvent) eventObj); 3515 } 3516 }; 3517 } 3518 FrameworkEvent event = new FrameworkEvent(type, bundle, throwable); 3519 m_dispatchQueue.dispatch( 3520 m_frameworkDispatcher, FrameworkListener.class, event); 3521 } 3522 3523 3529 private void fireBundleEvent(int type, Bundle bundle) 3530 { 3531 if (m_bundleDispatcher == null) 3532 { 3533 m_bundleDispatcher = new Dispatcher() { 3534 public void dispatch(EventListener l, EventObject eventObj) 3535 { 3536 ((BundleListener) l) 3537 .bundleChanged((BundleEvent) eventObj); 3538 } 3539 }; 3540 } 3541 BundleEvent event = null; 3542 event = new BundleEvent(type, bundle); 3543 m_dispatchQueue.dispatch(m_bundleDispatcher, 3544 BundleListener.class, event); 3545 } 3546 3547 3553 private void fireServiceEvent(int type, ServiceReference ref) 3554 { 3555 if (m_serviceDispatcher == null) 3556 { 3557 m_serviceDispatcher = new Dispatcher() { 3558 public void dispatch(EventListener l, EventObject eventObj) 3559 { 3560 ((ServiceListener) l) 3561 .serviceChanged((ServiceEvent) eventObj); 3562 } 3563 }; 3564 } 3565 ServiceEvent event = null; 3566 event = new ServiceEvent(type, ref); 3567 m_dispatchQueue.dispatch(m_serviceDispatcher, 3568 ServiceListener.class, event); 3569 } 3570 3571 3576 private void removeListeners(BundleImpl bundle) 3577 { 3578 Oscar.debug("Removing all listeners for bundle " 3579 + bundle.getInfo().getBundleId()); 3580 if (bundle == null) 3581 { 3582 return; 3583 } 3584 3585 Object [] listeners = m_dispatchQueue.getListeners(); 3590 for (int i = listeners.length - 2; i >= 0; i -= 2) 3591 { 3592 if (listeners[i + 1] instanceof ListenerWrapper) 3594 { 3595 ListenerWrapper lw = (ListenerWrapper) listeners[i + 1]; 3596 if ((lw.getBundle() != null) && (lw.getBundle().equals(bundle))) 3597 { 3598 m_dispatchQueue.removeListener( 3599 (Class ) listeners[i], (EventListener) listeners[i+1]); 3600 } 3601 } 3602 } 3603 3604 Oscar.debug("Removed all listeners for bundle " 3605 + bundle.getInfo().getBundleId()); 3606 } 3607 3608 3612 private static boolean s_initialized = false; 3613 3614 3625 public static void initializeSystemProperties() 3626 { 3627 if (!s_initialized) 3630 { 3631 s_initialized = true; 3633 3634 3638 File propFile = null; 3640 String custom = System.getProperty(OscarConstants.SYSTEM_PROPERTIES_PROP); 3641 if (custom != null) 3642 { 3643 propFile = new File(custom); 3644 } 3645 else 3646 { 3647 String jarLoc = null; 3650 String classpath = System.getProperty("java.class.path"); 3651 int index = classpath.toLowerCase().indexOf("oscar.jar"); 3652 int start = classpath.lastIndexOf(File.pathSeparator, index) + 1; 3653 if (index > start) 3654 { 3655 jarLoc = classpath.substring(start, index); 3656 if (jarLoc.length() == 0) 3657 { 3658 jarLoc = "."; 3659 } 3660 } 3661 else 3662 { 3663 jarLoc = System.getProperty("user.dir"); 3665 } 3666 3667 propFile = new File(jarLoc, OscarConstants.SYSTEM_PROPERTY_FILE_VALUE); 3668 } 3669 3670 Properties props = new Properties(); 3672 try 3673 { 3674 FileInputStream fis = new FileInputStream(propFile); 3676 props.load(fis); 3677 fis.close(); 3678 } 3679 catch (FileNotFoundException ex) 3680 { 3681 } 3683 catch (Exception ex) 3684 { 3685 Oscar.error("Oscar: Error loading system properties from " 3686 + OscarConstants.SYSTEM_PROPERTY_FILE_VALUE + ": " + ex); 3687 } 3688 3689 for (Enumeration e = props.propertyNames(); e.hasMoreElements(); ) 3691 { 3692 String name = (String ) e.nextElement(); 3693 System.setProperty(name, substVars((String ) props.getProperty(name))); 3694 } 3695 } 3696 } 3697 3698 private void initializeOsgiProperties() 3699 { 3700 setProperty(OscarConstants.FRAMEWORK_VERSION, 3701 OscarConstants.FRAMEWORK_VERSION_VALUE); 3702 setProperty(OscarConstants.FRAMEWORK_VENDOR, 3703 OscarConstants.FRAMEWORK_VENDOR_VALUE); 3704 setProperty(OscarConstants.FRAMEWORK_LANGUAGE, 3705 System.getProperty("user.language")); 3706 setProperty(OscarConstants.FRAMEWORK_OS_NAME, 3707 System.getProperty("os.name")); 3708 setProperty(OscarConstants.FRAMEWORK_OS_VERSION, 3709 System.getProperty("os.version")); 3710 setProperty(OscarConstants.FRAMEWORK_PROCESSOR, 3711 System.getProperty("os.arch")); 3712 } 3713 3714 private void initializeBundleProperties() 3715 { 3716 3720 setProperty(OscarConstants.OSCAR_VERSION_PROPERTY, 3722 OscarConstants.OSCAR_VERSION_VALUE); 3723 3724 String strict = getConfigProperty(OscarConstants.STRICT_OSGI_PROP); 3726 boolean isStrict = (strict == null) ? true : strict.equals("true"); 3727 setProperty(OscarConstants.STRICT_OSGI_PROP, (isStrict) ? "true" : "false"); 3728 3729 String val = System.getProperty("user.home"); 3731 if (val != null) 3732 setProperty("user.home", val); 3733 3734 val = System.getProperty("user.dir"); 3736 if (val != null) 3737 setProperty("user.dir", val); 3738 3739 Properties props = readBundlePropertiesFile(); 3742 for (Enumeration e = props.propertyNames(); e.hasMoreElements(); ) 3743 { 3744 String name = (String ) e.nextElement(); 3745 setProperty(name, (String ) props.getProperty(name)); 3746 } 3747 } 3748 3749 3761 private Properties readBundlePropertiesFile() 3762 { 3763 3767 File propFile = null; 3769 String custom = getConfigProperty(OscarConstants.BUNDLE_PROPERTIES_PROP); 3770 if (custom != null) 3771 { 3772 propFile = new File(custom); 3773 } 3774 else 3775 { 3776 String jarLoc = null; 3779 String classpath = System.getProperty("java.class.path"); 3780 int index = classpath.toLowerCase().indexOf("oscar.jar"); 3781 int start = classpath.lastIndexOf(File.pathSeparator, index) + 1; 3782 if (index > start) 3783 { 3784 jarLoc = classpath.substring(start, index); 3785 if (jarLoc.length() == 0) 3786 { 3787 jarLoc = "."; 3788 } 3789 } 3790 else 3791 { 3792 jarLoc = System.getProperty("user.dir"); 3794 } 3795 3796 propFile = new File(jarLoc, OscarConstants.BUNDLE_PROPERTY_FILE_VALUE); 3797 } 3798 3799 Properties props = new Properties(); 3801 try 3802 { 3803 FileInputStream fis = new FileInputStream(propFile); 3805 props.load(fis); 3806 fis.close(); 3807 } 3808 catch (FileNotFoundException ex) 3809 { 3810 } 3812 catch (Exception ex) 3813 { 3814 Oscar.error("Oscar: Error loading bundle properties from " 3815 + OscarConstants.BUNDLE_PROPERTY_FILE_VALUE + ": " + ex); 3816 return null; 3817 } 3818 3819 return props; 3820 } 3821 3822 private static final String DELIM_START = "${"; 3823 private static final char DELIM_STOP = '}'; 3824 private static final int DELIM_START_LEN = 2; 3825 private static final int DELIM_STOP_LEN = 1; 3826 3827 private static String substVars(String val) 3828 throws IllegalArgumentException 3829 { 3830 StringBuffer sbuf = new StringBuffer (); 3831 3832 if (val == null) 3833 { 3834 return val; 3835 } 3836 3837 int i = 0; 3838 int j, k; 3839 3840 while (true) 3841 { 3842 j = val.indexOf(DELIM_START, i); 3843 if (j == -1) 3844 { 3845 if (i == 0) 3846 { 3847 return val; 3848 } 3849 else 3850 { 3851 sbuf.append(val.substring(i, val.length())); 3852 return sbuf.toString(); 3853 } 3854 } 3855 else 3856 { 3857 sbuf.append(val.substring(i, j)); 3858 k = val.indexOf(DELIM_STOP, j); 3859 if (k == -1) 3860 { 3861 throw new IllegalArgumentException ( 3862 '"' + val + 3863 "\" has no closing brace. Opening brace at position " 3864 + j + '.'); 3865 } 3866 else 3867 { 3868 j += DELIM_START_LEN; 3869 String key = val.substring(j, k); 3870 String replacement = System.getProperty(key, null); 3872 if (replacement != null) 3873 { 3874 sbuf.append(replacement); 3875 } 3876 i = k + DELIM_STOP_LEN; 3877 } 3878 } 3879 } 3880 } 3881 3882 private void processAutoProperties() 3883 { 3884 3892 String prop = getConfigProperty(OscarConstants.AUTO_INSTALL_PROP); 3899 int level = 0; 3900 do 3901 { 3902 if (prop != null) 3903 { 3904 StringTokenizer st = new StringTokenizer(prop, "\" ",true); 3905 if (st.countTokens() > 0) 3906 { 3907 String location = null; 3908 do 3909 { 3910 location = nextLocation(st); 3911 if (location != null) 3912 { 3913 try 3914 { 3915 BundleImpl b = (BundleImpl) installBundle(location, null); 3916 b.getInfo().setStartLevel((level == 0) ? 1 : level); 3918 } 3919 catch (Exception ex) 3920 { 3921 System.err.println("Oscar: Auto-properties install."); 3922 ex.printStackTrace(); 3923 } 3924 } 3925 } 3926 while (location != null); 3927 } 3928 } 3929 3930 level++; 3931 prop = getConfigProperty(OscarConstants.AUTO_INSTALL_PROP + "." + level); 3932 } 3933 while (prop != null); 3934 3935 3943 prop = getConfigProperty(OscarConstants.AUTO_START_PROP); 3950 level = 0; 3951 do 3952 { 3953 if (prop != null) 3954 { 3955 StringTokenizer st = new StringTokenizer(prop, "\" ",true); 3958 if (st.countTokens() > 0) 3959 { 3960 String location = null; 3961 do 3962 { 3963 location = nextLocation(st); 3964 if (location != null) 3965 { 3966 try 3967 { 3968 BundleImpl b = (BundleImpl) installBundle(location, null); 3969 b.getInfo().setStartLevel((level == 0) ? 1 : level); 3971 } 3972 catch (Exception ex) 3973 { 3974 System.err.println("Oscar: Auto-properties install."); 3975 ex.printStackTrace(); 3976 } 3977 } 3978 } 3979 while (location != null); 3980 } 3981 3982 st = new StringTokenizer(prop, "\" ",true); 3984 if (st.countTokens() > 0) 3985 { 3986 String location = null; 3987 do 3988 { 3989 location = nextLocation(st); 3990 if (location != null) 3991 { 3992 try 3994 { 3995 BundleImpl bundle = (BundleImpl) installBundle(location, null); 3996 startBundle(bundle); 3997 } 3998 catch (Exception ex) 3999 { 4000 System.err.println("Oscar: Auto-properties start."); 4001 ex.printStackTrace(); 4002 } 4003 } 4004 } 4005 while (location != null); 4006 } 4007 } 4008 4009 level++; 4010 prop = getConfigProperty(OscarConstants.AUTO_START_PROP + "." + level); 4011 } 4012 while (prop != null); 4013 } 4014 4015 private String nextLocation(StringTokenizer st) 4016 { 4017 String retVal = null; 4018 4019 if (st.countTokens() > 0) 4020 { 4021 String tokenList = "\" "; 4022 StringBuffer tokBuf = new StringBuffer (10); 4023 String tok = null; 4024 String location = null; 4025 boolean inQuote = false; 4026 boolean tokStarted = false; 4027 boolean exit = false; 4028 while ((st.hasMoreTokens()) && (!exit)) 4029 { 4030 tok = st.nextToken(tokenList); 4031 if (tok.equals("\"")) 4032 { 4033 inQuote = ! inQuote; 4034 if (inQuote) 4035 { 4036 tokenList = "\""; 4037 } 4038 else 4039 { 4040 tokenList = "\" "; 4041 } 4042 4043 } 4044 else if (tok.equals(" ")) 4045 { 4046 if (tokStarted) 4047 { 4048 retVal = tokBuf.toString(); 4049 tokStarted=false; 4050 tokBuf = new StringBuffer (10); 4051 exit = true; 4052 } 4053 } 4054 else 4055 { 4056 tokStarted = true; 4057 tokBuf.append(tok.trim()); 4058 } 4059 } 4060 4061 if ((!exit) && (tokStarted)) 4064 { 4065 retVal = tokBuf.toString(); 4066 } 4067 } 4068 4069 return retVal; 4070 } 4071 4072 4076 public static void setDebug(PrintStream ps) 4077 { 4078 synchronized (m_outputLockObj) 4079 { 4080 m_debugOut = ps; 4081 } 4082 } 4083 4084 public static void debug(String s) 4085 { 4086 synchronized (m_outputLockObj) 4087 { 4088 if (m_debugOut != null) 4089 { 4090 m_debugOut.println(s); 4091 } 4092 } 4093 } 4094 4095 public static void error(String s) 4096 { 4097 synchronized (m_outputLockObj) 4098 { 4099 if (m_errorOut != null) 4100 { 4101 m_errorOut.println(s); 4102 } 4103 } 4104 } 4105 4106 public static void error(String s, Throwable th) 4107 { 4108 synchronized (m_outputLockObj) 4109 { 4110 if (m_errorOut != null) 4111 { 4112 m_errorOut.println(s); 4113 th.printStackTrace(m_errorOut); 4114 } 4115 } 4116 } 4117 4118 4122 4125 private synchronized long getNextId() 4126 { 4127 return m_nextId++; 4128 } 4129 4130 4133 private synchronized long getNextServiceId() 4134 { 4135 return m_nextServiceId++; 4136 } 4137 4138 4142 4147 private class RefreshHelper 4148 { 4149 private BundleImpl m_bundle = null; 4150 private boolean m_active = false; 4151 4152 public RefreshHelper(Bundle bundle) 4153 { 4154 m_bundle = (BundleImpl) bundle; 4155 } 4156 4157 public void stop() 4158 { 4159 if (m_bundle.getInfo().getPersistentState() == Bundle.ACTIVE) 4160 { 4161 m_active = true; 4162 try 4163 { 4164 stopBundle(m_bundle); 4165 } 4166 catch (BundleException ex) 4167 { 4168 fireFrameworkEvent(FrameworkEvent.ERROR, m_bundle, ex); 4169 } 4170 } 4171 } 4172 4173 public void purgeOrRemove() 4174 { 4175 try 4176 { 4177 BundleInfo info = m_bundle.getInfo(); 4178 4179 if (info.getState() == Bundle.UNINSTALLED) 4182 { 4183 removeBundle(m_bundle); 4186 m_bundle = null; 4187 } 4188 else 4189 { 4190 purgeBundle(m_bundle); 4194 } 4195 } 4196 catch (Exception ex) 4197 { 4198 fireFrameworkEvent(FrameworkEvent.ERROR, m_bundle, ex); 4199 } 4200 } 4201 4202 public void reinitialize() 4203 { 4204 if (m_bundle != null) 4205 { 4206 try 4207 { 4208 synchronized (m_quickLock) 4209 { 4210 BundleInfo info = m_bundle.getInfo(); 4211 BundleInfo newInfo = createBundleInfo(info.getArchive()); 4212 m_bundle.setInfo(newInfo); 4213 } 4214 } 4215 catch (Exception ex) 4216 { 4217 fireFrameworkEvent(FrameworkEvent.ERROR, m_bundle, ex); 4218 } 4219 } 4220 } 4221 4222 public void restart() 4223 { 4224 if (m_bundle != null) 4225 { 4226 if (m_active) 4227 { 4228 try 4229 { 4230 startBundle(m_bundle); 4231 } 4232 catch (BundleException ex) 4233 { 4234 fireFrameworkEvent(FrameworkEvent.ERROR, m_bundle, ex); 4235 } 4236 } 4237 } 4238 } 4239 4240 public String toString() 4241 { 4242 return m_bundle.toString(); 4243 } 4244 } 4245 4246 4250 private static class CheckImportsPrivileged implements PrivilegedExceptionAction 4251 { 4252 private URL m_url = null; 4253 private BundleImpl m_bundle = null; 4254 4255 public CheckImportsPrivileged(URL url, BundleImpl bundle) 4256 { 4257 m_url = url; 4258 m_bundle = bundle; 4259 } 4260 4261 public Object run() throws Exception 4262 { 4263 CodeSource cs = new CodeSource(m_url, (Certificate []) null); 4270 PermissionCollection pc = Policy.getPolicy().getPermissions(cs); 4271 4272 Object [][] imports = 4274 ImportSearchPolicy.getImportsAttribute(m_bundle.getInfo().getCurrentModule()); 4275 for (int i = 0; i < imports.length; i++) 4276 { 4277 PackagePermission perm = new PackagePermission( 4278 (String ) imports[i][ImportSearchPolicy.IDENTIFIER_IDX], 4279 PackagePermission.IMPORT); 4280 if (!pc.implies(perm)) 4281 { 4282 throw new AccessControlException("access denied " + perm); 4283 } 4284 } 4285 4286 imports = 4288 ImportSearchPolicy.getExportsAttribute(m_bundle.getInfo().getCurrentModule()); 4289 for (int i = 0; i < imports.length; i++) 4290 { 4291 PackagePermission perm = new PackagePermission( 4292 (String ) imports[i][ImportSearchPolicy.IDENTIFIER_IDX], 4293 PackagePermission.EXPORT); 4294 if (!pc.implies(perm)) 4295 { 4296 throw new AccessControlException("access denied " + perm); 4297 } 4298 } 4299 4300 return null; 4301 } 4302 } 4303 4304 private static class StartStopPrivileged implements PrivilegedExceptionAction 4305 { 4306 private Oscar m_oscar = null; 4307 private int m_action = 0; 4308 private BundleImpl m_bundle = null; 4309 4310 public static final int START_ACTION = 0; 4311 public static final int STOP_ACTION = 1; 4312 4313 public StartStopPrivileged(Oscar oscar) 4314 { 4315 m_oscar = oscar; 4316 } 4317 4318 public void setAction(int i) 4319 { 4320 m_action = i; 4321 } 4322 4323 public void setBundle(BundleImpl bundle) 4324 { 4325 m_bundle = bundle; 4326 } 4327 4328 public Object run() throws Exception 4329 { 4330 if (m_action == START_ACTION) 4331 { 4332 m_bundle.getInfo().getActivator().start(m_bundle.getInfo().getContext()); 4333 } 4334 else 4335 { 4336 m_bundle.getInfo().getActivator().stop(m_bundle.getInfo().getContext()); 4337 } 4338 return null; 4339 } 4340 } 4341} 4342 | Popular Tags |