From c82fb5fde4d5afbe48a3c60ff6b2d4c538285d91 Mon Sep 17 00:00:00 2001 From: jensp Date: Wed, 2 Dec 2015 16:47:26 +0000 Subject: [PATCH] CCM NG: New devel bundle using Wildfly Swarm for runtime. Not tested yet! git-svn-id: https://svn.libreccm.org/ccm/ccm_ng@3758 8810af33-2d31-482b-a856-94f89814c4df --- ccm-bundle-devel/pom.xml | 86 ++++++++ .../org/libreccm/wildfly/swarm/Bundle.java | 187 ++++++++++++++++++ .../main/resources/META-INF/persistence.xml | 42 ++++ pom.xml | 3 +- 4 files changed, 317 insertions(+), 1 deletion(-) create mode 100644 ccm-bundle-devel/pom.xml create mode 100644 ccm-bundle-devel/src/main/java/org/libreccm/wildfly/swarm/Bundle.java create mode 100644 ccm-bundle-devel/src/main/resources/META-INF/persistence.xml diff --git a/ccm-bundle-devel/pom.xml b/ccm-bundle-devel/pom.xml new file mode 100644 index 000000000..bb95a59d6 --- /dev/null +++ b/ccm-bundle-devel/pom.xml @@ -0,0 +1,86 @@ + + + + 4.0.0 + + + UTF-8 + ${maven.build.timestamp} + yyyy-MM-dd'T'HH:mm:ss'Z'Z + 1.0.0.Alpha5 + + + + org.libreccm + libreccm-parent + 7.0.0-SNAPSHOT + + + org.libreccm + ccm-bundle-devel + + LibreCCM Devel Bundle + + + + Lesser GPL 2.1 + http://www.gnu.org/licenses/old-licenses/lgpl-2.1 + + + + + + org.libreccm + ccm-core + 7.0.0-SNAPSHOT + + + + com.h2database + h2 + + + + org.wildfly.swarm + wildfly-swarm-undertow + ${wildfly.swarm.version} + + + + org.wildfly.swarm + wildfly-swarm-jpa + ${wildfly.swarm.version} + + + + org.wildfly.swarm + wildfly-swarm-datasources + ${wildfly.swarm.version} + + + + + + + + org.wildfly.swarm + wildfly-swarm-plugin + ${wildfly.swarm.version} + + org.libreccm.wildfly.swarm.Bundle + + + + + package + + + + + + + + diff --git a/ccm-bundle-devel/src/main/java/org/libreccm/wildfly/swarm/Bundle.java b/ccm-bundle-devel/src/main/java/org/libreccm/wildfly/swarm/Bundle.java new file mode 100644 index 000000000..b996f473a --- /dev/null +++ b/ccm-bundle-devel/src/main/java/org/libreccm/wildfly/swarm/Bundle.java @@ -0,0 +1,187 @@ +/* + * Copyright (C) 2015 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.wildfly.swarm; + +import org.jboss.shrinkwrap.api.ShrinkWrap; +import org.jboss.shrinkwrap.api.asset.ClassLoaderAsset; +import org.wildfly.swarm.config.datasources.DataSource; +import org.wildfly.swarm.config.datasources.DataSourceConfigurator; +import org.wildfly.swarm.config.datasources.JDBCDriver; +import org.wildfly.swarm.config.datasources.JDBCDriverConfigurator; +import org.wildfly.swarm.container.Container; +import org.wildfly.swarm.datasources.DatasourcesFraction; +import org.wildfly.swarm.jpa.JPAFraction; +import org.wildfly.swarm.undertow.WARArchive; + +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.Properties; + +/** + * + * @author Jens Pelzetter + */ +public class Bundle { + + public static void main(final String[] args) throws Exception { + + final Properties config = getConfiguration(); + + final Container container = new Container(); + + final JDBCDriverConfigurator configurator + = new JDBCDriverConfigurator() { + + @Override + public void configure(final JDBCDriver driver) { + driver.driverDatasourceClassName(config.getProperty( + "db.driver.datasource.classname")); + driver.xaDatasourceClass(config.getProperty( + "db.xa.datasource.classname")); + driver.driverModuleName("db.driver.module.name"); + } + + }; + + final DataSourceConfigurator dsConfigurator + = new DataSourceConfigurator() { + + @Override + public void configure(final DataSource dataSource) { + dataSource.driverName("db-driver"); + dataSource.connectionUrl( + config.getProperty("db.connection.url")); + dataSource.userName(config.getProperty("db.user.name")); + dataSource.password(config.getProperty("db.password")); + } + + }; + + container.fraction(new DatasourcesFraction().jdbcDriver("db-driver", + configurator) + .dataSource("java:/comp/env/jdbc/ccm-core/db", dsConfigurator)); + + container.fraction(new JPAFraction().inhibitDefaultDatasource() + .defaultDatasource("java:/comp/env/jdbc/ccm-core/db")); + + //Remove when CCM installer is available + setup(config); + + container.start(); + + final WARArchive deployment = ShrinkWrap.create(WARArchive.class); + deployment.addAsWebInfResource( + new ClassLoaderAsset( + "META-INF/persistence.xml", + Bundle.class.getClassLoader()), + "classes/META-INF/persistence.xml"); + deployment.addAllDependencies(); + + container.deploy(deployment); + } + + private static Properties getConfiguration() throws IOException { + final String defaultConfig = String.format( + "%s/configuration.properties", System.getProperty("user.dir")); + final String config = System.getProperty("ccm.config", defaultConfig); + + final FileInputStream stream = new FileInputStream(config); + final Properties properties = new Properties(); + properties.load(stream); + + return properties; + } + + private static Properties getSetupParameters() throws IOException { + final String defaultParameters = String.format( + "%s/setup.properties", System.getProperty("user.dir")); + final String parameters = System.getProperty("ccm.setup.parameters", + defaultParameters); + + final FileInputStream stream = new FileInputStream(parameters); + final Properties properties = new Properties(); + properties.load(stream); + + return properties; + + } + + private static void setup(final Properties config) throws + ClassNotFoundException, SQLException, IOException { + + final Properties setupParameters = getSetupParameters(); + + Class.forName("org.h2.Driver"); + try (final Connection connection = DriverManager.getConnection(config + .getProperty("db.connection.url")); + final Statement statement = connection.createStatement()) { + + final ResultSet result = statement.executeQuery( + "SELECT COUNT(*) FROM USERS WHERE NAME NOT LIKE 'public-user'"); + result.next(); + final int numberOfUsers = result.getInt(1); + result.close(); + + if (numberOfUsers <= 0) { + final String adminName = setupParameters.getProperty( + "admin.name"); + final String adminEmail = setupParameters.getProperty( + "admin.email"); + final String adminGivenName = setupParameters.getProperty( + "admin.givenname"); + final String adminFamilyName = setupParameters.getProperty( + "admin.familyname"); + final String adminPassword = setupParameters.getProperty( + "admin.password"); + + statement.executeUpdate(String.format( + "INSERT INTO PARTIES(PARTY_ID, NAME) " + + "VALUES(-10, '%s')", + adminName)); + statement.executeUpdate(String.format( + "INSERT INTO USERS(PARTY_ID, GIVEN_NAME, FAMILY_NAME, EMAIL_ADDRESS, PASSWORD) " + + "VALUES (-10, '%s', '%s', '%s', '%s'),", + adminGivenName, + adminFamilyName, + adminEmail, + adminPassword + )); + statement.executeUpdate("INSERT INTO CCM_ROLES(roleId, name) " + + "VALUES(-10, 'admin'"); + statement.executeUpdate("INSERT INTO ROLE_MEMBERSHIPS(" + + "MEMBERSHIP_ID, MEMBER_ID, ROLE_ID) " + + "VALUES(-10, -10, 10)"); + + statement.close(); + } + + //} catch(SQLException ex) { + } finally { + + } + + } + +} diff --git a/ccm-bundle-devel/src/main/resources/META-INF/persistence.xml b/ccm-bundle-devel/src/main/resources/META-INF/persistence.xml new file mode 100644 index 000000000..e9f749299 --- /dev/null +++ b/ccm-bundle-devel/src/main/resources/META-INF/persistence.xml @@ -0,0 +1,42 @@ + + + + + + + + + org.hibernate.jpa.HibernatePersistenceProvider + + java:/comp/env/jdbc/ccm-core/db + + + + + + + + + + + + + + + diff --git a/pom.xml b/pom.xml index 240b5d918..783cf9fc4 100644 --- a/pom.xml +++ b/pom.xml @@ -41,8 +41,9 @@ + ccm-bundle-devel ccm-test-bundle-wildfly8 - + ccm-archetype-module ccm-cms