use of org.exoplatform.container.spi.Container in project kernel by exoplatform.
the class TestKernelExtensionMT method testInterceptorsInternal.
private void testInterceptorsInternal(ExoContainer parent) {
MockInterceptor1 i1 = null;
MockInterceptor2 i2 = null;
MockInterceptor3 i3 = null;
MockInterceptor4 i4 = null;
MockInterceptor5 i5 = null;
MockInterceptor6 i6 = null;
MockInterceptor7 i7 = null;
MockInterceptor8 i8 = null;
MockInterceptor9 i9 = null;
ExoContainer holder = new ExoContainer(parent);
Container container = holder;
while ((container = container.getSuccessor()) != null) {
if (container instanceof MockInterceptor1) {
i1 = (MockInterceptor1) container;
} else if (container instanceof MockInterceptor2) {
i2 = (MockInterceptor2) container;
} else if (container instanceof MockInterceptor3) {
i3 = (MockInterceptor3) container;
} else if (container instanceof MockInterceptor4) {
i4 = (MockInterceptor4) container;
} else if (container instanceof MockInterceptor5) {
i5 = (MockInterceptor5) container;
} else if (container instanceof MockInterceptor6) {
i6 = (MockInterceptor6) container;
} else if (container instanceof MockInterceptor7) {
i7 = (MockInterceptor7) container;
} else if (container instanceof MockInterceptor8) {
i8 = (MockInterceptor8) container;
} else if (container instanceof MockInterceptor9) {
i9 = (MockInterceptor9) container;
}
}
assertNotNull(i1);
assertNotNull(i1.getSuccessor());
assertSame(parent, i1.getParent());
assertSame(holder, i1.getHolder());
assertEquals("MockInterceptor1", i1.getId());
assertNotNull(i2);
assertNotNull(i2.getSuccessor());
assertSame(parent, i2.getParent());
assertSame(holder, i2.getHolder());
assertEquals("MockInterceptor2", i2.getId());
assertNotNull(i3);
assertNotNull(i3.getSuccessor());
assertSame(parent, i3.getParent());
assertSame(holder, i3.getHolder());
assertEquals("MockInterceptor3", i3.getId());
assertNotNull(i4);
assertNotNull(i4.getSuccessor());
assertSame(parent, i4.getParent());
assertSame(holder, i4.getHolder());
assertEquals("MockInterceptor4", i4.getId());
assertNotNull(i5);
assertNotNull(i5.getSuccessor());
assertSame(parent, i5.getParent());
assertSame(holder, i5.getHolder());
assertEquals("MockInterceptor5", i5.getId());
assertNotNull(i6);
assertNotNull(i6.getSuccessor());
assertSame(parent, i6.getParent());
assertSame(holder, i6.getHolder());
assertEquals("MockInterceptor6", i6.getId());
assertNotNull(i7);
assertNotNull(i7.getSuccessor());
assertSame(parent, i7.getParent());
assertSame(holder, i7.getHolder());
assertEquals("MockInterceptor7", i7.getId());
assertNotNull(i8);
assertNull(i8.getSuccessor());
assertSame(parent, i8.getParent());
assertSame(holder, i8.getHolder());
assertEquals("MockInterceptor8", i8.getId());
assertNotNull(i9);
assertNotNull(i9.getSuccessor());
assertSame(i5, i9.getSuccessor());
assertSame(parent, i9.getParent());
assertSame(holder, i9.getHolder());
assertEquals("MockInterceptor9", i9.getId());
assertSame(i9, holder.getSuccessor());
Test0 t0I = new Test0();
Test1 t1I = new Test1();
Test2 t2I = new Test2();
Test3 t3I = new Test3();
holder.registerComponentInstance(Test0.class, t0I);
holder.registerComponentInstance(Test1.class, t1I);
holder.registerComponentInstance(Test2.class, t2I);
holder.registerComponentInstance(Test3.class, t3I);
holder.registerComponentImplementation(Test5.class);
assertSame(t0I, holder.getComponentInstanceOfType(Test0.class));
Test1 t1FC = holder.getComponentInstanceOfType(Test1.class);
assertNotSame(t1I, t1FC);
// A new instance is created at each call so it must not be the same as before
assertNotSame(t1FC, holder.getComponentInstanceOfType(Test1.class));
Test2 t2FC = holder.getComponentInstanceOfType(Test2.class);
assertNotSame(t2I, t2FC);
// It comes from the cache so it must be the same than before
assertSame(t2FC, holder.getComponentInstanceOfType(Test2.class));
Test3 t3FC = holder.getComponentInstanceOfType(Test3.class);
assertSame(MockInterceptor1.TEST, t3FC);
Test5 t4FC = holder.getComponentInstanceOfType(Test5.class);
assertSame(t0I, t4FC.t0);
assertNotSame(t1I, t4FC.t1);
assertNotSame(t2I, t4FC.t2);
assertSame(t3FC, t4FC.t3);
}
use of org.exoplatform.container.spi.Container in project kernel by exoplatform.
the class ManageableComponentAdapter method destroy.
/**
* {@inheritDoc}
*/
@Override
public void destroy(T instance, CreationalContext<T> creationalContext) {
try {
Container co = exocontainer;
do {
if (co instanceof ManageableContainer) {
break;
}
} while ((co = co.getSuccessor()) != null);
if (co instanceof ManageableContainer) {
ManageableContainer container = (ManageableContainer) co;
if (container.managementContext != null) {
// UnRegister the instance against the management context
if (LOG.isDebugEnabled())
LOG.debug("==> remove " + instance + " from a mbean server");
container.managementContext.unregister(instance);
}
}
creationalContext.release();
} catch (Exception e) {
LOG.error("Could not destroy the instance " + instance + ": " + e.getMessage());
}
}
use of org.exoplatform.container.spi.Container in project kernel by exoplatform.
the class ManagementContextImpl method register.
public void register(Object o) {
Object resource = null;
// Apply managed by annotation
ManagedBy managedBy = o.getClass().getAnnotation(ManagedBy.class);
if (managedBy != null) {
try {
Class<?> managedByClass = managedBy.value();
Constructor<?> blah = managedByClass.getConstructor(o.getClass());
resource = blah.newInstance(o);
} catch (NoSuchMethodException e) {
LOG.error(e.getLocalizedMessage(), e);
} catch (InstantiationException e) {
LOG.error(e.getLocalizedMessage(), e);
} catch (IllegalAccessException e) {
LOG.error(e.getLocalizedMessage(), e);
} catch (InvocationTargetException e) {
LOG.error(e.getLocalizedMessage(), e);
}
} else {
resource = o;
}
//
if (resource != null) {
MetaDataBuilder builder = new MetaDataBuilder(resource.getClass());
if (builder.isBuildable()) {
ManagedTypeMetaData metaData = builder.build();
//
ManagementContextImpl managementContext;
if (resource instanceof Container) {
managementContext = (ManagementContextImpl) ((Container) resource).getManagementContext();
} else {
managementContext = new ManagementContextImpl(this, resource, metaData);
}
//
if (registrations.containsKey(resource)) {
LOG.debug("The component " + resource + " has already been registered");
return;
} else {
registrations.put(resource, managementContext);
}
//
ManageableContainer container = findContainer();
// Install for all the providers related
for (ManagementProvider provider : container.getProviders()) {
Object name = provider.manage(managementContext);
if (name != null) {
managementContext.managedSet.put(provider, name);
}
}
// Allow for more resource management
if (resource instanceof ManagementAware) {
((ManagementAware) resource).setContext(managementContext);
}
}
}
}
use of org.exoplatform.container.spi.Container in project kernel by exoplatform.
the class ManageableComponentAdapterMT method destroy.
/**
* {@inheritDoc}
*/
@Override
public void destroy(T instance, CreationalContext<T> creationalContext) {
try {
Container co = exocontainer;
do {
if (co instanceof ManageableContainer) {
break;
}
} while ((co = co.getSuccessor()) != null);
if (co instanceof ManageableContainer) {
ManageableContainer container = (ManageableContainer) co;
if (container.managementContext != null) {
// UnRegister the instance against the management context
if (LOG.isDebugEnabled())
LOG.debug("==> remove " + instance + " from a mbean server");
container.managementContext.unregister(instance);
}
}
creationalContext.release();
} catch (Exception e) {
LOG.error("Could not destroy the instance " + instance + ": " + e.getMessage());
}
}
Aggregations