}
LOGGER.debug("Running blueprint container for bundle {} in state {}", bundleContext.getBundle().getSymbolicName(), state);
switch (state) {
case Unknown:
checkDirectives();
eventDispatcher.blueprintEvent(new BlueprintEvent(BlueprintEvent.CREATING, getBundleContext().getBundle(), getExtenderBundle()));
parser = new Parser();
parser.parse(getResources());
namespaces = parser.getNamespaces();
handlerSet = handlers.getNamespaceHandlers(namespaces, getBundleContext().getBundle());
handlerSet.addListener(this);
state = State.WaitForNamespaceHandlers;
break;
case WaitForNamespaceHandlers:
{
List<String> missing = new ArrayList<String>();
for (URI ns : namespaces) {
if (handlerSet.getNamespaceHandler(ns) == null) {
missing.add("(&(" + Constants.OBJECTCLASS + "=" + NamespaceHandler.class.getName() + ")(" + NamespaceHandlerRegistryImpl.NAMESPACE + "=" + ns + "))");
}
}
if (missing.size() > 0) {
LOGGER.warn("Bundle " + bundleContext.getBundle().getSymbolicName() + " is waiting for namespace handlers " + missing);
eventDispatcher.blueprintEvent(new BlueprintEvent(BlueprintEvent.GRACE_PERIOD, getBundleContext().getBundle(), getExtenderBundle(), missing.toArray(new String[missing.size()])));
return;
}
componentDefinitionRegistry.registerComponentDefinition(new PassThroughMetadataImpl("blueprintContainer", this));
componentDefinitionRegistry.registerComponentDefinition(new PassThroughMetadataImpl("blueprintBundle", bundleContext.getBundle()));
componentDefinitionRegistry.registerComponentDefinition(new PassThroughMetadataImpl("blueprintBundleContext", bundleContext));
componentDefinitionRegistry.registerComponentDefinition(new PassThroughMetadataImpl("blueprintConverter", converter));
if (xmlValidation) {
parser.validate(handlerSet.getSchema());
}
parser.populate(handlerSet, componentDefinitionRegistry);
state = State.Populated;
break;
}
case Populated:
getRepository();
trackServiceReferences();
Runnable r = new Runnable() {
public void run() {
synchronized (scheduled) {
Throwable t = new TimeoutException();
state = State.Failed;
unregisterServices();
untrackServiceReferences();
destroyComponents();
String[] missingDependecies = getMissingDependencies();
LOGGER.error("Unable to start blueprint container for bundle " + bundleContext.getBundle().getSymbolicName() + " due to unresolved dependencies " + Arrays.asList(missingDependecies), t);
eventDispatcher.blueprintEvent(new BlueprintEvent(BlueprintEvent.FAILURE, getBundleContext().getBundle(), getExtenderBundle(), missingDependecies, t));
}
}
};
timeoutFuture = executors.schedule(r, timeout, TimeUnit.MILLISECONDS);
state = State.WaitForInitialReferences;
break;
case WaitForInitialReferences:
if (waitForDependencies) {
String[] missingDependencies = getMissingDependencies();
if (missingDependencies.length > 0) {
eventDispatcher.blueprintEvent(new BlueprintEvent(BlueprintEvent.GRACE_PERIOD, getBundleContext().getBundle(), getExtenderBundle(), missingDependencies));
return;
}
}
state = State.InitialReferencesSatisfied;
break;
case InitialReferencesSatisfied:
processTypeConverters();
processProcessors();
state = State.WaitForInitialReferences2;
break;
case WaitForInitialReferences2:
if (waitForDependencies) {
String[] missingDependencies = getMissingDependencies();
if (missingDependencies.length > 0) {
eventDispatcher.blueprintEvent(new BlueprintEvent(BlueprintEvent.GRACE_PERIOD, getBundleContext().getBundle(), getExtenderBundle(), missingDependencies));
return;
}
}
state = State.Create;
break;
case Create:
timeoutFuture.cancel(false);
registerServices();
instantiateEagerComponents();
// Register the BlueprintContainer in the OSGi registry
if (registration == null) {
Properties props = new Properties();
props.put(BlueprintConstants.CONTAINER_SYMBOLIC_NAME_PROPERTY,
bundleContext.getBundle().getSymbolicName());
props.put(BlueprintConstants.CONTAINER_VERSION_PROPERTY,
JavaUtils.getBundleVersion(bundleContext.getBundle()));
registration = registerService(new String [] { BlueprintContainer.class.getName() }, this, props);
eventDispatcher.blueprintEvent(new BlueprintEvent(BlueprintEvent.CREATED, getBundleContext().getBundle(), getExtenderBundle()));
state = State.Created;
}
break;
case Created:
case Failed:
return;
}
}
} catch (Throwable t) {
state = State.Failed;
if (timeoutFuture != null) {
timeoutFuture.cancel(false);
}
unregisterServices();
untrackServiceReferences();
destroyComponents();
LOGGER.error("Unable to start blueprint container for bundle " + bundleContext.getBundle().getSymbolicName(), t);
eventDispatcher.blueprintEvent(new BlueprintEvent(BlueprintEvent.FAILURE, getBundleContext().getBundle(), getExtenderBundle(), t));
}
}