@Test public void testAttachedFragment() throws Exception { Bundle hostA = installBundle(getHostA()); assertBundleState(Bundle.INSTALLED, hostA.getState()); Bundle fragA = installBundle(getFragmentA()); assertBundleState(Bundle.INSTALLED, fragA.getState()); hostA.start(); assertBundleState(Bundle.ACTIVE, hostA.getState()); assertBundleState(Bundle.RESOLVED, fragA.getState()); URL entryURL = hostA.getEntry("resource.txt"); assertNull("Entry URL null", entryURL); URL resourceURL = hostA.getResource("resource.txt"); assertEquals("bundle", resourceURL.getProtocol()); assertEquals("/resource.txt", resourceURL.getPath()); BufferedReader br = new BufferedReader(new InputStreamReader(resourceURL.openStream())); assertEquals("fragA", br.readLine()); // Load class provided by the fragment assertLoadClass(hostA, FragBeanA.class.getName()); // Load a private class from host assertLoadClass(hostA, SubBeanA.class.getName()); // PackageAdmin.getBundleType PackageAdmin pa = getPackageAdmin(); assertEquals("Bundle type", 0, pa.getBundleType(hostA)); assertEquals("Bundle type", PackageAdmin.BUNDLE_TYPE_FRAGMENT, pa.getBundleType(fragA)); // PackageAdmin.getHosts Bundle[] hosts = pa.getHosts(hostA); assertNull("Not a fragment", hosts); hosts = pa.getHosts(fragA); assertNotNull("Hosts not null", hosts); assertEquals("Hosts length", 1, hosts.length); assertEquals("Hosts equals", hostA, hosts[0]); // PackageAdmin.getFragments Bundle[] fragments = pa.getFragments(fragA); assertNull("Not a host", fragments); fragments = pa.getFragments(hostA); assertNotNull("Fragments not null", fragments); assertEquals("Fragments length", 1, fragments.length); assertEquals("Fragments equals", fragA, fragments[0]); hostA.uninstall(); assertBundleState(Bundle.UNINSTALLED, hostA.getState()); assertBundleState(Bundle.RESOLVED, fragA.getState()); fragA.uninstall(); assertBundleState(Bundle.UNINSTALLED, fragA.getState()); }
/** On start, we do two things We register a listener for bundles and we start our JPA server */ public void start(BundleContext context) throws Exception { Activator.context = context; String initializer = null; ServiceReference packageAdminRef = context.getServiceReference("org.osgi.service.packageadmin.PackageAdmin"); PackageAdmin packageAdmin = (PackageAdmin) context.getService(packageAdminRef); Bundle[] fragments = packageAdmin.getFragments(context.getBundle()); if (fragments != null) { for (int i = 0; i < fragments.length; i++) { Bundle fragment = fragments[i]; initializer = (String) fragment.getHeaders().get("JPA-Initializer"); if (initializer != null) { AbstractSessionLog.getLog() .log( SessionLog.CONFIG, LoggingLocalization.buildMessage("osgi_initializer", new Object[] {initializer})); break; } } } osgiProvider = new PersistenceProvider(initializer); registerBundleListener(); }
public Bundle[] getFragments(Bundle bundle) { PackageAdmin packageAdmin = getPackageAdmin(); if (packageAdmin == null) return null; return packageAdmin.getFragments(bundle); }