use of org.osgi.framework.SynchronousBundleListener in project rt.equinox.framework by eclipse.
the class ClassLoadingBundleTests method testBug258659_04.
public void testBug258659_04() throws Exception {
// install a bundle
// $NON-NLS-1$
Bundle test = installer.installBundle("test");
test.start();
SynchronousBundleListener testLoadClassListener = new SynchronousBundleListener() {
public void bundleChanged(BundleEvent event) {
if (event.getType() == BundleEvent.STARTED)
try {
event.getBundle().stop();
} catch (BundleException e) {
simpleResults.addEvent(e);
}
}
};
// clear the results from the initial start
simpleResults.getResults(0);
// listen for the events from refreshing
OSGiTestsActivator.getContext().addBundleListener(testLoadClassListener);
try {
installer.refreshPackages(new Bundle[] { test });
Object[] expectedEvents = new Object[3];
expectedEvents[0] = new BundleEvent(BundleEvent.STOPPED, test);
expectedEvents[1] = new BundleEvent(BundleEvent.STARTED, test);
expectedEvents[2] = new BundleEvent(BundleEvent.STOPPED, test);
Object[] actualEvents = simpleResults.getResults(3);
compareResults(expectedEvents, actualEvents);
} finally {
OSGiTestsActivator.getContext().removeBundleListener(testLoadClassListener);
}
}
use of org.osgi.framework.SynchronousBundleListener in project rt.equinox.framework by eclipse.
the class SystemBundleTests method testSystemBundleListener.
public void testSystemBundleListener() throws BundleException, InterruptedException {
File config = OSGiTestsActivator.getContext().getDataFile(getName());
config.mkdirs();
Map<String, Object> configuration = new HashMap<String, Object>();
configuration.put(Constants.FRAMEWORK_STORAGE, config.getAbsolutePath());
Equinox equinox = new Equinox(configuration);
equinox.start();
BundleContext systemContext = equinox.getBundleContext();
final AtomicInteger stoppingEvent = new AtomicInteger();
final AtomicInteger stoppedEvent = new AtomicInteger();
BundleListener systemBundleListener = new SynchronousBundleListener() {
@Override
public void bundleChanged(BundleEvent event) {
if (event.getBundle().getBundleId() == 0) {
switch(event.getType()) {
case BundleEvent.STOPPING:
stoppingEvent.incrementAndGet();
break;
case BundleEvent.STOPPED:
stoppedEvent.incrementAndGet();
default:
break;
}
}
}
};
systemContext.addBundleListener(systemBundleListener);
equinox.stop();
equinox.waitForStop(5000);
assertEquals("Wrong number of STOPPING events", 1, stoppingEvent.get());
assertEquals("Wrong number of STOPPED events", 1, stoppedEvent.get());
}
use of org.osgi.framework.SynchronousBundleListener in project fabric8 by jboss-fuse.
the class GitPatchManagementServiceImpl method checkPendingPatches.
@Override
public void checkPendingPatches() {
File[] pendingPatches = patchesDir.listFiles(new FileFilter() {
@Override
public boolean accept(File pathname) {
return 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));
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);
IOUtils.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.SynchronousBundleListener in project jackrabbit-oak by apache.
the class OakOSGiRepositoryFactory method shutdown.
static void shutdown(PojoServiceRegistry registry, int timeoutInSecs) throws BundleException {
if (registry == null) {
return;
}
final Bundle systemBundle = registry.getBundleContext().getBundle();
final CountDownLatch shutdownLatch = new CountDownLatch(1);
// Logic here is similar to org.apache.felix.connect.PojoServiceRegistryFactoryImpl.FrameworkImpl.waitForStop()
systemBundle.getBundleContext().addBundleListener(new SynchronousBundleListener() {
public void bundleChanged(BundleEvent event) {
if (event.getBundle() == systemBundle && event.getType() == BundleEvent.STOPPED) {
shutdownLatch.countDown();
}
}
});
// Initiate shutdown
systemBundle.stop();
// Wait for framework shutdown to complete
try {
boolean shutdownWithinTimeout = shutdownLatch.await(timeoutInSecs, TimeUnit.SECONDS);
if (!shutdownWithinTimeout) {
throw new BundleException("Timed out while waiting for repository " + "shutdown for " + timeoutInSecs + " secs");
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new BundleException("Timed out while waiting for repository " + "shutdown for " + timeoutInSecs + " secs", e);
}
}
use of org.osgi.framework.SynchronousBundleListener 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);
}
}
}
Aggregations