1 11 12 package org.eclipse.osgi.framework.internal.core; 13 14 import java.io.*; 15 import java.lang.reflect.Field ; 16 import java.lang.reflect.Method ; 17 import java.net.URL ; 18 import java.security.ProtectionDomain ; 19 import java.util.*; 20 21 import org.eclipse.osgi.framework.console.CommandInterpreter; 22 import org.eclipse.osgi.framework.console.CommandProvider; 23 import org.eclipse.osgi.framework.launcher.Launcher; 24 import org.eclipse.osgi.internal.profile.Profile; 25 import org.eclipse.osgi.service.resolver.*; 26 import org.eclipse.osgi.util.NLS; 27 import org.osgi.framework.*; 28 import org.osgi.service.condpermadmin.ConditionalPermissionAdmin; 29 import org.osgi.service.condpermadmin.ConditionalPermissionInfo; 30 import org.osgi.service.packageadmin.PackageAdmin; 31 import org.osgi.service.packageadmin.RequiredBundle; 32 import org.osgi.service.permissionadmin.PermissionAdmin; 33 34 82 public class FrameworkCommandProvider implements CommandProvider, SynchronousBundleListener { 83 84 85 private OSGi osgi; 86 87 private org.osgi.framework.BundleContext context; 88 89 private StartLevelManager slImpl; 90 private ConditionalPermissionAdmin condPermAdmin; 91 private PermissionAdmin permAdmin; 92 93 94 private String tab = "\t"; private String newline = "\r\n"; 97 98 private final List lazyActivation = new ArrayList(); 99 100 107 public FrameworkCommandProvider(OSGi osgi) { 108 this.osgi = osgi; 109 context = osgi.getBundleContext(); 110 slImpl = osgi.framework.startLevelManager; 111 condPermAdmin = osgi.framework.condPermAdmin; 112 permAdmin = osgi.framework.permissionAdmin; 113 } 114 115 123 public FrameworkCommandProvider intialize() { 124 Dictionary props = new Hashtable(); 125 props.put(Constants.SERVICE_RANKING, new Integer (Integer.MAX_VALUE)); 126 context.registerService(CommandProvider.class.getName(), this, props); 127 128 context.addBundleListener(this); 129 return this; 130 } 131 132 139 public String getHelp() { 140 StringBuffer help = new StringBuffer (1024); 141 help.append(newline); 142 help.append(ConsoleMsg.CONSOLE_HELP_VALID_COMMANDS_HEADER); 143 help.append(newline); 144 addHeader(ConsoleMsg.CONSOLE_HELP_CONTROLLING_FRAMEWORK_HEADER, help); 145 addCommand("launch", ConsoleMsg.CONSOLE_HELP_LAUNCH_COMMAND_DESCRIPTION, help); addCommand("shutdown", ConsoleMsg.CONSOLE_HELP_SHUTDOWN_COMMAND_DESCRIPTION, help); addCommand("close", ConsoleMsg.CONSOLE_HELP_CLOSE_COMMAND_DESCRIPTION, help); addCommand("exit", ConsoleMsg.CONSOLE_HELP_EXIT_COMMAND_DESCRIPTION, help); addCommand("init", ConsoleMsg.CONSOLE_HELP_INIT_COMMAND_DESCRIPTION, help); addCommand("setprop", ConsoleMsg.CONSOLE_HELP_KEYVALUE_ARGUMENT_DESCRIPTION, ConsoleMsg.CONSOLE_HELP_SETPROP_COMMAND_DESCRIPTION, help); addHeader(ConsoleMsg.CONSOLE_HELP_CONTROLLING_BUNDLES_HEADER, help); 152 addCommand("install", ConsoleMsg.CONSOLE_HELP_INSTALL_COMMAND_DESCRIPTION, help); addCommand("uninstall", ConsoleMsg.CONSOLE_HELP_UNINSTALL_COMMAND_DESCRIPTION, help); addCommand("start", ConsoleMsg.CONSOLE_HELP_START_COMMAND_DESCRIPTION, help); addCommand("stop", ConsoleMsg.CONSOLE_HELP_STOP_COMMAND_DESCRIPTION, help); addCommand("refresh", ConsoleMsg.CONSOLE_HELP_REFRESH_COMMAND_DESCRIPTION, help); addCommand("update", ConsoleMsg.CONSOLE_HELP_UPDATE_COMMAND_DESCRIPTION, help); addHeader(ConsoleMsg.CONSOLE_HELP_DISPLAYING_STATUS_HEADER, help); 159 addCommand("status", ConsoleMsg.CONSOLE_HELP_STATE_ARGUMENT_DESCRIPTION, ConsoleMsg.CONSOLE_HELP_STATUS_COMMAND_DESCRIPTION, help); addCommand("ss", ConsoleMsg.CONSOLE_HELP_STATE_ARGUMENT_DESCRIPTION, ConsoleMsg.CONSOLE_HELP_SS_COMMAND_DESCRIPTION, help); addCommand("services", ConsoleMsg.CONSOLE_HELP_FILTER_ARGUMENT_DESCRIPTION, ConsoleMsg.CONSOLE_HELP_SERVICES_COMMAND_DESCRIPTION, help); addCommand("packages", ConsoleMsg.CONSOLE_HELP_PACKAGES_ARGUMENT_DESCRIPTION, ConsoleMsg.CONSOLE_HELP_PACKAGES_COMMAND_DESCRIPTION, help); addCommand("bundles", ConsoleMsg.CONSOLE_HELP_STATE_ARGUMENT_DESCRIPTION, ConsoleMsg.CONSOLE_HELP_BUNDLES_COMMAND_DESCRIPTION, help); addCommand("bundle", ConsoleMsg.CONSOLE_HELP_IDLOCATION_ARGUMENT_DESCRIPTION, ConsoleMsg.CONSOLE_HELP_BUNDLE_COMMAND_DESCRIPTION, help); addCommand("headers", ConsoleMsg.CONSOLE_HELP_IDLOCATION_ARGUMENT_DESCRIPTION, ConsoleMsg.CONSOLE_HELP_HEADERS_COMMAND_DESCRIPTION, help); addCommand("log", ConsoleMsg.CONSOLE_HELP_IDLOCATION_ARGUMENT_DESCRIPTION, ConsoleMsg.CONSOLE_HELP_LOG_COMMAND_DESCRIPTION, help); addHeader(ConsoleMsg.CONSOLE_HELP_EXTRAS_HEADER, help); 168 addCommand("exec", ConsoleMsg.CONSOLE_HELP_COMMAND_ARGUMENT_DESCRIPTION, ConsoleMsg.CONSOLE_HELP_EXEC_COMMAND_DESCRIPTION, help); addCommand("fork", ConsoleMsg.CONSOLE_HELP_COMMAND_ARGUMENT_DESCRIPTION, ConsoleMsg.CONSOLE_HELP_FORK_COMMAND_DESCRIPTION, help); addCommand("gc", ConsoleMsg.CONSOLE_HELP_GC_COMMAND_DESCRIPTION, help); addCommand("getprop ", ConsoleMsg.CONSOLE_HELP_GETPROP_ARGUMENT_DESCRIPTION, ConsoleMsg.CONSOLE_HELP_GETPROP_COMMAND_DESCRIPTION, help); addHeader(ConsoleMsg.STARTLEVEL_HELP_HEADING, help); 173 addCommand("sl", ConsoleMsg.CONSOLE_HELP_OPTIONAL_IDLOCATION_ARGUMENT_DESCRIPTION, ConsoleMsg.STARTLEVEL_HELP_SL, help); addCommand("setfwsl", ConsoleMsg.STARTLEVEL_ARGUMENT_DESCRIPTION, ConsoleMsg.STARTLEVEL_HELP_SETFWSL, help); addCommand("setbsl", ConsoleMsg.STARTLEVEL_IDLOCATION_ARGUMENT_DESCRIPTION, ConsoleMsg.STARTLEVEL_HELP_SETBSL, help); addCommand("setibsl", ConsoleMsg.STARTLEVEL_ARGUMENT_DESCRIPTION, ConsoleMsg.STARTLEVEL_HELP_SETIBSL, help); addHeader(ConsoleMsg.CONSOLE_HELP_PROFILE_HEADING, help); 178 addCommand("profilelog", ConsoleMsg.CONSOLE_HELP_PROFILELOG_DESCRIPTION, help); return help.toString(); 180 } 181 182 183 private void addHeader(String header, StringBuffer help) { 184 help.append("---"); help.append(header); 186 help.append("---"); help.append(newline); 188 } 189 190 191 private void addCommand(String command, String description, StringBuffer help) { 192 help.append(tab); 193 help.append(command); 194 help.append(" - "); help.append(description); 196 help.append(newline); 197 } 198 199 200 private void addCommand(String command, String parameters, String description, StringBuffer help) { 201 help.append(tab); 202 help.append(command); 203 help.append(" "); help.append(parameters); 205 help.append(" - "); help.append(description); 207 help.append(newline); 208 } 209 210 215 public void _exit(CommandInterpreter intp) throws Exception { 216 intp.println(); 217 System.exit(0); 218 } 219 220 225 public void _launch(CommandInterpreter intp) throws Exception { 226 osgi.launch(); 227 } 228 229 234 public void _shutdown(CommandInterpreter intp) throws Exception { 235 osgi.shutdown(); 236 } 237 238 243 public void _sta(CommandInterpreter intp) throws Exception { 244 _start(intp); 245 } 246 247 252 public void _start(CommandInterpreter intp) throws Exception { 253 String nextArg = intp.nextArgument(); 254 if (nextArg == null) { 255 intp.println(ConsoleMsg.CONSOLE_NO_BUNDLE_SPECIFIED_ERROR); 256 } 257 while (nextArg != null) { 258 AbstractBundle bundle = getBundleFromToken(intp, nextArg, true); 259 if (bundle != null) { 260 bundle.start(); 261 } 262 nextArg = intp.nextArgument(); 263 } 264 } 265 266 271 public void _sto(CommandInterpreter intp) throws Exception { 272 _stop(intp); 273 } 274 275 280 public void _stop(CommandInterpreter intp) throws Exception { 281 String nextArg = intp.nextArgument(); 282 if (nextArg == null) { 283 intp.println(ConsoleMsg.CONSOLE_NO_BUNDLE_SPECIFIED_ERROR); 284 } 285 while (nextArg != null) { 286 AbstractBundle bundle = getBundleFromToken(intp, nextArg, true); 287 if (bundle != null) { 288 bundle.stop(); 289 } 290 nextArg = intp.nextArgument(); 291 } 292 } 293 294 299 public void _i(CommandInterpreter intp) throws Exception { 300 _install(intp); 301 } 302 303 308 public void _install(CommandInterpreter intp) throws Exception { 309 String url = intp.nextArgument(); 310 if (url == null) { 311 intp.println(ConsoleMsg.CONSOLE_NOTHING_TO_INSTALL_ERROR); 312 } else { 313 AbstractBundle bundle = (AbstractBundle) context.installBundle(url); 314 intp.print(ConsoleMsg.CONSOLE_BUNDLE_ID_MESSAGE); 315 intp.println(new Long (bundle.getBundleId())); 316 317 String nextArg = intp.nextArgument(); 318 if (nextArg != null) { 319 String start = nextArg.toLowerCase(); 320 321 if (Launcher.matchCommand("start", start, 1)) { bundle.start(); 323 } 324 } 325 } 326 327 } 328 329 334 public void _up(CommandInterpreter intp) throws Exception { 335 _update(intp); 336 } 337 338 343 public void _update(CommandInterpreter intp) throws Exception { 344 String token = intp.nextArgument(); 345 if (token == null) { 346 intp.println(ConsoleMsg.CONSOLE_NO_BUNDLE_SPECIFIED_ERROR); 347 } 348 while (token != null) { 349 350 if ("*".equals(token)) { AbstractBundle[] bundles = (AbstractBundle[]) context.getBundles(); 352 353 int size = bundles.length; 354 355 if (size > 0) { 356 for (int i = 0; i < size; i++) { 357 AbstractBundle bundle = bundles[i]; 358 359 if (bundle.getBundleId() != 0) { 360 try { 361 bundle.update(); 362 } catch (BundleException e) { 363 intp.printStackTrace(e); 364 } 365 } 366 } 367 } else { 368 intp.println(ConsoleMsg.CONSOLE_NO_INSTALLED_BUNDLES_ERROR); 369 } 370 } else { 371 AbstractBundle bundle = getBundleFromToken(intp, token, true); 372 if (bundle != null) { 373 String source = intp.nextArgument(); 374 try { 375 if (source != null) { 376 bundle.update(new URL (source).openStream()); 377 } else { 378 bundle.update(); 379 } 380 } catch (BundleException e) { 381 intp.printStackTrace(e); 382 } 383 } 384 } 385 token = intp.nextArgument(); 386 } 387 } 388 389 394 public void _un(CommandInterpreter intp) throws Exception { 395 _uninstall(intp); 396 } 397 398 403 public void _uninstall(CommandInterpreter intp) throws Exception { 404 String nextArg = intp.nextArgument(); 405 if (nextArg == null) { 406 intp.println(ConsoleMsg.CONSOLE_NO_BUNDLE_SPECIFIED_ERROR); 407 } 408 while (nextArg != null) { 409 AbstractBundle bundle = getBundleFromToken(intp, nextArg, true); 410 if (bundle != null) { 411 bundle.uninstall(); 412 } 413 nextArg = intp.nextArgument(); 414 } 415 } 416 417 422 public void _s(CommandInterpreter intp) throws Exception { 423 _status(intp); 424 } 425 426 private Object [] processOption(CommandInterpreter intp) { 427 String option = intp.nextArgument(); 428 String filteredName = null; 429 int stateFilter = -1; 430 if (option != null && option.equals("-s")) { 431 String searchedState = intp.nextArgument(); 432 StringTokenizer tokens = new StringTokenizer(searchedState, ","); 433 while (tokens.hasMoreElements()) { 434 String desiredState = (String ) tokens.nextElement(); 435 Field match = null; 436 try { 437 match = Bundle.class.getField(desiredState.toUpperCase()); 438 if (stateFilter == -1) 439 stateFilter = 0; 440 stateFilter |= match.getInt(match); 441 } catch (NoSuchFieldException e) { 442 intp.println(ConsoleMsg.CONSOLE_INVALID_INPUT + ": " + desiredState); 443 return null; 444 } catch (IllegalAccessException e) { 445 intp.println(ConsoleMsg.CONSOLE_INVALID_INPUT + ": " + desiredState); 446 return null; 447 } 448 } 449 } else { 450 filteredName = option; 451 } 452 String tmp = intp.nextArgument(); 453 if (tmp != null) 454 filteredName = tmp; 455 return new Object [] {filteredName, new Integer (stateFilter)}; 456 } 457 458 463 public void _status(CommandInterpreter intp) throws Exception { 464 if (osgi.isActive()) { 465 intp.println(ConsoleMsg.CONSOLE_FRAMEWORK_IS_LAUNCHED_MESSAGE); 466 } else { 467 intp.println(ConsoleMsg.CONSOLE_FRAMEWORK_IS_SHUTDOWN_MESSAGE); 468 } 469 intp.println(); 470 471 Object [] options = processOption(intp); 472 if (options == null) 473 return; 474 475 AbstractBundle[] bundles = (AbstractBundle[]) context.getBundles(); 476 int size = bundles.length; 477 478 if (size == 0) { 479 intp.println(ConsoleMsg.CONSOLE_NO_INSTALLED_BUNDLES_ERROR); 480 return; 481 } 482 intp.print(ConsoleMsg.CONSOLE_ID); 483 intp.print(tab); 484 intp.println(ConsoleMsg.CONSOLE_BUNDLE_LOCATION_MESSAGE); 485 intp.println(ConsoleMsg.CONSOLE_STATE_BUNDLE_FILE_NAME_HEADER); 486 for (int i = 0; i < size; i++) { 487 AbstractBundle bundle = bundles[i]; 488 if (!match(bundle, (String ) options[0], ((Integer ) options[1]).intValue())) 489 continue; 490 intp.print(new Long (bundle.getBundleId())); 491 intp.print(tab); 492 intp.println(bundle.getLocation()); 493 intp.print(" "); intp.print(getStateName(bundle)); 495 intp.println(bundle.bundledata); 496 } 497 498 ServiceReference[] services = context.getServiceReferences(null, null); 499 if (services != null) { 500 intp.println(ConsoleMsg.CONSOLE_REGISTERED_SERVICES_MESSAGE); 501 size = services.length; 502 for (int i = 0; i < size; i++) { 503 intp.println(services[i]); 504 } 505 } 506 } 507 508 513 public void _se(CommandInterpreter intp) throws Exception { 514 _services(intp); 515 } 516 517 522 public void _services(CommandInterpreter intp) throws Exception { 523 String filter = null; 524 525 String nextArg = intp.nextArgument(); 526 if (nextArg != null) { 527 StringBuffer buf = new StringBuffer (); 528 while (nextArg != null) { 529 buf.append(' '); 530 buf.append(nextArg); 531 nextArg = intp.nextArgument(); 532 } 533 filter = buf.toString(); 534 } 535 536 ServiceReference[] services = context.getServiceReferences(null, filter); 537 if (services != null) { 538 int size = services.length; 539 if (size > 0) { 540 for (int j = 0; j < size; j++) { 541 ServiceReference service = services[j]; 542 intp.println(service); 543 intp.print(" "); intp.print(ConsoleMsg.CONSOLE_REGISTERED_BY_BUNDLE_MESSAGE); 545 intp.print(" "); intp.println(service.getBundle()); 547 AbstractBundle[] users = (AbstractBundle[]) service.getUsingBundles(); 548 if (users != null) { 549 intp.print(" "); intp.println(ConsoleMsg.CONSOLE_BUNDLES_USING_SERVICE_MESSAGE); 551 for (int k = 0; k < users.length; k++) { 552 intp.print(" "); intp.println(users[k]); 554 } 555 } else { 556 intp.print(" "); intp.println(ConsoleMsg.CONSOLE_NO_BUNDLES_USING_SERVICE_MESSAGE); 558 } 559 } 560 return; 561 } 562 } 563 intp.println(ConsoleMsg.CONSOLE_NO_REGISTERED_SERVICES_MESSAGE); 564 } 565 566 571 public void _p(CommandInterpreter intp) throws Exception { 572 _packages(intp); 573 } 574 575 580 public void _packages(CommandInterpreter intp) throws Exception { 581 org.osgi.framework.Bundle bundle = null; 582 583 String token = intp.nextArgument(); 584 if (token != null) { 585 bundle = getBundleFromToken(intp, token, false); 586 } 587 588 org.osgi.framework.ServiceReference packageAdminRef = context.getServiceReference("org.osgi.service.packageadmin.PackageAdmin"); if (packageAdminRef != null) { 590 org.osgi.service.packageadmin.PackageAdmin packageAdmin = (org.osgi.service.packageadmin.PackageAdmin) context.getService(packageAdminRef); 591 if (packageAdmin != null) { 592 try { 593 org.osgi.service.packageadmin.ExportedPackage[] packages = null; 594 595 if (token != null) 596 packages = packageAdmin.getExportedPackages(token); 597 if (packages == null) 598 packages = packageAdmin.getExportedPackages(bundle); 599 600 if (packages == null) { 601 intp.println(ConsoleMsg.CONSOLE_NO_EXPORTED_PACKAGES_MESSAGE); 602 } else { 603 for (int i = 0; i < packages.length; i++) { 604 org.osgi.service.packageadmin.ExportedPackage pkg = packages[i]; 605 intp.print(pkg); 606 607 boolean removalPending = pkg.isRemovalPending(); 608 if (removalPending) { 609 intp.print("("); intp.print(ConsoleMsg.CONSOLE_REMOVAL_PENDING_MESSAGE); 611 intp.println(")"); } 613 614 org.osgi.framework.Bundle exporter = pkg.getExportingBundle(); 615 if (exporter != null) { 616 intp.print("<"); intp.print(exporter); 618 intp.println(">"); 620 org.osgi.framework.Bundle[] importers = pkg.getImportingBundles(); 621 for (int j = 0; j < importers.length; j++) { 622 intp.print(" "); intp.print(importers[j]); 624 intp.print(" "); intp.println(ConsoleMsg.CONSOLE_IMPORTS_MESSAGE); 626 } 627 } else { 628 intp.print("<"); intp.print(ConsoleMsg.CONSOLE_STALE_MESSAGE); 630 intp.println(">"); } 632 633 } 634 } 635 } finally { 636 context.ungetService(packageAdminRef); 637 } 638 } 639 } else { 640 intp.println(ConsoleMsg.CONSOLE_NO_EXPORTED_PACKAGES_NO_PACKAGE_ADMIN_MESSAGE); 641 } 642 } 643 644 649 public void _bundles(CommandInterpreter intp) throws Exception { 650 Object [] options = processOption(intp); 651 if (options == null) 652 return; 653 654 AbstractBundle[] bundles = (AbstractBundle[]) context.getBundles(); 655 int size = bundles.length; 656 657 if (size == 0) { 658 intp.println(ConsoleMsg.CONSOLE_NO_INSTALLED_BUNDLES_ERROR); 659 return; 660 } 661 662 for (int i = 0; i < size; i++) { 663 AbstractBundle bundle = bundles[i]; 664 if (!match(bundle, (String ) options[0], ((Integer ) options[1]).intValue())) 665 continue; 666 long id = bundle.getBundleId(); 667 intp.println(bundle); 668 intp.print(" "); intp.print(NLS.bind(ConsoleMsg.CONSOLE_ID_MESSAGE, String.valueOf(id))); 670 intp.print(", "); intp.print(NLS.bind(ConsoleMsg.CONSOLE_STATUS_MESSAGE, getStateName(bundle))); 672 if (id != 0) { 673 File dataRoot = osgi.framework.getDataFile(bundle, ""); 675 String root = (dataRoot == null) ? null : dataRoot.getAbsolutePath(); 676 677 intp.print(NLS.bind(ConsoleMsg.CONSOLE_DATA_ROOT_MESSAGE, root)); 678 } else { 679 intp.println(); 680 } 681 682 ServiceReference[] services = bundle.getRegisteredServices(); 683 if (services != null) { 684 intp.print(" "); intp.println(ConsoleMsg.CONSOLE_REGISTERED_SERVICES_MESSAGE); 686 for (int j = 0; j < services.length; j++) { 687 intp.print(" "); intp.println(services[j]); 689 } 690 } else { 691 intp.print(" "); intp.println(ConsoleMsg.CONSOLE_NO_REGISTERED_SERVICES_MESSAGE); 693 } 694 695 services = bundle.getServicesInUse(); 696 if (services != null) { 697 intp.print(" "); intp.println(ConsoleMsg.CONSOLE_SERVICES_IN_USE_MESSAGE); 699 for (int j = 0; j < services.length; j++) { 700 intp.print(" "); intp.println(services[j]); 702 } 703 } else { 704 intp.print(" "); intp.println(ConsoleMsg.CONSOLE_NO_SERVICES_IN_USE_MESSAGE); 706 } 707 } 708 } 709 710 715 public void _b(CommandInterpreter intp) throws Exception { 716 _bundle(intp); 717 } 718 719 724 public void _bundle(CommandInterpreter intp) throws Exception { 725 String nextArg = intp.nextArgument(); 726 if (nextArg == null) { 727 intp.println(ConsoleMsg.CONSOLE_NO_BUNDLE_SPECIFIED_ERROR); 728 } 729 while (nextArg != null) { 730 AbstractBundle bundle = getBundleFromToken(intp, nextArg, true); 731 if (bundle != null) { 732 long id = bundle.getBundleId(); 733 intp.println(bundle); 734 intp.print(" "); intp.print(NLS.bind(ConsoleMsg.CONSOLE_ID_MESSAGE, String.valueOf(id))); 736 intp.print(", "); intp.print(NLS.bind(ConsoleMsg.CONSOLE_STATUS_MESSAGE, getStateName(bundle))); 738 if (id != 0) { 739 File dataRoot = osgi.framework.getDataFile(bundle, ""); 741 String root = (dataRoot == null) ? null : dataRoot.getAbsolutePath(); 742 743 intp.print(NLS.bind(ConsoleMsg.CONSOLE_DATA_ROOT_MESSAGE, root)); 744 intp.println(); 745 } else { 746 intp.println(); 747 } 748 749 ServiceReference[] services = bundle.getRegisteredServices(); 750 if (services != null) { 751 intp.print(" "); intp.println(ConsoleMsg.CONSOLE_REGISTERED_SERVICES_MESSAGE); 753 for (int j = 0; j < services.length; j++) { 754 intp.print(" "); intp.println(services[j]); 756 } 757 } else { 758 intp.print(" "); intp.println(ConsoleMsg.CONSOLE_NO_REGISTERED_SERVICES_MESSAGE); 760 } 761 762 services = bundle.getServicesInUse(); 763 if (services != null) { 764 intp.print(" "); intp.println(ConsoleMsg.CONSOLE_SERVICES_IN_USE_MESSAGE); 766 for (int j = 0; j < services.length; j++) { 767 intp.print(" "); intp.println(services[j]); 769 } 770 } else { 771 intp.print(" "); intp.println(ConsoleMsg.CONSOLE_NO_SERVICES_IN_USE_MESSAGE); 773 } 774 775 org.osgi.framework.ServiceReference packageAdminRef = context.getServiceReference("org.osgi.service.packageadmin.PackageAdmin"); if (packageAdminRef != null) { 777 BundleDescription desc = bundle.getBundleDescription(); 778 if (desc != null) { 779 boolean title = true; 780 try { 781 ExportPackageDescription[] exports = desc.getExportPackages(); 782 if (exports == null || exports.length == 0) { 783 intp.print(" "); intp.println(ConsoleMsg.CONSOLE_NO_EXPORTED_PACKAGES_MESSAGE); 785 } else { 786 title = true; 787 788 for (int i = 0; i < exports.length; i++) { 789 if (title) { 790 intp.print(" "); intp.println(ConsoleMsg.CONSOLE_EXPORTED_PACKAGES_MESSAGE); 792 title = false; 793 } 794 intp.print(" "); intp.print(exports[i].getName()); 796 intp.print("; version=\""); intp.print(exports[i].getVersion()); 798 intp.print("\""); if (desc.isRemovalPending()) { 800 intp.println(ConsoleMsg.CONSOLE_EXPORTED_REMOVAL_PENDING_MESSAGE); 801 } else { 802 intp.println(ConsoleMsg.CONSOLE_EXPORTED_MESSAGE); 803 } 804 } 805 806 if (title) { 807 intp.print(" "); intp.println(ConsoleMsg.CONSOLE_NO_EXPORTED_PACKAGES_MESSAGE); 809 } 810 } 811 title = true; 812 if (desc != null) { 813 ExportPackageDescription[] imports = desc.getContainingState().getStateHelper().getVisiblePackages(desc, StateHelper.VISIBLE_INCLUDE_EE_PACKAGES); 814 title = printImportedPackages(imports, intp, title); 815 } 816 817 if (title) { 818 intp.print(" "); intp.println(ConsoleMsg.CONSOLE_NO_IMPORTED_PACKAGES_MESSAGE); 820 } 821 822 PackageAdmin packageAdmin = (PackageAdmin) context.getService(packageAdminRef); 823 if (packageAdmin != null) { 824 intp.print(" "); if ((packageAdmin.getBundleType(bundle) & PackageAdminImpl.BUNDLE_TYPE_FRAGMENT) > 0) { 826 org.osgi.framework.Bundle[] hosts = packageAdmin.getHosts(bundle); 827 if (hosts != null) { 828 intp.println(ConsoleMsg.CONSOLE_HOST_MESSAGE); 829 for (int i = 0; i < hosts.length; i++) { 830 intp.print(" "); intp.println(hosts[i]); 832 } 833 } else { 834 intp.println(ConsoleMsg.CONSOLE_NO_HOST_MESSAGE); 835 } 836 } else { 837 org.osgi.framework.Bundle[] fragments = packageAdmin.getFragments(bundle); 838 if (fragments != null) { 839 intp.println(ConsoleMsg.CONSOLE_FRAGMENT_MESSAGE); 840 for (int i = 0; i < fragments.length; i++) { 841 intp.print(" "); intp.println(fragments[i]); 843 } 844 } else { 845 intp.println(ConsoleMsg.CONSOLE_NO_FRAGMENT_MESSAGE); 846 } 847 } 848 849 RequiredBundle[] requiredBundles = packageAdmin.getRequiredBundles(null); 850 RequiredBundle requiredBundle = null; 851 if (requiredBundles != null) { 852 for (int i = 0; i < requiredBundles.length; i++) { 853 if (requiredBundles[i].getBundle() == bundle) { 854 requiredBundle = requiredBundles[i]; 855 break; 856 } 857 } 858 } 859 860 if (requiredBundle == null) { 861 intp.print(" "); intp.println(ConsoleMsg.CONSOLE_NO_NAMED_CLASS_SPACES_MESSAGE); 863 } else { 864 intp.print(" "); intp.println(ConsoleMsg.CONSOLE_NAMED_CLASS_SPACE_MESSAGE); 866 intp.print(" "); intp.print(requiredBundle); 868 if (requiredBundle.isRemovalPending()) { 869 intp.println(ConsoleMsg.CONSOLE_REMOVAL_PENDING_MESSAGE); 870 } else { 871 intp.println(ConsoleMsg.CONSOLE_PROVIDED_MESSAGE); 872 } 873 } 874 title = true; 875 for (int i = 0; i < requiredBundles.length; i++) { 876 if (requiredBundles[i] == requiredBundle) 877 continue; 878 879 org.osgi.framework.Bundle[] depBundles = requiredBundles[i].getRequiringBundles(); 880 if (depBundles == null) 881 continue; 882 883 for (int j = 0; j < depBundles.length; j++) { 884 if (depBundles[j] == bundle) { 885 if (title) { 886 intp.print(" "); intp.println(ConsoleMsg.CONSOLE_REQUIRED_BUNDLES_MESSAGE); 888 title = false; 889 } 890 intp.print(" "); intp.print(requiredBundles[i]); 892 893 org.osgi.framework.Bundle provider = requiredBundles[i].getBundle(); 894 intp.print("<"); intp.print(provider); 896 intp.println(">"); } 898 } 899 } 900 if (title) { 901 intp.print(" "); intp.println(ConsoleMsg.CONSOLE_NO_REQUIRED_BUNDLES_MESSAGE); 903 } 904 905 } 906 } finally { 907 context.ungetService(packageAdminRef); 908 } 909 } 910 } else { 911 intp.print(" "); intp.println(ConsoleMsg.CONSOLE_NO_EXPORTED_PACKAGES_NO_PACKAGE_ADMIN_MESSAGE); 913 } 914 915 SecurityManager sm = System.getSecurityManager(); 916 if (sm != null) { 917 ProtectionDomain domain = bundle.getProtectionDomain(); 918 919 intp.println(domain); 920 } 921 } 922 nextArg = intp.nextArgument(); 923 } 924 } 925 926 private boolean printImportedPackages(ExportPackageDescription[] importedPkgs, CommandInterpreter intp, boolean title) { 927 for (int i = 0; i < importedPkgs.length; i++) { 928 if (title) { 929 intp.print(" "); intp.println(ConsoleMsg.CONSOLE_IMPORTED_PACKAGES_MESSAGE); 931 title = false; 932 } 933 intp.print(" "); intp.print(importedPkgs[i].getName()); 935 intp.print("; version=\""); intp.print(importedPkgs[i].getVersion()); 937 intp.print("\""); Bundle exporter = context.getBundle(importedPkgs[i].getSupplier().getBundleId()); 939 if (exporter != null) { 940 intp.print("<"); intp.print(exporter); 942 intp.println(">"); } else { 944 intp.print("<"); intp.print(ConsoleMsg.CONSOLE_STALE_MESSAGE); 946 intp.println(">"); } 948 } 949 return title; 950 } 951 952 957 public void _l(CommandInterpreter intp) throws Exception { 958 _log(intp); 959 } 960 961 966 public void _log(CommandInterpreter intp) throws Exception { 967 long logid = -1; 968 String token = intp.nextArgument(); 969 if (token != null) { 970 AbstractBundle bundle = getBundleFromToken(intp, token, false); 971 972 if (bundle == null) { 973 try { 974 logid = Long.parseLong(token); 975 } catch (NumberFormatException e) { 976 return; 977 } 978 } else { 979 logid = bundle.getBundleId(); 980 } 981 } 982 983 org.osgi.framework.ServiceReference logreaderRef = context.getServiceReference("org.osgi.service.log.LogReaderService"); if (logreaderRef != null) { 985 Object logreader = context.getService(logreaderRef); 986 if (logreader != null) { 987 try { 988 Enumeration logs = (Enumeration) (logreader.getClass().getMethod("getLog", null).invoke(logreader, null)); ArrayList entriesList = new ArrayList(); 990 while (logs.hasMoreElements()) 991 entriesList.add(0, logs.nextElement()); 992 Object [] entries = entriesList.toArray(); 993 if (entries.length == 0) 994 return; 995 Class clazz = entries[0].getClass(); 996 Method getBundle = clazz.getMethod("getBundle", null); Method getLevel = clazz.getMethod("getLevel", null); Method getMessage = clazz.getMethod("getMessage", null); Method getServiceReference = clazz.getMethod("getServiceReference", null); Method getException = clazz.getMethod("getException", null); 1002 for (int i = 0; i < entries.length; i++) { 1003 Object logentry = entries[i]; 1004 AbstractBundle bundle = (AbstractBundle) getBundle.invoke(logentry, null); 1005 1006 if ((logid == -1) || ((bundle != null) && (logid == bundle.getBundleId()))) { 1007 Integer level = (Integer ) getLevel.invoke(logentry, null); 1008 switch (level.intValue()) { 1009 case 4 : 1010 intp.print(">"); intp.print(ConsoleMsg.CONSOLE_DEBUG_MESSAGE); 1012 intp.print(" "); break; 1014 case 3 : 1015 intp.print(">"); intp.print(ConsoleMsg.CONSOLE_INFO_MESSAGE); 1017 intp.print(" "); break; 1019 case 2 : 1020 intp.print(">"); intp.print(ConsoleMsg.CONSOLE_WARNING_MESSAGE); 1022 intp.print(" "); break; 1024 case 1 : 1025 intp.print(">"); intp.print(ConsoleMsg.CONSOLE_ERROR_MESSAGE); 1027 intp.print(" "); break; 1029 default : 1030 intp.print(">"); intp.print(level); 1032 intp.print(" "); break; 1034 } 1035 1036 if (bundle != null) { 1037 intp.print("["); intp.print(new Long (bundle.getBundleId())); 1039 intp.print("] "); } 1041 1042 intp.print(getMessage.invoke(logentry, null)); 1043 intp.print(" "); 1045 ServiceReferenceImpl svcref = (ServiceReferenceImpl) getServiceReference.invoke(logentry, null); 1046 if (svcref != null) { 1047 intp.print("{"); intp.print(Constants.SERVICE_ID); 1049 intp.print("="); intp.print(svcref.getProperty(Constants.SERVICE_ID).toString()); 1051 intp.println("}"); } else { 1053 if (bundle != null) { 1054 intp.println(bundle.getLocation()); 1055 } else { 1056 intp.println(); 1057 } 1058 } 1059 1060 Throwable t = (Throwable ) getException.invoke(logentry, null); 1061 if (t != null) { 1062 intp.printStackTrace(t); 1063 } 1064 } 1065 } 1066 } finally { 1067 context.ungetService(logreaderRef); 1068 } 1069 return; 1070 } 1071 } 1072 1073 intp.println(ConsoleMsg.CONSOLE_LOGSERVICE_NOT_REGISTERED_MESSAGE); 1074 } 1075 1076 1081 public void _gc(CommandInterpreter intp) throws Exception { 1082 long before = Runtime.getRuntime().freeMemory(); 1083 1084 1085 System.gc(); 1086 System.gc(); 1087 System.runFinalization(); 1088 try { 1089 Thread.sleep(100); 1090 } catch (InterruptedException e) { 1091 } 1093 1094 long after = Runtime.getRuntime().freeMemory(); 1095 intp.print(ConsoleMsg.CONSOLE_TOTAL_MEMORY_MESSAGE); 1096 intp.println(String.valueOf(Runtime.getRuntime().totalMemory())); 1097 intp.print(ConsoleMsg.CONSOLE_FREE_MEMORY_BEFORE_GARBAGE_COLLECTION_MESSAGE); 1098 intp.println(String.valueOf(before)); 1099 intp.print(ConsoleMsg.CONSOLE_FREE_MEMORY_AFTER_GARBAGE_COLLECTION_MESSAGE); 1100 intp.println(String.valueOf(after)); 1101 intp.print(ConsoleMsg.CONSOLE_MEMORY_GAINED_WITH_GARBAGE_COLLECTION_MESSAGE); 1102 intp.println(String.valueOf(after - before)); 1103 } 1104 1105 1110 public void _init(CommandInterpreter intp) throws Exception { 1111 if (osgi.isActive()) { 1112 intp.print(newline); 1113 intp.println(ConsoleMsg.CONSOLE_FRAMEWORK_LAUNCHED_PLEASE_SHUTDOWN_MESSAGE); 1114 return; 1115 } 1116 1117 AbstractBundle[] bundles = (AbstractBundle[]) context.getBundles(); 1118 1119 int size = bundles.length; 1120 1121 if (size > 0) { 1122 for (int i = 0; i < size; i++) { 1123 AbstractBundle bundle = bundles[i]; 1124 1125 if (bundle.getBundleId() != 0) { 1126 try { 1127 bundle.uninstall(); 1128 } catch (BundleException e) { 1129 intp.printStackTrace(e); 1130 } 1131 } 1132 } 1133 } else { 1134 intp.println(ConsoleMsg.CONSOLE_NO_INSTALLED_BUNDLES_ERROR); 1135 } 1136 if (permAdmin != null) { 1137 permAdmin.setDefaultPermissions(null); 1139 String [] permLocations = permAdmin.getLocations(); 1140 if (permLocations != null) 1141 for (int i = 0; i < permLocations.length; i++) 1142 permAdmin.setPermissions(permLocations[i], null); 1143 } 1144 if (condPermAdmin != null) 1146 for (Enumeration infos = condPermAdmin.getConditionalPermissionInfos(); infos.hasMoreElements();) 1147 ((ConditionalPermissionInfo) infos.nextElement()).delete(); 1148 } 1149 1150 1155 public void _close(CommandInterpreter intp) throws Exception { 1156 intp.println(); 1157 osgi.close(); 1158 System.exit(0); 1159 } 1160 1161 1166 public void _r(CommandInterpreter intp) throws Exception { 1167 _refresh(intp); 1168 } 1169 1170 1175 public void _refresh(CommandInterpreter intp) throws Exception { 1176 org.osgi.framework.ServiceReference packageAdminRef = context.getServiceReference("org.osgi.service.packageadmin.PackageAdmin"); if (packageAdminRef != null) { 1178 org.osgi.service.packageadmin.PackageAdmin packageAdmin = (org.osgi.service.packageadmin.PackageAdmin) context.getService(packageAdminRef); 1179 if (packageAdmin != null) { 1180 try { 1181 AbstractBundle[] refresh = null; 1182 1183 String token = intp.nextArgument(); 1184 if (token != null) { 1185 Vector bundles = new Vector(); 1186 1187 while (token != null) { 1188 AbstractBundle bundle = getBundleFromToken(intp, token, true); 1189 1190 if (bundle != null) { 1191 bundles.addElement(bundle); 1192 } 1193 token = intp.nextArgument(); 1194 } 1195 1196 int size = bundles.size(); 1197 1198 if (size == 0) { 1199 intp.println(ConsoleMsg.CONSOLE_INVALID_BUNDLE_SPECIFICATION_ERROR); 1200 return; 1201 } 1202 1203 refresh = new AbstractBundle[size]; 1204 bundles.copyInto(refresh); 1205 } 1206 1207 packageAdmin.refreshPackages(refresh); 1208 } finally { 1209 context.ungetService(packageAdminRef); 1210 } 1211 } 1212 } else { 1213 intp.println(ConsoleMsg.CONSOLE_CAN_NOT_REFRESH_NO_PACKAGE_ADMIN_ERROR); 1214 } 1215 } 1216 1217 1223 public void _exec(CommandInterpreter intp) throws Exception { 1224 String command = intp.nextArgument(); 1225 if (command == null) { 1226 intp.println(ConsoleMsg.CONSOLE_NO_COMMAND_SPECIFIED_ERROR); 1227 return; 1228 } 1229 1230 Process p = Runtime.getRuntime().exec(command); 1231 1232 intp.println(NLS.bind(ConsoleMsg.CONSOLE_STARTED_IN_MESSAGE, command, String.valueOf(p))); 1233 int result = p.waitFor(); 1234 intp.println(NLS.bind(ConsoleMsg.CONSOLE_EXECUTED_RESULT_CODE_MESSAGE, command, String.valueOf(result))); 1235 } 1236 1237 1243 public void _fork(CommandInterpreter intp) throws Exception { 1244 String command = intp.nextArgument(); 1245 if (command == null) { 1246 intp.println(ConsoleMsg.CONSOLE_NO_COMMAND_SPECIFIED_ERROR); 1247 return; 1248 } 1249 1250 Process p = Runtime.getRuntime().exec(command); 1251 intp.println(NLS.bind(ConsoleMsg.CONSOLE_STARTED_IN_MESSAGE, command, String.valueOf(p))); 1252 } 1253 1254 1259 public void _h(CommandInterpreter intp) throws Exception { 1260 _headers(intp); 1261 } 1262 1263 1268 public void _headers(CommandInterpreter intp) throws Exception { 1269 1270 String nextArg = intp.nextArgument(); 1271 if (nextArg == null) { 1272 intp.println(ConsoleMsg.CONSOLE_NO_BUNDLE_SPECIFIED_ERROR); 1273 } 1274 while (nextArg != null) { 1275 AbstractBundle bundle = getBundleFromToken(intp, nextArg, true); 1276 if (bundle != null) { 1277 intp.printDictionary(bundle.getHeaders(), ConsoleMsg.CONSOLE_BUNDLE_HEADERS_TITLE); 1278 } 1279 nextArg = intp.nextArgument(); 1280 } 1281 } 1282 1283 1288 public void _pr(CommandInterpreter intp) throws Exception { 1289 _props(intp); 1290 } 1291 1292 1297 public void _props(CommandInterpreter intp) throws Exception { 1298 intp.printDictionary(FrameworkProperties.getProperties(), ConsoleMsg.CONSOLE_SYSTEM_PROPERTIES_TITLE); 1299 } 1300 1301 1306 public void _setp(CommandInterpreter intp) throws Exception { 1307 _setprop(intp); 1308 } 1309 1310 1315 public void _setprop(CommandInterpreter intp) throws Exception { 1316 String argument = intp.nextArgument(); 1317 if (argument == null) { 1318 intp.println(ConsoleMsg.CONSOLE_NO_PARAMETERS_SPECIFIED_TITLE); 1319 _props(intp); 1320 } else { 1321 InputStream in = new ByteArrayInputStream(argument.getBytes()); 1322 try { 1323 Properties sysprops = FrameworkProperties.getProperties(); 1324 Properties newprops = new Properties(); 1325 newprops.load(in); 1326 intp.println(ConsoleMsg.CONSOLE_SETTING_PROPERTIES_TITLE); 1327 Enumeration keys = newprops.propertyNames(); 1328 while (keys.hasMoreElements()) { 1329 String key = (String ) keys.nextElement(); 1330 String value = (String ) newprops.get(key); 1331 sysprops.put(key, value); 1332 intp.println(tab + key + " = " + value); } 1334 } catch (IOException e) { 1335 } finally { 1337 try { 1338 in.close(); 1339 } catch (IOException e) { 1340 } 1342 } 1343 } 1344 } 1345 1346 1352 public void _ss(CommandInterpreter intp) throws Exception { 1353 if (osgi.isActive()) { 1354 intp.println(); 1355 intp.println(ConsoleMsg.CONSOLE_FRAMEWORK_IS_LAUNCHED_MESSAGE); 1356 } else { 1357 intp.println(); 1358 intp.println(ConsoleMsg.CONSOLE_FRAMEWORK_IS_SHUTDOWN_MESSAGE); 1359 } 1360 1361 Object [] options = processOption(intp); 1362 if (options == null) 1363 return; 1364 1365 AbstractBundle[] bundles = (AbstractBundle[]) context.getBundles(); 1366 if (bundles.length == 0) { 1367 intp.println(ConsoleMsg.CONSOLE_NO_INSTALLED_BUNDLES_ERROR); 1368 } else { 1369 intp.print(newline); 1370 intp.print(ConsoleMsg.CONSOLE_ID); 1371 intp.print(tab); 1372 intp.println(ConsoleMsg.CONSOLE_STATE_BUNDLE_TITLE); 1373 for (int i = 0; i < bundles.length; i++) { 1374 AbstractBundle b = bundles[i]; 1375 if (!match(b, (String ) options[0], ((Integer ) options[1]).intValue())) 1376 continue; 1377 String label = b.getSymbolicName(); 1378 if (label == null || label.length() == 0) 1379 label = b.toString(); 1380 else 1381 label = label + "_" + b.getVersion(); intp.println(b.getBundleId() + "\t" + getStateName(b) + label); if (b.isFragment()) { 1384 BundleLoaderProxy[] hosts = b.getHosts(); 1385 if (hosts != null) 1386 for (int j = 0; j < hosts.length; j++) 1387 intp.println("\t Master=" + hosts[j].getBundleHost().getBundleId()); } else { 1389 org.osgi.framework.Bundle fragments[] = b.getFragments(); 1390 if (fragments != null) { 1391 intp.print("\t Fragments="); for (int f = 0; f < fragments.length; f++) { 1393 AbstractBundle fragment = (AbstractBundle) fragments[f]; 1394 intp.print((f > 0 ? ", " : "") + fragment.getBundleId()); } 1396 intp.println(); 1397 } 1398 } 1399 } 1400 } 1401 } 1402 1403 private boolean match(Bundle toFilter, String searchedName, int searchedState) { 1404 if ((toFilter.getState() & searchedState) == 0) { 1405 return false; 1406 } 1407 if (searchedName != null && toFilter.getSymbolicName() != null && toFilter.getSymbolicName().indexOf(searchedName) == -1) { 1408 return false; 1409 } 1410 return true; 1411 } 1412 1413 1418 public void _t(CommandInterpreter intp) throws Exception { 1419 _threads(intp); 1420 } 1421 1422 1429 public void _threads(CommandInterpreter intp) throws Exception { 1430 1431 ThreadGroup [] threadGroups = getThreadGroups(); 1432 Util.sort(threadGroups); 1433 1434 ThreadGroup tg = getTopThreadGroup(); 1435 Thread [] threads = new Thread [tg.activeCount()]; 1436 int count = tg.enumerate(threads, true); 1437 Util.sort(threads); 1438 1439 StringBuffer sb = new StringBuffer (120); 1440 intp.println(); 1441 intp.println(ConsoleMsg.CONSOLE_THREADGROUP_TITLE); 1442 for (int i = 0; i < threadGroups.length; i++) { 1443 tg = threadGroups[i]; 1444 int all = tg.activeCount(); int local = tg.enumerate(new Thread [all], false); ThreadGroup p = tg.getParent(); 1447 String parent = (p == null) ? "-none-" : p.getName(); sb.setLength(0); 1449 sb.append(Util.toString(simpleClassName(tg), 18)).append(" ").append(Util.toString(tg.getName(), 21)).append(" ").append(Util.toString(parent, 16)).append(Util.toString(new Integer (tg.getMaxPriority()), 3)).append(Util.toString(new Integer (local), 4)).append("/").append(Util.toString(String.valueOf(all), 6)); intp.println(sb.toString()); 1451 } 1452 intp.print(newline); 1453 intp.println(ConsoleMsg.CONSOLE_THREADTYPE_TITLE); 1454 for (int j = 0; j < count; j++) { 1455 Thread t = threads[j]; 1456 if (t != null) { 1457 sb.setLength(0); 1458 sb.append(Util.toString(simpleClassName(t), 18)).append(" ").append(Util.toString(t.getName(), 21)).append(" ").append(Util.toString(t.getThreadGroup().getName(), 16)).append(Util.toString(new Integer (t.getPriority()), 3)); if (t.isDaemon()) 1460 sb.append(" [daemon]"); intp.println(sb.toString()); 1462 } 1463 } 1464 } 1465 1466 1471 public void _sl(CommandInterpreter intp) throws Exception { 1472 if (isStartLevelSvcPresent(intp)) { 1473 org.osgi.framework.Bundle bundle = null; 1474 String token = intp.nextArgument(); 1475 int value = 0; 1476 if (token != null) { 1477 bundle = getBundleFromToken(intp, token, true); 1478 if (bundle == null) { 1479 return; 1480 } 1481 } 1482 if (bundle == null) { value = slImpl.getStartLevel(); 1484 intp.println(NLS.bind(ConsoleMsg.STARTLEVEL_FRAMEWORK_ACTIVE_STARTLEVEL, String.valueOf(value))); 1485 } else { value = slImpl.getBundleStartLevel(bundle); 1487 intp.println(NLS.bind(ConsoleMsg.STARTLEVEL_BUNDLE_STARTLEVEL, new Long (bundle.getBundleId()), new Integer (value))); 1488 } 1489 } 1490 } 1491 1492 1497 public void _setfwsl(CommandInterpreter intp) throws Exception { 1498 if (isStartLevelSvcPresent(intp)) { 1499 int value = 0; 1500 String token = intp.nextArgument(); 1501 if (token == null) { 1502 intp.println(ConsoleMsg.STARTLEVEL_NO_STARTLEVEL_GIVEN); 1503 value = slImpl.getStartLevel(); 1504 intp.println(NLS.bind(ConsoleMsg.STARTLEVEL_FRAMEWORK_ACTIVE_STARTLEVEL, String.valueOf(value))); 1505 } else { 1506 value = this.getStartLevelFromToken(intp, token); 1507 if (value > 0) { 1508 try { 1509 slImpl.setStartLevel(value); 1510 intp.println(NLS.bind(ConsoleMsg.STARTLEVEL_FRAMEWORK_ACTIVE_STARTLEVEL, String.valueOf(value))); 1511 } catch (IllegalArgumentException e) { 1512 intp.println(e.getMessage()); 1513 } 1514 } 1515 } 1516 } 1517 } 1518 1519 1524 public void _setbsl(CommandInterpreter intp) throws Exception { 1525 if (isStartLevelSvcPresent(intp)) { 1526 String token; 1527 AbstractBundle bundle = null; 1528 token = intp.nextArgument(); 1529 if (token == null) { 1530 intp.println(ConsoleMsg.STARTLEVEL_NO_STARTLEVEL_OR_BUNDLE_GIVEN); 1531 return; 1532 } 1533 1534 int newSL = this.getStartLevelFromToken(intp, token); 1535 1536 token = intp.nextArgument(); 1537 if (token == null) { 1538 intp.println(ConsoleMsg.STARTLEVEL_NO_STARTLEVEL_OR_BUNDLE_GIVEN); 1539 return; 1540 } 1541 while (token != null) { 1542 bundle = getBundleFromToken(intp, token, true); 1543 if (bundle != null) { 1544 try { 1545 slImpl.setBundleStartLevel(bundle, newSL); 1546 intp.println(NLS.bind(ConsoleMsg.STARTLEVEL_BUNDLE_STARTLEVEL, new Long (bundle.getBundleId()), new Integer (newSL))); 1547 } catch (IllegalArgumentException e) { 1548 intp.println(e.getMessage()); 1549 } 1550 } 1551 token = intp.nextArgument(); 1552 } 1553 } 1554 } 1555 1556 1561 public void _setibsl(CommandInterpreter intp) throws Exception { 1562 if (isStartLevelSvcPresent(intp)) { 1563 int value = 0; 1564 String token = intp.nextArgument(); 1565 if (token == null) { 1566 intp.println(ConsoleMsg.STARTLEVEL_NO_STARTLEVEL_GIVEN); 1567 value = slImpl.getInitialBundleStartLevel(); 1568 intp.println(NLS.bind(ConsoleMsg.STARTLEVEL_INITIAL_BUNDLE_STARTLEVEL, String.valueOf(value))); 1569 } else { 1570 value = this.getStartLevelFromToken(intp, token); 1571 if (value > 0) { 1572 try { 1573 slImpl.setInitialBundleStartLevel(value); 1574 intp.println(NLS.bind(ConsoleMsg.STARTLEVEL_INITIAL_BUNDLE_STARTLEVEL, String.valueOf(value))); 1575 } catch (IllegalArgumentException e) { 1576 intp.println(e.getMessage()); 1577 } 1578 } 1579 } 1580 } 1581 } 1582 1583 public void _requiredBundles(CommandInterpreter intp) { 1584 _classSpaces(intp); 1585 } 1586 1587 public void _classSpaces(CommandInterpreter intp) { 1588 1589 String token = intp.nextArgument(); 1590 1591 org.osgi.framework.ServiceReference packageAdminRef = context.getServiceReference("org.osgi.service.packageadmin.PackageAdmin"); if (packageAdminRef != null) { 1593 org.osgi.service.packageadmin.PackageAdmin packageAdmin = (org.osgi.service.packageadmin.PackageAdmin) context.getService(packageAdminRef); 1594 if (packageAdmin != null) { 1595 try { 1596 org.osgi.service.packageadmin.RequiredBundle[] symBundles = null; 1597 1598 symBundles = packageAdmin.getRequiredBundles(token); 1599 1600 if (symBundles == null) { 1601 intp.println(ConsoleMsg.CONSOLE_NO_NAMED_CLASS_SPACES_MESSAGE); 1602 } else { 1603 for (int i = 0; i < symBundles.length; i++) { 1604 org.osgi.service.packageadmin.RequiredBundle symBundle = symBundles[i]; 1605 intp.print(symBundle); 1606 1607 boolean removalPending = symBundle.isRemovalPending(); 1608 if (removalPending) { 1609 intp.print("("); intp.print(ConsoleMsg.CONSOLE_REMOVAL_PENDING_MESSAGE); 1611 intp.println(")"); } 1613 1614 org.osgi.framework.Bundle provider = symBundle.getBundle(); 1615 if (provider != null) { 1616 intp.print("<"); intp.print(provider); 1618 intp.println(">"); 1620 org.osgi.framework.Bundle[] requiring = symBundle.getRequiringBundles(); 1621 if (requiring != null) 1622 for (int j = 0; j < requiring.length; j++) { 1623 intp.print(" "); intp.print(requiring[j]); 1625 intp.print(" "); intp.println(ConsoleMsg.CONSOLE_REQUIRES_MESSAGE); 1627 } 1628 } else { 1629 intp.print("<"); intp.print(ConsoleMsg.CONSOLE_STALE_MESSAGE); 1631 intp.println(">"); } 1633 1634 } 1635 } 1636 } finally { 1637 context.ungetService(packageAdminRef); 1638 } 1639 } 1640 } else { 1641 intp.println(ConsoleMsg.CONSOLE_NO_EXPORTED_PACKAGES_NO_PACKAGE_ADMIN_MESSAGE); 1642 } 1643 } 1644 1645 1650 public void _profilelog(CommandInterpreter intp) throws Exception { 1651 intp.println(Profile.getProfileLog()); 1652 } 1653 1654 public void _getPackages(CommandInterpreter intp) { 1655 1656 String nextArg = intp.nextArgument(); 1657 if (nextArg == null) 1658 return; 1659 AbstractBundle bundle = getBundleFromToken(intp, nextArg, true); 1660 ServiceReference ref = context.getServiceReference("org.eclipse.osgi.service.resolver.PlatformAdmin"); if (ref == null) 1662 return; 1663 PlatformAdmin platformAdmin = (PlatformAdmin) context.getService(ref); 1664 try { 1665 ExportPackageDescription[] exports = platformAdmin.getStateHelper().getVisiblePackages(bundle.getBundleDescription(), StateHelper.VISIBLE_INCLUDE_EE_PACKAGES); 1666 for (int i = 0; i < exports.length; i++) { 1667 intp.println(exports[i] + ": " + platformAdmin.getStateHelper().getAccessCode(bundle.getBundleDescription(), exports[i])); } 1669 } finally { 1670 context.ungetService(ref); 1671 } 1672 } 1673 1674 1679 protected boolean isStartLevelSvcPresent(CommandInterpreter intp) { 1680 boolean retval = false; 1681 org.osgi.framework.ServiceReference slSvcRef = context.getServiceReference("org.osgi.service.startlevel.StartLevel"); if (slSvcRef != null) { 1683 org.osgi.service.startlevel.StartLevel slSvc = (org.osgi.service.startlevel.StartLevel) context.getService(slSvcRef); 1684 if (slSvc != null) { 1685 retval = true; 1686 } 1687 } else { 1688 intp.println(ConsoleMsg.CONSOLE_CAN_NOT_USE_STARTLEVEL_NO_STARTLEVEL_SVC_ERROR); 1689 } 1690 return retval; 1691 } 1692 1693 1703 protected AbstractBundle getBundleFromToken(CommandInterpreter intp, String token, boolean error) { 1704 AbstractBundle bundle = null; 1705 try { 1706 long id = Long.parseLong(token); 1707 bundle = (AbstractBundle) context.getBundle(id); 1708 } catch (NumberFormatException nfe) { 1709 1710 String symbolicName = token; 1712 Version version = null; 1713 1714 int ix = token.indexOf("@"); if (ix != -1) { 1718 if ((ix + 1) != token.length()) { 1719 try { 1720 version = Version.parseVersion(token.substring(ix + 1, token.length())); 1722 symbolicName = token.substring(0, ix); 1723 } catch (IllegalArgumentException e) { 1724 } 1726 } 1727 } 1728 1729 Bundle[] bundles = context.getBundles(); 1730 for (int i = 0, n = bundles.length; i < n; i++) { 1731 AbstractBundle b = (AbstractBundle) bundles[i]; 1732 if ((symbolicName.equals(b.getSymbolicName()) && (version == null || version.equals(b.getVersion()))) || token.equals(b.getLocation())) { 1735 bundle = b; 1736 break; 1737 } 1738 } 1739 } 1740 1741 if ((bundle == null) && error) { 1742 intp.println(NLS.bind(ConsoleMsg.CONSOLE_CANNOT_FIND_BUNDLE_ERROR, token)); 1743 } 1744 1745 return (bundle); 1746 } 1747 1748 1755 protected int getStartLevelFromToken(CommandInterpreter intp, String value) { 1756 int retval = -1; 1757 try { 1758 retval = Integer.parseInt(value); 1759 if (Integer.parseInt(value) <= 0) { 1760 intp.println(ConsoleMsg.STARTLEVEL_POSITIVE_INTEGER); 1761 } 1762 } catch (NumberFormatException nfe) { 1763 intp.println(ConsoleMsg.STARTLEVEL_POSITIVE_INTEGER); 1764 } 1765 return retval; 1766 } 1767 1768 1774 protected String getStateName(Bundle bundle) { 1775 int state = bundle.getState(); 1776 switch (state) { 1777 case Bundle.UNINSTALLED : 1778 return "UNINSTALLED "; 1780 case Bundle.INSTALLED : 1781 return "INSTALLED "; 1783 case Bundle.RESOLVED : 1784 return "RESOLVED "; 1786 case Bundle.STARTING : 1787 synchronized (lazyActivation) { 1788 if (lazyActivation.contains(bundle)) { 1789 return "<<LAZY>> "; } 1791 return "STARTING "; } 1793 1794 case Bundle.STOPPING : 1795 return "STOPPING "; 1797 case Bundle.ACTIVE : 1798 return "ACTIVE "; 1800 default : 1801 return Integer.toHexString(state); 1802 } 1803 } 1804 1805 1810 protected ThreadGroup [] getThreadGroups() { 1811 ThreadGroup tg = getTopThreadGroup(); 1812 ThreadGroup [] groups = new ThreadGroup [tg.activeGroupCount()]; 1813 int count = tg.enumerate(groups, true); 1814 if (count == groups.length) { 1815 return groups; 1816 } 1817 ThreadGroup [] ngroups = new ThreadGroup [count]; 1819 System.arraycopy(groups, 0, ngroups, 0, count); 1820 return ngroups; 1821 } 1822 1823 1831 protected ThreadGroup getTopThreadGroup() { 1832 ThreadGroup topGroup = Thread.currentThread().getThreadGroup(); 1833 if (topGroup != null) { 1834 while (topGroup.getParent() != null) { 1835 topGroup = topGroup.getParent(); 1836 } 1837 } 1838 return topGroup; 1839 } 1840 1841 1847 public String simpleClassName(Object o) { 1848 java.util.StringTokenizer t = new java.util.StringTokenizer (o.getClass().getName(), "."); int ct = t.countTokens(); 1850 for (int i = 1; i < ct; i++) { 1851 t.nextToken(); 1852 } 1853 return t.nextToken(); 1854 } 1855 1856 public void _getprop(CommandInterpreter ci) throws Exception { 1857 Properties allProperties = FrameworkProperties.getProperties(); 1858 String filter = ci.nextArgument(); 1859 Enumeration propertyNames = allProperties.keys(); 1860 while (propertyNames.hasMoreElements()) { 1861 String prop = (String ) propertyNames.nextElement(); 1862 if (filter == null || prop.startsWith(filter)) { 1863 ci.println(prop + '=' + allProperties.getProperty(prop)); 1864 } 1865 } 1866 } 1867 1868 1871 public void bundleChanged(BundleEvent event) { 1872 int type = event.getType(); 1873 Bundle bundle = event.getBundle(); 1874 synchronized (lazyActivation) { 1875 switch (type) { 1876 case BundleEvent.LAZY_ACTIVATION: 1877 if (!lazyActivation.contains(bundle)) { 1878 lazyActivation.add(bundle); 1879 } 1880 break; 1881 1882 default: 1883 lazyActivation.remove(bundle); 1884 break; 1885 } 1886 } 1887 1888 } 1889} 1890 | Popular Tags |