@Test public void testAttachedFragmentEar() throws Exception { ModelControllerClient client = FrameworkUtils.waitForService(context, ModelControllerClient.class); ServerDeploymentHelper server = new ServerDeploymentHelper(client); // Deploy the fragment InputStream input = deployer.getDeployment(GOOD_FRAGMENT_EAR); String earName = server.deploy(GOOD_FRAGMENT_EAR, input); // Find the deployed fragment Bundle fragment = packageAdmin.getBundles(GOOD_FRAGMENT, null)[0]; Assert.assertEquals("Bundle RESOLVED", Bundle.RESOLVED, fragment.getState()); // Find the deployed bundle Bundle host = packageAdmin.getBundles(GOOD_BUNDLE, null)[0]; Assert.assertEquals("Bundle ACTIVE", Bundle.ACTIVE, host.getState()); Class<?> clazz = host.loadClass("org.jboss.as.test.integration.osgi.deployment.bundle.AttachedType"); Assert.assertNotNull("Class not null", clazz); Assert.assertSame(host, ((BundleReference) clazz.getClassLoader()).getBundle()); server.undeploy(earName); Assert.assertEquals("Bundle UNINSTALLED", Bundle.UNINSTALLED, fragment.getState()); Assert.assertEquals("Bundle UNINSTALLED", Bundle.UNINSTALLED, host.getState()); }
@Test public void testUnattachedFragment() throws Exception { ModelControllerClient client = FrameworkUtils.waitForService(context, ModelControllerClient.class); ServerDeploymentHelper server = new ServerDeploymentHelper(client); // Deploy the bundle InputStream input = deployer.getDeployment(GOOD_BUNDLE); String hostName = server.deploy("bundle-host", input); // Find the deployed bundle Bundle host = packageAdmin.getBundles(GOOD_BUNDLE, null)[0]; Assert.assertEquals("Bundle ACTIVE", Bundle.ACTIVE, host.getState()); // Deploy the fragment input = deployer.getDeployment(GOOD_FRAGMENT); String fragmentName = server.deploy("bundle-fragment", input); // Find the deployed bundle Bundle fragment = packageAdmin.getBundles(GOOD_FRAGMENT, null)[0]; Assert.assertEquals("Bundle INSTALLED", Bundle.INSTALLED, fragment.getState()); try { host.loadClass("org.jboss.as.test.integration.osgi.deployment.bundle.AttachedType"); Assert.fail("ClassNotFoundException expected"); } catch (ClassNotFoundException ex) { // expected } server.undeploy(fragmentName); Assert.assertEquals("Bundle UNINSTALLED", Bundle.UNINSTALLED, fragment.getState()); server.undeploy(hostName); Assert.assertEquals("Bundle UNINSTALLED", Bundle.UNINSTALLED, host.getState()); }
@Test public void testRedeployAfterUndeploy() throws Exception { ModelControllerClient client = FrameworkUtils.waitForService(context, ModelControllerClient.class); ServerDeploymentHelper server = new ServerDeploymentHelper(client); // Deploy the bundle InputStream input = deployer.getDeployment(GOOD_BUNDLE); String runtimeName = server.deploy("redeploy", input); // Find the deployed bundle Bundle bundle = packageAdmin.getBundles(GOOD_BUNDLE, null)[0]; Assert.assertEquals("Bundle ACTIVE", Bundle.ACTIVE, bundle.getState()); server.undeploy(runtimeName); Assert.assertEquals("Bundle UNINSTALLED", Bundle.UNINSTALLED, bundle.getState()); // Redeploy the same bundle input = deployer.getDeployment(GOOD_BUNDLE); runtimeName = server.deploy("redeploy", input); // Find the deployed bundle bundle = packageAdmin.getBundles(GOOD_BUNDLE, null)[0]; Assert.assertEquals("Bundle ACTIVE", Bundle.ACTIVE, bundle.getState()); server.undeploy(runtimeName); Assert.assertEquals("Bundle UNINSTALLED", Bundle.UNINSTALLED, bundle.getState()); }
@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())); }
private static Bundle getBundle(PackageAdmin packageAdmin, String symbolicName) { Bundle[] bundles = packageAdmin.getBundles(symbolicName, null); if (bundles == null) return null; for (int i = 0; i < bundles.length; i++) { Bundle bundle = bundles[i]; if ((bundle.getState() & (Bundle.INSTALLED | Bundle.UNINSTALLED)) == 0) return bundle; } return null; }
public static Bundle getBundle(String symbolicName) { if (packageAdmin == null) return null; Bundle[] bundles = packageAdmin.getBundles(symbolicName, null); if (bundles == null) return null; // Return the first bundle that is not installed or uninstalled for (int i = 0; i < bundles.length; i++) { if ((bundles[i].getState() & (Bundle.INSTALLED | Bundle.UNINSTALLED)) == 0) { return bundles[i]; } } return null; }
/** * Returns the bundles with the given name. * * @param bundleName The bundle name. * @return The bundles with the given name. */ public static Bundle[] getBundles(String bundleName) { Bundle[] bundle = null; BundleContext context = AcceleoCommonPlugin.getDefault().getContext(); ServiceReference packageAdminReference = context.getServiceReference(PackageAdmin.class.getName()); PackageAdmin packageAdmin = null; if (packageAdminReference != null) { packageAdmin = (PackageAdmin) context.getService(packageAdminReference); } if (packageAdmin != null) { bundle = packageAdmin.getBundles(bundleName, null); } if (packageAdminReference != null) { context.ungetService(packageAdminReference); } return bundle; }
protected String getPathsToRequiredBundles(String requireTldBundles) throws Exception { if (requireTldBundles == null) return null; ServiceReference ref = _bundle .getBundleContext() .getServiceReference(org.osgi.service.packageadmin.PackageAdmin.class.getName()); PackageAdmin packageAdmin = (ref == null) ? null : (PackageAdmin) _bundle.getBundleContext().getService(ref); if (packageAdmin == null) throw new IllegalStateException( "Unable to get PackageAdmin reference to locate required Tld bundles"); StringBuilder paths = new StringBuilder(); String[] symbNames = requireTldBundles.split(", "); for (String symbName : symbNames) { Bundle[] bs = packageAdmin.getBundles(symbName, null); if (bs == null || bs.length == 0) { throw new IllegalArgumentException( "Unable to locate the bundle '" + symbName + "' specified by " + OSGiWebappConstants.REQUIRE_TLD_BUNDLE + " in manifest of " + (_bundle == null ? "unknown" : _bundle.getSymbolicName())); } File f = BundleFileLocatorHelperFactory.getFactory().getHelper().getBundleInstallLocation(bs[0]); if (paths.length() > 0) paths.append(", "); paths.append(f.toURI().toURL().toString()); LOG.debug( "getPathsToRequiredBundles: bundle path=" + bs[0].getLocation() + " uri=" + f.toURI()); } return paths.toString(); }