Example #1
0
 @Override
 public void undeployBundle(String bundleName) throws BundleException {
   if (bundleName == null) {
     // ignore
     return;
   }
   log.info(
       String.format(
           "Before undeploy bundle with name '%s'.\n" + "%s", bundleName, getRuntimeStatus()));
   BundleContext ctx = getBundleContext();
   ServiceReference ref = ctx.getServiceReference(PackageAdmin.class.getName());
   PackageAdmin srv = (PackageAdmin) ctx.getService(ref);
   try {
     for (Bundle b : srv.getBundles(bundleName, null)) {
       if (b != null && b.getState() == Bundle.ACTIVE) {
         Transaction tx = TransactionHelper.suspendTransaction();
         try {
           b.stop();
           b.uninstall();
         } finally {
           TransactionHelper.resumeTransaction(tx);
         }
       }
     }
   } finally {
     ctx.ungetService(ref);
   }
   log.info(String.format("Undeploy done.\n" + "%s", getRuntimeStatus()));
 }
Example #2
0
  @Override
  public String deployBundle(File file, boolean reloadResourceClasspath) throws BundleException {
    String name = getOSGIBundleName(file);
    if (name == null) {
      log.error(
          String.format(
              "No Bundle-SymbolicName found in MANIFEST for jar at '%s'", file.getAbsolutePath()));
      return null;
    }

    String path = file.getAbsolutePath();

    log.info(
        String.format("Before deploy bundle for file at '%s'\n" + "%s", path, getRuntimeStatus()));

    if (reloadResourceClasspath) {
      URL url;
      try {
        url = new File(path).toURI().toURL();
      } catch (MalformedURLException e) {
        throw new RuntimeException(e);
      }
      Framework.reloadResourceLoader(Arrays.asList(url), null);
    }

    // check if this is a bundle first
    Bundle newBundle = getBundleContext().installBundle(path);
    if (newBundle == null) {
      throw new IllegalArgumentException("Could not find a valid bundle at path: " + path);
    }
    Transaction tx = TransactionHelper.suspendTransaction();
    try {
      newBundle.start();
    } finally {
      TransactionHelper.resumeTransaction(tx);
    }

    log.info(
        String.format(
            "Deploy done for bundle with name '%s'.\n" + "%s",
            newBundle.getSymbolicName(), getRuntimeStatus()));

    return newBundle.getSymbolicName();
  }