use of org.exoplatform.container.xml.Component in project kernel by exoplatform.
the class WeldContainer method start.
/**
* {@inheritDoc}
*/
@Override
public void start() {
ConfigurationManager cm = super.getComponentInstanceOfType(ConfigurationManager.class, false);
// We check if the component has been defined in the configuration of the current container
// The goal is to enable the WeldContainer only if it is needed
Component component = cm.getComponent(WeldContainerHelper.class);
if (component == null) {
if (LOG.isDebugEnabled()) {
LOG.debug("No WeldContainerHelper has been defined, thus the WeldContainer will be disabled." + " To enable the Weld Integration please define an WeldContainerHelper");
}
} else {
Weld weld = new Weld();
weld.addExtension(new WeldExtension());
WeldContainerHelper helper = super.getComponentInstanceOfType(WeldContainerHelper.class, false);
List<Extension> extensions = helper.getExtensions();
if (extensions != null) {
for (Extension e : extensions) {
weld.addExtension(e);
}
}
this.helper = helper;
this.container = weld.initialize();
// This is an ugly hack to make sure that the BeanManagerProxy is initialized with the right Container
// This is needed especially when we intend to initialize several weld containers within the same instance
container.getBeanManager().getBeans(org.jboss.weld.environment.se.WeldContainer.class);
this.weldContainer = org.jboss.weld.Container.instance();
this.weld = weld;
LOG.info("A WeldContainer has been enabled using the WeldContainerHelper " + helper.getClass());
}
super.start();
}
use of org.exoplatform.container.xml.Component in project kernel by exoplatform.
the class MX4JComponentAdapterMT method getCreateTask.
/**
* {@inheritDoc}
*/
@SuppressWarnings("unchecked")
protected ComponentTask<T> getCreateTask() {
Component component = null;
String componentKey;
InitParams params = null;
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) {
params = component.getInitParams();
debug = component.getShowDeployInfo();
}
if (debug)
LOG.debug("==> get constructor of the component : " + getComponentImplementation());
List<Dependency> lDependencies = new ArrayList<Dependency>();
Constructor<?> constructor = container.getConstructor(getComponentImplementation(), lDependencies);
setCreateDependencies(lDependencies);
if (debug)
LOG.debug("==> create component : " + getComponentImplementation());
return (ComponentTask<T>) container.createComponentTask(constructor, params, lDependencies, this);
} catch (Exception e) {
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, e);
}
}
use of org.exoplatform.container.xml.Component 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);
}
}
use of org.exoplatform.container.xml.Component in project kernel by exoplatform.
the class TestConfigurationXML method testTrimValue.
@SuppressWarnings("unchecked")
public void testTrimValue() throws Exception {
InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("test-trim-value.xml");
assertNotNull(is);
try {
IBindingFactory bfact = BindingDirectory.getFactory(XMLObject.class);
IUnmarshallingContext uctx = bfact.createUnmarshallingContext();
Configuration conf = (Configuration) uctx.unmarshalDocument(is, null);
assertNotNull(conf);
Iterator it = conf.getContainerLifecyclePluginIterator();
assertNotNull(it);
assertTrue(it.hasNext());
ContainerLifecyclePlugin conlp = (ContainerLifecyclePlugin) it.next();
assertEquals("container-lifecycle-plugin-type", conlp.getType());
assertNotNull(conlp.getInitParams());
assertEquals("container-lifecycle-plugin-value-param-value", (conlp.getInitParams().getValueParam("container-lifecycle-plugin-value-param-name")).getValue());
it = conf.getComponentLifecyclePluginIterator();
assertNotNull(it);
assertTrue(it.hasNext());
ComponentLifecyclePlugin comlp = (ComponentLifecyclePlugin) it.next();
assertEquals("component-lifecycle-plugin", comlp.getType());
ManageableComponents mc = comlp.getManageableComponents();
assertNotNull(mc);
assertEquals("manageable-components-component-type", mc.getComponentsType().get(0));
ValuesParam valuesParam = comlp.getInitParams().getValuesParam("component-lifecycle-plugin-values-param-name");
assertNotNull(valuesParam);
assertNotNull(valuesParam.getValues());
assertTrue(valuesParam.getValues().contains("component-lifecycle-plugin-values-param-value1"));
assertTrue(valuesParam.getValues().contains("component-lifecycle-plugin-values-param-value2"));
Component c = conf.getComponent("component-key1");
assertNotNull(c);
assertEquals("component-type1", c.getType());
PropertiesParam propertiesParam = c.getInitParams().getPropertiesParam("component-key1-properties-param-name");
assertNotNull(propertiesParam);
assertEquals("component-key1-properties-param-prop-value", propertiesParam.getProperty("component-key1-properties-param-prop-name"));
c = conf.getComponent("component-type2");
assertNotNull(c);
ObjectParameter objectParameter = c.getInitParams().getObjectParam("component-key2-object-param-name");
assertNotNull(objectParameter);
MyObject o = (MyObject) objectParameter.getObject();
assertNotNull(o);
assertEquals("string-value", o.field1);
assertEquals(1, o.field2);
assertEquals(1l, o.field3);
assertEquals(1d, o.field4);
assertEquals(true, o.field5);
assertNotNull(o.field6);
assertEquals("entry-value", o.field6.get("entry-name"));
assertNotNull(o.field7);
assertTrue(o.field7.contains("string-value"));
assertNotNull(o.field8);
assertEquals(1, o.field8[0]);
List list = c.getComponentPlugins();
assertNotNull(list);
assertFalse(list.isEmpty());
ComponentPlugin cp = (ComponentPlugin) list.get(0);
assertEquals("component-plugins-name", cp.getName());
assertEquals("set-method-name", cp.getSetMethod());
assertEquals("component-plugins-type", cp.getType());
assertEquals(1, cp.getPriority());
it = conf.getExternalComponentPluginsIterator();
assertNotNull(it);
assertTrue(it.hasNext());
ExternalComponentPlugins ecps = (ExternalComponentPlugins) it.next();
assertEquals("target-component-name", ecps.getTargetComponent());
list = ecps.getComponentPlugins();
assertNotNull(list);
assertFalse(list.isEmpty());
cp = (ComponentPlugin) list.get(0);
assertEquals("component-plugins-name", cp.getName());
assertEquals("set-method-name", cp.getSetMethod());
assertEquals("component-plugins-type", cp.getType());
assertEquals(1, cp.getPriority());
list = conf.getImports();
assertNotNull(list);
assertFalse(list.isEmpty());
assertEquals("import-value", list.get(0));
list = conf.getRemoveConfiguration();
assertNotNull(list);
assertFalse(list.isEmpty());
assertEquals("remove-configuration-value", list.get(0));
} finally {
try {
is.close();
} catch (Exception e) {
// ignore me
}
}
}
use of org.exoplatform.container.xml.Component 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;
}
Aggregations