Search in sources :

Example 26 with FrameworkWiring

use of org.osgi.framework.wiring.FrameworkWiring in project opennms by OpenNMS.

the class Activator method start.

@Override
public void start(BundleContext context) throws Exception {
    // Determine if any vaadin theme fragments are unresoled
    final Set<Bundle> unresolvedVaadinThemeFragments = getUnresolvedVaadinThemeFragments(context);
    if (!unresolvedVaadinThemeFragments.isEmpty()) {
        // Get vaadin theme host bundle to initialize refresh
        final Bundle vaadinThemeHostBundle = getVaadinThemeHostbundle(context);
        final List<Bundle> bundlesToRefresh = new ArrayList<>();
        bundlesToRefresh.add(vaadinThemeHostBundle);
        // Refresh
        final Bundle systemBundle = context.getBundle(0);
        FrameworkWiring frameworkWiring = systemBundle.adapt(FrameworkWiring.class);
        frameworkWiring.refreshBundles(bundlesToRefresh);
    }
}
Also used : Bundle(org.osgi.framework.Bundle) ArrayList(java.util.ArrayList) FrameworkWiring(org.osgi.framework.wiring.FrameworkWiring)

Example 27 with FrameworkWiring

use of org.osgi.framework.wiring.FrameworkWiring in project fabric8 by jboss-fuse.

the class ServiceImpl method applyChanges.

private void applyChanges(Map<Bundle, String> toUpdate) throws BundleException, IOException {
    List<Bundle> toStop = new ArrayList<Bundle>();
    Map<Bundle, String> lessToUpdate = new HashMap<>();
    for (Bundle b : toUpdate.keySet()) {
        if (b.getState() != Bundle.UNINSTALLED) {
            toStop.add(b);
            lessToUpdate.put(b, toUpdate.get(b));
        }
    }
    while (!toStop.isEmpty()) {
        List<Bundle> bs = getBundlesToDestroy(toStop);
        for (Bundle bundle : bs) {
            String hostHeader = bundle.getHeaders().get(Constants.FRAGMENT_HOST);
            if (hostHeader == null && (bundle.getState() == Bundle.ACTIVE || bundle.getState() == Bundle.STARTING)) {
                if (!"org.ops4j.pax.url.mvn".equals(bundle.getSymbolicName())) {
                    bundle.stop();
                }
            }
            toStop.remove(bundle);
        }
    }
    // eagerly load some classes
    try {
        getClass().getClassLoader().loadClass(Parser.class.getName());
        getClass().getClassLoader().loadClass(Clause.class.getName());
        getClass().getClassLoader().loadClass(Attribute.class.getName());
        getClass().getClassLoader().loadClass(Directive.class.getName());
        getClass().getClassLoader().loadClass(RefreshListener.class.getName());
    } catch (Exception ignored) {
    }
    Set<Bundle> toRefresh = new HashSet<Bundle>();
    Set<Bundle> toStart = new HashSet<Bundle>();
    for (Map.Entry<Bundle, String> e : lessToUpdate.entrySet()) {
        Bundle bundle = e.getKey();
        if (!"org.ops4j.pax.url.mvn".equals(bundle.getSymbolicName())) {
            System.out.println("updating: " + bundle.getSymbolicName());
            try {
                BundleUtils.update(bundle, new URL(e.getValue()));
            } catch (BundleException ex) {
                System.err.println("Failed to update: " + bundle.getSymbolicName() + ", due to: " + e);
            }
            toStart.add(bundle);
            toRefresh.add(bundle);
        }
    }
    findBundlesWithOptionalPackagesToRefresh(toRefresh);
    findBundlesWithFragmentsToRefresh(toRefresh);
    if (!toRefresh.isEmpty()) {
        final CountDownLatch l = new CountDownLatch(1);
        FrameworkListener listener = new RefreshListener(l);
        FrameworkWiring wiring = bundleContext.getBundle(0).adapt(FrameworkWiring.class);
        wiring.refreshBundles(toRefresh, listener);
        try {
            l.await();
        } catch (InterruptedException e) {
            throw new PatchException("Bundle refresh interrupted", e);
        }
    }
    for (Bundle bundle : toStart) {
        String hostHeader = bundle.getHeaders().get(Constants.FRAGMENT_HOST);
        if (hostHeader == null) {
            try {
                bundle.start();
            } catch (BundleException e) {
                System.err.println("Failed to start: " + bundle.getSymbolicName() + ", due to: " + e);
            }
        }
    }
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Attribute(org.apache.felix.utils.manifest.Attribute) Bundle(org.osgi.framework.Bundle) ArrayList(java.util.ArrayList) FrameworkWiring(org.osgi.framework.wiring.FrameworkWiring) CountDownLatch(java.util.concurrent.CountDownLatch) URISyntaxException(java.net.URISyntaxException) PatchException(io.fabric8.patch.management.PatchException) BundleException(org.osgi.framework.BundleException) IOException(java.io.IOException) URL(java.net.URL) Parser(org.apache.felix.utils.manifest.Parser) Clause(org.apache.felix.utils.manifest.Clause) BundleException(org.osgi.framework.BundleException) PatchException(io.fabric8.patch.management.PatchException) FrameworkListener(org.osgi.framework.FrameworkListener) Directive(org.apache.felix.utils.manifest.Directive) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 28 with FrameworkWiring

use of org.osgi.framework.wiring.FrameworkWiring in project fabric8 by jboss-fuse.

the class ServiceImplTest method testPatchWithVersionRanges.

@Test
public void testPatchWithVersionRanges() throws Exception {
    ComponentContext componentContext = createMock(ComponentContext.class);
    BundleContext bundleContext = createMock(BundleContext.class);
    Bundle sysBundle = createMock(Bundle.class);
    BundleContext sysBundleContext = createMock(BundleContext.class);
    Bundle bundle = createMock(Bundle.class);
    Bundle bundle2 = createMock(Bundle.class);
    FrameworkWiring wiring = createMock(FrameworkWiring.class);
    GitPatchRepository repository = createMock(GitPatchRepository.class);
    // 
    // Create a new service, download a patch
    // 
    expect(componentContext.getBundleContext()).andReturn(bundleContext);
    expect(bundleContext.getBundle(0)).andReturn(sysBundle).anyTimes();
    expect(sysBundle.getBundleContext()).andReturn(sysBundleContext).anyTimes();
    expect(sysBundleContext.getProperty(Service.NEW_PATCH_LOCATION)).andReturn(storage.toString()).anyTimes();
    expect(repository.getManagedPatch(anyString())).andReturn(null).anyTimes();
    expect(repository.findOrCreateMainGitRepository()).andReturn(null).anyTimes();
    expect(sysBundleContext.getProperty("karaf.default.repository")).andReturn("system").anyTimes();
    expect(sysBundleContext.getProperty("karaf.home")).andReturn(karaf.getCanonicalPath()).anyTimes();
    expect(sysBundleContext.getProperty("karaf.base")).andReturn(karaf.getCanonicalPath()).anyTimes();
    expect(sysBundleContext.getProperty("karaf.name")).andReturn("root").anyTimes();
    expect(sysBundleContext.getProperty("karaf.instances")).andReturn(karaf.getCanonicalPath() + "/instances").anyTimes();
    expect(sysBundleContext.getProperty("karaf.data")).andReturn(karaf.getCanonicalPath() + "/data").anyTimes();
    replay(componentContext, sysBundleContext, sysBundle, bundleContext, bundle, repository);
    PatchManagement pm = mockManagementService(bundleContext);
    ((GitPatchManagementServiceImpl) pm).setGitPatchRepository(repository);
    ServiceImpl service = new ServiceImpl();
    setField(service, "patchManagement", pm);
    service.activate(componentContext);
    Iterable<Patch> patches = service.download(patch140.toURI().toURL());
    assertNotNull(patches);
    Iterator<Patch> it = patches.iterator();
    assertTrue(it.hasNext());
    Patch patch = it.next();
    assertNotNull(patch);
    assertEquals("patch-1.4.0", patch.getPatchData().getId());
    assertNotNull(patch.getPatchData().getBundles());
    assertEquals(1, patch.getPatchData().getBundles().size());
    Iterator<String> itb = patch.getPatchData().getBundles().iterator();
    assertEquals("mvn:foo/my-bsn/1.4.0", itb.next());
    assertNull(patch.getResult());
    verify(componentContext, sysBundleContext, sysBundle, bundleContext, bundle);
    // 
    // Simulate the patch
    // 
    reset(componentContext, sysBundleContext, sysBundle, bundleContext, bundle);
    expect(sysBundleContext.getBundles()).andReturn(new Bundle[] { bundle });
    expect(sysBundleContext.getServiceReference("io.fabric8.api.FabricService")).andReturn(null);
    expect(bundle.getSymbolicName()).andReturn("my-bsn").anyTimes();
    expect(bundle.getVersion()).andReturn(new Version("1.3.1")).anyTimes();
    expect(bundle.getLocation()).andReturn("location").anyTimes();
    expect(bundle.getBundleId()).andReturn(123L);
    BundleStartLevel bsl = createMock(BundleStartLevel.class);
    expect(bsl.getStartLevel()).andReturn(30).anyTimes();
    expect(bundle.adapt(BundleStartLevel.class)).andReturn(bsl).anyTimes();
    expect(bundle.getState()).andReturn(1);
    expect(sysBundleContext.getProperty("karaf.default.repository")).andReturn("system").anyTimes();
    replay(componentContext, sysBundleContext, sysBundle, bundleContext, bundle, bsl);
    PatchResult result = service.install(patch, true);
    assertNotNull(result);
    assertNull(patch.getResult());
    assertEquals(1, result.getBundleUpdates().size());
    assertTrue(result.isSimulation());
}
Also used : BundleStartLevel(org.osgi.framework.startlevel.BundleStartLevel) ComponentContext(org.osgi.service.component.ComponentContext) Bundle(org.osgi.framework.Bundle) GitPatchManagementServiceImpl(io.fabric8.patch.management.impl.GitPatchManagementServiceImpl) GitPatchRepository(io.fabric8.patch.management.impl.GitPatchRepository) FrameworkWiring(org.osgi.framework.wiring.FrameworkWiring) GitPatchManagementServiceImpl(io.fabric8.patch.management.impl.GitPatchManagementServiceImpl) Version(org.osgi.framework.Version) PatchManagement(io.fabric8.patch.management.PatchManagement) PatchResult(io.fabric8.patch.management.PatchResult) Patch(io.fabric8.patch.management.Patch) BundleContext(org.osgi.framework.BundleContext) Test(org.junit.Test)

Example 29 with FrameworkWiring

use of org.osgi.framework.wiring.FrameworkWiring in project fabric8 by jboss-fuse.

the class ServiceImplTest method testLoadWithoutRanges.

@Test
public void testLoadWithoutRanges() throws IOException, GitAPIException {
    BundleContext bundleContext = createMock(BundleContext.class);
    ComponentContext componentContext = createMock(ComponentContext.class);
    Bundle sysBundle = createMock(Bundle.class);
    BundleContext sysBundleContext = createMock(BundleContext.class);
    Bundle bundle = createMock(Bundle.class);
    Bundle bundle2 = createMock(Bundle.class);
    FrameworkWiring wiring = createMock(FrameworkWiring.class);
    GitPatchRepository repository = createMock(GitPatchRepository.class);
    // 
    // Create a new service, download a patch
    // 
    expect(componentContext.getBundleContext()).andReturn(bundleContext);
    expect(bundleContext.getBundle(0)).andReturn(sysBundle).anyTimes();
    expect(sysBundle.getBundleContext()).andReturn(sysBundleContext).anyTimes();
    expect(sysBundleContext.getProperty(Service.NEW_PATCH_LOCATION)).andReturn(storage.toString()).anyTimes();
    expect(repository.getManagedPatch(anyString())).andReturn(null).anyTimes();
    expect(repository.findOrCreateMainGitRepository()).andReturn(null).anyTimes();
    expect(sysBundleContext.getProperty("karaf.default.repository")).andReturn("system").anyTimes();
    expect(sysBundleContext.getProperty("karaf.home")).andReturn(karaf.getCanonicalPath()).anyTimes();
    expect(sysBundleContext.getProperty("karaf.base")).andReturn(karaf.getCanonicalPath()).anyTimes();
    expect(sysBundleContext.getProperty("karaf.name")).andReturn("root").anyTimes();
    expect(sysBundleContext.getProperty("karaf.instances")).andReturn(karaf.getCanonicalPath() + "/instances").anyTimes();
    expect(sysBundleContext.getProperty("karaf.data")).andReturn(karaf.getCanonicalPath() + "/data").anyTimes();
    replay(componentContext, sysBundleContext, sysBundle, bundleContext, bundle, repository);
    PatchManagement pm = new GitPatchManagementServiceImpl(bundleContext);
    ((GitPatchManagementServiceImpl) pm).setGitPatchRepository(repository);
    ServiceImpl service = new ServiceImpl();
    setField(service, "patchManagement", pm);
    service.activate(componentContext);
    PatchData pd = PatchData.load(getClass().getClassLoader().getResourceAsStream("test1.patch"));
    assertEquals(2, pd.getBundles().size());
    assertTrue(pd.getRequirements().isEmpty());
}
Also used : GitPatchManagementServiceImpl(io.fabric8.patch.management.impl.GitPatchManagementServiceImpl) PatchData(io.fabric8.patch.management.PatchData) ComponentContext(org.osgi.service.component.ComponentContext) Bundle(org.osgi.framework.Bundle) PatchManagement(io.fabric8.patch.management.PatchManagement) GitPatchManagementServiceImpl(io.fabric8.patch.management.impl.GitPatchManagementServiceImpl) GitPatchRepository(io.fabric8.patch.management.impl.GitPatchRepository) FrameworkWiring(org.osgi.framework.wiring.FrameworkWiring) BundleContext(org.osgi.framework.BundleContext) Test(org.junit.Test)

Example 30 with FrameworkWiring

use of org.osgi.framework.wiring.FrameworkWiring in project motech by motech.

the class MdsBundleWatcher method refreshBundles.

private void refreshBundles(List<Bundle> bundles, SchemaHolder schemaHolder) {
    if (LOGGER.isInfoEnabled()) {
        for (Bundle bundle : bundles) {
            LOGGER.info("Refreshing wiring for bundle {}", bundle.getSymbolicName());
        }
    }
    // we generate the entities bundle but not start it to avoid exceptions when the framework
    // will refresh bundles
    jarGeneratorService.regenerateMdsDataBundle(schemaHolder, false);
    FrameworkWiring framework = bundleContext.getBundle(0).adapt(FrameworkWiring.class);
    framework.refreshBundles(bundles);
    // give the framework 3 seconds to do a refresh
    ThreadSuspender.sleep(3000);
    // after refreshing all bundles we can start the entities bundle
    monitor.start();
}
Also used : Bundle(org.osgi.framework.Bundle) FrameworkWiring(org.osgi.framework.wiring.FrameworkWiring)

Aggregations

FrameworkWiring (org.osgi.framework.wiring.FrameworkWiring)39 Bundle (org.osgi.framework.Bundle)29 Test (org.junit.Test)12 URL (java.net.URL)10 FrameworkListener (org.osgi.framework.FrameworkListener)10 CountDownLatch (java.util.concurrent.CountDownLatch)8 IOException (java.io.IOException)7 ArrayList (java.util.ArrayList)7 BundleContext (org.osgi.framework.BundleContext)7 HashSet (java.util.HashSet)6 FrameworkEvent (org.osgi.framework.FrameworkEvent)6 Version (org.osgi.framework.Version)6 HashMap (java.util.HashMap)5 ComponentContext (org.osgi.service.component.ComponentContext)5 InputStream (java.io.InputStream)4 BundleStartLevel (org.osgi.framework.startlevel.BundleStartLevel)4 PatchManagement (io.fabric8.patch.management.PatchManagement)3 GitPatchManagementServiceImpl (io.fabric8.patch.management.impl.GitPatchManagementServiceImpl)3 GitPatchRepository (io.fabric8.patch.management.impl.GitPatchRepository)3 FileInputStream (java.io.FileInputStream)3