org.wildfly.swarm.datasources.DatasourcesFraction Java Examples

The following examples show how to use org.wildfly.swarm.datasources.DatasourcesFraction. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Main.java    From thorntail with Apache License 2.0 6 votes vote down vote up
public static void main(String... args) throws Exception {
    swarm = new Swarm(args);

    swarm.fraction(
            new DatasourcesFraction()
                    .jdbcDriver("h2", (d) -> {
                        d.driverClassName("org.h2.Driver");
                        d.xaDatasourceClass("org.h2.jdbcx.JdbcDataSource");
                        d.driverModuleName("com.h2database.h2");
                    })
                    .dataSource("ExampleDS", (ds) -> {
                        ds.driverName("h2");
                        ds.connectionUrl("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE");
                        ds.userName("sa");
                        ds.password("sa");
                    })
    );

    swarm.start().deploy();
}
 
Example #2
Source File: JPAFraction.java    From ARCHIVE-wildfly-swarm with Apache License 2.0 6 votes vote down vote up
@Override
public void initialize(Container.InitContext initContext) {
    if (!inhibitDefaultDatasource) {
        final DatasourcesFraction datasources = new DatasourcesFraction()
                .jdbcDriver("h2", (d) -> {
                    d.driverClassName("org.h2.Driver");
                    d.xaDatasourceClass("org.h2.jdbcx.JdbcDataSource");
                    d.driverModuleName("com.h2database.h2");
                })
                .dataSource("ExampleDS", (ds) -> {
                    ds.driverName("h2");
                    ds.connectionUrl("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE");
                    ds.userName("sa");
                    ds.password("sa");
                });

        initContext.fraction(datasources);
        System.err.println("setting default Datasource to ExampleDS");
        defaultDatasource("jboss/datasources/ExampleDS");
    }
}
 
Example #3
Source File: FractionHandlingTest.java    From ARCHIVE-wildfly-swarm with Apache License 2.0 6 votes vote down vote up
private void verifyValidDataSourceFraction(DatasourcesFraction dsFraction, String dsName, String driverName) {
    // Verify default DataSource Fraction
    assertThat(dsFraction).overridingErrorMessage("DataSourceFraction was null").isNotNull();
    assertThat(dsFraction.subresources().dataSources()).overridingErrorMessage("No DataSources specified").isNotEmpty();
    assertThat(dsFraction.subresources().dataSources().size()).overridingErrorMessage("More than one Datasource specified").isEqualTo(1);
    assertThat(dsFraction.subresources().jdbcDrivers()).overridingErrorMessage("No drivers specified").isNotEmpty();
    assertThat(dsFraction.subresources().jdbcDrivers().size()).overridingErrorMessage("More than one Driver specified").isEqualTo(1);

    // Verify DataSource
    DataSource ds = dsFraction.subresources().dataSources().get(0);
    assertThat(ds.getKey()).overridingErrorMessage("DataSource name is not " + dsName).isEqualTo(dsName);
    assertThat(ds.driverName()).overridingErrorMessage("DataSource driver is not " + driverName).isEqualTo(driverName);
    assertThat(ds.connectionUrl()).isEqualTo("jdbc:" + driverName + ":mem:test;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE");
    assertThat(ds.userName()).isEqualTo("sa");
    assertThat(ds.password()).isEqualTo("sa");

    // Verify Driver
    JDBCDriver driver = dsFraction.subresources().jdbcDrivers().get(0);
    assertThat(driver.getKey()).overridingErrorMessage("Driver name is not " + driverName).isEqualTo(driverName);
    assertThat(driver.driverDatasourceClassName()).overridingErrorMessage("Driver datasource class name is not 'org.h2.Driver'").isEqualTo("org.h2.Driver");
    assertThat(driver.driverXaDatasourceClassName()).overridingErrorMessage("Driver XA datasource class name is not 'org.h2.jdbcx.JdbcDataSource'").isEqualTo("org.h2.jdbcx.JdbcDataSource");
}
 
Example #4
Source File: Main.java    From kubernetes-lab with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
	Container container = new Container();

	container.fraction(new DatasourcesFraction().jdbcDriver("com.mysql", (d) -> {
		d.driverClassName("com.mysql.jdbc.Driver");
		d.xaDatasourceClass("com.mysql.jdbc.jdbc2.optional.MysqlXADataSource");
		d.driverModuleName("com.mysql");
	}).dataSource("MySQLDS", (ds) -> {
		ds.driverName("com.mysql");
		ds.connectionUrl(System.getenv().getOrDefault("JDBC_URL", "jdbc:mysql://mysql:3306/guestbook?useSSL=false&autoReconnect=true"));
		ds.userName(System.getenv().getOrDefault("DATASOURCE_USERNAME", "myuser"));
		ds.password(System.getenv().getOrDefault("DATASOURCE_PASSWORD", "mypassword"));
		ds.backgroundValidation(true);
		ds.validConnectionCheckerClassName("org.jboss.jca.adapters.jdbc.extensions.mysql.MySQLValidConnectionChecker");
		ds.validateOnMatch(true);
		ds.checkValidConnectionSql("SELECT 1");
	}));

	// Start the container and deploy the default war
	container.start().deploy();
}
 
Example #5
Source File: FractionHandlingTest.java    From ARCHIVE-wildfly-swarm with Apache License 2.0 6 votes vote down vote up
public void userSpecifiedFractionOverridesDependentFraction() throws Exception {
    container.fraction(new DatasourcesFraction()
                    .jdbcDriver(new JDBCDriver("myDriver")
                            .driverDatasourceClassName("org.h2.Driver")
                            .driverXaDatasourceClassName("org.h2.jdbcx.JdbcDataSource")
                            .driverModuleName("com.h2database.h2"))
                    .dataSource(new DataSource("MyDS")
                            .driverName("myDriver")
                            .connectionUrl("jdbc:myDriver:mem:test;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE")
                            .userName("sa")
                            .password("sa"))
    );

    container.start();

    verifyFractions(container.fractions(), dsFraction -> verifyValidDataSourceFraction(dsFraction, "MyDS", "myDriver"));
}
 
Example #6
Source File: DriverInfo.java    From thorntail with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public void installDatasource(DatasourcesFraction fraction, String dsName, DataSourceConsumer config) {
    fraction.dataSource(dsName, (ds) -> {
        ds.driverName(this.name);
        this.configureDefaultDS(ds);
        config.accept(ds);
    });
}
 
Example #7
Source File: FractionHandlingTest.java    From ARCHIVE-wildfly-swarm with Apache License 2.0 4 votes vote down vote up
private void verifyEmptyDataSourceFraction(DatasourcesFraction dsFraction) {
    assertThat(dsFraction).overridingErrorMessage("DataSourceFraction was null").isNotNull();
    assertThat(dsFraction.subresources().dataSources()).overridingErrorMessage("DataSources were specified").isEmpty();
    assertThat(dsFraction.subresources().jdbcDrivers()).overridingErrorMessage("Drivers were specified").isEmpty();
}
 
Example #8
Source File: KeycloakServerFraction.java    From ARCHIVE-wildfly-swarm with Apache License 2.0 4 votes vote down vote up
@Override
public void postInitialize(Container.PostInitContext initContext) {

    if (System.getProperty("jboss.server.config.dir") == null) {
        try {
            //Path dir = Files.createTempDirectory("swarm-keycloak-config");
            File dir = TempFileManager.INSTANCE.newTempDirectory( "swarm-keycloak-config", ".d" );
            System.setProperty("jboss.server.config.dir", dir.getAbsolutePath() );
            Files.copy(getClass().getClassLoader().getResourceAsStream("keycloak-server.json"),
                    dir.toPath().resolve("keycloak-server.json"),
                    StandardCopyOption.REPLACE_EXISTING);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    InfinispanFraction infinispan = (InfinispanFraction) initContext.fraction("infinispan");

    CacheContainer cache = infinispan.subresources().cacheContainer("keycloak");
    if (cache == null) {
        infinispan.cacheContainer("keycloak", (c) -> c.jndiName("infinispan/Keycloak")
                .localCache("realms")
                .localCache("users")
                .localCache("sessions")
                .localCache("offlineSessions")
                .localCache("loginFailures"));
    }

    DatasourcesFraction datasources = (DatasourcesFraction) initContext.fraction("datasources");

    if (datasources.subresources().dataSource("KeycloakDS") == null) {
        if (datasources.subresources().jdbcDriver("h2") == null) {
            datasources.jdbcDriver("h2", (driver) -> {
                driver.driverModuleName("com.h2database.h2");
                driver.moduleSlot("main");
                driver.xaDatasourceClass("org.h2.jdbcx.JdbcDataSource");
            });
        }

        datasources.dataSource("KeycloakDS", (ds) -> {
            ds.jndiName("java:jboss/datasources/KeycloakDS");
            ds.useJavaContext(true);
            ds.connectionUrl("jdbc:h2:${wildfly.swarm.keycloak.server.db:./keycloak};AUTO_SERVER=TRUE");
            ds.driverName("h2");
            ds.userName("sa");
            ds.password("sa");
        });
    }
}
 
Example #9
Source File: DatasourcesConfiguration.java    From ARCHIVE-wildfly-swarm with Apache License 2.0 4 votes vote down vote up
public DatasourcesConfiguration() {
    super(DatasourcesFraction.class);
}
 
Example #10
Source File: DriverInfo.java    From thorntail with Apache License 2.0 4 votes vote down vote up
public boolean detect(DatasourcesFraction fraction) {
    if (fraction.subresources().jdbcDriver(this.name) != null) {
        // already installed
        return true;
    }

    DatasourcesMessages.MESSAGES.attemptToAutoDetectJdbcDriver(this.name);

    File primaryJar = attemptDetection();

    if (primaryJar != null) {
        Set<File> optionalJars = findOptionalJars();

        optionalJars.add(primaryJar);

        fraction.jdbcDriver(this.name, (driver) -> {
            @SuppressWarnings("deprecation")
            ModuleIdentifier identifier = ModuleIdentifier.fromString(this.moduleIdentifier);
            driver.driverModuleName(identifier.getName());
            driver.moduleSlot(identifier.getSlot());
            this.configureDriver(driver);
        });

        DynamicModuleFinder.register(this.moduleIdentifier, (id, loader) -> {
            ModuleSpec.Builder builder = ModuleSpec.build(id);

            for (File eachJar : optionalJars) {
                try {
                    JarFile jar = JarFileManager.INSTANCE.addJarFile(eachJar);
                    builder.addResourceRoot(ResourceLoaderSpec.createResourceLoaderSpec(
                            ResourceLoaders.createIterableJarResourceLoader(jar.getName(), jar)
                    ));
                } catch (IOException e) {
                    DatasourcesMessages.MESSAGES.errorLoadingAutodetectedJdbcDriver(this.name, e);
                    return null;
                }
            }

            builder.addDependency(new ModuleDependencySpecBuilder()
                    .setName("javax.api")
                    .build());
            builder.addDependency(new ModuleDependencySpecBuilder()
                    .setName("javax.transactions.api")
                    .setExport(false)
                    .setOptional(true)
                    .build());
            builder.addDependency(DependencySpec.createLocalDependencySpec());
            addModuleDependencies(builder);

            return builder.create();
        });

        this.installed = true;
    }

    return this.installed;
}
 
Example #11
Source File: BatchFraction.java    From thorntail with Apache License 2.0 2 votes vote down vote up
/**
 * Creates a new JDBC job repository.
 *
 * @param name       the name for the job repository
 * @param datasource the datasource to use to connect to the database
 * @return this fraction
 */
public BatchFraction jdbcJobRepository(final String name, final DatasourcesFraction datasource) {
    return jdbcJobRepository(new JDBCJobRepository<>(name).dataSource(datasource.getKey()));
}
 
Example #12
Source File: BatchFraction.java    From thorntail with Apache License 2.0 2 votes vote down vote up
/**
 * Creates a new JDBC job repository using the name of the datasource for the job repository name.
 *
 * @param datasource the datasource to use to connect to the database
 * @return this fraction
 */
public BatchFraction jdbcJobRepository(final DatasourcesFraction datasource) {
    return jdbcJobRepository(datasource.getKey(), datasource);
}
 
Example #13
Source File: BatchFraction.java    From thorntail with Apache License 2.0 2 votes vote down vote up
/**
 * Adds a new JDBC job repository and sets it as the default job repository.
 *
 * @param name       the name for the JDBC job repository
 * @param datasource the datasource to use to connect to the database
 * @return this fraction
 */
public BatchFraction defaultJobRepository(final String name, final DatasourcesFraction datasource) {
    jdbcJobRepository(name, datasource);
    return defaultJobRepository(name);
}
 
Example #14
Source File: BatchFraction.java    From thorntail with Apache License 2.0 2 votes vote down vote up
/**
 * Adds a new JDBC job repository using the datasource name as the job repository name and sets it as the default job
 * repository.
 *
 * @param datasource the datasource to use to connect to the database
 * @return this fraction
 */
public BatchFraction defaultJobRepository(final DatasourcesFraction datasource) {
    return defaultJobRepository(datasource.getKey(), datasource);
}
 
Example #15
Source File: BatchFraction.java    From ARCHIVE-wildfly-swarm with Apache License 2.0 2 votes vote down vote up
/**
 * Adds a new JDBC job repository using the datasource name as the job repository name and sets it as the default job
 * repository.
 *
 * @param datasource the datasource to use to connect to the database
 * @return this fraction
 */
public BatchFraction defaultJobRepository(final DatasourcesFraction datasource) {
    return defaultJobRepository(datasource.getKey(), datasource);
}
 
Example #16
Source File: BatchFraction.java    From ARCHIVE-wildfly-swarm with Apache License 2.0 2 votes vote down vote up
/**
 * Adds a new JDBC job repository and sets it as the default job repository.
 *
 * @param name       the name for the JDBC job repository
 * @param datasource the datasource to use to connect to the database
 * @return this fraction
 */
public BatchFraction defaultJobRepository(final String name, final DatasourcesFraction datasource) {
    jdbcJobRepository(name, datasource);
    return defaultJobRepository(name);
}
 
Example #17
Source File: BatchFraction.java    From ARCHIVE-wildfly-swarm with Apache License 2.0 2 votes vote down vote up
/**
 * Creates a new JDBC job repository using the name of the datasource for the job repository name.
 *
 * @param datasource the datasource to use to connect to the database
 * @return this fraction
 */
public BatchFraction jdbcJobRepository(final DatasourcesFraction datasource) {
    return jdbcJobRepository(datasource.getKey(), datasource);
}
 
Example #18
Source File: BatchFraction.java    From ARCHIVE-wildfly-swarm with Apache License 2.0 2 votes vote down vote up
/**
 * Creates a new JDBC job repository.
 *
 * @param name       the name for the job repository
 * @param datasource the datasource to use to connect to the database
 * @return this fraction
 */
public BatchFraction jdbcJobRepository(final String name, final DatasourcesFraction datasource) {
    return jdbcJobRepository(new JDBCJobRepository<>(name).dataSource(datasource.getKey()));
}