use of org.exoplatform.container.ExoContainer in project kernel by exoplatform.
the class ContextManagerListener method requestInitialized.
/**
* {@inheritDoc}
*/
public void requestInitialized(ServletRequestEvent event) {
final ExoContainer oldContainer = ExoContainerContext.getCurrentContainerIfPresent();
ExoContainer container = null;
boolean hasBeenSet = false;
try {
container = getContainer(event);
if (container == null)
return;
if (!container.equals(oldContainer)) {
if (container instanceof PortalContainer) {
PortalContainer.setInstance((PortalContainer) container);
}
ExoContainerContext.setCurrentContainer(container);
hasBeenSet = true;
}
onRequestInitialized(container, event);
} finally {
if (hasBeenSet) {
if (container instanceof PortalContainer) {
// Remove the current Portal Container and the current ExoContainer
PortalContainer.setInstance(null);
}
// Re-set the old container
ExoContainerContext.setCurrentContainer(oldContainer);
}
}
}
use of org.exoplatform.container.ExoContainer in project kernel by exoplatform.
the class RequestLifeCycleStack method begin.
void begin(ExoContainer container, boolean local) {
// Need to make a copy as modifying the list is cached by the container
List<ComponentRequestLifecycle> components = new ArrayList<ComponentRequestLifecycle>((List<ComponentRequestLifecycle>) container.getComponentInstancesOfType(ComponentRequestLifecycle.class));
//
if (!local) {
for (ExoContainer current = container.getParent(); current != null; current = current.getParent()) {
components.addAll((List<ComponentRequestLifecycle>) current.getComponentInstancesOfType(ComponentRequestLifecycle.class));
}
}
// Remove components that have already started their life cycle
components.removeAll(allComponents);
// Contribute to the all component set
allComponents.addAll(components);
//
RequestLifeCycle lifeCycle = new RequestLifeCycle(container, components);
//
addLast(lifeCycle);
//
lifeCycle.doBegin();
}
use of org.exoplatform.container.ExoContainer in project kernel by exoplatform.
the class ThreadContextHandler method store.
/**
* Stores into memory the current values of all the Thread Local variables
* of all the registered {@link ThreadContextHolder} of the {@link ExoContainer}
*/
public void store() {
final List<ThreadContextHolder> components = new ArrayList<ThreadContextHolder>((List<ThreadContextHolder>) container.getComponentInstancesOfType(ThreadContextHolder.class));
for (ExoContainer current = container.getParent(); current != null; current = current.getParent()) {
components.addAll((List<ThreadContextHolder>) current.getComponentInstancesOfType(ThreadContextHolder.class));
}
contexts = new ArrayList<ThreadContext>(components.size());
SecurityHelper.doPrivilegedAction(new PrivilegedAction<Void>() {
public Void run() {
for (int i = 0, length = components.size(); i < length; i++) {
ThreadContextHolder holder = components.get(i);
ThreadContext tc = holder.getThreadContext();
if (tc == null) {
// This ThreadContextHolder has nothing valuable to share so we skip it
continue;
}
contexts.add(tc);
tc.store();
}
return null;
}
});
}
use of org.exoplatform.container.ExoContainer in project kernel by exoplatform.
the class TestRegistration method testFoo.
public void testFoo() throws Exception {
RootContainer root = createRootContainer("registration-configuration.xml");
assertNotNull(root.getMBeanServer());
Object instance = root.getComponentInstance("Foo");
assertNotNull(instance);
MBeanServer server = root.getMBeanServer();
Set<ObjectInstance> set = server.queryMBeans(ObjectName.getInstance("exo:object=Foo"), null);
assertEquals(1, set.size());
ObjectInstance oi = set.iterator().next();
ExoContainer oldContainer = ExoContainerContext.getCurrentContainerIfPresent();
ExoContainer currentContainer = new ExoContainer();
ExoContainerContext.setCurrentContainer(currentContainer);
try {
ExoContainerFinder proxyObject = MBeanServerInvocationHandler.newProxyInstance(server, oi.getObjectName(), ExoContainerFinder.class, false);
assertTrue("We expect to get the current exo container", oldContainer == proxyObject.getCurrentExoContainer());
assertTrue("We expect to get the previous exo container", ExoContainerContext.getCurrentContainerIfPresent() == currentContainer);
ExoContainerContext.setCurrentContainer(oldContainer);
assertTrue("We expect to get the current exo container", oldContainer == proxyObject.getCurrentExoContainer());
assertTrue("We expect to get the previous exo container", ExoContainerContext.getCurrentContainerIfPresent() == oldContainer);
ExoContainerContext.setCurrentContainer(null);
assertTrue("We expect to get the current exo container", oldContainer == proxyObject.getCurrentExoContainer());
assertTrue("We expect to get the previous exo container", ExoContainerContext.getCurrentContainerIfPresent() == oldContainer);
} finally {
ExoContainerContext.setCurrentContainer(oldContainer);
}
// Manual
root.registerComponentInstance("Bar", new ManagedWithObjectNameTemplate("Bar"));
Object instance2 = root.getComponentInstance("Bar");
assertNotNull(instance2);
Set set2 = server.queryMBeans(ObjectName.getInstance("exo:object=Bar"), null);
assertEquals(1, set2.size());
}
use of org.exoplatform.container.ExoContainer in project kernel by exoplatform.
the class MX4JComponentAdapterMT method getInitTasks.
/**
* {@inheritDoc}
*/
protected Collection<ComponentTask<Void>> getInitTasks() {
Component component = null;
String componentKey;
boolean debug = false;
// Get the component
Object key = getComponentKey();
if (key instanceof String)
componentKey = (String) key;
else
componentKey = ((Class<?>) key).getName();
try {
ConfigurationManager manager = (ConfigurationManager) exocontainer.getComponentInstanceOfType(ConfigurationManager.class);
component = manager == null ? null : manager.getComponent(componentKey);
if (component != null) {
debug = component.getShowDeployInfo();
}
List<ComponentTask<Void>> tasks = new ArrayList<ComponentTask<Void>>();
Set<Dependency> dependencies = new HashSet<Dependency>();
final Class<T> implementationClass = getComponentImplementation();
boolean isSingleton = this.isSingleton;
boolean isInitialized = this.isInitialized;
if (debug)
LOG.debug("==> create component : " + implementationClass.getName());
boolean hasInjectableConstructor = !isSingleton || ContainerUtil.hasInjectableConstructor(implementationClass);
boolean hasOnlyEmptyPublicConstructor = !isSingleton || ContainerUtil.hasOnlyEmptyPublicConstructor(implementationClass);
if (hasInjectableConstructor || hasOnlyEmptyPublicConstructor) {
// There is at least one constructor JSR 330 compliant or we already know
// that it is not a singleton such that the new behavior is expected
List<Dependency> lDependencies = new ArrayList<Dependency>();
boolean isInjectPresent = container.initializeComponent(implementationClass, lDependencies, tasks, this);
dependencies.addAll(lDependencies);
isSingleton = manageScope(isSingleton, isInitialized, hasInjectableConstructor, isInjectPresent);
} else if (!isInitialized) {
// The adapter has not been initialized yet
// The old behavior is expected as there is no constructor JSR 330 compliant
isSingleton = this.isSingleton = true;
scope.set(Singleton.class);
}
if (component != null && component.getComponentPlugins() != null) {
addComponentPlugin(tasks, dependencies, debug, component.getComponentPlugins());
}
ExternalComponentPlugins ecplugins = manager == null ? null : manager.getConfiguration().getExternalComponentPlugins(componentKey);
if (ecplugins != null) {
addComponentPlugin(tasks, dependencies, debug, ecplugins.getComponentPlugins());
}
initDependencies.compareAndSet(null, new CopyOnWriteArraySet<Dependency>(dependencies));
tasks.add(new ComponentTask<Void>("initialize component " + getComponentImplementation().getName(), container, this, ComponentTaskType.INIT) {
public Void execute(CreationalContextComponentAdapter<?> cCtx) throws Exception {
// check if component implement the ComponentLifecycle
if (cCtx.get() instanceof ComponentLifecycle && exocontainer instanceof ExoContainer) {
ComponentLifecycle lc = (ComponentLifecycle) cCtx.get();
lc.initComponent((ExoContainer) exocontainer);
}
return null;
}
});
if (!isInitialized) {
this.isInitialized = true;
}
return tasks;
} catch (Exception e) {
String msg = "Cannot initialize component " + getComponentImplementation();
if (component != null) {
msg = "Cannot initialize component key=" + component.getKey() + " type=" + component.getType() + " found at " + component.getDocumentURL();
}
throw new RuntimeException(msg, e);
}
}
Aggregations