use of org.picocontainer.Startable in project kernel by exoplatform.
the class ConcurrentContainerMT method start.
/**
* Starts all the provided adapters
*/
protected void start(Collection<ComponentAdapter<?>> adapters, final Map<ComponentAdapter<?>, Object> alreadyStarted, final Set<ComponentAdapter<?>> startInProgress, final AtomicReference<Exception> error, final boolean skippable) {
if (adapters == null || adapters.isEmpty())
return;
boolean enableMultiThreading = Mode.hasMode(Mode.MULTI_THREADED) && adapters.size() > 1;
List<Future<?>> submittedTasks = null;
ThreadPoolExecutor executor = enableMultiThreading ? getExecutor() : null;
if (enableMultiThreading && executor == null) {
enableMultiThreading = false;
}
for (final ComponentAdapter<?> adapter : adapters) {
if (error.get() != null)
break;
if (ExoContainer.class.isAssignableFrom(adapter.getComponentImplementation())) {
// Only non containers are expected and it is a container
continue;
} else if (alreadyStarted.containsKey(adapter) || (skippable && startInProgress.contains(adapter))) {
// The component has already been started or is in progress
continue;
}
if (enableMultiThreading && LockManager.getInstance().getTotalUncompletedTasks() < executor.getCorePoolSize() && !(adapter instanceof InstanceComponentAdapter)) {
final ExoContainer container = ExoContainerContext.getCurrentContainerIfPresent();
final ClassLoader cl = Thread.currentThread().getContextClassLoader();
Runnable task = new Runnable() {
public void run() {
SecurityHelper.doPrivilegedAction(new PrivilegedAction<Void>() {
public Void run() {
if (error.get() != null) {
return null;
} else if (alreadyStarted.containsKey(adapter) || (skippable && startInProgress.contains(adapter))) {
// The component has already been started or is in progress
return null;
}
ExoContainer oldContainer = ExoContainerContext.getCurrentContainerIfPresent();
ClassLoader oldCl = Thread.currentThread().getContextClassLoader();
try {
ExoContainerContext.setCurrentContainer(container);
Thread.currentThread().setContextClassLoader(cl);
if (adapter instanceof ComponentAdapterDependenciesAware) {
ComponentAdapterDependenciesAware<?> cada = (ComponentAdapterDependenciesAware<?>) adapter;
startDependencies(alreadyStarted, startInProgress, error, cada);
}
if (!Startable.class.isAssignableFrom(adapter.getComponentImplementation())) {
alreadyStarted.put(adapter, adapter);
return null;
} else if (alreadyStarted.containsKey(adapter)) {
// The component has already been started
return null;
}
synchronized (adapter) {
if (alreadyStarted.containsKey(adapter)) {
// The component has already been started
return null;
}
try {
Startable startable = (Startable) adapter.getComponentInstance();
startable.start();
} finally {
alreadyStarted.put(adapter, adapter);
}
}
} catch (Exception e) {
error.compareAndSet(null, e);
} finally {
Thread.currentThread().setContextClassLoader(oldCl);
ExoContainerContext.setCurrentContainer(oldContainer);
}
return null;
}
});
}
};
if (submittedTasks == null) {
submittedTasks = new ArrayList<Future<?>>();
}
submittedTasks.add(executor.submit(task));
} else {
if (adapter instanceof ComponentAdapterDependenciesAware) {
ComponentAdapterDependenciesAware<?> cada = (ComponentAdapterDependenciesAware<?>) adapter;
startDependencies(alreadyStarted, startInProgress, error, cada);
}
if (!Startable.class.isAssignableFrom(adapter.getComponentImplementation())) {
alreadyStarted.put(adapter, adapter);
continue;
} else if (alreadyStarted.containsKey(adapter)) {
// The component has already been started
continue;
}
synchronized (adapter) {
if (alreadyStarted.containsKey(adapter)) {
// The component has already been started
continue;
}
try {
Startable startable = (Startable) adapter.getComponentInstance();
startable.start();
} catch (Exception e) {
error.compareAndSet(null, e);
} finally {
alreadyStarted.put(adapter, adapter);
}
}
}
}
if (submittedTasks != null) {
for (int i = 0, length = submittedTasks.size(); i < length; i++) {
Future<?> task = submittedTasks.get(i);
try {
task.get();
} catch (ExecutionException e) {
Throwable cause = e.getCause();
if (cause instanceof RuntimeException) {
throw (RuntimeException) cause;
}
throw new RuntimeException(cause);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}
}
use of org.picocontainer.Startable in project kernel by exoplatform.
the class MX4JComponentAdapter method create.
/**
* {@inheritDoc}
*/
public T create(CreationalContext<T> creationalContext) {
//
T instance;
Component component = null;
ConfigurationManager manager;
String componentKey;
InitParams params = null;
boolean debug = false;
CreationalContextComponentAdapter<T> ctx = (CreationalContextComponentAdapter<T>) creationalContext;
try {
// Avoid to create duplicate instances if it is called at the same time by several threads
if (instance_ != null)
return instance_;
else if (ctx.get() != null)
return ctx.get();
// Get the component
Object key = getComponentKey();
if (key instanceof String)
componentKey = (String) key;
else
componentKey = ((Class<?>) key).getName();
manager = exocontainer.getComponentInstanceOfType(ConfigurationManager.class);
component = manager == null ? null : manager.getComponent(componentKey);
if (component != null) {
params = component.getInitParams();
debug = component.getShowDeployInfo();
}
instance = createInstance(ctx, component, manager, componentKey, params, debug);
if (instance instanceof Startable && exocontainer.canBeStopped()) {
// Start the component if the container is already started
((Startable) instance).start();
}
} catch (Exception ex) {
String msg = "Cannot instantiate component " + getComponentImplementation();
if (component != null) {
msg = "Cannot instantiate component key=" + component.getKey() + " type=" + component.getType() + " found at " + component.getDocumentURL();
}
throw new RuntimeException(msg, ex);
}
return instance;
}
use of org.picocontainer.Startable in project kernel by exoplatform.
the class MX4JComponentAdapterMT method create.
/**
* {@inheritDoc}
*/
public T create(CreationalContext<T> creationalContext) {
CreationalContextComponentAdapter<T> ctx = (CreationalContextComponentAdapter<T>) creationalContext;
// Avoid to create duplicate instances if it is called at the same time by several threads
if (instance_ != null)
return instance_;
else if (ctx.get() != null)
return ctx.get();
ComponentTaskContext taskCtx = container.getComponentTaskContext();
boolean isRoot = taskCtx.isRoot();
if (!isRoot) {
container.setComponentTaskContext(taskCtx = taskCtx.setLastTaskType(ComponentTaskType.CREATE));
}
try {
ComponentTask<T> task = createTask.get();
T result = task.call(ctx);
if (instance_ != null) {
// to component plugins
return instance_;
} else if (ctx.get() != null)
return ctx.get();
ctx.push(result);
} catch (CyclicDependencyException e) {
throw e;
} catch (Exception e) {
throw new RuntimeException("Cannot create component " + getComponentImplementation(), e);
}
if (isRoot) {
container.setComponentTaskContext(taskCtx = taskCtx.resetDependencies(getComponentKey(), ComponentTaskType.INIT));
} else {
container.setComponentTaskContext(taskCtx = taskCtx.setLastTaskType(ComponentTaskType.INIT));
}
Collection<ComponentTask<Void>> tasks = initTasks.get();
ComponentTask<Void> task = null;
try {
if (tasks != null && !tasks.isEmpty()) {
container.loadDependencies(getComponentKey(), taskCtx, getInitDependencies(), ComponentTaskType.INIT);
for (Iterator<ComponentTask<Void>> it = tasks.iterator(); it.hasNext(); ) {
task = it.next();
task.call(ctx);
task = null;
}
}
if (instance_ != null) {
return instance_;
} else if (instance_ == null && isSingleton) {
// In case of cyclic dependency the component could be already initialized
// so we need to recheck the state
instance_ = ctx.get();
}
} catch (CyclicDependencyException e) {
throw e;
} catch (Exception e) {
if (task != null) {
throw new RuntimeException("Cannot " + task.getName() + " for the component " + getComponentImplementation(), e);
}
throw new RuntimeException("Cannot initialize component " + getComponentImplementation(), e);
}
if (ctx.get() instanceof Startable && exocontainer.canBeStopped()) {
try {
// Start the component if the container is already started
((Startable) ctx.get()).start();
} catch (Exception e) {
throw new RuntimeException("Cannot auto-start component " + getComponentImplementation(), e);
}
}
return ctx.get();
}
Aggregations