Search in sources :

Example 1 with ComponentPlugin

use of org.exoplatform.container.component.ComponentPlugin in project kernel by exoplatform.

the class MX4JComponentAdapter method addComponentPlugin.

private void addComponentPlugin(boolean debug, final Object component, List<org.exoplatform.container.xml.ComponentPlugin> plugins, ExoContainer container) throws Exception {
    if (plugins == null)
        return;
    for (org.exoplatform.container.xml.ComponentPlugin plugin : plugins) {
        try {
            Class<?> pluginClass = ClassLoading.forName(plugin.getType(), this);
            ComponentPlugin cplugin = (ComponentPlugin) container.createComponent(pluginClass, plugin.getInitParams());
            cplugin.setName(plugin.getName());
            cplugin.setDescription(plugin.getDescription());
            Class<?> clazz = component.getClass();
            final Method m = getSetMethod(clazz, plugin.getSetMethod(), pluginClass);
            if (m == null) {
                LOG.error("Cannot find the method '" + plugin.getSetMethod() + "' that has only one parameter of type '" + pluginClass.getName() + "' in the class '" + clazz.getName() + "'.");
                continue;
            }
            final Object[] params = { cplugin };
            SecurityHelper.doPrivilegedExceptionAction(new PrivilegedExceptionAction<Void>() {

                public Void run() throws Exception {
                    m.invoke(component, params);
                    return null;
                }
            });
            if (debug)
                LOG.debug("==> add component plugin: " + cplugin);
            cplugin.setName(plugin.getName());
            cplugin.setDescription(plugin.getDescription());
        } catch (Exception ex) {
            LOG.error("Failed to instanciate plugin " + plugin.getName() + " for component " + component + ": " + ex.getMessage(), ex);
        }
    }
}
Also used : ComponentPlugin(org.exoplatform.container.component.ComponentPlugin) Method(java.lang.reflect.Method) PrivilegedActionException(java.security.PrivilegedActionException) DefinitionException(org.exoplatform.container.context.DefinitionException)

Example 2 with ComponentPlugin

use of org.exoplatform.container.component.ComponentPlugin in project kernel by exoplatform.

the class MX4JComponentAdapterMT method createPlugin.

private ComponentTask<Void> createPlugin(final MX4JComponentAdapterMT<T> caller, final ConcurrentContainerMT exocontainer, final Class<?> pluginClass, final boolean debug, final org.exoplatform.container.xml.ComponentPlugin plugin, final Constructor<T> constructor, InitParams params, List<Dependency> lDependencies) throws Exception {
    final Object[] args = exocontainer.getArguments(constructor, params, lDependencies);
    return new ComponentTask<Void>("create/add plugin " + plugin.getName() + " for component " + getComponentImplementation().getName(), exocontainer, caller, ComponentTaskType.INIT) {

        public Void execute(final CreationalContextComponentAdapter<?> cCtx) throws Exception {
            try {
                getContainer().loadArguments(args);
                ComponentPlugin cplugin = (ComponentPlugin) constructor.newInstance(args);
                cplugin.setName(plugin.getName());
                cplugin.setDescription(plugin.getDescription());
                Class<?> clazz = getComponentImplementation();
                final Method m = getSetMethod(clazz, plugin.getSetMethod(), pluginClass);
                if (m == null) {
                    LOG.error("Cannot find the method '" + plugin.getSetMethod() + "' that has only one parameter of type '" + pluginClass.getName() + "' in the class '" + clazz.getName() + "'.");
                    return null;
                }
                final Object[] params = { cplugin };
                SecurityHelper.doPrivilegedExceptionAction(new PrivilegedExceptionAction<Void>() {

                    public Void run() throws Exception {
                        m.invoke(cCtx.get(), params);
                        return null;
                    }
                });
                if (debug)
                    LOG.debug("==> add component plugin: " + cplugin);
                cplugin.setName(plugin.getName());
                cplugin.setDescription(plugin.getDescription());
                return null;
            } catch (InvocationTargetException e) {
                if (e.getCause() instanceof Exception) {
                    throw (Exception) e.getCause();
                }
                throw e;
            }
        }
    };
}
Also used : ComponentPlugin(org.exoplatform.container.component.ComponentPlugin) ComponentTask(org.exoplatform.container.ComponentTask) Method(java.lang.reflect.Method) CyclicDependencyException(org.exoplatform.container.CyclicDependencyException) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException) CreationalContextComponentAdapter(org.exoplatform.container.ConcurrentContainer.CreationalContextComponentAdapter)

Example 3 with ComponentPlugin

use of org.exoplatform.container.component.ComponentPlugin in project kernel by exoplatform.

the class TestContainer method testPriorityPlugins.

public void testPriorityPlugins() {
    RootContainer rootContainer = RootContainer.getInstance();
    PortalContainer pcontainer = rootContainer.getPortalContainer("portal");
    assertNotNull(pcontainer);
    PriorityService ps = (PriorityService) pcontainer.getComponentInstanceOfType(PriorityService.class);
    assertNotNull(ps);
    List<ComponentPlugin> l = ps.getPlugins();
    assertNotNull(l);
    assertEquals(3, l.size());
    assertEquals("PluginPriority3", l.get(0).getName());
    assertEquals("PluginPriority1", l.get(1).getName());
    assertEquals("PluginPriority2", l.get(2).getName());
}
Also used : ComponentPlugin(org.exoplatform.container.component.ComponentPlugin) PriorityService(org.exoplatform.mocks.PriorityService) RootContainer(org.exoplatform.container.RootContainer) PortalContainer(org.exoplatform.container.PortalContainer)

Aggregations

ComponentPlugin (org.exoplatform.container.component.ComponentPlugin)3 Method (java.lang.reflect.Method)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 PrivilegedActionException (java.security.PrivilegedActionException)1 ComponentTask (org.exoplatform.container.ComponentTask)1 CreationalContextComponentAdapter (org.exoplatform.container.ConcurrentContainer.CreationalContextComponentAdapter)1 CyclicDependencyException (org.exoplatform.container.CyclicDependencyException)1 PortalContainer (org.exoplatform.container.PortalContainer)1 RootContainer (org.exoplatform.container.RootContainer)1 DefinitionException (org.exoplatform.container.context.DefinitionException)1 PriorityService (org.exoplatform.mocks.PriorityService)1