diff --git a/build.gradle b/build.gradle index 1a41a79..d9b4617 100644 --- a/build.gradle +++ b/build.gradle @@ -1,6 +1,11 @@ plugins { - id "de.marcphilipp.nexus-publish" version "0.4.0" - id "io.codearte.nexus-staging" version "0.21.1" + id "checkstyle" + id "pmd" + id 'maven-publish' + id 'signing' + id "io.github.gradle-nexus.publish-plugin" version "1.3.0" + id "com.github.spotbugs" version "5.0.14" + id "org.cyclonedx.bom" version "1.7.2" } wrapper { @@ -24,9 +29,14 @@ ext { } subprojects { - apply plugin: 'java-library' apply from: rootProject.file('gradle/ide/idea.gradle') + apply from: rootProject.file('gradle/repositories/maven.gradle') apply from: rootProject.file('gradle/compile/java.gradle') apply from: rootProject.file('gradle/test/junit5.gradle') - apply from: rootProject.file('gradle/publishing/publication.gradle') + apply from: rootProject.file('gradle/quality/checkstyle.gradle') + apply from: rootProject.file('gradle/quality/pmd.gradle') + apply from: rootProject.file('gradle/quality/spotbugs.gradle') + apply from: rootProject.file('gradle/publish/maven.gradle') } +apply from: rootProject.file('gradle/publish/sonatype.gradle') +apply from: rootProject.file('gradle/quality/cyclonedx.gradle') diff --git a/gradle.properties b/gradle.properties index c8933e5..390bf42 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,5 +1,5 @@ group = org.xbib name = database -version = 1.0.0 +version = 1.1.0 org.gradle.warning.mode = ALL diff --git a/gradle/compile/java.gradle b/gradle/compile/java.gradle index 33d7316..d3046aa 100644 --- a/gradle/compile/java.gradle +++ b/gradle/compile/java.gradle @@ -3,6 +3,8 @@ apply plugin: 'java-library' java { modularity.inferModulePath.set(true) + withSourcesJar() + withJavadocJar() } compileJava { @@ -21,19 +23,6 @@ jar { } } -task sourcesJar(type: Jar, dependsOn: classes) { - classifier 'sources' - from sourceSets.main.allSource -} - -task javadocJar(type: Jar, dependsOn: javadoc) { - classifier 'javadoc' -} - -artifacts { - archives sourcesJar, javadocJar -} - tasks.withType(JavaCompile) { options.compilerArgs << '-Xlint:all,-fallthrough,-exports,-try' } diff --git a/gradle/publish/ivy.gradle b/gradle/publish/ivy.gradle new file mode 100644 index 0000000..fe0a848 --- /dev/null +++ b/gradle/publish/ivy.gradle @@ -0,0 +1,27 @@ +apply plugin: 'ivy-publish' + +publishing { + repositories { + ivy { + url = "https://xbib.org/repo" + } + } + publications { + ivy(IvyPublication) { + from components.java + descriptor { + license { + name = 'The Apache License, Version 2.0' + url = 'http://www.apache.org/licenses/LICENSE-2.0.txt' + } + author { + name = 'Jörg Prante' + url = 'http://example.com/users/jane' + } + descriptor.description { + text = rootProject.ext.description + } + } + } + } +} \ No newline at end of file diff --git a/gradle/publishing/publication.gradle b/gradle/publish/maven.gradle similarity index 78% rename from gradle/publishing/publication.gradle rename to gradle/publish/maven.gradle index c35fcb9..ce6a26f 100644 --- a/gradle/publishing/publication.gradle +++ b/gradle/publish/maven.gradle @@ -1,13 +1,10 @@ -apply plugin: "de.marcphilipp.nexus-publish" - publishing { publications { - mavenJava(MavenPublication) { + "${project.name}"(MavenPublication) { from components.java - artifact sourcesJar - artifact javadocJar pom { + artifactId = project.name name = project.name description = rootProject.ext.description url = rootProject.ext.url @@ -49,16 +46,6 @@ publishing { if (project.hasProperty("signing.keyId")) { apply plugin: 'signing' signing { - sign publishing.publications.mavenJava - } -} - -nexusPublishing { - repositories { - sonatype { - username = project.property('ossrhUsername') - password = project.property('ossrhPassword') - packageGroup = "org.xbib" - } + sign publishing.publications."${project.name}" } } diff --git a/gradle/publish/sonatype.gradle b/gradle/publish/sonatype.gradle new file mode 100644 index 0000000..02744cd --- /dev/null +++ b/gradle/publish/sonatype.gradle @@ -0,0 +1,12 @@ + +if (project.hasProperty('ossrhUsername') && project.hasProperty('ossrhPassword')) { + nexusPublishing { + repositories { + sonatype { + username = project.property('ossrhUsername') + password = project.property('ossrhPassword') + packageGroup = "org.xbib" + } + } + } +} diff --git a/gradle/publishing/sonatype.gradle b/gradle/publishing/sonatype.gradle deleted file mode 100644 index e1813f3..0000000 --- a/gradle/publishing/sonatype.gradle +++ /dev/null @@ -1,11 +0,0 @@ - -if (project.hasProperty('ossrhUsername') && project.hasProperty('ossrhPassword')) { - - apply plugin: 'io.codearte.nexus-staging' - - nexusStaging { - username = project.property('ossrhUsername') - password = project.property('ossrhPassword') - packageGroup = "org.xbib" - } -} diff --git a/gradle/quality/checkstyle.gradle b/gradle/quality/checkstyle.gradle new file mode 100644 index 0000000..85b8bd8 --- /dev/null +++ b/gradle/quality/checkstyle.gradle @@ -0,0 +1,19 @@ + +apply plugin: 'checkstyle' + +tasks.withType(Checkstyle) { + ignoreFailures = true + reports { + xml.getRequired().set(true) + html.getRequired().set(true) + } +} + +checkstyle { + configFile = rootProject.file('gradle/quality/checkstyle.xml') + ignoreFailures = true + showViolations = true + checkstyleMain { + source = sourceSets.main.allSource + } +} diff --git a/gradle/quality/checkstyle.xml b/gradle/quality/checkstyle.xml new file mode 100644 index 0000000..66a9aae --- /dev/null +++ b/gradle/quality/checkstyle.xml @@ -0,0 +1,333 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/gradle/quality/cyclonedx.gradle b/gradle/quality/cyclonedx.gradle new file mode 100644 index 0000000..d94a87c --- /dev/null +++ b/gradle/quality/cyclonedx.gradle @@ -0,0 +1,11 @@ +cyclonedxBom { + includeConfigs = [ 'runtimeClasspath' ] + skipConfigs = [ 'compileClasspath', 'testCompileClasspath' ] + projectType = "library" + schemaVersion = "1.4" + destination = file("build/reports") + outputName = "bom" + outputFormat = "json" + includeBomSerialNumber = true + componentVersion = "2.0.0" +} \ No newline at end of file diff --git a/gradle/quality/pmd.gradle b/gradle/quality/pmd.gradle new file mode 100644 index 0000000..55fcfda --- /dev/null +++ b/gradle/quality/pmd.gradle @@ -0,0 +1,17 @@ + +apply plugin: 'pmd' + +tasks.withType(Pmd) { + ignoreFailures = true + reports { + xml.getRequired().set(true) + html.getRequired().set(true) + } +} + +pmd { + ignoreFailures = true + consoleOutput = false + toolVersion = "6.51.0" + ruleSetFiles = rootProject.files('gradle/quality/pmd/category/java/bestpractices.xml') +} diff --git a/gradle/quality/sonarqube.gradle b/gradle/quality/sonarqube.gradle new file mode 100644 index 0000000..d8eddd0 --- /dev/null +++ b/gradle/quality/sonarqube.gradle @@ -0,0 +1,10 @@ + +sonarqube { + properties { + property "sonar.projectName", "${project.group} ${project.name}" + property "sonar.sourceEncoding", "UTF-8" + property "sonar.tests", "src/test/java" + property "sonar.scm.provider", "git" + property "sonar.junit.reportsPath", "build/test-results/test/" + } +} diff --git a/gradle/quality/spotbugs.gradle b/gradle/quality/spotbugs.gradle new file mode 100644 index 0000000..2e5b0cd --- /dev/null +++ b/gradle/quality/spotbugs.gradle @@ -0,0 +1,15 @@ + +apply plugin: 'com.github.spotbugs' + +spotbugs { + effort = "max" + reportLevel = "low" + ignoreFailures = true +} + +spotbugsMain { + reports { + xml.getRequired().set(false) + html.getRequired().set(true) + } +} diff --git a/gradle/repositories/maven.gradle b/gradle/repositories/maven.gradle new file mode 100644 index 0000000..ec58acb --- /dev/null +++ b/gradle/repositories/maven.gradle @@ -0,0 +1,4 @@ +repositories { + mavenLocal() + mavenCentral() +} diff --git a/gradle/test/junit5.gradle b/gradle/test/junit5.gradle index 514af77..82fa7ea 100644 --- a/gradle/test/junit5.gradle +++ b/gradle/test/junit5.gradle @@ -21,4 +21,12 @@ test { } } systemProperty 'java.util.logging.config.file', 'src/test/resources/logging.properties' + OperatingSystem os = org.gradle.nativeplatform.platform.internal.DefaultNativePlatform.currentOperatingSystem + if (os.isLinux()) { + def uid = ["id", "-u"].execute().text.trim() + environment "DOCKER_HOST", "unix:///run/user/$uid/podman/podman.sock" + } else if (os.isMacOsX()) { + environment "DOCKER_HOST", "unix:///tmp/podman.sock" + } + environment "TESTCONTAINERS_RYUK_DISABLED", "true" } diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar index 249e583..c1962a7 100644 Binary files a/gradle/wrapper/gradle-wrapper.jar and b/gradle/wrapper/gradle-wrapper.jar differ diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 8fad3f5..8707e8b 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,5 +1,6 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-7.5.1-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-8.1.1-all.zip +networkTimeout=10000 zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists diff --git a/gradlew b/gradlew index a69d9cb..aeb74cb 100755 --- a/gradlew +++ b/gradlew @@ -55,7 +55,7 @@ # Darwin, MinGW, and NonStop. # # (3) This script is generated from the Groovy template -# https://github.com/gradle/gradle/blob/master/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt +# https://github.com/gradle/gradle/blob/HEAD/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt # within the Gradle project. # # You can find Gradle at https://github.com/gradle/gradle/. @@ -80,13 +80,10 @@ do esac done -APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit - -APP_NAME="Gradle" +# This is normally unused +# shellcheck disable=SC2034 APP_BASE_NAME=${0##*/} - -# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. -DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' +APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit # Use the maximum available, or set MAX_FD != -1 to use that value. MAX_FD=maximum @@ -143,12 +140,16 @@ fi if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then case $MAX_FD in #( max*) + # In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC3045 MAX_FD=$( ulimit -H -n ) || warn "Could not query maximum file descriptor limit" esac case $MAX_FD in #( '' | soft) :;; #( *) + # In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC3045 ulimit -n "$MAX_FD" || warn "Could not set maximum file descriptor limit to $MAX_FD" esac @@ -193,6 +194,10 @@ if "$cygwin" || "$msys" ; then done fi + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' + # Collect all arguments for the java command; # * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of # shell script including quotes and variable substitutions, so put them in diff --git a/gradlew.bat b/gradlew.bat index 53a6b23..6689b85 100644 --- a/gradlew.bat +++ b/gradlew.bat @@ -26,6 +26,7 @@ if "%OS%"=="Windows_NT" setlocal set DIRNAME=%~dp0 if "%DIRNAME%"=="" set DIRNAME=. +@rem This is normally unused set APP_BASE_NAME=%~n0 set APP_HOME=%DIRNAME% diff --git a/jdbc-query/src/main/java/org/xbib/jdbc/query/DatabaseProvider.java b/jdbc-query/src/main/java/org/xbib/jdbc/query/DatabaseProvider.java index d790c4a..dcd7179 100644 --- a/jdbc-query/src/main/java/org/xbib/jdbc/query/DatabaseProvider.java +++ b/jdbc-query/src/main/java/org/xbib/jdbc/query/DatabaseProvider.java @@ -40,6 +40,23 @@ public final class DatabaseProvider implements Supplier, Closeable { this.builder = builder; } + /** + * Use an externally configured DataSource and a Flavor. + */ + public static DatabaseProviderBuilder builder(DataSource ds, + String flavor) { + return new DatabaseProviderBuilderImpl(ds, () -> { + try { + if (ds == null) { + throw new NullPointerException(); + } + return ds.getConnection(); + } catch (Exception e) { + throw new DatabaseException("unable to obtain a connection from the DataSource", e); + } + }, new OptionsDefault(Flavor.valueOf(flavor))); + } + /** * Configure the database from the following properties read from the provided configuration: *
@@ -59,46 +76,52 @@ public final class DatabaseProvider implements Supplier, Closeable {
      *
      * 

A database pool will be created using jdbc-connection-pool.

*/ - public static DatabaseProviderBuilder builder(Config config) - throws ClassNotFoundException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException { - return builder(createDataSource(config), getFlavor(config)); - } - - /** - * Use an externally configured DataSource and a Flavor. - */ - public static DatabaseProviderBuilder builder(DataSource ds, Flavor flavor) { - return new DatabaseProviderBuilderImpl(ds, () -> { - try { - return ds.getConnection(); - } catch (Exception e) { - throw new DatabaseException("unable to obtain a connection from the DataSource", e); + public static DatabaseProviderBuilder builder(Config config) { + String flavorString = config.getString("database.flavor"); + if (flavorString == null) { + String url = config.getString("database.url"); + if (url == null) { + throw new DatabaseException("You must provide database.url"); } - }, new OptionsDefault(flavor)); + flavorString = Flavor.fromJdbcUrl(url).getName(); + } + return builder(createDataSource(config), flavorString); } /** * Builder method to create and initialize an instance of this class using * the JDBC standard DriverManager method. The url parameter will be inspected - * to determine the Flavor for this database. + * to determine the Flavor for this database. Short version for + * anonymous access without user name or password. */ - public static DatabaseProviderBuilder builder(ClassLoader classLoader, String url) { - return builder(classLoader, url, Flavor.fromJdbcUrl(url), null, null, null); + public static DatabaseProviderBuilder builder(ClassLoader classLoader, + String url) { + return builder(classLoader, url, Flavor.fromJdbcUrl(url).driverClass(), null, null, null); } - private static DatabaseProviderBuilder builder(ClassLoader classLoader, - String url, - Flavor flavor, - Properties info, - String user, - String password) { - Options options = new OptionsDefault(flavor); + /** + * Builder method to create and initialize an instance of this class using + * the JDBC standard DriverManager method. + * @param classLoader the classloader + * @param url the database URL + * @param driverClass the driver class + * @param properties the driver properties or null + * @param user user name or null + * @param password user password or null + * @return a database provider builder + */ + public static DatabaseProviderBuilder builder(ClassLoader classLoader, + String url, + String driverClass, + Properties properties, + String user, + String password) { try { DriverManager.getDriver(url); } catch (SQLException e) { - if (flavor.driverClass() != null) { + if (driverClass != null) { try { - Class.forName(flavor.driverClass(), true, classLoader); + Class.forName(driverClass, true, classLoader); } catch (ClassNotFoundException e1) { throw new DatabaseException("couldn't locate JDBC driver", e1); } @@ -106,8 +129,8 @@ public final class DatabaseProvider implements Supplier, Closeable { } return new DatabaseProviderBuilderImpl(null, () -> { try { - if (info != null) { - return DriverManager.getConnection(url, info); + if (properties != null) { + return DriverManager.getConnection(url, properties); } else if (user != null) { return DriverManager.getConnection(url, user, password); } @@ -115,12 +138,10 @@ public final class DatabaseProvider implements Supplier, Closeable { } catch (Exception e) { throw new DatabaseException("unable to obtain a connection from DriverManager", e); } - }, options); + }, new OptionsDefault(Flavor.fromJdbcUrl(url))); } - private static DataSource createDataSource(Config config) - throws ClassNotFoundException, InvocationTargetException, NoSuchMethodException, InstantiationException, - IllegalAccessException { + private static DataSource createDataSource(Config config) { String url = config.getString("database.url"); if (url == null) { throw new DatabaseException("You must provide database.url"); @@ -137,22 +158,12 @@ public final class DatabaseProvider implements Supplier, Closeable { poolConfig.setPassword(config.getString("database.password")); poolConfig.setMaximumPoolSize(config.getInteger("database.pool.size", 8)); poolConfig.setAutoCommit(false); - return new PoolDataSource(poolConfig); - } - - private static Flavor getFlavor(Config config) { - String url = config.getString("database.url"); - if (url == null) { - throw new DatabaseException("You must provide database.url"); - } - Flavor flavor; - String flavorString = config.getString("database.flavor"); - if (flavorString != null) { - flavor = Flavor.valueOf(flavorString); - } else { - flavor = Flavor.fromJdbcUrl(url); + try { + return new PoolDataSource(poolConfig); + } catch (Exception e) { + logger.log(Level.SEVERE, e.getMessage(), e); + return null; } - return flavor; } public void transact(final DbCode code) { diff --git a/jdbc-query/src/main/java/org/xbib/jdbc/query/Flavor.java b/jdbc-query/src/main/java/org/xbib/jdbc/query/Flavor.java index cf7b3fc..244cb7b 100644 --- a/jdbc-query/src/main/java/org/xbib/jdbc/query/Flavor.java +++ b/jdbc-query/src/main/java/org/xbib/jdbc/query/Flavor.java @@ -90,7 +90,7 @@ public interface Flavor { String failInQueue(String table); - public static Flavor fromJdbcUrl(String url) { + static Flavor fromJdbcUrl(String url) { if (url == null) { throw new DatabaseException("url must not be null"); } @@ -103,7 +103,10 @@ public interface Flavor { throw new DatabaseException("Cannot determine database flavor from url"); } - public static Flavor valueOf(String name) { + static Flavor valueOf(String name) { + if (name == null) { + throw new DatabaseException("name must not be null"); + } ServiceLoader serviceLoader = ServiceLoader.load(Flavor.class); for (Flavor flavor : serviceLoader) { if (flavor.getName().equals(name)) { diff --git a/settings.gradle b/settings.gradle index 613d067..7f394fd 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1,9 +1,9 @@ dependencyResolutionManagement { versionCatalogs { libs { - version('gradle', '7.5.1') - version('junit', '5.9.1') - version('testcontainers', '1.17.5') + version('gradle', '8.1.1') + version('junit', '5.9.3') + version('testcontainers', '1.18.0') library('junit-jupiter-api', 'org.junit.jupiter', 'junit-jupiter-api').versionRef('junit') library('junit-jupiter-params', 'org.junit.jupiter', 'junit-jupiter-params').versionRef('junit') library('junit-jupiter-engine', 'org.junit.jupiter', 'junit-jupiter-engine').versionRef('junit') @@ -12,10 +12,10 @@ dependencyResolutionManagement { library('derby', 'org.apache.derby', 'derby').version('10.16.1.1') library('hsqldb', 'org.hsqldb', 'hsqldb').version('2.7.1') library('h2', 'com.h2database', 'h2').version('2.1.214') - library('mariadb', 'org.mariadb.jdbc', 'mariadb-java-client').version('3.0.8') - library('oracle', 'com.oracle.database.jdbc','ojdbc11').version('21.7.0.0') - library('postgresql', 'org.postgresql', 'postgresql').version('42.5.0') - library('mockito-core', 'org.mockito', 'mockito-core').version('4.8.1') + library('mariadb', 'org.mariadb.jdbc', 'mariadb-java-client').version('3.1.3') + library('oracle', 'com.oracle.database.jdbc','ojdbc11').version('23.2.0.0') + library('postgresql', 'org.postgresql', 'postgresql').version('42.6.0') + library('mockito-core', 'org.mockito', 'mockito-core').version('5.3.1') library('testcontainers', 'org.testcontainers', 'testcontainers').versionRef('testcontainers') library('testcontainers-junit-jupiter', 'org.testcontainers', 'junit-jupiter').versionRef('testcontainers') library('testcontainers-mariadb', 'org.testcontainers', 'mariadb').versionRef('testcontainers')