From e4f8ca3b973178a13e1e991092ae3b2dbd8b8ffe Mon Sep 17 00:00:00 2001 From: jensp Date: Thu, 9 Jun 2016 09:40:09 +0000 Subject: [PATCH] CCM NG: Shortcuts module now deploys successfully git-svn-id: https://svn.libreccm.org/ccm/ccm_ng@4147 8810af33-2d31-482b-a856-94f89814c4df --- .../src/main/resources/log4j2.xml | 3 + .../org/libreccm/modules/CcmIntegrator.java | 77 +++++++++++-------- .../modules/DependencyTreeManager.java | 1 + .../security/ShortcutsRolesSetup.java | 50 ++++++++++++ .../org/libreccm/shortcuts/Shortcuts.java | 16 ++-- .../shortcuts/ShortcutsConstants.java | 2 + .../libreccm/shortcuts/ShortcutsSetup.java | 25 +++--- .../h2/V7_0_0_0__create_tables.sql | 9 --- .../oracle/V1_0_0_0__create_tables.sql | 12 --- .../pqsql/V7_0_0_0__create_tables.sql | 9 --- ...g.libreccm.shortcuts.Shortcuts.properties} | 0 11 files changed, 122 insertions(+), 82 deletions(-) create mode 100644 ccm-shortcuts/src/main/java/org/libreccm/security/ShortcutsRolesSetup.java delete mode 100644 ccm-shortcuts/src/main/resources/db/migrations/org.libreccm.ccm-shortcuts/h2/V7_0_0_0__create_tables.sql delete mode 100644 ccm-shortcuts/src/main/resources/db/migrations/org.libreccm.ccm-shortcuts/oracle/V1_0_0_0__create_tables.sql delete mode 100644 ccm-shortcuts/src/main/resources/db/migrations/org.libreccm.ccm-shortcuts/pqsql/V7_0_0_0__create_tables.sql rename ccm-shortcuts/src/main/resources/module-info/{org.libreccm.shortcuts.ccm-shortcuts.properties => org.libreccm.shortcuts.Shortcuts.properties} (100%) diff --git a/ccm-bundle-devel-wildfly-web/src/main/resources/log4j2.xml b/ccm-bundle-devel-wildfly-web/src/main/resources/log4j2.xml index c1a52dc5d..c7937b77e 100644 --- a/ccm-bundle-devel-wildfly-web/src/main/resources/log4j2.xml +++ b/ccm-bundle-devel-wildfly-web/src/main/resources/log4j2.xml @@ -36,6 +36,9 @@ + + diff --git a/ccm-core/src/main/java/org/libreccm/modules/CcmIntegrator.java b/ccm-core/src/main/java/org/libreccm/modules/CcmIntegrator.java index 0027ab61b..f7eae857b 100644 --- a/ccm-core/src/main/java/org/libreccm/modules/CcmIntegrator.java +++ b/ccm-core/src/main/java/org/libreccm/modules/CcmIntegrator.java @@ -54,14 +54,14 @@ import javax.sql.DataSource; public class CcmIntegrator implements Integrator { private static final Logger LOGGER = LogManager.getLogger( - CcmIntegrator.class); + CcmIntegrator.class); /** * Name of the property which is used to retrieve the data source in use * from Hibernate. */ private static final String DATASOURCE_PROPERTY - = "hibernate.connection.datasource"; + = "hibernate.connection.datasource"; /** * Service loader containing all modules. Initialised by the @@ -94,14 +94,23 @@ public class CcmIntegrator implements Integrator { try { //Create dependency tree for the modules final DependencyTreeManager treeManager - = new DependencyTreeManager(); + = new DependencyTreeManager(); final List tree = treeManager.generateTree(modules); final List orderedNodes = treeManager.orderModules(tree); + if (LOGGER.isDebugEnabled()) { + LOGGER.debug("Ordered list of modules:"); + orderedNodes.forEach(m -> { + LOGGER.debug("\t{}-{}", + m.getModuleInfo().getModuleName(), + m.getModuleInfo().getModuleVersion()); + }); + } + // //Get DataSource and Connection from the sessionFactory of // //Hibernate. final DataSource dataSource = (DataSource) sessionFactory. - getProperties().get(DATASOURCE_PROPERTY); + getProperties().get(DATASOURCE_PROPERTY); if (dataSource == null) { throw new IllegalStateException("No data source available."); } @@ -112,13 +121,16 @@ public class CcmIntegrator implements Integrator { final Flyway flyway = new Flyway(); flyway.setDataSource(dataSource); final StringBuffer buffer = new StringBuffer( - "db/migrations/org/libreccm/base"); + "db/migrations/org/libreccm/base"); appendDbLocation(buffer, connection); flyway.setLocations(buffer.toString()); flyway.migrate(); //Migrate the modules for (final TreeNode node : orderedNodes) { + LOGGER.debug("Applying migrations for module {}-{}", + node.getModuleInfo().getModuleName(), + node.getModuleInfo().getModuleVersion()); migrateModule(node.getModule().getClass(), dataSource); // for (Class entity : node.getModuleInfo().getModuleEntities()) { @@ -187,16 +199,15 @@ public class CcmIntegrator implements Integrator { * If the database is not supported an {@link IntegrationException} will be * thrown. * - * @param buffer Buffer for the location string. + * @param buffer Buffer for the location string. * @param connection The JDBC connection object. * - * @throws SQLException If an error occurs while accessing the - * database. + * @throws SQLException If an error occurs while accessing the database. * @throws IntegrationException If the database is not supported yet. */ private void appendDbLocation(final StringBuffer buffer, final Connection connection) - throws SQLException { + throws SQLException { switch (connection.getMetaData().getDatabaseProductName()) { case "H2": @@ -207,9 +218,9 @@ public class CcmIntegrator implements Integrator { break; default: throw new IntegrationException(String.format( - "Integration failed. Database \"%s\" is not supported yet.", - connection.getMetaData(). - getDatabaseProductName())); + "Integration failed. Database \"%s\" is not supported yet.", + connection.getMetaData(). + getDatabaseProductName())); } } @@ -225,10 +236,10 @@ public class CcmIntegrator implements Integrator { */ private String getLocation(final ModuleInfo moduleInfo, final Connection connection) - throws SQLException { + throws SQLException { final StringBuffer buffer = new StringBuffer( - "classpath:/db/migrations/"); + "classpath:/db/migrations/"); buffer.append(moduleInfo.getModuleDataPackage()); appendDbLocation(buffer, connection); @@ -238,7 +249,7 @@ public class CcmIntegrator implements Integrator { /** * Helper method for executing the migrations for a module. * - * @param module The module for which the migrations are executed. + * @param module The module for which the migrations are executed. * @param dataSource The JDBC data source for connecting to the database. * * @throws SQLException If an error occurs while applying the migrations. @@ -259,8 +270,8 @@ public class CcmIntegrator implements Integrator { //different RDBMS handle case different. if ("H2".equals(connection.getMetaData().getDatabaseProductName())) { flyway - .setSchemas(getSchemaName(moduleInfo).toUpperCase( - Locale.ROOT)); + .setSchemas(getSchemaName(moduleInfo).toUpperCase( + Locale.ROOT)); } else { flyway.setSchemas(getSchemaName(moduleInfo)); } @@ -292,11 +303,11 @@ public class CcmIntegrator implements Integrator { if (newModule) { try (Statement statement = connection.createStatement()) { statement.execute(String.format( - "INSERT INTO ccm_core.installed_modules " - + "(module_id, module_class_name, status) " - + "VALUES (%d, '%s', 'NEW')", - module.getName().hashCode(), - module.getName())); + "INSERT INTO ccm_core.installed_modules " + + "(module_id, module_class_name, status) " + + "VALUES (%d, '%s', 'NEW')", + module.getName().hashCode(), + module.getName())); } catch (SQLException ex) { throw new IntegrationException("Failed to integrate.", ex); } @@ -322,7 +333,7 @@ public class CcmIntegrator implements Integrator { //Get JDBC connection final DataSource dataSource = (DataSource) sessionFactory - .getProperties().get(DATASOURCE_PROPERTY); + .getProperties().get(DATASOURCE_PROPERTY); connection = dataSource.getConnection(); System.out.println("checking modules..."); LOGGER.info("Checking modules..."); @@ -333,10 +344,10 @@ public class CcmIntegrator implements Integrator { try (Statement query = connection.createStatement(); //Check status of each module ResultSet result = query.executeQuery( - String.format("SELECT module_class_name, status " - + "FROM ccm_core.installed_modules " + String.format("SELECT module_class_name, status " + + "FROM ccm_core.installed_modules " + "WHERE module_class_name = '%s'", - module.getClass().getName()))) { + module.getClass().getName()))) { System.out.printf("Checking status of module %s...%n", module.getClass().getName()); @@ -344,7 +355,7 @@ public class CcmIntegrator implements Integrator { //If there modules marked for uninstall remove the schema //of the module from the database. if (result.next() && ModuleStatus.UNINSTALL.toString() - .equals(result.getString("status"))) { + .equals(result.getString("status"))) { uninstallModule(connection, dataSource, module, @@ -378,7 +389,7 @@ public class CcmIntegrator implements Integrator { final DataSource dataSource, final CcmModule module, final ModuleInfo moduleInfo) - throws SQLException { + throws SQLException { LOGGER.info("Removing schema for module %s...", module.getClass().getName()); final Flyway flyway = new Flyway(); @@ -391,16 +402,16 @@ public class CcmIntegrator implements Integrator { //Delete the module from the installed modules table. try (final Statement statement = connection - .createStatement()) { + .createStatement()) { statement.addBatch(String.format( - "DELETE FROM ccm_core.installed_modules " - + "WHERE module_class_name = '%s'", - module.getClass().getName())); + "DELETE FROM ccm_core.installed_modules " + + "WHERE module_class_name = '%s'", + module.getClass().getName())); statement.executeBatch(); LOGGER.info("Done."); } catch (SQLException ex) { throw new IntegrationException( - "Failed to desintegrate", ex); + "Failed to desintegrate", ex); } } diff --git a/ccm-core/src/main/java/org/libreccm/modules/DependencyTreeManager.java b/ccm-core/src/main/java/org/libreccm/modules/DependencyTreeManager.java index e7008efb1..024e3c621 100644 --- a/ccm-core/src/main/java/org/libreccm/modules/DependencyTreeManager.java +++ b/ccm-core/src/main/java/org/libreccm/modules/DependencyTreeManager.java @@ -19,6 +19,7 @@ package org.libreccm.modules; import java.util.ArrayList; +import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; diff --git a/ccm-shortcuts/src/main/java/org/libreccm/security/ShortcutsRolesSetup.java b/ccm-shortcuts/src/main/java/org/libreccm/security/ShortcutsRolesSetup.java new file mode 100644 index 000000000..05c0faeca --- /dev/null +++ b/ccm-shortcuts/src/main/java/org/libreccm/security/ShortcutsRolesSetup.java @@ -0,0 +1,50 @@ +/* + * Copyright (C) 2016 LibreCCM Foundation. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301 USA + */ +package org.libreccm.security; + +import javax.persistence.EntityManager; +import org.libreccm.shortcuts.ShortcutsConstants; + +/** + * + * @author Jens Pelzetter + */ +public class ShortcutsRolesSetup { + + private final EntityManager entityManager; + + public ShortcutsRolesSetup(final EntityManager entityManager) { + this.entityManager = entityManager; + } + + public void setupShortcutsRoles() { + final Role shortcutsManager = new Role(); + shortcutsManager.setName("shortcuts-manager"); + entityManager.persist(shortcutsManager); + + final Permission permission = new Permission(); + permission.setGrantee(shortcutsManager); + permission.setGrantedPrivilege( + ShortcutsConstants.SHORTSCUT_MANAGE_PRIVILEGE); + permission.setObject(null); + + entityManager.persist(permission); + } + +} diff --git a/ccm-shortcuts/src/main/java/org/libreccm/shortcuts/Shortcuts.java b/ccm-shortcuts/src/main/java/org/libreccm/shortcuts/Shortcuts.java index 2f50e93ec..c40148db6 100644 --- a/ccm-shortcuts/src/main/java/org/libreccm/shortcuts/Shortcuts.java +++ b/ccm-shortcuts/src/main/java/org/libreccm/shortcuts/Shortcuts.java @@ -18,10 +18,12 @@ */ package org.libreccm.shortcuts; +import org.libreccm.core.CcmCore; import org.libreccm.modules.CcmModule; import org.libreccm.modules.InitEvent; import org.libreccm.modules.InstallEvent; import org.libreccm.modules.Module; +import org.libreccm.modules.RequiredModule; import org.libreccm.modules.ShutdownEvent; import org.libreccm.modules.UnInstallEvent; import org.libreccm.web.ApplicationType; @@ -30,11 +32,15 @@ import org.libreccm.web.ApplicationType; * * @author Jens Pelzetter */ -@Module(applicationTypes = { - @ApplicationType(name = ShortcutsConstants.SHORTCUTS_APP_TYPE, - descBundle = "org.libreccm.shortcuts.ShortcutsResources", - singleton = true, - creator = ShortcutsApplicationCreator.class)} +@Module( + requiredModules = { + @RequiredModule(module = CcmCore.class) + }, + applicationTypes = { + @ApplicationType(name = ShortcutsConstants.SHORTCUTS_APP_TYPE, + descBundle = "org.libreccm.shortcuts.ShortcutsResources", + singleton = true, + creator = ShortcutsApplicationCreator.class)} ) public class Shortcuts implements CcmModule { diff --git a/ccm-shortcuts/src/main/java/org/libreccm/shortcuts/ShortcutsConstants.java b/ccm-shortcuts/src/main/java/org/libreccm/shortcuts/ShortcutsConstants.java index 3b52e1641..5c2e36182 100644 --- a/ccm-shortcuts/src/main/java/org/libreccm/shortcuts/ShortcutsConstants.java +++ b/ccm-shortcuts/src/main/java/org/libreccm/shortcuts/ShortcutsConstants.java @@ -35,4 +35,6 @@ public final class ShortcutsConstants { */ public static final String SHORTCUTS_PRIMARY_URL = "/shortcuts/"; + public static final String SHORTSCUT_MANAGE_PRIVILEGE = "manage_shortcuts"; + } diff --git a/ccm-shortcuts/src/main/java/org/libreccm/shortcuts/ShortcutsSetup.java b/ccm-shortcuts/src/main/java/org/libreccm/shortcuts/ShortcutsSetup.java index 21c24dbb1..710e3dc4d 100644 --- a/ccm-shortcuts/src/main/java/org/libreccm/shortcuts/ShortcutsSetup.java +++ b/ccm-shortcuts/src/main/java/org/libreccm/shortcuts/ShortcutsSetup.java @@ -18,16 +18,12 @@ */ package org.libreccm.shortcuts; -import org.libreccm.cdi.utils.CdiUtil; import org.libreccm.modules.InstallEvent; -import org.libreccm.security.PermissionManager; -import org.libreccm.security.Role; -import org.libreccm.security.RoleManager; -import org.libreccm.security.RoleRepository; import org.libreccm.web.AbstractCcmApplicationSetup; import org.libreccm.web.CcmApplication; import java.util.UUID; +import org.libreccm.security.ShortcutsRolesSetup; /** * @@ -47,16 +43,17 @@ public class ShortcutsSetup extends AbstractCcmApplicationSetup { shortcuts.setPrimaryUrl(ShortcutsConstants.SHORTCUTS_PRIMARY_URL); getEntityManager().persist(shortcuts); - final CdiUtil cdiUtil = CdiUtil.createCdiUtil(); - final RoleRepository roleRepository = cdiUtil.findBean( - RoleRepository.class); - final Role shortcutsManager = new Role(); - shortcutsManager.setName("shortcuts-manager"); - roleRepository.save(shortcutsManager); + final ShortcutsRolesSetup rolesSetup = new ShortcutsRolesSetup( + getEntityManager()); + rolesSetup.setupShortcutsRoles(); - final PermissionManager permissionManager = cdiUtil.findBean( - PermissionManager.class); - permissionManager.grantPrivilege("manage_shortcuts", shortcutsManager); +// final CdiUtil cdiUtil = CdiUtil.createCdiUtil(); +// final RoleRepository roleRepository = cdiUtil.findBean( +// RoleRepository.class); +// roleRepository.save(shortcutsManager); +// final PermissionManager permissionManager = cdiUtil.findBean( +// PermissionManager.class); +// permissionManager.grantPrivilege("manage_shortcuts", shortcutsManager); } } diff --git a/ccm-shortcuts/src/main/resources/db/migrations/org.libreccm.ccm-shortcuts/h2/V7_0_0_0__create_tables.sql b/ccm-shortcuts/src/main/resources/db/migrations/org.libreccm.ccm-shortcuts/h2/V7_0_0_0__create_tables.sql deleted file mode 100644 index e24a0df37..000000000 --- a/ccm-shortcuts/src/main/resources/db/migrations/org.libreccm.ccm-shortcuts/h2/V7_0_0_0__create_tables.sql +++ /dev/null @@ -1,9 +0,0 @@ -create table SHORTCUTS ( - shortcut_id bigint generated by default as identity, - redirect varchar(1024), - url_key varchar(1024), - primary key (shortcut_id) -); - -alter table CCM_SHORTCUTS.SHORTCUTS - add constraint UK_4otuwtog6qqdbg4e6p8xdpw8h unique (URL_KEY); diff --git a/ccm-shortcuts/src/main/resources/db/migrations/org.libreccm.ccm-shortcuts/oracle/V1_0_0_0__create_tables.sql b/ccm-shortcuts/src/main/resources/db/migrations/org.libreccm.ccm-shortcuts/oracle/V1_0_0_0__create_tables.sql deleted file mode 100644 index 2dc4da75e..000000000 --- a/ccm-shortcuts/src/main/resources/db/migrations/org.libreccm.ccm-shortcuts/oracle/V1_0_0_0__create_tables.sql +++ /dev/null @@ -1,12 +0,0 @@ - - create table SHORTCUTS ( - shortcut_id number(19,0) not null, - redirect varchar2(1024 char), - url_key varchar2(1024 char), - primary key (shortcut_id) - ); - - create table SHORTCUTS_APP ( - object_id number(19,0) not null, - primary key (object_id) - ); diff --git a/ccm-shortcuts/src/main/resources/db/migrations/org.libreccm.ccm-shortcuts/pqsql/V7_0_0_0__create_tables.sql b/ccm-shortcuts/src/main/resources/db/migrations/org.libreccm.ccm-shortcuts/pqsql/V7_0_0_0__create_tables.sql deleted file mode 100644 index 51d267114..000000000 --- a/ccm-shortcuts/src/main/resources/db/migrations/org.libreccm.ccm-shortcuts/pqsql/V7_0_0_0__create_tables.sql +++ /dev/null @@ -1,9 +0,0 @@ -create table SHORTCUTS ( - shortcut_id int8 not null, - redirect varchar(1024), - url_key varchar(1024), - primary key (shortcut_id) -); - -alter table CCM_SHORTCUTS.SHORTCUTS - add constraint UK_4otuwtog6qqdbg4e6p8xdpw8h unique (URL_KEY); diff --git a/ccm-shortcuts/src/main/resources/module-info/org.libreccm.shortcuts.ccm-shortcuts.properties b/ccm-shortcuts/src/main/resources/module-info/org.libreccm.shortcuts.Shortcuts.properties similarity index 100% rename from ccm-shortcuts/src/main/resources/module-info/org.libreccm.shortcuts.ccm-shortcuts.properties rename to ccm-shortcuts/src/main/resources/module-info/org.libreccm.shortcuts.Shortcuts.properties