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);
}
}
}
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());
}
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());
}
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();
}
}
});
}
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();
}
}
});
}
Aggregations