use of org.osgi.service.blueprint.container.BlueprintEvent in project camel by apache.
the class CamelBlueprintHelper method waitForBlueprintContainer.
/**
* Synchronization method to wait for particular state of BlueprintContainer under test.
*/
public static void waitForBlueprintContainer(final Set<Long> eventHistory, BundleContext context, final String symbolicName, final int bpEvent, final Runnable runAndWait) throws InterruptedException {
final CountDownLatch latch = new CountDownLatch(1);
final Throwable[] pThrowable = new Throwable[] { null };
ServiceRegistration<BlueprintListener> registration = context.registerService(BlueprintListener.class, new BlueprintListener() {
@Override
public void blueprintEvent(BlueprintEvent event) {
if (event.getBundle().getSymbolicName().equals(symbolicName)) {
if (event.getType() == bpEvent) {
// it works with BP container reloads if next CREATE state is at least 1ms after previous one
if (eventHistory == null || eventHistory.add(event.getTimestamp())) {
latch.countDown();
}
} else if (event.getType() == BlueprintEvent.FAILURE) {
// we didn't wait for FAILURE, but we got it - fail fast then
pThrowable[0] = event.getCause();
latch.countDown();
}
}
}
}, null);
if (runAndWait != null) {
runAndWait.run();
}
boolean found = latch.await(CamelBlueprintHelper.DEFAULT_TIMEOUT, TimeUnit.MILLISECONDS);
registration.unregister();
if (!found) {
throw new RuntimeException("Gave up waiting for BlueprintContainer from bundle \"" + symbolicName + "\"");
}
if (pThrowable[0] != null) {
throw new RuntimeException(pThrowable[0].getMessage(), pThrowable[0]);
}
}
use of org.osgi.service.blueprint.container.BlueprintEvent in project aries by apache.
the class ParserServiceImportAndIncludeXSDsTest method waitForConfig.
private boolean waitForConfig() throws InterruptedException {
final AtomicBoolean ready = new AtomicBoolean();
@SuppressWarnings("rawtypes") ServiceRegistration reg = context().registerService(BlueprintListener.class, new BlueprintListener() {
@Override
public void blueprintEvent(BlueprintEvent event) {
if (TEST_BUNDLE.equals(event.getBundle().getSymbolicName()) && BlueprintEvent.CREATED == event.getType()) {
synchronized (ready) {
ready.set(true);
ready.notify();
}
}
}
}, null);
try {
synchronized (ready) {
if (!ready.get()) {
ready.wait(3000);
}
}
return ready.get();
} finally {
reg.unregister();
}
}
use of org.osgi.service.blueprint.container.BlueprintEvent in project aries by apache.
the class ParserServiceImportCmAndIncorrectNamespaceHandlersTest method waitForConfig.
private void waitForConfig() throws InterruptedException {
final CountDownLatch ready = new CountDownLatch(2);
final AtomicBoolean failure = new AtomicBoolean(false);
@SuppressWarnings("rawtypes") ServiceRegistration reg = context().registerService(BlueprintListener.class, new BlueprintListener() {
@Override
public void blueprintEvent(BlueprintEvent event) {
if (NS_HANDLER_BUNDLE.equals(event.getBundle().getSymbolicName()) && BlueprintEvent.CREATED == event.getType()) {
ready.countDown();
} else if (TEST_BUNDLE.equals(event.getBundle().getSymbolicName()) && (BlueprintEvent.CREATED == event.getType() || BlueprintEvent.FAILURE == event.getType())) {
ready.countDown();
if (BlueprintEvent.FAILURE == event.getType()) {
failure.set(true);
}
}
}
}, null);
try {
assertTrue(ready.await(3000, TimeUnit.MILLISECONDS));
assertFalse("org.apache.aries.blueprint.aries1503.test bundle should successfully start Blueprint container", failure.get());
} finally {
reg.unregister();
}
}
use of org.osgi.service.blueprint.container.BlueprintEvent in project aries by apache.
the class BlueprintExtender method getBlueprintPaths.
private List<URL> getBlueprintPaths(Bundle bundle) {
LOGGER.debug("Scanning bundle {}/{} for blueprint application", bundle.getSymbolicName(), bundle.getVersion());
try {
List<URL> pathList = new ArrayList<URL>();
String blueprintHeader = bundle.getHeaders().get(BlueprintConstants.BUNDLE_BLUEPRINT_HEADER);
String blueprintHeaderAnnotation = bundle.getHeaders().get(BlueprintConstants.BUNDLE_BLUEPRINT_ANNOTATION_HEADER);
if (blueprintHeader == null) {
blueprintHeader = "OSGI-INF/blueprint/";
}
List<PathElement> paths = HeaderParser.parseHeader(blueprintHeader);
for (PathElement path : paths) {
String name = path.getName();
if (name.endsWith("/")) {
addEntries(bundle, name, "*.xml", pathList);
} else {
String baseName;
String filePattern;
int pos = name.lastIndexOf('/');
if (pos < 0) {
baseName = "/";
filePattern = name;
} else {
baseName = name.substring(0, pos + 1);
filePattern = name.substring(pos + 1);
}
addEntries(bundle, baseName, filePattern, pathList);
}
}
// Check annotations
if (blueprintHeaderAnnotation != null && blueprintHeaderAnnotation.trim().equalsIgnoreCase("true")) {
LOGGER.debug("Scanning bundle {}/{} for blueprint annotations", bundle.getSymbolicName(), bundle.getVersion());
ServiceReference sr = this.context.getServiceReference(BlueprintAnnotationScanner.class.getName());
if (sr != null) {
BlueprintAnnotationScanner bas = (BlueprintAnnotationScanner) this.context.getService(sr);
try {
// try to generate the blueprint definition XML
URL url = bas.createBlueprintModel(bundle);
if (url != null) {
pathList.add(url);
}
} finally {
this.context.ungetService(sr);
}
}
}
if (!pathList.isEmpty()) {
LOGGER.debug("Found blueprint application in bundle {}/{} with paths: {}", bundle.getSymbolicName(), bundle.getVersion(), pathList);
// ServiceReference, or just not do this check, which could be quite harmful.
if (isCompatible(bundle)) {
return pathList;
} else {
LOGGER.info("Bundle {}/{} is not compatible with this blueprint extender", bundle.getSymbolicName(), bundle.getVersion());
}
} else {
LOGGER.debug("No blueprint application found in bundle {}/{}", bundle.getSymbolicName(), bundle.getVersion());
}
} catch (Throwable t) {
if (!stopping) {
LOGGER.warn("Error creating blueprint container for bundle {}/{}", bundle.getSymbolicName(), bundle.getVersion(), t);
eventDispatcher.blueprintEvent(new BlueprintEvent(BlueprintEvent.FAILURE, bundle, context.getBundle(), t));
}
}
return null;
}
use of org.osgi.service.blueprint.container.BlueprintEvent in project aries by apache.
the class BlueprintContainerImpl method destroy.
public void destroy() {
synchronized (scheduled) {
destroyed.set(true);
}
cancelFutureIfPresent();
eventDispatcher.blueprintEvent(new BlueprintEvent(BlueprintEvent.DESTROYING, getBundle(), getExtenderBundle()));
executors.shutdownNow();
if (handlerSet != null) {
handlerSet.removeListener(this);
handlerSet.destroy();
}
try {
executors.awaitTermination(5 * 60, TimeUnit.SECONDS);
} catch (InterruptedException e) {
LOGGER.debug("Interrupted waiting for executor to shut down");
}
tidyupComponents();
eventDispatcher.blueprintEvent(new BlueprintEvent(BlueprintEvent.DESTROYED, getBundle(), getExtenderBundle()));
LOGGER.debug("Blueprint container {} destroyed", getBundle().getSymbolicName(), getBundle().getVersion());
}
Aggregations