Search in sources :

Example 6 with BundleListener

use of org.osgi.framework.BundleListener in project fuse-karaf by jboss-fuse.

the class GitPatchManagementServiceImpl method checkPendingPatches.

@Override
public void checkPendingPatches() {
    File[] pendingPatches = patchesDir.listFiles(pathname -> pathname.exists() && pathname.getName().endsWith(".pending"));
    if (pendingPatches == null || pendingPatches.length == 0) {
        return;
    }
    final String dataCache = systemContext.getProperty("org.osgi.framework.storage");
    for (File pending : pendingPatches) {
        try {
            Pending what = Pending.valueOf(FileUtils.readFileToString(pending, "UTF-8"));
            final String prefix = what == Pending.ROLLUP_INSTALLATION ? "install" : "rollback";
            String name = pending.getName().replaceFirst("\\.pending$", "");
            if (isStandaloneChild()) {
                if (name.endsWith("." + System.getProperty("karaf.name") + ".patch")) {
                    name = name.replaceFirst("\\." + System.getProperty("karaf.name"), "");
                } else {
                    continue;
                }
            }
            File patchFile = new File(pending.getParentFile(), name);
            if (!patchFile.isFile()) {
                Activator.log(LogService.LOG_INFO, "Ignoring patch result file: " + patchFile.getName());
                continue;
            }
            PatchData patchData = PatchData.load(new FileInputStream(patchFile));
            Patch patch = loadPatch(new PatchDetailsRequest(patchData.getId()));
            String dataFilesName = patchData.getId() + ".datafiles";
            if (isStandaloneChild()) {
                dataFilesName = patchData.getId() + "." + System.getProperty("karaf.name") + ".datafiles";
            }
            final File dataFilesBackupDir = new File(pending.getParentFile(), dataFilesName);
            final Properties backupProperties = new Properties();
            FileInputStream inStream = new FileInputStream(new File(dataFilesBackupDir, "backup-" + prefix + ".properties"));
            backupProperties.load(inStream);
            Utils.closeQuietly(inStream);
            // maybe one of those bundles has data directory to restore?
            for (Bundle b : systemContext.getBundles()) {
                if (b.getSymbolicName() != null) {
                    String key = String.format("%s$$%s", stripSymbolicName(b.getSymbolicName()), b.getVersion().toString());
                    if (backupProperties.containsKey(key)) {
                        String backupDirName = backupProperties.getProperty(key);
                        File backupDir = new File(dataFilesBackupDir, prefix + "/" + backupDirName + "/data");
                        restoreDataDirectory(dataCache, b, backupDir);
                        // we no longer want to restore this dir
                        backupProperties.remove(key);
                    }
                }
            }
            // 2. We can however have more bundle data backups - we'll restore them after each bundle
            // is INSTALLED and we'll use listener for this
            BundleListener bundleListener = new SynchronousBundleListener() {

                @Override
                public void bundleChanged(BundleEvent event) {
                    Bundle b = event.getBundle();
                    if (event.getType() == BundleEvent.INSTALLED && b.getSymbolicName() != null) {
                        String key = String.format("%s$$%s", stripSymbolicName(b.getSymbolicName()), b.getVersion().toString());
                        if (backupProperties.containsKey(key)) {
                            String backupDirName = backupProperties.getProperty(key);
                            File backupDir = new File(dataFilesBackupDir, prefix + "/" + backupDirName + "/data");
                            restoreDataDirectory(dataCache, b, backupDir);
                        }
                    }
                }
            };
            systemContext.addBundleListener(bundleListener);
            pendingPatchesListeners.put(patchData.getId(), bundleListener);
        } catch (Exception e) {
            Activator.log(LogService.LOG_ERROR, null, e.getMessage(), e, true);
        }
    }
}
Also used : PatchData(org.jboss.fuse.patch.management.PatchData) Bundle(org.osgi.framework.Bundle) Properties(java.util.Properties) PatchDetailsRequest(org.jboss.fuse.patch.management.PatchDetailsRequest) FileInputStream(java.io.FileInputStream) PatchException(org.jboss.fuse.patch.management.PatchException) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) IOException(java.io.IOException) BundleEvent(org.osgi.framework.BundleEvent) BundleListener(org.osgi.framework.BundleListener) SynchronousBundleListener(org.osgi.framework.SynchronousBundleListener) ZipFile(org.apache.commons.compress.archivers.zip.ZipFile) File(java.io.File) Patch(org.jboss.fuse.patch.management.Patch) ManagedPatch(org.jboss.fuse.patch.management.ManagedPatch) Pending(org.jboss.fuse.patch.management.Pending) SynchronousBundleListener(org.osgi.framework.SynchronousBundleListener)

Example 7 with BundleListener

use of org.osgi.framework.BundleListener in project aries by apache.

the class BundleStateTest method createBundle.

private void createBundle(StateConfig stateConfig, final List<Notification> received, final List<AttributeChangeNotification> attributeChanges) throws Exception {
    BundleContext context = mock(BundleContext.class);
    when(context.getBundles()).thenReturn(new Bundle[] {});
    PackageAdmin admin = mock(PackageAdmin.class);
    StartLevel startLevel = mock(StartLevel.class);
    Logger logger = mock(Logger.class);
    BundleState bundleState = new BundleState(context, admin, startLevel, stateConfig, logger);
    Bundle b1 = mock(Bundle.class);
    when(b1.getBundleId()).thenReturn(new Long(9));
    when(b1.getSymbolicName()).thenReturn("bundle");
    when(b1.getLocation()).thenReturn("file:/location");
    BundleEvent installedEvent = mock(BundleEvent.class);
    when(installedEvent.getBundle()).thenReturn(b1);
    when(installedEvent.getType()).thenReturn(BundleEvent.INSTALLED);
    BundleEvent resolvedEvent = mock(BundleEvent.class);
    when(resolvedEvent.getBundle()).thenReturn(b1);
    when(resolvedEvent.getType()).thenReturn(BundleEvent.RESOLVED);
    MBeanServer server = mock(MBeanServer.class);
    // setup for notification
    ObjectName objectName = new ObjectName(OBJECTNAME);
    bundleState.preRegister(server, objectName);
    bundleState.postRegister(true);
    // add NotificationListener to receive the events
    bundleState.addNotificationListener(new NotificationListener() {

        public void handleNotification(Notification notification, Object handback) {
            if (notification instanceof AttributeChangeNotification) {
                attributeChanges.add((AttributeChangeNotification) notification);
            } else {
                received.add(notification);
            }
        }
    }, null, null);
    // capture the BundleListener registered with BundleContext to issue BundleEvents
    ArgumentCaptor<BundleListener> argument = ArgumentCaptor.forClass(BundleListener.class);
    verify(context).addBundleListener(argument.capture());
    // send events
    BundleListener listener = argument.getValue();
    listener.bundleChanged(installedEvent);
    listener.bundleChanged(resolvedEvent);
    // shutdown dispatcher via unregister callback
    bundleState.postDeregister();
    // check the BundleListener is cleaned up
    verify(context).removeBundleListener(listener);
    ExecutorService dispatcher = bundleState.getEventDispatcher();
    assertTrue(dispatcher.isShutdown());
    dispatcher.awaitTermination(2, TimeUnit.SECONDS);
    assertTrue(dispatcher.isTerminated());
}
Also used : AttributeChangeNotification(javax.management.AttributeChangeNotification) Bundle(org.osgi.framework.Bundle) Logger(org.apache.aries.jmx.Logger) AttributeChangeNotification(javax.management.AttributeChangeNotification) Notification(javax.management.Notification) ObjectName(javax.management.ObjectName) PackageAdmin(org.osgi.service.packageadmin.PackageAdmin) ExecutorService(java.util.concurrent.ExecutorService) StartLevel(org.osgi.service.startlevel.StartLevel) BundleEvent(org.osgi.framework.BundleEvent) BundleListener(org.osgi.framework.BundleListener) BundleContext(org.osgi.framework.BundleContext) MBeanServer(javax.management.MBeanServer) NotificationListener(javax.management.NotificationListener)

Example 8 with BundleListener

use of org.osgi.framework.BundleListener in project aries by apache.

the class BundleStateTest method testLifeCycleOfNotificationSupport.

@Test
public void testLifeCycleOfNotificationSupport() throws Exception {
    BundleContext context = mock(BundleContext.class);
    PackageAdmin admin = mock(PackageAdmin.class);
    StartLevel startLevel = mock(StartLevel.class);
    Logger logger = mock(Logger.class);
    BundleState bundleState = new BundleState(context, admin, startLevel, new StateConfig(), logger);
    MBeanServer server1 = mock(MBeanServer.class);
    MBeanServer server2 = mock(MBeanServer.class);
    ObjectName objectName = new ObjectName(OBJECTNAME);
    bundleState.preRegister(server1, objectName);
    bundleState.postRegister(true);
    // capture the BundleListener registered with BundleContext
    ArgumentCaptor<BundleListener> argument = ArgumentCaptor.forClass(BundleListener.class);
    verify(context).addBundleListener(argument.capture());
    assertEquals(1, argument.getAllValues().size());
    BundleListener listener = argument.getValue();
    assertNotNull(listener);
    ExecutorService dispatcher = bundleState.getEventDispatcher();
    // do registration with another server
    bundleState.preRegister(server2, objectName);
    bundleState.postRegister(true);
    // check no more actions on BundleContext
    argument = ArgumentCaptor.forClass(BundleListener.class);
    verify(context, atMost(1)).addBundleListener(argument.capture());
    assertEquals(1, argument.getAllValues().size());
    // do one unregister
    bundleState.postDeregister();
    // verify bundleListener not invoked
    verify(context, never()).removeBundleListener(listener);
    assertFalse(dispatcher.isShutdown());
    // do second unregister and check cleanup
    bundleState.postDeregister();
    verify(context).removeBundleListener(listener);
    assertTrue(dispatcher.isShutdown());
    dispatcher.awaitTermination(2, TimeUnit.SECONDS);
    assertTrue(dispatcher.isTerminated());
}
Also used : PackageAdmin(org.osgi.service.packageadmin.PackageAdmin) ExecutorService(java.util.concurrent.ExecutorService) StartLevel(org.osgi.service.startlevel.StartLevel) BundleListener(org.osgi.framework.BundleListener) Logger(org.apache.aries.jmx.Logger) BundleContext(org.osgi.framework.BundleContext) MBeanServer(javax.management.MBeanServer) ObjectName(javax.management.ObjectName) Test(org.junit.Test)

Example 9 with BundleListener

use of org.osgi.framework.BundleListener in project jo-client-platform by jo-source.

the class CapClientOsgiWorkbenchRunner method start.

@Override
public void start(final BundleContext context) throws Exception {
    final Bundle bundle = context.getBundle();
    context.addBundleListener(new BundleListener() {

        @Override
        public void bundleChanged(final BundleEvent event) {
            if (event.getBundle().equals(bundle) && BundleEvent.STARTED == event.getType()) {
                bundleStartet();
            }
        }
    });
}
Also used : Bundle(org.osgi.framework.Bundle) BundleEvent(org.osgi.framework.BundleEvent) BundleListener(org.osgi.framework.BundleListener)

Example 10 with BundleListener

use of org.osgi.framework.BundleListener in project opennms by OpenNMS.

the class NCSCriteriaServiceManager method setBundleContext.

public void setBundleContext(BundleContext context) {
    m_bundleContext = context;
    m_bundleContext.addBundleListener(new BundleListener() {

        @Override
        public void bundleChanged(BundleEvent event) {
            switch(event.getType()) {
                case BundleEvent.STOPPING:
                    removeAllServices();
            }
        }
    });
}
Also used : BundleEvent(org.osgi.framework.BundleEvent) BundleListener(org.osgi.framework.BundleListener)

Aggregations

BundleListener (org.osgi.framework.BundleListener)18 BundleEvent (org.osgi.framework.BundleEvent)14 SynchronousBundleListener (org.osgi.framework.SynchronousBundleListener)9 Bundle (org.osgi.framework.Bundle)8 File (java.io.File)5 BundleContext (org.osgi.framework.BundleContext)5 IOException (java.io.IOException)4 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)4 FileInputStream (java.io.FileInputStream)3 Properties (java.util.Properties)3 ZipFile (org.apache.commons.compress.archivers.zip.ZipFile)3 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 ExecutorService (java.util.concurrent.ExecutorService)2 MBeanServer (javax.management.MBeanServer)2 ObjectName (javax.management.ObjectName)2 Logger (org.apache.aries.jmx.Logger)2 Git (org.eclipse.jgit.api.Git)2