Search in sources :

Example 1 with Module

use of com.qcadoo.plugin.api.Module in project qcadoo by qcadoo.

the class ModuleFactoryAccessorTest method shouldCallInitOnAllModuleFactories.

@Test
public void shouldCallInitOnAllModuleFactories() throws Exception {
    // given
    MultiTenantUtil multiTenantUtil = new MultiTenantUtil();
    ReflectionTestUtils.setField(multiTenantUtil, "multiTenantService", new DefaultMultiTenantService());
    multiTenantUtil.init();
    PluginStateResolver mockPluginStateResolver = mock(PluginStateResolver.class);
    given(mockPluginStateResolver.isEnabled("plugin1")).willReturn(false);
    given(mockPluginStateResolver.isEnabled("plugin2")).willReturn(true);
    PluginUtilsService pluginUtil = new PluginUtilsService(mockPluginStateResolver);
    pluginUtil.init();
    ModuleFactory<?> moduleFactory1 = mock(ModuleFactory.class);
    given(moduleFactory1.getIdentifier()).willReturn("module1");
    ModuleFactory<?> moduleFactory2 = mock(ModuleFactory.class);
    given(moduleFactory2.getIdentifier()).willReturn("module2");
    DefaultModuleFactoryAccessor moduleFactoryAccessor = new DefaultModuleFactoryAccessor();
    List<ModuleFactory<?>> factoriesList = new ArrayList<>();
    factoriesList.add(moduleFactory1);
    factoriesList.add(moduleFactory2);
    moduleFactoryAccessor.setModuleFactories(factoriesList);
    InternalPlugin plugin1 = mock(InternalPlugin.class);
    Module module111 = mock(Module.class);
    Module module112 = mock(Module.class);
    Module module12 = mock(Module.class);
    given(plugin1.getModules(moduleFactory1)).willReturn(newArrayList(module111, module112));
    given(plugin1.getModules(moduleFactory2)).willReturn(newArrayList(module12));
    given(plugin1.hasState(PluginState.ENABLED)).willReturn(false);
    given(plugin1.getIdentifier()).willReturn("plugin1");
    InternalPlugin plugin2 = mock(InternalPlugin.class);
    Module module21 = mock(Module.class);
    Module module22 = mock(Module.class);
    given(plugin2.getModules(moduleFactory1)).willReturn(newArrayList(module21));
    given(plugin2.getModules(moduleFactory2)).willReturn(newArrayList(module22));
    given(plugin2.hasState(PluginState.ENABLED)).willReturn(true);
    given(plugin2.getIdentifier()).willReturn("plugin2");
    List<Plugin> plugins = newArrayList(plugin1, plugin2);
    // when
    moduleFactoryAccessor.init(plugins);
    // then
    InOrder inOrder = inOrder(moduleFactory1, moduleFactory2, module111, module112, module12, module21, module22);
    inOrder.verify(moduleFactory1).preInit();
    inOrder.verify(module111).init();
    inOrder.verify(module112).init();
    inOrder.verify(module21).init();
    inOrder.verify(moduleFactory1).postInit();
    inOrder.verify(moduleFactory2).preInit();
    inOrder.verify(module12).init();
    inOrder.verify(module22).init();
    inOrder.verify(moduleFactory2).postInit();
    inOrder.verify(module21).enableOnStartup();
    inOrder.verify(module21).multiTenantEnableOnStartup();
    inOrder.verify(module22).enableOnStartup();
    inOrder.verify(module22).multiTenantEnableOnStartup();
    inOrder.verify(module12).disableOnStartup();
    inOrder.verify(module12).multiTenantDisableOnStartup();
    inOrder.verify(module112).disableOnStartup();
    inOrder.verify(module112).multiTenantDisableOnStartup();
    inOrder.verify(module111).disableOnStartup();
    inOrder.verify(module111).multiTenantDisableOnStartup();
}
Also used : MultiTenantUtil(com.qcadoo.tenant.api.MultiTenantUtil) PluginUtilsService(com.qcadoo.plugin.internal.PluginUtilsService) InOrder(org.mockito.InOrder) ArrayList(java.util.ArrayList) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) InternalPlugin(com.qcadoo.plugin.internal.api.InternalPlugin) ModuleFactory(com.qcadoo.plugin.api.ModuleFactory) PluginStateResolver(com.qcadoo.plugin.api.PluginStateResolver) Module(com.qcadoo.plugin.api.Module) DefaultMultiTenantService(com.qcadoo.tenant.internal.DefaultMultiTenantService) Plugin(com.qcadoo.plugin.api.Plugin) InternalPlugin(com.qcadoo.plugin.internal.api.InternalPlugin) Test(org.junit.Test)

Example 2 with Module

use of com.qcadoo.plugin.api.Module in project qcadoo by qcadoo.

the class DefaultModuleFactoryAccessor method multiTenantDisable.

@Override
public void multiTenantDisable(final int tenantId, final Plugin plugin) {
    List<ModuleFactory<?>> factories = new ArrayList<ModuleFactory<?>>(moduleFactoryRegistry.values());
    Collections.reverse(factories);
    for (ModuleFactory<?> moduleFactory : factories) {
        List<Module> modules = ((InternalPlugin) plugin).getModules(moduleFactory);
        Collections.reverse(modules);
        for (final Module module : modules) {
            MultiTenantUtil.doInMultiTenantContext(tenantId, new MultiTenantCallback() {

                @Override
                public void invoke() {
                    module.multiTenantDisable();
                }
            });
        }
    }
}
Also used : InternalPlugin(com.qcadoo.plugin.internal.api.InternalPlugin) MultiTenantCallback(com.qcadoo.tenant.api.MultiTenantCallback) ModuleFactory(com.qcadoo.plugin.api.ModuleFactory) ArrayList(java.util.ArrayList) Module(com.qcadoo.plugin.api.Module)

Example 3 with Module

use of com.qcadoo.plugin.api.Module in project qcadoo by qcadoo.

the class DefaultModuleFactoryAccessor method init.

@Override
public void init(final List<Plugin> pluginsToInitialize) {
    for (ModuleFactory<?> moduleFactory : moduleFactoryRegistry.values()) {
        moduleFactory.preInit();
        for (Plugin plugin : pluginsToInitialize) {
            for (Module module : ((InternalPlugin) plugin).getModules(moduleFactory)) {
                module.init();
            }
        }
        moduleFactory.postInit();
    }
    List<ModuleFactory<?>> factories = new ArrayList<ModuleFactory<?>>(moduleFactoryRegistry.values());
    List<Plugin> plugins = new ArrayList<Plugin>(pluginsToInitialize);
    for (ModuleFactory<?> moduleFactory : factories) {
        for (final Plugin plugin : plugins) {
            List<Module> modules = ((InternalPlugin) plugin).getModules(moduleFactory);
            for (final Module module : modules) {
                if (plugin.hasState(PluginState.ENABLED)) {
                    module.enableOnStartup();
                    MultiTenantUtil.doInMultiTenantContext(new MultiTenantCallback() {

                        @Override
                        public void invoke() {
                            module.multiTenantEnableOnStartup();
                        }
                    });
                }
            }
        }
    }
    Collections.reverse(factories);
    Collections.reverse(plugins);
    for (ModuleFactory<?> moduleFactory : factories) {
        for (final Plugin plugin : plugins) {
            List<Module> modules = ((InternalPlugin) plugin).getModules(moduleFactory);
            Collections.reverse(modules);
            for (final Module module : modules) {
                if (!plugin.hasState(PluginState.ENABLED) && !plugin.hasState(PluginState.ENABLING)) {
                    module.disableOnStartup();
                    MultiTenantUtil.doInMultiTenantContext(new MultiTenantCallback() {

                        @Override
                        public void invoke() {
                            module.multiTenantDisableOnStartup();
                        }
                    });
                }
            }
        }
    }
}
Also used : InternalPlugin(com.qcadoo.plugin.internal.api.InternalPlugin) MultiTenantCallback(com.qcadoo.tenant.api.MultiTenantCallback) ModuleFactory(com.qcadoo.plugin.api.ModuleFactory) ArrayList(java.util.ArrayList) Module(com.qcadoo.plugin.api.Module) Plugin(com.qcadoo.plugin.api.Plugin) InternalPlugin(com.qcadoo.plugin.internal.api.InternalPlugin)

Example 4 with Module

use of com.qcadoo.plugin.api.Module in project qcadoo by qcadoo.

the class PluginChangeStateToTest method assertOperationSupported.

private void assertOperationSupported(final PluginState from, final PluginState to, final boolean callEnable, final boolean callDisable) throws Exception {
    // given
    ModuleFactory<?> moduleFactory = mock(ModuleFactory.class);
    Module module1 = mock(Module.class);
    Module module2 = mock(Module.class);
    InternalPlugin plugin = DefaultPlugin.Builder.identifier("identifier", Lists.<ModuleFactory<?>>newArrayList(moduleFactory)).withModule(moduleFactory, module1).withModule(moduleFactory, module2).build();
    if (from != null) {
        plugin.changeStateTo(from);
    }
    given(mockPluginStateResolver.isEnabled("identifier")).willReturn(PluginState.ENABLED.equals(to));
    // when
    plugin.changeStateTo(to);
    // then
    assertEquals(to, plugin.getState());
    assertTrue(plugin.hasState(to));
    if (callEnable) {
        verify(module1).enable();
        // verify(module1).multiTenantEnable();
        verify(module2).enable();
    } else {
        verify(module1, never()).enable();
        verify(module2, never()).multiTenantEnable();
    }
    if (callDisable) {
        verify(module1).disable();
    // verify(module2).multiTenantDisable();
    } else {
        verify(module1, never()).disable();
        verify(module2, never()).multiTenantDisable();
    }
}
Also used : InternalPlugin(com.qcadoo.plugin.internal.api.InternalPlugin) ModuleFactory(com.qcadoo.plugin.api.ModuleFactory) Module(com.qcadoo.plugin.api.Module)

Example 5 with Module

use of com.qcadoo.plugin.api.Module in project qcadoo by qcadoo.

the class DefaultPluginDescriptorParser method addModules.

private void addModules(final Node modulesNode, final Builder pluginBuilder, final String pluginIdentifier) {
    for (Node child : getChildNodes(modulesNode)) {
        ModuleFactory<?> moduleFactory = moduleFactoryAccessor.getModuleFactory(child.getLocalName());
        LOG.info("Parsing module " + child.getLocalName() + " for plugin " + pluginIdentifier);
        Module module = moduleFactory.parse(pluginIdentifier, convertNodeToJdomElement(child));
        checkNotNull(module, "Module for " + child.getLocalName() + " is null");
        pluginBuilder.withModule(moduleFactory, module);
    }
}
Also used : Node(org.w3c.dom.Node) Module(com.qcadoo.plugin.api.Module)

Aggregations

Module (com.qcadoo.plugin.api.Module)6 ModuleFactory (com.qcadoo.plugin.api.ModuleFactory)4 InternalPlugin (com.qcadoo.plugin.internal.api.InternalPlugin)4 ArrayList (java.util.ArrayList)3 Plugin (com.qcadoo.plugin.api.Plugin)2 MultiTenantCallback (com.qcadoo.tenant.api.MultiTenantCallback)2 Lists.newArrayList (com.google.common.collect.Lists.newArrayList)1 PluginStateResolver (com.qcadoo.plugin.api.PluginStateResolver)1 PluginUtilsService (com.qcadoo.plugin.internal.PluginUtilsService)1 MultiTenantUtil (com.qcadoo.tenant.api.MultiTenantUtil)1 DefaultMultiTenantService (com.qcadoo.tenant.internal.DefaultMultiTenantService)1 Test (org.junit.Test)1 InOrder (org.mockito.InOrder)1 Node (org.w3c.dom.Node)1