diff --git a/.agents/skills/gradle-developer/SKILL.md b/.agents/skills/gradle-developer/SKILL.md index 3a171e85246..eea3ffb0630 100644 --- a/.agents/skills/gradle-developer/SKILL.md +++ b/.agents/skills/gradle-developer/SKILL.md @@ -136,7 +136,7 @@ Presence-based flags (property **present**, value optional) match `skipFunctiona ## Gradle Version Sync (Hard Rule) -Current line: **Gradle 9.9.1** (`distributionUrl` + `gradleToolingApiVersion=9.9.1`). Upstream may already ship a newer 9.9.x patch - this repo rides close to latest **only after** a deliberate multi-location bump PR. Do not "helpfully" jump one wrapper ahead of the rest. +Current line: **Gradle 9.9.2** (`distributionUrl` + `gradleToolingApiVersion=9.9.2`). Upstream may already ship a newer 9.9.x patch - this repo rides close to latest **only after** a deliberate multi-location bump PR. Do not "helpfully" jump one wrapper ahead of the rest. **Two Groovy stacks:** Gradle itself embeds **Groovy 4** for build logic. Application/runtime code on 8.0.x is **Groovy 5**. That is why `dependencies.gradle` keeps separate maps: @@ -665,16 +665,16 @@ Develocity: `https://develocity.apache.org` - build scans publish when authentic Use official docs for API signatures and deprecations (pin URLs to the version you are bumping toward): -- [Gradle 9.9 release notes](https://docs.gradle.org/9.9.1/release-notes.html) -- [Upgrading major version 9](https://docs.gradle.org/9.9.1/userguide/upgrading_major_version_9.html) -- [Upgrading within Gradle 9.x](https://docs.gradle.org/9.9.1/userguide/upgrading_version_9.html) -- [Java Library plugin](https://docs.gradle.org/9.9.1/userguide/java_library_plugin.html) -- [Java Platform / BOM](https://docs.gradle.org/9.9.1/userguide/java_platform_plugin.html) -- [Platforms](https://docs.gradle.org/9.9.1/userguide/platforms.html) -- [Sharing build logic via included builds](https://docs.gradle.org/9.9.1/userguide/sharing_build_logic_between_subprojects.html) -- [Task configuration avoidance](https://docs.gradle.org/9.9.1/userguide/task_configuration_avoidance.html) -- [Configuration cache](https://docs.gradle.org/9.9.1/userguide/configuration_cache.html) and its [requirements](https://docs.gradle.org/9.9.1/userguide/configuration_cache_requirements.html) -- [Version catalogs](https://docs.gradle.org/9.9.1/userguide/version_catalogs.html) (docs like them - **this repo does not**) +- [Gradle 9.9 release notes](https://docs.gradle.org/9.9.2/release-notes.html) +- [Upgrading major version 9](https://docs.gradle.org/9.9.2/userguide/upgrading_major_version_9.html) +- [Upgrading within Gradle 9.x](https://docs.gradle.org/9.9.2/userguide/upgrading_version_9.html) +- [Java Library plugin](https://docs.gradle.org/9.9.2/userguide/java_library_plugin.html) +- [Java Platform / BOM](https://docs.gradle.org/9.9.2/userguide/java_platform_plugin.html) +- [Platforms](https://docs.gradle.org/9.9.2/userguide/platforms.html) +- [Sharing build logic via included builds](https://docs.gradle.org/9.9.2/userguide/sharing_build_logic_between_subprojects.html) +- [Task configuration avoidance](https://docs.gradle.org/9.9.2/userguide/task_configuration_avoidance.html) +- [Configuration cache](https://docs.gradle.org/9.9.2/userguide/configuration_cache.html) and its [requirements](https://docs.gradle.org/9.9.2/userguide/configuration_cache_requirements.html) +- [Version catalogs](https://docs.gradle.org/9.9.2/userguide/version_catalogs.html) (docs like them - **this repo does not**) ### Docs say X - we do Y diff --git a/.agents/skills/mono-repo-integration/SKILL.md b/.agents/skills/mono-repo-integration/SKILL.md index 1540da96f95..e73bcf8dd65 100644 --- a/.agents/skills/mono-repo-integration/SKILL.md +++ b/.agents/skills/mono-repo-integration/SKILL.md @@ -140,7 +140,7 @@ The apps were physically moved to `grails-test-examples//...` back in Init - Remove now-empty imported folders. - Match property/naming style to existing conventions (e.g. `xxxVersion`). - **CodeNarc**: the imported source will almost certainly trip `codeStyle` (wildcard imports, tabs, `if(`, unnecessary GStrings/semicolons, brace spacing, missing trailing newline, class-starts-with-blank-line). Fix mechanically until `:grails-:codenarcMain` passes; test-example apps don't apply `grails-code-style`, so they aren't style-checked. -- **SBOM (`cyclonedxBom`)**: a merged plugin pulls transitives the SBOM plugin can't auto-resolve a license for, failing with "Could not determine License id for dependency: …". Trace it (`./gradlew :grails-:dependencyInsight --dependency --configuration runtimeClasspath`) to confirm what pulls it and that the license is ASF-acceptable, then add a mapping to `LICENSE_MAPPING` (and a `LICENSES` entry if the license has no SPDX id) in `build-logic/plugins/.../SbomPlugin.groovy` — the same place `jline`/`sitemesh` are mapped. (Redis: `org.json:json` via `jedis`, relicensed to Public Domain → mapped.) +- **SBOM (`cyclonedxBom`)**: a merged plugin pulls transitives the SBOM plugin can't auto-resolve a license for, failing with "Could not determine License id for dependency: …". Trace it (`./gradlew :grails-:dependencyInsight --dependency --configuration runtimeClasspath`) to confirm what pulls it and that the license is ASF-acceptable, then add a mapping to `LICENSE_MAPPING` (and a `LICENSES` entry if the license has no SPDX id) in `build-logic/plugins/.../SbomPlugin.groovy` — the same place `sitemesh` is mapped. (Redis: `org.json:json` via `jedis`, relicensed to Public Domain → mapped.) - **RAT** noise: a local working copy often has stale `bin/`/`build/` artifacts that `rat` flags; filter the report to your new files (excluding `build/`/`bin/`) to confirm the contribution itself is clean. - **Missing ASF license headers on imported files**: the plugin source usually already has Apache headers, but the imported **example/test app** files (controllers, GSP views, assets, config, i18n) frequently do not, and `rat` flags them. Add headers with the per-filetype scripts in `etc/bin/` — `add-license-groovy-java.groovy`, `add-license-gsp.groovy`, `add-license-css.groovy`, `add-license-js.groovy`, `add-license-yml.groovy`, `add-license-properties.groovy`, etc. — each takes a target path and recurses (skipping files that already have the marker): `groovy etc/bin/add-license-.groovy grails-test-examples/`. There is **no** `add-license-xml`; add the XML comment header (e.g. to `logback-spring.xml`/`logback-test.xml`) by hand, matching an existing example app's format. - **Gradle can't run in the sandbox here** (wrapper needs `~/.gradle`; signing needs the 1Password agent) — run every `./gradlew` with the sandbox disabled, and `export GRADLE_OPTS="-Dorg.gradle.jvmargs=-Xmx4G"`. diff --git a/.sdkmanrc b/.sdkmanrc index acd0c33eafd..5c3977baa3e 100644 --- a/.sdkmanrc +++ b/.sdkmanrc @@ -8,4 +8,4 @@ java=21.0.12-librca # Keep gradle version synced with gradle.properties (gradleToolingApiVersion). # Update the gradle-bootstrap project to propagate the version to all gradle-wrapper.properties files. -gradle=9.9.1 +gradle=9.9.2 diff --git a/build-logic/gradle/wrapper/gradle-wrapper.properties b/build-logic/gradle/wrapper/gradle-wrapper.properties index b60350078ef..a46e1bafcb5 100644 --- a/build-logic/gradle/wrapper/gradle-wrapper.properties +++ b/build-logic/gradle/wrapper/gradle-wrapper.properties @@ -2,7 +2,7 @@ # and grails-forge/grails-forge-core/grails-app/views/forge/feature/build/gradle/templates/gradleWrapperProperties.gsp distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-9.9.1-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-9.9.2-bin.zip networkTimeout=10000 retries=0 retryBackOffMs=500 diff --git a/build-logic/plugins/src/main/groovy/org/apache/grails/buildsrc/SbomPlugin.groovy b/build-logic/plugins/src/main/groovy/org/apache/grails/buildsrc/SbomPlugin.groovy index e064c884773..365dad13fa5 100644 --- a/build-logic/plugins/src/main/groovy/org/apache/grails/buildsrc/SbomPlugin.groovy +++ b/build-logic/plugins/src/main/groovy/org/apache/grails/buildsrc/SbomPlugin.groovy @@ -114,26 +114,11 @@ class SbomPlugin implements Plugin { 'pkg:maven/org.antlr/ST4@4.3.4?type=jar' : 'BSD-3-Clause', 'pkg:maven/org.antlr/antlr-runtime@3.5.3?type=jar' : 'BSD-3-Clause', // mongo-java-server declares only "The BSD License" so it maps to BSD-4-Clause for the same - // reason as the org.jline group below. https://github.com/bwaldvogel/mongo-java-server/blob/main/LICENSE + // reason as the org.jline group in forcedLicenseFor. https://github.com/bwaldvogel/mongo-java-server/blob/main/LICENSE // has three numbered clauses and no advertising clause, making it BSD-3-Clause. 'pkg:maven/de.bwaldvogel/mongo-java-server@1.47.0?type=jar' : 'BSD-3-Clause', 'pkg:maven/de.bwaldvogel/mongo-java-server-core@1.47.0?type=jar' : 'BSD-3-Clause', 'pkg:maven/de.bwaldvogel/mongo-java-server-memory-backend@1.47.0?type=jar': 'BSD-3-Clause', - // The whole org.jline group declares "The BSD License", which maps incorrectly because of - // https://github.com/CycloneDX/cyclonedx-core-java/issues/205 - the POMs point at BSD-3-Clause. - // jline.version tracks the JLine version Groovy ships, so every module resolves to one version. - // Groovy 6 may also pull JLine 4, handled by forcedLicenseFor below. - 'pkg:maven/org.jline/jansi@3.30.16?type=jar' : 'BSD-3-Clause', - 'pkg:maven/org.jline/jline@3.30.16?type=jar' : 'BSD-3-Clause', - 'pkg:maven/org.jline/jline-builtins@3.30.16?type=jar' : 'BSD-3-Clause', - 'pkg:maven/org.jline/jline-console@3.30.16?type=jar' : 'BSD-3-Clause', - 'pkg:maven/org.jline/jline-native@3.30.16?type=jar' : 'BSD-3-Clause', - 'pkg:maven/org.jline/jline-reader@3.30.16?type=jar' : 'BSD-3-Clause', - 'pkg:maven/org.jline/jline-style@3.30.16?type=jar' : 'BSD-3-Clause', - 'pkg:maven/org.jline/jline-terminal@3.30.16?type=jar' : 'BSD-3-Clause', - 'pkg:maven/org.jline/jline-terminal-jansi@3.30.16?type=jar' : 'BSD-3-Clause', - 'pkg:maven/org.jline/jline-terminal-jna@3.30.16?type=jar' : 'BSD-3-Clause', - 'pkg:maven/org.jline/jline-terminal-jni@3.30.16?type=jar' : 'BSD-3-Clause', 'pkg:maven/org.jruby/jzlib@1.1.5?type=jar' : 'BSD-3-Clause', // https://web.archive.org/web/20240822213507/http://www.jcraft.com/jzlib/LICENSE.txt shows it's a 3 clause 'pkg:maven/org.liquibase.ext/liquibase-hibernate5@4.27.0?type=jar': 'Apache-2.0', // maps incorrectly because of https://github.com/liquibase/liquibase/issues/2445 & the base pom does not define a license 'pkg:maven/org.json/json@20260719?type=jar' : 'Public-Domain', // required due to jedis, https://issues.apache.org/jira/browse/LEGAL-666 approves this usage @@ -147,8 +132,10 @@ class SbomPlugin implements Plugin { if (LICENSE_MAPPING.containsKey(bomRef)) { return LICENSE_MAPPING[bomRef] } - // JLine 4 LICENSE.txt confirms BSD-3-Clause, but CycloneDX misreports it as BSD-4-Clause - // (cyclonedx-core-java#205). This is limited to the org.jline Maven jar family resolved via Groovy 6. + // The whole org.jline group declares "The BSD License", which CycloneDX misreports as BSD-4-Clause + // (cyclonedx-core-java#205); JLine 4's LICENSE.txt confirms BSD-3-Clause. jline.version tracks the + // JLine version Groovy 6 ships, so every module resolves to one JLine 4 version, and this is + // limited to that org.jline Maven jar family. if (bomRef.matches('pkg:maven/org\\.jline/[^/@?]+@4\\.[^?]+\\?type=jar')) { return 'BSD-3-Clause' } diff --git a/build-logic/plugins/src/test/groovy/org/apache/grails/buildsrc/SbomPluginSpec.groovy b/build-logic/plugins/src/test/groovy/org/apache/grails/buildsrc/SbomPluginSpec.groovy index 0e3b047f441..6e37adf8b48 100644 --- a/build-logic/plugins/src/test/groovy/org/apache/grails/buildsrc/SbomPluginSpec.groovy +++ b/build-logic/plugins/src/test/groovy/org/apache/grails/buildsrc/SbomPluginSpec.groovy @@ -110,8 +110,14 @@ class SbomPluginSpec extends Specification { void "JLine 4 Maven jars use the BSD-3-Clause correction for new versions"() { expect: - SbomPlugin.pickLicense(LOGGER, 'grails-console', 'grails-console', - 'pkg:maven/org.jline/jansi@4.3.1?type=jar', bsd4Choice()).id == 'BSD-3-Clause' + SbomPlugin.pickLicense(LOGGER, 'grails-console', 'grails-console', bomRef, bsd4Choice()).id == 'BSD-3-Clause' + + where: + bomRef << [ + 'pkg:maven/org.jline/jansi@4.3.1?type=jar', + 'pkg:maven/org.jline/jline@4.4.5?type=jar', + 'pkg:maven/org.jline/jline-shell@4.4.5?type=jar' + ] } void "org.json resolved through Jedis uses the public domain license approved in LEGAL-666"() { @@ -134,7 +140,8 @@ class SbomPluginSpec extends Specification { where: bomRef << [ 'pkg:maven/com.example/jansi@4.3.1?type=jar', - 'pkg:maven/org.jline/jansi@3.3.1?type=jar' + 'pkg:maven/org.jline/jansi@3.3.1?type=jar', + 'pkg:maven/org.jline/jline@3.30.16?type=jar' ] } diff --git a/build-logic/vulnerability-scan/src/main/groovy/org/apache/grails/buildsrc/scan/VulnerabilityScanPlugin.groovy b/build-logic/vulnerability-scan/src/main/groovy/org/apache/grails/buildsrc/scan/VulnerabilityScanPlugin.groovy index 6b146735914..ce6bcbd07a5 100644 --- a/build-logic/vulnerability-scan/src/main/groovy/org/apache/grails/buildsrc/scan/VulnerabilityScanPlugin.groovy +++ b/build-logic/vulnerability-scan/src/main/groovy/org/apache/grails/buildsrc/scan/VulnerabilityScanPlugin.groovy @@ -93,7 +93,7 @@ class VulnerabilityScanPlugin implements Plugin { 'CVE-2026-22747. Remove once Sonatype narrows its affected range'), new VulnerabilityExclusion('CVE-2026-47842', 'org.springframework.security:spring-security-crypto', 'Sonatype flagged 7.1.1, but https://spring.io/security/cve-2026-47842 lists only 7.1.0 on ' + - 'the 7.1 line as affected and 7.1.1 as fixed. Spring Boot 4.2.0-RC2 manages 7.2.0-RC1, which ' + + 'the 7.1 line as affected and 7.1.1 as fixed. Spring Boot 4.2.0 manages 7.2.0, which ' + 'the advisory does not list and which carries the fix (the AesCbcBytesEncryptor and ' + 'AesGcmBytesEncryptor replacements 7.1.1 introduced). Remove once Sonatype corrects its ' + 'affected range; reassess on any Spring Security upgrade'), diff --git a/dependencies.gradle b/dependencies.gradle index 452d0e4844e..19f69f25882 100644 --- a/dependencies.gradle +++ b/dependencies.gradle @@ -34,12 +34,12 @@ ext { 'grails-publish-plugin.version' : '1.0.0', 'javaparser-core.version' : '3.28.2', // Tracks the JLine version Groovy ships (groovy-groovysh), so the framework and groovysh - // share one terminal stack - keep in step with groovy.version. - 'jline.version' : '3.30.16', + // share one terminal stack - keep in step with groovy.version. Groovy 6 moved to JLine 4. + 'jline.version' : '4.4.5', 'jna.version' : '5.19.1', 'maven-model.version' : '3.9.16', 'objenesis.version' : '3.6', - 'spring-boot.version' : '4.2.0-RC2', + 'spring-boot.version' : '4.2.0', ] // Note: the name of the dependency must be the prefix of the property name so properties in the pom are resolved correctly diff --git a/end-to-end/README.md b/end-to-end/README.md index 1462cd80360..d0769de3383 100644 --- a/end-to-end/README.md +++ b/end-to-end/README.md @@ -148,7 +148,7 @@ neither the core build nor a contributor's default environment inherits a second | Where | JDK | Why | |---|---|---| -| `legacy-g7-command-plugin/.sdkmanrc` | 17, Gradle 8.14.7 | What Grails 7 pins, so the fixture is built the way a Grails 7 plugin actually was. `gradle-bootstrap` generates this wrapper from that file via its `legacyG7Wrapper` task, rather than copying the shared one. | +| `legacy-g7-command-plugin/.sdkmanrc` | 17, Gradle 8.14.8 | What Grails 7 pins, so the fixture is built the way a Grails 7 plugin actually was. `gradle-bootstrap` generates this wrapper from that file via its `legacyG7Wrapper` task, rather than copying the shared one. | | the repository's root `.sdkmanrc` | 21 | The Grails 8 baseline. This build consumes artifacts from the core build, so it runs on whatever the core build runs on — it deliberately does not re-pin that. | ## Running locally diff --git a/end-to-end/gradle/wrapper/gradle-wrapper.properties b/end-to-end/gradle/wrapper/gradle-wrapper.properties index 04d7959523b..d0d1a39c1f5 100644 --- a/end-to-end/gradle/wrapper/gradle-wrapper.properties +++ b/end-to-end/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-9.9.1-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-9.9.2-bin.zip networkTimeout=10000 retries=0 retryBackOffMs=500 diff --git a/end-to-end/legacy-g7-command-plugin/.sdkmanrc b/end-to-end/legacy-g7-command-plugin/.sdkmanrc index a88bd346a7d..6706cc997a9 100644 --- a/end-to-end/legacy-g7-command-plugin/.sdkmanrc +++ b/end-to-end/legacy-g7-command-plugin/.sdkmanrc @@ -1,4 +1,4 @@ -# Matches Grails 7: JDK 17 and Gradle 8.14.7 are what the 7.0.x branch pins. +# Matches Grails 7: JDK 17 and Gradle 8.14.8 are what the 7.0.x branch pins. # Deliberately not synced with the repository root, which tracks Grails 8. java=17.0.18-librca -gradle=8.14.7 +gradle=8.14.8 diff --git a/end-to-end/legacy-g7-command-plugin/gradle/wrapper/gradle-wrapper.properties b/end-to-end/legacy-g7-command-plugin/gradle/wrapper/gradle-wrapper.properties index 3ba557cce88..af35ed11ef3 100644 --- a/end-to-end/legacy-g7-command-plugin/gradle/wrapper/gradle-wrapper.properties +++ b/end-to-end/legacy-g7-command-plugin/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-8.14.7-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-8.14.8-bin.zip networkTimeout=10000 retries=0 retryBackOffMs=500 diff --git a/gradle.properties b/gradle.properties index 27939632757..7a2c6f0ea8c 100644 --- a/gradle.properties +++ b/gradle.properties @@ -48,7 +48,7 @@ geantyrefVersion=1.3.16 gparsVersion=1.2.1 # Keep gradle version synced with .sdkmanrc, all gradle-wrapper.properties files, # and grails-forge/grails-forge-core/grails-app/views/forge/feature/build/gradle/templates/gradleWrapperProperties.gsp -gradleToolingApiVersion=9.9.1 +gradleToolingApiVersion=9.9.2 greenmailVersion=2.1.14 javassistVersion=3.33.0-GA jnrPosixVersion=3.2.4 diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index b60350078ef..a46e1bafcb5 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -2,7 +2,7 @@ # and grails-forge/grails-forge-core/grails-app/views/forge/feature/build/gradle/templates/gradleWrapperProperties.gsp distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-9.9.1-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-9.9.2-bin.zip networkTimeout=10000 retries=0 retryBackOffMs=500 diff --git a/grails-console/src/main/groovy/grails/ui/shell/support/GroovyshApplicationContext.groovy b/grails-console/src/main/groovy/grails/ui/shell/support/GroovyshApplicationContext.groovy index 01d7f3155d1..be3864f3e97 100644 --- a/grails-console/src/main/groovy/grails/ui/shell/support/GroovyshApplicationContext.groovy +++ b/grails-console/src/main/groovy/grails/ui/shell/support/GroovyshApplicationContext.groovy @@ -20,6 +20,7 @@ package grails.ui.shell.support import groovy.transform.CompileStatic +import org.apache.groovy.groovysh.GroovyshOptions import org.apache.groovy.groovysh.Main import org.springframework.context.support.GenericApplicationContext @@ -39,7 +40,18 @@ class GroovyshApplicationContext extends GenericApplicationContext { } protected void startConsole() { - Main.start([ + Main.start(shellOptions().build()) + } + + /** + * Returns the options the shell starts with: this context as {@code ctx} and the Grails application in + * its binding, on the terminal of the JVM. A subclass can add to them, for example a terminal of its own. + * + * @return the options of the shell + * @since 9.0 + */ + protected GroovyshOptions.Builder shellOptions() { + GroovyshOptions.builder().bindings([ ctx: this, (GrailsApplication.APPLICATION_ID): getBean(GrailsApplication) ]) diff --git a/grails-console/src/test/groovy/grails/ui/shell/support/GroovyshApplicationContextSpec.groovy b/grails-console/src/test/groovy/grails/ui/shell/support/GroovyshApplicationContextSpec.groovy new file mode 100644 index 00000000000..a854a78cb20 --- /dev/null +++ b/grails-console/src/test/groovy/grails/ui/shell/support/GroovyshApplicationContextSpec.groovy @@ -0,0 +1,73 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information + * regarding copyright ownership. The ASF licenses this file to You under + * the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package grails.ui.shell.support + +import java.nio.file.Path + +import org.apache.groovy.groovysh.GroovyshOptions +import org.jline.terminal.Terminal +import org.jline.terminal.TerminalBuilder +import spock.lang.Specification +import spock.lang.TempDir +import spock.util.environment.RestoreSystemProperties + +import grails.core.DefaultGrailsApplication +import grails.core.GrailsApplication + +/** + * Starts the Groovy shell {@code grails shell} opens, on the class path the console module ships. + */ +@RestoreSystemProperties +class GroovyshApplicationContextSpec extends Specification { + + @TempDir + Path userHome + + void setup() { + // groovysh keeps its history and settings under the user's home directory + System.setProperty('user.home', userHome.toString()) + } + + void 'the shell starts with the application in its binding and ends when its input ends'() { + given: 'a terminal whose input is empty, so the shell never waits for the console of an IDE' + ByteArrayOutputStream output = new ByteArrayOutputStream() + Terminal terminal = TerminalBuilder.builder().system(false).dumb(true) + .streams(new ByteArrayInputStream(new byte[0]), output).build() + + and: 'an application context with a Grails application, as grails shell starts it' + def context = new GroovyshApplicationContext() { + @Override + protected GroovyshOptions.Builder shellOptions() { + super.shellOptions().terminal(terminal) + } + } + context.registerBean(GrailsApplication.APPLICATION_ID, GrailsApplication, { new DefaultGrailsApplication() }) + + when: 'the context refreshes, which starts the shell' + context.refresh() + + then: 'the shell runs on that terminal and returns without a JLine class missing from the class path' + noExceptionThrown() + context.active + output.toString('UTF-8').contains('groovy> ') + + cleanup: + context.close() + terminal.close() + } +} diff --git a/grails-core/src/main/groovy/grails/config/Settings.groovy b/grails-core/src/main/groovy/grails/config/Settings.groovy index ed946a6ccb9..5cc2daa272e 100644 --- a/grails-core/src/main/groovy/grails/config/Settings.groovy +++ b/grails-core/src/main/groovy/grails/config/Settings.groovy @@ -212,6 +212,24 @@ interface Settings { */ String MIME_DISABLE_ACCEPT_HEADER_FOR_USER_AGENTS_XHR = 'grails.mime.disable.accept.header.userAgentsXhr' + /** + * Whether {@code request.format}, {@code response.format}, {@code withFormat} and the format {@code respond} writes + * in are resolved with Spring MVC's content negotiation manager, which also applies the {@code grails.mime} rules + * Grails keeps to what Spring MVC writes for the requests a URL mapping handles, rather than by Grails' own rules. + * Defaults to {@link #MIME_NEGOTIATION_SPRING_DEFAULT}. + * + * @since 9.0 + */ + String MIME_NEGOTIATION_SPRING = 'grails.mime.negotiation.spring' + + /** + * The default of {@link #MIME_NEGOTIATION_SPRING}, as a property placeholder reads it: {@code false} in Grails 9, + * and {@code true} from Grails 10. + * + * @since 9.0 + */ + String MIME_NEGOTIATION_SPRING_DEFAULT = 'false' + /** * The default scope for controllers */ diff --git a/grails-data-docs/guide-developer/src/main/docs/stepByStep.adoc b/grails-data-docs/guide-developer/src/main/docs/stepByStep.adoc index 38902dc4831..23ff5aa9df2 100644 --- a/grails-data-docs/guide-developer/src/main/docs/stepByStep.adoc +++ b/grails-data-docs/guide-developer/src/main/docs/stepByStep.adoc @@ -135,13 +135,15 @@ class Setup { xyz.mappingContext.addMappingContextListener({ e -> enhancer.enhance e } as MappingContext.Listener) xyz.applicationContext.addApplicationListener new DomainEventListener(xyz) - xyz.applicationContext.addApplicationListener new AutoTimestampEventListener(xyz) + xyz.applicationContext.addApplicationListener xyz.autoTimestampEventListener xyz.connect() } } ---- +Here `XyzDatastore` implements `AutoTimestampingDatastore`: it creates its `AutoTimestampEventListener` once, as `new AutoTimestampEventListener(this)`, and returns it from `getAutoTimestampEventListener()`, so that `Book.autoTimestampEventListener` on a domain class finds the listener that handles the class's saves. The TCK's `AutoTimestampEventListenerLookupSpec` checks this. A datastore that builds a datastore for each of its connections registers the listeners of each one, because a listener only handles the events whose source is its own datastore. + Then in `src/test/groovy` create test suite class to allow running tests in IDE (without this you won't be able to run TCK tests from the IDE). Example test suite: [source,groovy] diff --git a/grails-data-hibernate5/core/src/main/groovy/org/grails/orm/hibernate/AbstractHibernateDatastore.java b/grails-data-hibernate5/core/src/main/groovy/org/grails/orm/hibernate/AbstractHibernateDatastore.java index b50cacb41ef..8a9d2b3bc41 100644 --- a/grails-data-hibernate5/core/src/main/groovy/org/grails/orm/hibernate/AbstractHibernateDatastore.java +++ b/grails-data-hibernate5/core/src/main/groovy/org/grails/orm/hibernate/AbstractHibernateDatastore.java @@ -49,6 +49,7 @@ import grails.gorm.multitenancy.Tenants; import org.grails.datastore.gorm.events.AutoTimestampEventListener; +import org.grails.datastore.gorm.events.AutoTimestampingDatastore; import org.grails.datastore.gorm.jdbc.schema.DefaultSchemaHandler; import org.grails.datastore.gorm.jdbc.schema.SchemaHandler; import org.grails.datastore.gorm.validation.registry.support.ValidatorRegistries; @@ -84,7 +85,7 @@ * @author Graeme Rocher * @since 2.0 */ -public abstract class AbstractHibernateDatastore extends AbstractDatastore implements ApplicationContextAware, Settings, SchemaMultiTenantCapableDatastore, TransactionCapableDatastore, Closeable, MessageSourceAware, MultipleConnectionSourceCapableDatastore { +public abstract class AbstractHibernateDatastore extends AbstractDatastore implements ApplicationContextAware, Settings, SchemaMultiTenantCapableDatastore, TransactionCapableDatastore, Closeable, MessageSourceAware, MultipleConnectionSourceCapableDatastore, AutoTimestampingDatastore { public static final String CONFIG_PROPERTY_CACHE_QUERIES = "grails.hibernate.cache.queries"; public static final String CONFIG_PROPERTY_OSIV_READONLY = "grails.hibernate.osiv.readonly"; @@ -301,6 +302,7 @@ public AbstractHibernateEventListener getEventTriggeringInterceptor() { /** * @return The event listener that populates lastUpdated and dateCreated */ + @Override public AutoTimestampEventListener getAutoTimestampEventListener() { return autoTimestampEventListener; } diff --git a/grails-data-hibernate5/core/src/main/groovy/org/grails/orm/hibernate/AbstractHibernateGormStaticApi.groovy b/grails-data-hibernate5/core/src/main/groovy/org/grails/orm/hibernate/AbstractHibernateGormStaticApi.groovy index 39047af8249..093220fb287 100644 --- a/grails-data-hibernate5/core/src/main/groovy/org/grails/orm/hibernate/AbstractHibernateGormStaticApi.groovy +++ b/grails-data-hibernate5/core/src/main/groovy/org/grails/orm/hibernate/AbstractHibernateGormStaticApi.groovy @@ -41,6 +41,7 @@ import org.springframework.core.convert.ConversionService import org.springframework.transaction.PlatformTransactionManager import org.grails.datastore.gorm.GormStaticApi +import org.grails.datastore.gorm.events.AutoTimestampEventListener import org.grails.datastore.gorm.finders.DynamicFinder import org.grails.datastore.gorm.finders.FinderMethod import org.grails.datastore.mapping.proxy.ProxyHandler @@ -93,6 +94,15 @@ abstract class AbstractHibernateGormStaticApi extends GormStaticApi { return hibernateTemplate } + /** + * Hibernate saves an instance through the datastore this API is bound to, inside a tenant block as well, so this + * is that datastore's listener. + */ + @Override + AutoTimestampEventListener getAutoTimestampEventListener() { + autoTimestampEventListenerOf(getDatastore()) + } + @Override T withNewSession(Closure callable) { AbstractHibernateDatastore hibernateDatastore = (AbstractHibernateDatastore) datastore diff --git a/grails-data-hibernate5/core/src/test/groovy/org/grails/orm/hibernate/connections/AutoTimestampEventListenerConnectionsSpec.groovy b/grails-data-hibernate5/core/src/test/groovy/org/grails/orm/hibernate/connections/AutoTimestampEventListenerConnectionsSpec.groovy new file mode 100644 index 00000000000..df43d5b119f --- /dev/null +++ b/grails-data-hibernate5/core/src/test/groovy/org/grails/orm/hibernate/connections/AutoTimestampEventListenerConnectionsSpec.groovy @@ -0,0 +1,130 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.grails.orm.hibernate.connections + +import java.util.function.Supplier + +import org.hibernate.dialect.H2Dialect +import spock.lang.AutoCleanup +import spock.lang.Specification + +import org.springframework.context.support.GenericApplicationContext + +import grails.gorm.annotation.CreatedBy +import grails.gorm.annotation.Entity +import grails.gorm.annotation.LastModifiedBy +import org.grails.datastore.gorm.timestamp.AuditorAware +import org.grails.datastore.mapping.config.Settings +import org.grails.datastore.mapping.core.DatastoreUtils +import org.grails.orm.hibernate.HibernateDatastore + +/** + * The listener of every connection, not only that of the default one, sets the auditor properties and follows the + * auto-timestamp settings of the configuration. + */ +class AutoTimestampEventListenerConnectionsSpec extends Specification { + + private static final Date PAST = new Date(110_000_000_000L) + + @AutoCleanup + GenericApplicationContext context + + @AutoCleanup + HibernateDatastore datastore + + void 'every connection sets the auditor properties from the application context of the datastore'() { + given: + context = new GenericApplicationContext() + context.registerBean(AuditorAware, { { -> Optional.of('auditor') } as AuditorAware } as Supplier) + context.refresh() + datastore = new HibernateDatastore(DatastoreUtils.createPropertyResolver(configuration()), AuditedConnectionNote) + datastore.setApplicationContext(context) + AuditedConnectionNote onDefault = new AuditedConnectionNote(text: 'default') + AuditedConnectionNote onSecondary = new AuditedConnectionNote(text: 'secondary') + + when: + AuditedConnectionNote.withTransaction { + onDefault.save(flush: true) + } + AuditedConnectionNote.secondary.withTransaction { + onSecondary.secondary.save(flush: true) + } + + then: + onDefault.createdBy == 'auditor' + onDefault.lastModifiedBy == 'auditor' + onSecondary.createdBy == 'auditor' + onSecondary.lastModifiedBy == 'auditor' + } + + void 'every connection keeps a given timestamp when the configuration turns overwriting off'() { + given: + datastore = new HibernateDatastore(DatastoreUtils.createPropertyResolver(configuration() + [ + (Settings.SETTING_AUTO_TIMESTAMP_INSERT_OVERWRITE): false + ]), AuditedConnectionNote) + AuditedConnectionNote onDefault = new AuditedConnectionNote(text: 'default', dateCreated: PAST) + AuditedConnectionNote onSecondary = new AuditedConnectionNote(text: 'secondary', dateCreated: PAST) + + when: + AuditedConnectionNote.withTransaction { + onDefault.save(flush: true) + } + AuditedConnectionNote.secondary.withTransaction { + onSecondary.secondary.save(flush: true) + } + + then: + AuditedConnectionNote.withNewTransaction { AuditedConnectionNote.get(onDefault.id).dateCreated } == PAST + AuditedConnectionNote.secondary.withNewTransaction { AuditedConnectionNote.secondary.get(onSecondary.id).dateCreated } == PAST + onSecondary.lastUpdated != null + } + + private static Map configuration() { + [ + 'dataSource.url' : 'jdbc:h2:mem:timestampConnectionsDefaultDB;LOCK_TIMEOUT=10000', + 'dataSource.dbCreate' : 'create-drop', + 'dataSource.dialect' : H2Dialect.name, + 'hibernate.hbm2ddl.auto': 'create', + 'dataSources.secondary' : [url: 'jdbc:h2:mem:timestampConnectionsSecondaryDB;LOCK_TIMEOUT=10000'] + ] as Map + } +} + +@Entity +class AuditedConnectionNote { + + String text + Date dateCreated + Date lastUpdated + + @CreatedBy + String createdBy + + @LastModifiedBy + String lastModifiedBy + + static constraints = { + createdBy nullable: true + lastModifiedBy nullable: true + } + + static mapping = { + datasource 'ALL' + } +} diff --git a/grails-data-hibernate5/core/src/test/groovy/org/grails/orm/hibernate/connections/AutoTimestampEventListenerTenantSpec.groovy b/grails-data-hibernate5/core/src/test/groovy/org/grails/orm/hibernate/connections/AutoTimestampEventListenerTenantSpec.groovy new file mode 100644 index 00000000000..42c7c15edda --- /dev/null +++ b/grails-data-hibernate5/core/src/test/groovy/org/grails/orm/hibernate/connections/AutoTimestampEventListenerTenantSpec.groovy @@ -0,0 +1,188 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.grails.orm.hibernate.connections + +import org.hibernate.dialect.H2Dialect +import spock.lang.AutoCleanup +import spock.lang.Issue +import spock.lang.Specification +import spock.util.environment.RestoreSystemProperties + +import grails.gorm.MultiTenant +import grails.gorm.annotation.Entity +import grails.gorm.multitenancy.Tenants +import org.grails.datastore.gorm.events.AutoTimestampEventListener +import org.grails.datastore.mapping.core.DatastoreUtils +import org.grails.datastore.mapping.multitenancy.AllTenantsResolver +import org.grails.datastore.mapping.multitenancy.resolvers.SystemPropertyTenantResolver +import org.grails.orm.hibernate.HibernateDatastore + +/** + * A tenant with a database or a schema of its own has an auto-timestamp listener of its own, and a multi-tenant + * domain class answers with the current tenant's. + */ +@Issue('https://github.com/apache/grails-core/issues/14509') +@RestoreSystemProperties +class AutoTimestampEventListenerTenantSpec extends Specification { + + private static final Date PAST = new Date(110_000_000_000L) + + @AutoCleanup + HibernateDatastore datastore + + void setup() { + System.setProperty(SystemPropertyTenantResolver.PROPERTY_NAME, '') + } + + void 'a tenant with a database of its own answers with the listener of that database'() { + given: + datastore = databasePerTenant() + AutoTimestampEventListener books = listenerOf('books') + AutoTimestampEventListener moreBooks = listenerOf('moreBooks') + + expect: + !books.is(moreBooks) + !books.is(datastore.autoTimestampEventListener) + Tenants.withId('books') { TenantTimestampNote.autoTimestampEventListener }.is(books) + Tenants.withId('moreBooks') { TenantTimestampNote.autoTimestampEventListener }.is(moreBooks) + TenantTimestampNote.withTenant('moreBooks').autoTimestampEventListener.is(moreBooks) + + when: 'the current tenant comes from the resolver' + System.setProperty(SystemPropertyTenantResolver.PROPERTY_NAME, 'moreBooks') + + then: + TenantTimestampNote.autoTimestampEventListener.is(moreBooks) + } + + void 'the listener of the current tenant turns off the timestamps of its saves, and another tenant\'s does not'() { + given: + datastore = databasePerTenant() + System.setProperty(SystemPropertyTenantResolver.PROPERTY_NAME, 'books') + TenantTimestampNote kept = new TenantTimestampNote(text: 'kept', dateCreated: PAST, lastUpdated: PAST) + TenantTimestampNote stamped = new TenantTimestampNote(text: 'stamped', dateCreated: PAST, lastUpdated: PAST) + + when: + TenantTimestampNote.withTransaction { + TenantTimestampNote.autoTimestampEventListener.withoutTimestamps { + kept.save(flush: true) + } + listenerOf('moreBooks').withoutTimestamps { + stamped.save(flush: true) + } + } + + then: + TenantTimestampNote.withNewTransaction { TenantTimestampNote.get(kept.id).dateCreated } == PAST + TenantTimestampNote.withNewTransaction { TenantTimestampNote.get(kept.id).lastUpdated } == PAST + TenantTimestampNote.withNewTransaction { TenantTimestampNote.get(stamped.id).dateCreated } != PAST + TenantTimestampNote.withNewTransaction { TenantTimestampNote.get(stamped.id).lastUpdated } != PAST + } + + void 'a class that is not multi-tenant answers inside a tenant block with the listener of the default database'() { + given: 'Hibernate saves a class that is not multi-tenant to the default database inside a tenant block too' + datastore = databasePerTenant() + PlainTimestampNote kept = new PlainTimestampNote(text: 'kept', dateCreated: PAST, lastUpdated: PAST) + + when: + def listener = Tenants.withId('books') { + PlainTimestampNote.withTransaction { + PlainTimestampNote.autoTimestampEventListener.withoutTimestamps { + kept.save(flush: true) + } + } + PlainTimestampNote.autoTimestampEventListener + } + + then: + listener.is(datastore.autoTimestampEventListener) + PlainTimestampNote.withNewTransaction { PlainTimestampNote.get(kept.id).dateCreated } == PAST + PlainTimestampNote.withNewTransaction { PlainTimestampNote.get(kept.id).lastUpdated } == PAST + } + + void 'a tenant with a schema of its own answers with the listener of that schema'() { + given: + datastore = new HibernateDatastore(DatastoreUtils.createPropertyResolver([ + 'grails.gorm.multiTenancy.mode' : 'SCHEMA', + 'grails.gorm.multiTenancy.tenantResolverClass': TwoSchemaResolver, + 'dataSource.url' : 'jdbc:h2:mem:timestampSchemaDB;LOCK_TIMEOUT=10000', + 'dataSource.dbCreate' : 'create-drop', + 'dataSource.dialect' : H2Dialect.name, + 'hibernate.hbm2ddl.auto' : 'create' + ]), TenantTimestampNote) + AutoTimestampEventListener books = listenerOf('books') + AutoTimestampEventListener moreBooks = listenerOf('moreBooks') + TenantTimestampNote kept = new TenantTimestampNote(text: 'kept', dateCreated: PAST, lastUpdated: PAST) + + when: + Tenants.withId('books') { + TenantTimestampNote.withTransaction { + TenantTimestampNote.autoTimestampEventListener.withoutTimestamps { + kept.save(flush: true) + } + } + } + + then: + !books.is(moreBooks) + Tenants.withId('books') { TenantTimestampNote.autoTimestampEventListener }.is(books) + TenantTimestampNote.withTenant('moreBooks').autoTimestampEventListener.is(moreBooks) + Tenants.withId('books') { TenantTimestampNote.withNewTransaction { TenantTimestampNote.get(kept.id).dateCreated } } == PAST + } + + private HibernateDatastore databasePerTenant() { + new HibernateDatastore(DatastoreUtils.createPropertyResolver([ + 'grails.gorm.multiTenancy.mode' : 'DATABASE', + 'grails.gorm.multiTenancy.tenantResolverClass': SystemPropertyTenantResolver, + 'dataSource.url' : 'jdbc:h2:mem:timestampDefaultDB;LOCK_TIMEOUT=10000', + 'dataSource.dbCreate' : 'create-drop', + 'dataSource.dialect' : H2Dialect.name, + 'hibernate.hbm2ddl.auto' : 'create', + 'dataSources.books' : [url: 'jdbc:h2:mem:timestampBooksDB;LOCK_TIMEOUT=10000'], + 'dataSources.moreBooks' : [url: 'jdbc:h2:mem:timestampMoreBooksDB;LOCK_TIMEOUT=10000'] + ]), TenantTimestampNote, PlainTimestampNote) + } + + private AutoTimestampEventListener listenerOf(String connectionName) { + datastore.getDatastoreForConnection(connectionName).autoTimestampEventListener + } + + static class TwoSchemaResolver extends SystemPropertyTenantResolver implements AllTenantsResolver { + + @Override + Iterable resolveTenantIds() { + ['books', 'moreBooks'] + } + } +} + +@Entity +class TenantTimestampNote implements MultiTenant { + + String text + Date dateCreated + Date lastUpdated +} + +@Entity +class PlainTimestampNote { + + String text + Date dateCreated + Date lastUpdated +} diff --git a/grails-data-hibernate5/docs/src/docs/asciidoc/advancedGORMFeatures/eventsAutoTimestamping.adoc b/grails-data-hibernate5/docs/src/docs/asciidoc/advancedGORMFeatures/eventsAutoTimestamping.adoc index aa77c905a0f..376e270d441 100644 --- a/grails-data-hibernate5/docs/src/docs/asciidoc/advancedGORMFeatures/eventsAutoTimestamping.adoc +++ b/grails-data-hibernate5/docs/src/docs/asciidoc/advancedGORMFeatures/eventsAutoTimestamping.adoc @@ -381,21 +381,45 @@ WARNING: If you have `nullable: false` constraints on either `dateCreated` or `l It is also possible to disable the automatic timestamping temporarily. This is most typically done in the case of a test where you need to define values for the `dateCreated` or `lastUpdated` in the past. It may also be useful for importing old data from other systems where you would like to keep the current values of the timestamps. -Timestamps can be temporarily disabled for all domains, a specified list of domains, or a single domain. To get started, you need to get a reference to the `AutoTimestampEventListener`. If you already have access to the datastore, you can execute the `getAutoTimestampEventListener` method. If you don't have access to the datastore, inject the `autoTimestampEventListener` bean. +Timestamps can be temporarily disabled for all domains, a specified list of domains, or a single domain. To get started, you need a reference to the `AutoTimestampEventListener` that handles the domain class, which the domain class answers with: + +[source,groovy] +---- +Foo.autoTimestampEventListener.withoutTimestamps { + new Foo(dateCreated: new Date() - 2, lastUpdated: new Date() - 1).save(flush: true) +} +---- + +Each datastore has a listener of its own, and so does each data source of an application with several and each tenant with a database or schema of its own. A listener only handles the saves made through its own datastore, so turning timestamps off on one leaves the saves to another data source timestamped. `Foo.autoTimestampEventListener` is the listener of the data source that a save of `Foo` made from the same place goes to: its default data source, the one a `Foo.secondary.withTransaction { }` block or a method annotated `@Transactional(connection = 'secondary')` routes it to, or the current tenant's for a multi-tenant class. For another data source of a class mapped to several, name the data source, as in `Foo.secondary.autoTimestampEventListener`. + +[source,groovy] +---- +// Car is mapped to the default data source and Person to the 'people' data source +Car.autoTimestampEventListener.withoutTimestamps { + car.save(flush: true) +} +Person.autoTimestampEventListener.withoutTimestamps { + person.save(flush: true) +} +---- + +The `autoTimestampEventListener` bean, and the `getAutoTimestampEventListener` method of the datastore bean, return the listener of the default data source. Once you have a reference to the event listener, you can execute `withoutDateCreated`, `withoutLastUpdated`, or `withoutTimestamps`. The `withoutTimestamps` method will temporarily disable both `dateCreated` and `lastUpdated`. -Example: +Example, where `Foo`, `Bar` and `FooBar` share a data source: [source,groovy] ---- +def autoTimestampEventListener = Foo.autoTimestampEventListener + //Only the dateCreated property handling will be disabled for only the Foo domain autoTimestampEventListener.withoutDateCreated(Foo) { new Foo(dateCreated: new Date() - 1).save(flush: true) } //Only the lastUpdated property handling will be disabled for only the Foo and Bar domains -autoTimestampEventListener.withoutLastUpdated(Foo, Bar) { +autoTimestampEventListener.withoutLastUpdated([Foo, Bar]) { new Foo(lastUpdated: new Date() - 1, bar: new Bar(lastUpdated: new Date() + 1)).save(flush: true) } diff --git a/grails-data-hibernate5/grails-plugin/src/main/groovy/org/grails/plugin/hibernate/support/GrailsOpenSessionInViewInterceptor.java b/grails-data-hibernate5/grails-plugin/src/main/groovy/org/grails/plugin/hibernate/support/GrailsOpenSessionInViewInterceptor.java index 20fa6a1085c..3bba841aef9 100644 --- a/grails-data-hibernate5/grails-plugin/src/main/groovy/org/grails/plugin/hibernate/support/GrailsOpenSessionInViewInterceptor.java +++ b/grails-data-hibernate5/grails-plugin/src/main/groovy/org/grails/plugin/hibernate/support/GrailsOpenSessionInViewInterceptor.java @@ -24,11 +24,13 @@ import org.hibernate.FlushMode; import org.hibernate.Session; import org.hibernate.SessionFactory; +import org.hibernate.engine.spi.SharedSessionContractImplementor; import org.springframework.dao.DataAccessException; import org.springframework.dao.DataAccessResourceFailureException; import org.springframework.transaction.support.TransactionSynchronizationManager; import org.springframework.ui.ModelMap; +import org.springframework.util.ReflectionUtils; import org.springframework.web.context.request.WebRequest; import org.grails.datastore.mapping.core.connections.ConnectionSource; @@ -45,13 +47,20 @@ * The default datasource's SessionFactory is managed by the parent class. * Additional (non-default) datasource SessionFactories are managed by this * subclass, which opens and closes sessions for each one alongside the - * default session. + * default session. As the parent class does for the default session, a request + * that finds a session already bound, such as an include within a request that + * opened one, uses that session and leaves it to the one that opened it, and a + * request that goes asynchronous closes the sessions it opened, so that they do + * not stay bound to the thread for the next request that thread serves. * * @author Graeme Rocher * @since 0.5 */ public class GrailsOpenSessionInViewInterceptor extends OpenSessionInViewInterceptor { + /** The value of {@code GrailsApplicationAttributes.ASYNC_STARTED}, which is in a module this plugin does not use. */ + private static final String GRAILS_ASYNC_STARTED = "org.grails.ASYNC_STARTED"; + protected FlushMode hibernateFlushMode = FlushMode.MANUAL; private final List additionalSessionFactories = new ArrayList<>(); @@ -90,6 +99,8 @@ public void preHandle(WebRequest request) throws DataAccessException { for (AdditionalSessionFactoryConfig config : additionalSessionFactories) { SessionFactory sf = config.sessionFactory; if (TransactionSynchronizationManager.hasResource(sf)) { + // a session is already open for this request: use it, and leave it to the one that opened it + incrementParticipateCount(request, sf); continue; } if (logger.isDebugEnabled()) { @@ -165,28 +176,146 @@ public void postHandle(WebRequest request, ModelMap model) throws DataAccessExce @Override public void afterCompletion(WebRequest request, Exception ex) throws DataAccessException { try { - for (int i = additionalSessionFactories.size() - 1; i >= 0; i--) { - AdditionalSessionFactoryConfig config = additionalSessionFactories.get(i); - SessionFactory sf = config.sessionFactory; - SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager.getResource(sf); - if (sessionHolder != null) { - Session session = sessionHolder.getSession(); - TransactionSynchronizationManager.unbindResource(sf); - if (logger.isDebugEnabled()) { - logger.debug("Closing additional Hibernate Session for datasource '" + config.connectionName + "' in OpenSessionInViewInterceptor"); + closeAdditionalSessions(request, false); + } + finally { + super.afterCompletion(request, ex); + } + } + + /** + * Called instead of {@link #postHandle} and {@link #afterCompletion} on the thread that started asynchronous + * handling. The sessions this interceptor opened for the other datasources are closed here, so that the thread + * goes back to its pool without them. Each is flushed first when {@link #postHandle} would flush it and Hibernate + * allows the flush: inside a transaction, or with {@code hibernate.allow_update_outside_transaction}. + * + *

The parent class only unbinds the default session, and leaves closing it to the interceptors it registers for + * a {@code Callable} or {@code DeferredResult}. A promise or {@code startAsync()} in a Grails controller starts + * asynchronous handling without them, so the default session this interceptor opened is closed here too. + */ + @Override + public void afterConcurrentHandlingStarted(WebRequest request) { + try { + closeAdditionalSessions(request, true); + } + finally { + if (isGrailsAsync(request) && !isParticipating(request) && + TransactionSynchronizationManager.hasResource(getSessionFactory())) { + SessionHolder sessionHolder = + (SessionHolder) TransactionSynchronizationManager.unbindResource(getSessionFactory()); + if (logger.isDebugEnabled()) { + logger.debug("Closing Hibernate Session in OpenSessionInViewInterceptor as asynchronous handling starts"); + } + SessionFactoryUtils.closeSession(sessionHolder.getSession()); + } + else { + super.afterConcurrentHandlingStarted(request); + } + } + } + + /** + * @return whether Grails started asynchronous handling, for a promise or {@code startAsync()}: both set + * {@code GrailsApplicationAttributes.ASYNC_STARTED} + */ + private static boolean isGrailsAsync(WebRequest request) { + return request.getAttribute(GRAILS_ASYNC_STARTED, WebRequest.SCOPE_REQUEST) != null; + } + + /** + * @return whether the default session was already bound when the request started, so that it belongs to + * whoever bound it + */ + private boolean isParticipating(WebRequest request) { + return request.getAttribute(getParticipateAttributeName(), WebRequest.SCOPE_REQUEST) != null; + } + + private void closeAdditionalSessions(WebRequest request, boolean flushFirst) { + Throwable firstFlushFailure = null; + for (int i = additionalSessionFactories.size() - 1; i >= 0; i--) { + AdditionalSessionFactoryConfig config = additionalSessionFactories.get(i); + SessionFactory sf = config.sessionFactory; + if (decrementParticipateCount(request, sf)) { + continue; + } + SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager.getResource(sf); + if (sessionHolder != null) { + Session session = sessionHolder.getSession(); + try { + if (flushFirst && isFlushAllowed(session)) { + if (logger.isDebugEnabled()) { + logger.debug("Flushing additional Hibernate session for datasource '" + config.connectionName + "' as asynchronous handling starts"); + } + session.flush(); } - try { - SessionFactoryUtils.closeSession(session); + } + catch (Throwable flushFailure) { + // an Error too, such as a failed assert in a domain's beforeUpdate: every session is released first + if (firstFlushFailure == null) { + firstFlushFailure = flushFailure; } - catch (RuntimeException closeEx) { - logger.error("Unexpected exception on closing additional Hibernate Session for datasource '" + config.connectionName + "'", closeEx); + else { + firstFlushFailure.addSuppressed(flushFailure); } } + TransactionSynchronizationManager.unbindResource(sf); + if (logger.isDebugEnabled()) { + logger.debug("Closing additional Hibernate Session for datasource '" + config.connectionName + "' in OpenSessionInViewInterceptor"); + } + try { + SessionFactoryUtils.closeSession(session); + } + catch (RuntimeException closeEx) { + logger.error("Unexpected exception on closing additional Hibernate Session for datasource '" + config.connectionName + "'", closeEx); + } } } - finally { - super.afterCompletion(request, ex); + if (firstFlushFailure != null) { + ReflectionUtils.rethrowRuntimeException(firstFlushFailure); + } + } + + /** + * @return whether {@link #postHandle} would flush the session, and Hibernate would let it: a session it would not + * flush (MANUAL or COMMIT) is not flushed here either, and one outside a transaction only when + * {@code hibernate.allow_update_outside_transaction} is set, since Hibernate refuses that flush otherwise + */ + private static boolean isFlushAllowed(Session session) { + FlushMode flushMode = session.getHibernateFlushMode(); + if (flushMode == FlushMode.MANUAL || flushMode == FlushMode.COMMIT) { + return false; } + return ((SharedSessionContractImplementor) session).isTransactionInProgress() || + session.getSessionFactory().getSessionFactoryOptions().isAllowOutOfTransactionUpdateOperations(); + } + + private static void incrementParticipateCount(WebRequest request, SessionFactory sf) { + String participateAttributeName = participateAttributeName(sf); + Integer count = (Integer) request.getAttribute(participateAttributeName, WebRequest.SCOPE_REQUEST); + request.setAttribute(participateAttributeName, count != null ? count + 1 : 1, WebRequest.SCOPE_REQUEST); + } + + /** + * @return {@code true} when this request used a session of the given factory that was already open, which is + * then left as it is + */ + private static boolean decrementParticipateCount(WebRequest request, SessionFactory sf) { + String participateAttributeName = participateAttributeName(sf); + Integer count = (Integer) request.getAttribute(participateAttributeName, WebRequest.SCOPE_REQUEST); + if (count == null) { + return false; + } + if (count > 1) { + request.setAttribute(participateAttributeName, count - 1, WebRequest.SCOPE_REQUEST); + } + else { + request.removeAttribute(participateAttributeName, WebRequest.SCOPE_REQUEST); + } + return true; + } + + private static String participateAttributeName(SessionFactory sf) { + return sf.toString() + PARTICIPATE_SUFFIX; } public void setHibernateDatastore(AbstractHibernateDatastore hibernateDatastore) { diff --git a/grails-data-hibernate5/grails-plugin/src/test/groovy/org/grails/plugin/hibernate/support/MultiDataSourceSessionSpec.groovy b/grails-data-hibernate5/grails-plugin/src/test/groovy/org/grails/plugin/hibernate/support/MultiDataSourceSessionSpec.groovy index 16ed354f357..bf2d1f8e2dc 100644 --- a/grails-data-hibernate5/grails-plugin/src/test/groovy/org/grails/plugin/hibernate/support/MultiDataSourceSessionSpec.groovy +++ b/grails-data-hibernate5/grails-plugin/src/test/groovy/org/grails/plugin/hibernate/support/MultiDataSourceSessionSpec.groovy @@ -21,6 +21,7 @@ package org.grails.plugin.hibernate.support import grails.gorm.annotation.Entity import org.grails.datastore.mapping.core.DatastoreUtils import org.grails.orm.hibernate.HibernateDatastore +import org.hibernate.FlushMode import org.hibernate.Session import org.hibernate.SessionFactory import org.hibernate.dialect.H2Dialect @@ -46,6 +47,17 @@ class MultiDataSourceSessionSpec extends Specification { @Shared @AutoCleanup HibernateDatastore datastore = new HibernateDatastore(DatastoreUtils.createPropertyResolver(config), OsivBook, OsivAuthor) + void cleanup() { + [datastore.sessionFactory, datastore.getDatastoreForConnection('secondary').sessionFactory].each { SessionFactory sessionFactory -> + if (TransactionSynchronizationManager.hasResource(sessionFactory)) { + Session session = ((SessionHolder) TransactionSynchronizationManager.unbindResource(sessionFactory)).session + if (session.open) { + session.close() + } + } + } + } + def "withSession on default datasource works with OSIV"() { given: "OSIV interceptor configured with the datastore" def interceptor = new GrailsOpenSessionInViewInterceptor() @@ -119,7 +131,7 @@ class MultiDataSourceSessionSpec extends Specification { given: "a pre-bound session for the secondary datasource" def interceptor = new GrailsOpenSessionInViewInterceptor() interceptor.setHibernateDatastore(datastore) - WebRequest webRequest = Mock(WebRequest) + WebRequest webRequest = webRequest() def secondaryDatastore = datastore.getDatastoreForConnection('secondary') SessionFactory secondarySf = secondaryDatastore.sessionFactory @@ -134,12 +146,277 @@ class MultiDataSourceSessionSpec extends Specification { def currentHolder = TransactionSynchronizationManager.getResource(secondarySf) currentHolder.is(preBoundHolder) + when: "the request completes" + interceptor.afterCompletion(webRequest, null) + + then: "the pre-bound session is left bound and open" + TransactionSynchronizationManager.getResource(secondarySf).is(preBoundHolder) + preBoundSession.open + cleanup: + if (TransactionSynchronizationManager.hasResource(secondarySf)) { + TransactionSynchronizationManager.unbindResource(secondarySf) + } + preBoundSession.close() + } + + def "an include uses the sessions of the request for every datasource and leaves them open"() { + given: "the request has opened its sessions" + def interceptor = new GrailsOpenSessionInViewInterceptor() + interceptor.setHibernateDatastore(datastore) + WebRequest webRequest = webRequest() + SessionFactory secondarySf = datastore.getDatastoreForConnection('secondary').sessionFactory + interceptor.preHandle(webRequest) + Session defaultSession = boundSession(datastore.sessionFactory) + Session secondarySession = boundSession(secondarySf) + + when: "an include within it starts and completes" + interceptor.preHandle(webRequest) + interceptor.postHandle(webRequest, null) interceptor.afterCompletion(webRequest, null) + + then: "the sessions of the request are still bound and open" + boundSession(datastore.sessionFactory).is(defaultSession) + boundSession(secondarySf).is(secondarySession) + secondarySession.open + + when: "the request completes" + interceptor.afterCompletion(webRequest, null) + + then: "they are unbound and closed" + !TransactionSynchronizationManager.hasResource(datastore.sessionFactory) + !TransactionSynchronizationManager.hasResource(secondarySf) + !secondarySession.open + } + + def "a request that goes asynchronous closes the sessions it opened and leaves the thread without them"() { + given: "the request has opened its sessions" + def interceptor = new GrailsOpenSessionInViewInterceptor() + interceptor.setHibernateDatastore(datastore) + WebRequest webRequest = webRequest() + SessionFactory secondarySf = datastore.getDatastoreForConnection('secondary').sessionFactory + interceptor.preHandle(webRequest) + Session defaultSession = boundSession(datastore.sessionFactory) + Session secondarySession = boundSession(secondarySf) + + when: "the request goes asynchronous" + interceptor.afterConcurrentHandlingStarted(webRequest) + + then: "no session is bound to the thread any more" + !TransactionSynchronizationManager.hasResource(datastore.sessionFactory) + !TransactionSynchronizationManager.hasResource(secondarySf) + + and: "the session of the other datasource is closed" + !secondarySession.open + + cleanup: + defaultSession?.close() + } + + def "a request that goes asynchronous flushes another datasource's session under AUTO when updates outside a transaction are allowed"() { + given: "a datastore that flushes in AUTO mode and allows updates outside a transaction, with a book on its secondary datasource" + HibernateDatastore autoFlush = new HibernateDatastore(DatastoreUtils.createPropertyResolver(config + [ + 'hibernate.flush.mode': 'AUTO', + 'hibernate.allow_update_outside_transaction': 'true', + 'dataSource.url': 'jdbc:h2:mem:outsideTxDefaultDb;LOCK_TIMEOUT=10000', + 'dataSources.secondary': [url: 'jdbc:h2:mem:outsideTxSecondaryDb;LOCK_TIMEOUT=10000'], + ]), OutsideTransactionBook) + SessionFactory secondarySf = autoFlush.getDatastoreForConnection('secondary').sessionFactory + Serializable bookId = OutsideTransactionBook.withNewTransaction { new OutsideTransactionBook(title: 'Before').save(flush: true).id } + + and: "a request that changes it without a flush" + def interceptor = new GrailsOpenSessionInViewInterceptor() + interceptor.setHibernateDatastore(autoFlush) + WebRequest webRequest = webRequest() + interceptor.preHandle(webRequest) + Session secondarySession = boundSession(secondarySf) + secondarySession.get(OutsideTransactionBook, bookId).title = 'After' + + when: "the request goes asynchronous" + interceptor.afterConcurrentHandlingStarted(webRequest) + + then: "the change is written, as postHandle would write it, and the session is closed" + titleOf(secondarySf, OutsideTransactionBook, bookId) == 'After' + !secondarySession.open + !TransactionSynchronizationManager.hasResource(secondarySf) + + cleanup: + autoFlush?.close() + } + + def "a request that goes asynchronous does not flush another datasource's session under AUTO when Hibernate would refuse it"() { + given: "a datastore that flushes in AUTO mode with the default setting, which refuses a flush outside a transaction" + HibernateDatastore autoFlush = new HibernateDatastore(DatastoreUtils.createPropertyResolver(config + [ + 'hibernate.flush.mode': 'AUTO', + 'dataSource.url': 'jdbc:h2:mem:insideTxDefaultDb;LOCK_TIMEOUT=10000', + 'dataSources.secondary': [url: 'jdbc:h2:mem:insideTxSecondaryDb;LOCK_TIMEOUT=10000'], + ]), InsideTransactionBook) + SessionFactory secondarySf = autoFlush.getDatastoreForConnection('secondary').sessionFactory + Serializable bookId = InsideTransactionBook.withNewTransaction { new InsideTransactionBook(title: 'Before').save(flush: true).id } + + and: "a request that changes it without a flush" + def interceptor = new GrailsOpenSessionInViewInterceptor() + interceptor.setHibernateDatastore(autoFlush) + WebRequest webRequest = webRequest() + interceptor.preHandle(webRequest) + Session secondarySession = boundSession(secondarySf) + secondarySession.get(InsideTransactionBook, bookId).title = 'After' + + when: "the request goes asynchronous" + interceptor.afterConcurrentHandlingStarted(webRequest) + + then: "nothing is flushed or thrown, and the session is closed" + notThrown(Exception) + titleOf(secondarySf, InsideTransactionBook, bookId) == 'Before' + !secondarySession.open + !TransactionSynchronizationManager.hasResource(secondarySf) + + cleanup: + autoFlush?.close() + } + + def "a request that goes asynchronous releases every session it opened when a flush there throws an Error"() { + given: "a datastore that flushes in AUTO mode and allows updates outside a transaction, with a book on each of two other datasources" + HibernateDatastore autoFlush = new HibernateDatastore(DatastoreUtils.createPropertyResolver(config + [ + 'hibernate.flush.mode': 'AUTO', + 'hibernate.allow_update_outside_transaction': 'true', + 'dataSource.url': 'jdbc:h2:mem:refusedDefaultDb;LOCK_TIMEOUT=10000', + 'dataSources.secondary': [url: 'jdbc:h2:mem:refusedSecondaryDb;LOCK_TIMEOUT=10000'], + 'dataSources.third': [url: 'jdbc:h2:mem:refusedThirdDb;LOCK_TIMEOUT=10000'], + ]), RefusedSecondaryBook, RefusedThirdBook) + SessionFactory secondarySf = autoFlush.getDatastoreForConnection('secondary').sessionFactory + SessionFactory thirdSf = autoFlush.getDatastoreForConnection('third').sessionFactory + Serializable secondaryId = RefusedSecondaryBook.withNewTransaction { new RefusedSecondaryBook(title: 'Before').save(flush: true).id } + Serializable thirdId = RefusedThirdBook.withNewTransaction { new RefusedThirdBook(title: 'Before').save(flush: true).id } + + and: "a request, made asynchronous by Grails, that changes both so that each one's beforeUpdate fails its assert" + def interceptor = new GrailsOpenSessionInViewInterceptor() + interceptor.setHibernateDatastore(autoFlush) + WebRequest webRequest = webRequest() + interceptor.preHandle(webRequest) + Session defaultSession = boundSession(autoFlush.sessionFactory) + Session secondarySession = boundSession(secondarySf) + Session thirdSession = boundSession(thirdSf) + secondarySession.get(RefusedSecondaryBook, secondaryId).title = 'Refused' + thirdSession.get(RefusedThirdBook, thirdId).title = 'Refused' + webRequest.setAttribute('org.grails.ASYNC_STARTED', true, WebRequest.SCOPE_REQUEST) + + when: "the request goes asynchronous" + interceptor.afterConcurrentHandlingStarted(webRequest) + + then: "every session the request opened is unbound and closed" + AssertionError failure = thrown() + [autoFlush.sessionFactory, secondarySf, thirdSf].every { !TransactionSynchronizationManager.hasResource(it) } + !defaultSession.open + !secondarySession.open + !thirdSession.open + + and: "the first failure is thrown, with the second attached" + failure.suppressed.length == 1 + failure.suppressed[0] instanceof AssertionError + + cleanup: + autoFlush?.close() + } + + def "a request that Grails makes asynchronous closes its default session too"() { + given: "the request has opened its sessions" + def interceptor = new GrailsOpenSessionInViewInterceptor() + interceptor.setHibernateDatastore(datastore) + WebRequest webRequest = webRequest() + interceptor.preHandle(webRequest) + Session defaultSession = boundSession(datastore.sessionFactory) + + when: "the action returns a promise or calls startAsync(), which Spring's asynchronous interceptors never see" + webRequest.setAttribute('org.grails.ASYNC_STARTED', true, WebRequest.SCOPE_REQUEST) + interceptor.afterConcurrentHandlingStarted(webRequest) + + then: "the default session is unbound and closed" + !TransactionSynchronizationManager.hasResource(datastore.sessionFactory) + !defaultSession.open + + cleanup: + if (defaultSession?.open) { + defaultSession.close() + } + } + + def "a request that Spring makes asynchronous leaves its default session open for Spring to close"() { + given: "the request has opened its sessions" + def interceptor = new GrailsOpenSessionInViewInterceptor() + interceptor.setHibernateDatastore(datastore) + WebRequest webRequest = webRequest() + interceptor.preHandle(webRequest) + Session defaultSession = boundSession(datastore.sessionFactory) + + when: "the action returns a Callable or DeferredResult, whose processing rebinds the session and closes it" + interceptor.afterConcurrentHandlingStarted(webRequest) + + then: "the default session is unbound, and left open" + !TransactionSynchronizationManager.hasResource(datastore.sessionFactory) + defaultSession.open + + cleanup: + defaultSession?.close() + } + + def "a session of another datasource bound before the request stays bound and open when the request goes asynchronous"() { + given: "a pre-bound session for the secondary datasource" + def interceptor = new GrailsOpenSessionInViewInterceptor() + interceptor.setHibernateDatastore(datastore) + WebRequest webRequest = webRequest() + SessionFactory secondarySf = datastore.getDatastoreForConnection('secondary').sessionFactory + Session preBoundSession = secondarySf.openSession() + SessionHolder preBoundHolder = new SessionHolder(preBoundSession) + TransactionSynchronizationManager.bindResource(secondarySf, preBoundHolder) + interceptor.preHandle(webRequest) + Session defaultSession = boundSession(datastore.sessionFactory) + + when: "the request goes asynchronous" + interceptor.afterConcurrentHandlingStarted(webRequest) + + then: "the pre-bound session is left bound and open" + TransactionSynchronizationManager.getResource(secondarySf).is(preBoundHolder) + preBoundSession.open + + cleanup: if (TransactionSynchronizationManager.hasResource(secondarySf)) { TransactionSynchronizationManager.unbindResource(secondarySf) } preBoundSession.close() + defaultSession?.close() + } + + def "a request that goes asynchronous closes the session of another datasource without a flush"() { + given: "a book of the other datasource" + OsivBook.withTransaction { new OsivBook(title: 'Before').save() } + + and: "a request that changes it without a flush" + def interceptor = new GrailsOpenSessionInViewInterceptor() + interceptor.setHibernateDatastore(datastore) + WebRequest webRequest = webRequest() + SessionFactory secondarySf = datastore.getDatastoreForConnection('secondary').sessionFactory + interceptor.preHandle(webRequest) + Session defaultSession = boundSession(datastore.sessionFactory) + Session secondarySession = boundSession(secondarySf) + OsivBook book = OsivBook.findByTitle('Before') + book.title = 'Changed before going asynchronous' + + expect: "the default flush mode, under which the change is not written when an action returns either" + secondarySession.hibernateFlushMode == FlushMode.COMMIT + + when: "the request goes asynchronous" + interceptor.afterConcurrentHandlingStarted(webRequest) + + then: "the change is not written, and the session is closed" + OsivBook.withNewSession { OsivBook.findByTitle('Before') } != null + OsivBook.withNewSession { OsivBook.findByTitle('Changed before going asynchronous') } == null + !TransactionSynchronizationManager.hasResource(secondarySf) + !secondarySession.open + + cleanup: + defaultSession?.close() + OsivBook.withTransaction { OsivBook.list()*.delete(flush: true) } } def "CRUD operations work on secondary datasource with OSIV"() { @@ -174,6 +451,29 @@ class MultiDataSourceSessionSpec extends Specification { } interceptor.afterCompletion(webRequest, null) } + + private static String titleOf(SessionFactory sessionFactory, Class type, Serializable id) { + Session session = sessionFactory.openSession() + try { + session.get(type, id).title + } + finally { + session.close() + } + } + + private WebRequest webRequest() { + Map attributes = [:] + Stub(WebRequest) { + getAttribute(_ as String, WebRequest.SCOPE_REQUEST) >> { String name, int scope -> attributes[name] } + setAttribute(_ as String, _, WebRequest.SCOPE_REQUEST) >> { String name, Object value, int scope -> attributes[name] = value } + removeAttribute(_ as String, WebRequest.SCOPE_REQUEST) >> { String name, int scope -> attributes.remove(name) } + } + } + + private static Session boundSession(SessionFactory sessionFactory) { + ((SessionHolder) TransactionSynchronizationManager.getResource(sessionFactory))?.session + } } @Entity @@ -191,3 +491,47 @@ class OsivBook { class OsivAuthor { String name } + +@Entity +class OutsideTransactionBook { + String title + + static mapping = { + datasource 'secondary' + } +} + +@Entity +class InsideTransactionBook { + String title + + static mapping = { + datasource 'secondary' + } +} + +@Entity +class RefusedSecondaryBook { + String title + + def beforeUpdate() { + assert title != 'Refused' + } + + static mapping = { + datasource 'secondary' + } +} + +@Entity +class RefusedThirdBook { + String title + + def beforeUpdate() { + assert title != 'Refused' + } + + static mapping = { + datasource 'third' + } +} diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/HibernateDatastore.java b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/HibernateDatastore.java index 71345e0f034..86da125485a 100644 --- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/HibernateDatastore.java +++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/HibernateDatastore.java @@ -79,6 +79,7 @@ import grails.gorm.multitenancy.Tenants; import org.grails.datastore.gorm.GormRegistry; import org.grails.datastore.gorm.events.AutoTimestampEventListener; +import org.grails.datastore.gorm.events.AutoTimestampingDatastore; import org.grails.datastore.gorm.events.ConfigurableApplicationContextEventPublisher; import org.grails.datastore.gorm.events.ConfigurableApplicationEventPublisher; import org.grails.datastore.gorm.events.DefaultApplicationEventPublisher; @@ -151,7 +152,8 @@ public class HibernateDatastore extends AbstractDatastore TransactionCapableDatastore, Closeable, MessageSourceAware, - MultipleConnectionSourceCapableDatastore { + MultipleConnectionSourceCapableDatastore, + AutoTimestampingDatastore { private static final Logger LOG = LoggerFactory.getLogger(HibernateDatastore.class); /** @@ -1013,6 +1015,7 @@ public HibernateEventListener getEventTriggeringInterceptor() { return eventTriggeringInterceptor; } + @Override public AutoTimestampEventListener getAutoTimestampEventListener() { return autoTimestampEventListener; } diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/HibernateGormStaticApi.groovy b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/HibernateGormStaticApi.groovy index 9f2358e309b..aca747fbb36 100644 --- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/HibernateGormStaticApi.groovy +++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/HibernateGormStaticApi.groovy @@ -59,6 +59,7 @@ import org.springframework.transaction.PlatformTransactionManager import grails.orm.HibernateCriteriaBuilder import grails.gorm.DetachedCriteria import org.grails.datastore.gorm.GormStaticApi +import org.grails.datastore.gorm.events.AutoTimestampEventListener import org.grails.datastore.gorm.finders.FinderMethod import org.grails.datastore.gorm.internal.RefreshLockArguments import org.grails.datastore.mapping.core.connections.ConnectionSource @@ -134,6 +135,15 @@ class HibernateGormStaticApi extends GormStaticApi { (GormStaticApi) HibernateGormEnhancer.findStaticApi(persistentClass, qualifier) } + /** + * Hibernate saves an instance through the datastore this API is bound to, inside a tenant block as well, so this + * is that datastore's listener. + */ + @Override + AutoTimestampEventListener getAutoTimestampEventListener() { + autoTimestampEventListenerOf(getDatastore()) + } + @Override DetachedCriteria where(Closure callable) { new HibernateDetachedCriteria(persistentClass).build((Closure) callable) diff --git a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/connections/AutoTimestampEventListenerConnectionsSpec.groovy b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/connections/AutoTimestampEventListenerConnectionsSpec.groovy new file mode 100644 index 00000000000..df43d5b119f --- /dev/null +++ b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/connections/AutoTimestampEventListenerConnectionsSpec.groovy @@ -0,0 +1,130 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.grails.orm.hibernate.connections + +import java.util.function.Supplier + +import org.hibernate.dialect.H2Dialect +import spock.lang.AutoCleanup +import spock.lang.Specification + +import org.springframework.context.support.GenericApplicationContext + +import grails.gorm.annotation.CreatedBy +import grails.gorm.annotation.Entity +import grails.gorm.annotation.LastModifiedBy +import org.grails.datastore.gorm.timestamp.AuditorAware +import org.grails.datastore.mapping.config.Settings +import org.grails.datastore.mapping.core.DatastoreUtils +import org.grails.orm.hibernate.HibernateDatastore + +/** + * The listener of every connection, not only that of the default one, sets the auditor properties and follows the + * auto-timestamp settings of the configuration. + */ +class AutoTimestampEventListenerConnectionsSpec extends Specification { + + private static final Date PAST = new Date(110_000_000_000L) + + @AutoCleanup + GenericApplicationContext context + + @AutoCleanup + HibernateDatastore datastore + + void 'every connection sets the auditor properties from the application context of the datastore'() { + given: + context = new GenericApplicationContext() + context.registerBean(AuditorAware, { { -> Optional.of('auditor') } as AuditorAware } as Supplier) + context.refresh() + datastore = new HibernateDatastore(DatastoreUtils.createPropertyResolver(configuration()), AuditedConnectionNote) + datastore.setApplicationContext(context) + AuditedConnectionNote onDefault = new AuditedConnectionNote(text: 'default') + AuditedConnectionNote onSecondary = new AuditedConnectionNote(text: 'secondary') + + when: + AuditedConnectionNote.withTransaction { + onDefault.save(flush: true) + } + AuditedConnectionNote.secondary.withTransaction { + onSecondary.secondary.save(flush: true) + } + + then: + onDefault.createdBy == 'auditor' + onDefault.lastModifiedBy == 'auditor' + onSecondary.createdBy == 'auditor' + onSecondary.lastModifiedBy == 'auditor' + } + + void 'every connection keeps a given timestamp when the configuration turns overwriting off'() { + given: + datastore = new HibernateDatastore(DatastoreUtils.createPropertyResolver(configuration() + [ + (Settings.SETTING_AUTO_TIMESTAMP_INSERT_OVERWRITE): false + ]), AuditedConnectionNote) + AuditedConnectionNote onDefault = new AuditedConnectionNote(text: 'default', dateCreated: PAST) + AuditedConnectionNote onSecondary = new AuditedConnectionNote(text: 'secondary', dateCreated: PAST) + + when: + AuditedConnectionNote.withTransaction { + onDefault.save(flush: true) + } + AuditedConnectionNote.secondary.withTransaction { + onSecondary.secondary.save(flush: true) + } + + then: + AuditedConnectionNote.withNewTransaction { AuditedConnectionNote.get(onDefault.id).dateCreated } == PAST + AuditedConnectionNote.secondary.withNewTransaction { AuditedConnectionNote.secondary.get(onSecondary.id).dateCreated } == PAST + onSecondary.lastUpdated != null + } + + private static Map configuration() { + [ + 'dataSource.url' : 'jdbc:h2:mem:timestampConnectionsDefaultDB;LOCK_TIMEOUT=10000', + 'dataSource.dbCreate' : 'create-drop', + 'dataSource.dialect' : H2Dialect.name, + 'hibernate.hbm2ddl.auto': 'create', + 'dataSources.secondary' : [url: 'jdbc:h2:mem:timestampConnectionsSecondaryDB;LOCK_TIMEOUT=10000'] + ] as Map + } +} + +@Entity +class AuditedConnectionNote { + + String text + Date dateCreated + Date lastUpdated + + @CreatedBy + String createdBy + + @LastModifiedBy + String lastModifiedBy + + static constraints = { + createdBy nullable: true + lastModifiedBy nullable: true + } + + static mapping = { + datasource 'ALL' + } +} diff --git a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/connections/AutoTimestampEventListenerTenantSpec.groovy b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/connections/AutoTimestampEventListenerTenantSpec.groovy new file mode 100644 index 00000000000..42c7c15edda --- /dev/null +++ b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/connections/AutoTimestampEventListenerTenantSpec.groovy @@ -0,0 +1,188 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.grails.orm.hibernate.connections + +import org.hibernate.dialect.H2Dialect +import spock.lang.AutoCleanup +import spock.lang.Issue +import spock.lang.Specification +import spock.util.environment.RestoreSystemProperties + +import grails.gorm.MultiTenant +import grails.gorm.annotation.Entity +import grails.gorm.multitenancy.Tenants +import org.grails.datastore.gorm.events.AutoTimestampEventListener +import org.grails.datastore.mapping.core.DatastoreUtils +import org.grails.datastore.mapping.multitenancy.AllTenantsResolver +import org.grails.datastore.mapping.multitenancy.resolvers.SystemPropertyTenantResolver +import org.grails.orm.hibernate.HibernateDatastore + +/** + * A tenant with a database or a schema of its own has an auto-timestamp listener of its own, and a multi-tenant + * domain class answers with the current tenant's. + */ +@Issue('https://github.com/apache/grails-core/issues/14509') +@RestoreSystemProperties +class AutoTimestampEventListenerTenantSpec extends Specification { + + private static final Date PAST = new Date(110_000_000_000L) + + @AutoCleanup + HibernateDatastore datastore + + void setup() { + System.setProperty(SystemPropertyTenantResolver.PROPERTY_NAME, '') + } + + void 'a tenant with a database of its own answers with the listener of that database'() { + given: + datastore = databasePerTenant() + AutoTimestampEventListener books = listenerOf('books') + AutoTimestampEventListener moreBooks = listenerOf('moreBooks') + + expect: + !books.is(moreBooks) + !books.is(datastore.autoTimestampEventListener) + Tenants.withId('books') { TenantTimestampNote.autoTimestampEventListener }.is(books) + Tenants.withId('moreBooks') { TenantTimestampNote.autoTimestampEventListener }.is(moreBooks) + TenantTimestampNote.withTenant('moreBooks').autoTimestampEventListener.is(moreBooks) + + when: 'the current tenant comes from the resolver' + System.setProperty(SystemPropertyTenantResolver.PROPERTY_NAME, 'moreBooks') + + then: + TenantTimestampNote.autoTimestampEventListener.is(moreBooks) + } + + void 'the listener of the current tenant turns off the timestamps of its saves, and another tenant\'s does not'() { + given: + datastore = databasePerTenant() + System.setProperty(SystemPropertyTenantResolver.PROPERTY_NAME, 'books') + TenantTimestampNote kept = new TenantTimestampNote(text: 'kept', dateCreated: PAST, lastUpdated: PAST) + TenantTimestampNote stamped = new TenantTimestampNote(text: 'stamped', dateCreated: PAST, lastUpdated: PAST) + + when: + TenantTimestampNote.withTransaction { + TenantTimestampNote.autoTimestampEventListener.withoutTimestamps { + kept.save(flush: true) + } + listenerOf('moreBooks').withoutTimestamps { + stamped.save(flush: true) + } + } + + then: + TenantTimestampNote.withNewTransaction { TenantTimestampNote.get(kept.id).dateCreated } == PAST + TenantTimestampNote.withNewTransaction { TenantTimestampNote.get(kept.id).lastUpdated } == PAST + TenantTimestampNote.withNewTransaction { TenantTimestampNote.get(stamped.id).dateCreated } != PAST + TenantTimestampNote.withNewTransaction { TenantTimestampNote.get(stamped.id).lastUpdated } != PAST + } + + void 'a class that is not multi-tenant answers inside a tenant block with the listener of the default database'() { + given: 'Hibernate saves a class that is not multi-tenant to the default database inside a tenant block too' + datastore = databasePerTenant() + PlainTimestampNote kept = new PlainTimestampNote(text: 'kept', dateCreated: PAST, lastUpdated: PAST) + + when: + def listener = Tenants.withId('books') { + PlainTimestampNote.withTransaction { + PlainTimestampNote.autoTimestampEventListener.withoutTimestamps { + kept.save(flush: true) + } + } + PlainTimestampNote.autoTimestampEventListener + } + + then: + listener.is(datastore.autoTimestampEventListener) + PlainTimestampNote.withNewTransaction { PlainTimestampNote.get(kept.id).dateCreated } == PAST + PlainTimestampNote.withNewTransaction { PlainTimestampNote.get(kept.id).lastUpdated } == PAST + } + + void 'a tenant with a schema of its own answers with the listener of that schema'() { + given: + datastore = new HibernateDatastore(DatastoreUtils.createPropertyResolver([ + 'grails.gorm.multiTenancy.mode' : 'SCHEMA', + 'grails.gorm.multiTenancy.tenantResolverClass': TwoSchemaResolver, + 'dataSource.url' : 'jdbc:h2:mem:timestampSchemaDB;LOCK_TIMEOUT=10000', + 'dataSource.dbCreate' : 'create-drop', + 'dataSource.dialect' : H2Dialect.name, + 'hibernate.hbm2ddl.auto' : 'create' + ]), TenantTimestampNote) + AutoTimestampEventListener books = listenerOf('books') + AutoTimestampEventListener moreBooks = listenerOf('moreBooks') + TenantTimestampNote kept = new TenantTimestampNote(text: 'kept', dateCreated: PAST, lastUpdated: PAST) + + when: + Tenants.withId('books') { + TenantTimestampNote.withTransaction { + TenantTimestampNote.autoTimestampEventListener.withoutTimestamps { + kept.save(flush: true) + } + } + } + + then: + !books.is(moreBooks) + Tenants.withId('books') { TenantTimestampNote.autoTimestampEventListener }.is(books) + TenantTimestampNote.withTenant('moreBooks').autoTimestampEventListener.is(moreBooks) + Tenants.withId('books') { TenantTimestampNote.withNewTransaction { TenantTimestampNote.get(kept.id).dateCreated } } == PAST + } + + private HibernateDatastore databasePerTenant() { + new HibernateDatastore(DatastoreUtils.createPropertyResolver([ + 'grails.gorm.multiTenancy.mode' : 'DATABASE', + 'grails.gorm.multiTenancy.tenantResolverClass': SystemPropertyTenantResolver, + 'dataSource.url' : 'jdbc:h2:mem:timestampDefaultDB;LOCK_TIMEOUT=10000', + 'dataSource.dbCreate' : 'create-drop', + 'dataSource.dialect' : H2Dialect.name, + 'hibernate.hbm2ddl.auto' : 'create', + 'dataSources.books' : [url: 'jdbc:h2:mem:timestampBooksDB;LOCK_TIMEOUT=10000'], + 'dataSources.moreBooks' : [url: 'jdbc:h2:mem:timestampMoreBooksDB;LOCK_TIMEOUT=10000'] + ]), TenantTimestampNote, PlainTimestampNote) + } + + private AutoTimestampEventListener listenerOf(String connectionName) { + datastore.getDatastoreForConnection(connectionName).autoTimestampEventListener + } + + static class TwoSchemaResolver extends SystemPropertyTenantResolver implements AllTenantsResolver { + + @Override + Iterable resolveTenantIds() { + ['books', 'moreBooks'] + } + } +} + +@Entity +class TenantTimestampNote implements MultiTenant { + + String text + Date dateCreated + Date lastUpdated +} + +@Entity +class PlainTimestampNote { + + String text + Date dateCreated + Date lastUpdated +} diff --git a/grails-data-hibernate7/docs/src/docs/asciidoc/advancedGORMFeatures/eventsAutoTimestamping.adoc b/grails-data-hibernate7/docs/src/docs/asciidoc/advancedGORMFeatures/eventsAutoTimestamping.adoc index df5a9c418c2..dfa0b282015 100644 --- a/grails-data-hibernate7/docs/src/docs/asciidoc/advancedGORMFeatures/eventsAutoTimestamping.adoc +++ b/grails-data-hibernate7/docs/src/docs/asciidoc/advancedGORMFeatures/eventsAutoTimestamping.adoc @@ -387,21 +387,45 @@ WARNING: If you have `nullable: false` constraints on either `dateCreated` or `l It is also possible to disable the automatic timestamping temporarily. This is most typically done in the case of a test where you need to define values for the `dateCreated` or `lastUpdated` in the past. It may also be useful for importing old data from other systems where you would like to keep the current values of the timestamps. -Timestamps can be temporarily disabled for all domains, a specified list of domains, or a single domain. To get started, you need to get a reference to the `AutoTimestampEventListener`. If you already have access to the datastore, you can execute the `getAutoTimestampEventListener` method. If you don't have access to the datastore, inject the `autoTimestampEventListener` bean. +Timestamps can be temporarily disabled for all domains, a specified list of domains, or a single domain. To get started, you need a reference to the `AutoTimestampEventListener` that handles the domain class, which the domain class answers with: + +[source,groovy] +---- +Foo.autoTimestampEventListener.withoutTimestamps { + new Foo(dateCreated: new Date() - 2, lastUpdated: new Date() - 1).save(flush: true) +} +---- + +Each datastore has a listener of its own, and so does each data source of an application with several and each tenant with a database or schema of its own. A listener only handles the saves made through its own datastore, so turning timestamps off on one leaves the saves to another data source timestamped. `Foo.autoTimestampEventListener` is the listener of the data source that a save of `Foo` made from the same place goes to: its default data source, the one a `Foo.secondary.withTransaction { }` block or a method annotated `@Transactional(connection = 'secondary')` routes it to, or the current tenant's for a multi-tenant class. For another data source of a class mapped to several, name the data source, as in `Foo.secondary.autoTimestampEventListener`. + +[source,groovy] +---- +// Car is mapped to the default data source and Person to the 'people' data source +Car.autoTimestampEventListener.withoutTimestamps { + car.save(flush: true) +} +Person.autoTimestampEventListener.withoutTimestamps { + person.save(flush: true) +} +---- + +The `autoTimestampEventListener` bean, and the `getAutoTimestampEventListener` method of the datastore bean, return the listener of the default data source. Once you have a reference to the event listener, you can execute `withoutDateCreated`, `withoutLastUpdated`, or `withoutTimestamps`. The `withoutTimestamps` method will temporarily disable both `dateCreated` and `lastUpdated`. -Example: +Example, where `Foo`, `Bar` and `FooBar` share a data source: [source,groovy] ---- +def autoTimestampEventListener = Foo.autoTimestampEventListener + //Only the dateCreated property handling will be disabled for only the Foo domain autoTimestampEventListener.withoutDateCreated(Foo) { new Foo(dateCreated: new Date() - 1).save(flush: true) } //Only the lastUpdated property handling will be disabled for only the Foo and Bar domains -autoTimestampEventListener.withoutLastUpdated(Foo, Bar) { +autoTimestampEventListener.withoutLastUpdated([Foo, Bar]) { new Foo(lastUpdated: new Date() - 1, bar: new Bar(lastUpdated: new Date() + 1)).save(flush: true) } diff --git a/grails-data-hibernate7/grails-plugin/src/main/groovy/org/grails/plugin/hibernate/support/GrailsOpenSessionInViewInterceptor.java b/grails-data-hibernate7/grails-plugin/src/main/groovy/org/grails/plugin/hibernate/support/GrailsOpenSessionInViewInterceptor.java index b5afd5ddd82..a3357bd5410 100644 --- a/grails-data-hibernate7/grails-plugin/src/main/groovy/org/grails/plugin/hibernate/support/GrailsOpenSessionInViewInterceptor.java +++ b/grails-data-hibernate7/grails-plugin/src/main/groovy/org/grails/plugin/hibernate/support/GrailsOpenSessionInViewInterceptor.java @@ -24,11 +24,13 @@ import org.hibernate.FlushMode; import org.hibernate.Session; import org.hibernate.SessionFactory; +import org.hibernate.engine.spi.SharedSessionContractImplementor; import org.springframework.dao.DataAccessException; import org.springframework.dao.DataAccessResourceFailureException; import org.springframework.transaction.support.TransactionSynchronizationManager; import org.springframework.ui.ModelMap; +import org.springframework.util.ReflectionUtils; import org.springframework.web.context.request.WebRequest; import org.grails.datastore.mapping.core.connections.ConnectionSource; @@ -44,13 +46,20 @@ * The default datasource's SessionFactory is managed by the parent class. * Additional (non-default) datasource SessionFactories are managed by this * subclass, which opens and closes sessions for each one alongside the - * default session. + * default session. As the parent class does for the default session, a request + * that finds a session already bound, such as an include within a request that + * opened one, uses that session and leaves it to the one that opened it, and a + * request that goes asynchronous closes the sessions it opened, so that they do + * not stay bound to the thread for the next request that thread serves. * * @author Graeme Rocher * @since 0.5 */ public class GrailsOpenSessionInViewInterceptor extends OpenSessionInViewInterceptor { + /** The value of {@code GrailsApplicationAttributes.ASYNC_STARTED}, which is in a module this plugin does not use. */ + private static final String GRAILS_ASYNC_STARTED = "org.grails.ASYNC_STARTED"; + protected FlushMode hibernateFlushMode = FlushMode.MANUAL; private final List additionalSessionFactories = new ArrayList<>(); @@ -86,6 +95,8 @@ public void preHandle(WebRequest request) throws DataAccessException { for (AdditionalSessionFactoryConfig config : additionalSessionFactories) { SessionFactory sf = config.sessionFactory; if (TransactionSynchronizationManager.hasResource(sf)) { + // a session is already open for this request: use it, and leave it to the one that opened it + incrementParticipateCount(request, sf); continue; } if (logger.isDebugEnabled()) { @@ -158,26 +169,139 @@ public void postHandle(WebRequest request, ModelMap model) throws DataAccessExce @Override public void afterCompletion(WebRequest request, Exception ex) throws DataAccessException { try { - for (int i = additionalSessionFactories.size() - 1; i >= 0; i--) { - AdditionalSessionFactoryConfig config = additionalSessionFactories.get(i); - SessionFactory sf = config.sessionFactory; - SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager.getResource(sf); - if (sessionHolder != null) { - Session session = sessionHolder.getSession(); - TransactionSynchronizationManager.unbindResource(sf); - if (logger.isDebugEnabled()) { - logger.debug("Closing additional Hibernate Session for datasource '" + config.connectionName + "' in OpenSessionInViewInterceptor"); + closeAdditionalSessions(request, false); + } finally { + super.afterCompletion(request, ex); + } + } + + /** + * Called instead of {@link #postHandle} and {@link #afterCompletion} on the thread that started asynchronous + * handling. The sessions this interceptor opened for the other datasources are closed here, so that the thread + * goes back to its pool without them. Each is flushed first when {@link #postHandle} would flush it and Hibernate + * allows the flush: inside a transaction, or with {@code hibernate.allow_update_outside_transaction}. + * + *

The parent class only unbinds the default session, and leaves closing it to the interceptors it registers for + * a {@code Callable} or {@code DeferredResult}. A promise or {@code startAsync()} in a Grails controller starts + * asynchronous handling without them, so the default session this interceptor opened is closed here too. + */ + @Override + public void afterConcurrentHandlingStarted(WebRequest request) { + try { + closeAdditionalSessions(request, true); + } finally { + if (isGrailsAsync(request) && !isParticipating(request) && + TransactionSynchronizationManager.hasResource(getSessionFactory())) { + SessionHolder sessionHolder = + (SessionHolder) TransactionSynchronizationManager.unbindResource(getSessionFactory()); + if (logger.isDebugEnabled()) { + logger.debug("Closing Hibernate Session in OpenSessionInViewInterceptor as asynchronous handling starts"); + } + SessionFactoryUtils.closeSession(sessionHolder.getSession()); + } else { + super.afterConcurrentHandlingStarted(request); + } + } + } + + /** + * @return whether Grails started asynchronous handling, for a promise or {@code startAsync()}: both set + * {@code GrailsApplicationAttributes.ASYNC_STARTED} + */ + private static boolean isGrailsAsync(WebRequest request) { + return request.getAttribute(GRAILS_ASYNC_STARTED, WebRequest.SCOPE_REQUEST) != null; + } + + /** + * @return whether the default session was already bound when the request started, so that it belongs to + * whoever bound it + */ + private boolean isParticipating(WebRequest request) { + return request.getAttribute(getParticipateAttributeName(), WebRequest.SCOPE_REQUEST) != null; + } + + private void closeAdditionalSessions(WebRequest request, boolean flushFirst) { + Throwable firstFlushFailure = null; + for (int i = additionalSessionFactories.size() - 1; i >= 0; i--) { + AdditionalSessionFactoryConfig config = additionalSessionFactories.get(i); + SessionFactory sf = config.sessionFactory; + if (decrementParticipateCount(request, sf)) { + continue; + } + SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager.getResource(sf); + if (sessionHolder != null) { + Session session = sessionHolder.getSession(); + try { + if (flushFirst && isFlushAllowed(session)) { + if (logger.isDebugEnabled()) { + logger.debug("Flushing additional Hibernate session for datasource '" + config.connectionName + "' as asynchronous handling starts"); + } + session.flush(); } - try { - SessionFactoryUtils.closeSession(session); - } catch (RuntimeException closeEx) { - logger.error("Unexpected exception on closing additional Hibernate Session for datasource '" + config.connectionName + "'", closeEx); + } catch (Throwable flushFailure) { + // an Error too, such as a failed assert in a domain's beforeUpdate: every session is released first + if (firstFlushFailure == null) { + firstFlushFailure = flushFailure; + } else { + firstFlushFailure.addSuppressed(flushFailure); } } + TransactionSynchronizationManager.unbindResource(sf); + if (logger.isDebugEnabled()) { + logger.debug("Closing additional Hibernate Session for datasource '" + config.connectionName + "' in OpenSessionInViewInterceptor"); + } + try { + SessionFactoryUtils.closeSession(session); + } catch (RuntimeException closeEx) { + logger.error("Unexpected exception on closing additional Hibernate Session for datasource '" + config.connectionName + "'", closeEx); + } } - } finally { - super.afterCompletion(request, ex); } + if (firstFlushFailure != null) { + ReflectionUtils.rethrowRuntimeException(firstFlushFailure); + } + } + + /** + * @return whether {@link #postHandle} would flush the session, and Hibernate would let it: a session it would not + * flush (MANUAL or COMMIT) is not flushed here either, and one outside a transaction only when + * {@code hibernate.allow_update_outside_transaction} is set, since Hibernate refuses that flush otherwise + */ + private static boolean isFlushAllowed(Session session) { + FlushMode flushMode = session.getHibernateFlushMode(); + if (flushMode == FlushMode.MANUAL || flushMode == FlushMode.COMMIT) { + return false; + } + return ((SharedSessionContractImplementor) session).isTransactionInProgress() || + session.getSessionFactory().getSessionFactoryOptions().isAllowOutOfTransactionUpdateOperations(); + } + + private static void incrementParticipateCount(WebRequest request, SessionFactory sf) { + String participateAttributeName = participateAttributeName(sf); + Integer count = (Integer) request.getAttribute(participateAttributeName, WebRequest.SCOPE_REQUEST); + request.setAttribute(participateAttributeName, count != null ? count + 1 : 1, WebRequest.SCOPE_REQUEST); + } + + /** + * @return {@code true} when this request used a session of the given factory that was already open, which is + * then left as it is + */ + private static boolean decrementParticipateCount(WebRequest request, SessionFactory sf) { + String participateAttributeName = participateAttributeName(sf); + Integer count = (Integer) request.getAttribute(participateAttributeName, WebRequest.SCOPE_REQUEST); + if (count == null) { + return false; + } + if (count > 1) { + request.setAttribute(participateAttributeName, count - 1, WebRequest.SCOPE_REQUEST); + } else { + request.removeAttribute(participateAttributeName, WebRequest.SCOPE_REQUEST); + } + return true; + } + + private static String participateAttributeName(SessionFactory sf) { + return sf.toString() + PARTICIPATE_SUFFIX; } public void setHibernateDatastore(HibernateDatastore hibernateDatastore) { diff --git a/grails-data-hibernate7/grails-plugin/src/test/groovy/org/grails/plugin/hibernate/support/MultiDataSourceSessionSpec.groovy b/grails-data-hibernate7/grails-plugin/src/test/groovy/org/grails/plugin/hibernate/support/MultiDataSourceSessionSpec.groovy index 5c21e642017..3316ae50277 100644 --- a/grails-data-hibernate7/grails-plugin/src/test/groovy/org/grails/plugin/hibernate/support/MultiDataSourceSessionSpec.groovy +++ b/grails-data-hibernate7/grails-plugin/src/test/groovy/org/grails/plugin/hibernate/support/MultiDataSourceSessionSpec.groovy @@ -21,6 +21,7 @@ package org.grails.plugin.hibernate.support import grails.gorm.annotation.Entity import org.grails.datastore.mapping.core.DatastoreUtils import org.grails.orm.hibernate.HibernateDatastore +import org.hibernate.FlushMode import org.hibernate.Session import org.hibernate.SessionFactory import org.hibernate.dialect.H2Dialect @@ -45,6 +46,17 @@ class MultiDataSourceSessionSpec extends Specification { @Shared @AutoCleanup HibernateDatastore datastore = new HibernateDatastore(DatastoreUtils.createPropertyResolver(config), OsivBook, OsivAuthor) + void cleanup() { + [datastore.sessionFactory, datastore.getDatastoreForConnection('secondary').sessionFactory].each { SessionFactory sessionFactory -> + if (TransactionSynchronizationManager.hasResource(sessionFactory)) { + Session session = ((SessionHolder) TransactionSynchronizationManager.unbindResource(sessionFactory)).session + if (session.open) { + session.close() + } + } + } + } + def "withSession on default datasource works with OSIV"() { given: "OSIV interceptor configured with the datastore" def interceptor = new GrailsOpenSessionInViewInterceptor() @@ -118,7 +130,7 @@ class MultiDataSourceSessionSpec extends Specification { given: "a pre-bound session for the secondary datasource" def interceptor = new GrailsOpenSessionInViewInterceptor() interceptor.setHibernateDatastore(datastore) - WebRequest webRequest = Mock(WebRequest) + WebRequest webRequest = webRequest() def secondaryDatastore = datastore.getDatastoreForConnection('secondary') SessionFactory secondarySf = secondaryDatastore.sessionFactory @@ -133,12 +145,277 @@ class MultiDataSourceSessionSpec extends Specification { def currentHolder = TransactionSynchronizationManager.getResource(secondarySf) currentHolder.is(preBoundHolder) + when: "the request completes" + interceptor.afterCompletion(webRequest, null) + + then: "the pre-bound session is left bound and open" + TransactionSynchronizationManager.getResource(secondarySf).is(preBoundHolder) + preBoundSession.open + cleanup: + if (TransactionSynchronizationManager.hasResource(secondarySf)) { + TransactionSynchronizationManager.unbindResource(secondarySf) + } + preBoundSession.close() + } + + def "an include uses the sessions of the request for every datasource and leaves them open"() { + given: "the request has opened its sessions" + def interceptor = new GrailsOpenSessionInViewInterceptor() + interceptor.setHibernateDatastore(datastore) + WebRequest webRequest = webRequest() + SessionFactory secondarySf = datastore.getDatastoreForConnection('secondary').sessionFactory + interceptor.preHandle(webRequest) + Session defaultSession = boundSession(datastore.sessionFactory) + Session secondarySession = boundSession(secondarySf) + + when: "an include within it starts and completes" + interceptor.preHandle(webRequest) + interceptor.postHandle(webRequest, null) interceptor.afterCompletion(webRequest, null) + + then: "the sessions of the request are still bound and open" + boundSession(datastore.sessionFactory).is(defaultSession) + boundSession(secondarySf).is(secondarySession) + secondarySession.open + + when: "the request completes" + interceptor.afterCompletion(webRequest, null) + + then: "they are unbound and closed" + !TransactionSynchronizationManager.hasResource(datastore.sessionFactory) + !TransactionSynchronizationManager.hasResource(secondarySf) + !secondarySession.open + } + + def "a request that goes asynchronous closes the sessions it opened and leaves the thread without them"() { + given: "the request has opened its sessions" + def interceptor = new GrailsOpenSessionInViewInterceptor() + interceptor.setHibernateDatastore(datastore) + WebRequest webRequest = webRequest() + SessionFactory secondarySf = datastore.getDatastoreForConnection('secondary').sessionFactory + interceptor.preHandle(webRequest) + Session defaultSession = boundSession(datastore.sessionFactory) + Session secondarySession = boundSession(secondarySf) + + when: "the request goes asynchronous" + interceptor.afterConcurrentHandlingStarted(webRequest) + + then: "no session is bound to the thread any more" + !TransactionSynchronizationManager.hasResource(datastore.sessionFactory) + !TransactionSynchronizationManager.hasResource(secondarySf) + + and: "the session of the other datasource is closed" + !secondarySession.open + + cleanup: + defaultSession?.close() + } + + def "a request that goes asynchronous flushes another datasource's session under AUTO when updates outside a transaction are allowed"() { + given: "a datastore that flushes in AUTO mode and allows updates outside a transaction, with a book on its secondary datasource" + HibernateDatastore autoFlush = new HibernateDatastore(DatastoreUtils.createPropertyResolver(config + [ + 'hibernate.flush.mode': 'AUTO', + 'hibernate.allow_update_outside_transaction': 'true', + 'dataSource.url': 'jdbc:h2:mem:outsideTxDefaultDb;LOCK_TIMEOUT=10000', + 'dataSources.secondary': [url: 'jdbc:h2:mem:outsideTxSecondaryDb;LOCK_TIMEOUT=10000'], + ]), OutsideTransactionBook) + SessionFactory secondarySf = autoFlush.getDatastoreForConnection('secondary').sessionFactory + Serializable bookId = OutsideTransactionBook.withNewTransaction { new OutsideTransactionBook(title: 'Before').save(flush: true).id } + + and: "a request that changes it without a flush" + def interceptor = new GrailsOpenSessionInViewInterceptor() + interceptor.setHibernateDatastore(autoFlush) + WebRequest webRequest = webRequest() + interceptor.preHandle(webRequest) + Session secondarySession = boundSession(secondarySf) + secondarySession.get(OutsideTransactionBook, bookId).title = 'After' + + when: "the request goes asynchronous" + interceptor.afterConcurrentHandlingStarted(webRequest) + + then: "the change is written, as postHandle would write it, and the session is closed" + titleOf(secondarySf, OutsideTransactionBook, bookId) == 'After' + !secondarySession.open + !TransactionSynchronizationManager.hasResource(secondarySf) + + cleanup: + autoFlush?.close() + } + + def "a request that goes asynchronous does not flush another datasource's session under AUTO when Hibernate would refuse it"() { + given: "a datastore that flushes in AUTO mode with the default setting, which refuses a flush outside a transaction" + HibernateDatastore autoFlush = new HibernateDatastore(DatastoreUtils.createPropertyResolver(config + [ + 'hibernate.flush.mode': 'AUTO', + 'dataSource.url': 'jdbc:h2:mem:insideTxDefaultDb;LOCK_TIMEOUT=10000', + 'dataSources.secondary': [url: 'jdbc:h2:mem:insideTxSecondaryDb;LOCK_TIMEOUT=10000'], + ]), InsideTransactionBook) + SessionFactory secondarySf = autoFlush.getDatastoreForConnection('secondary').sessionFactory + Serializable bookId = InsideTransactionBook.withNewTransaction { new InsideTransactionBook(title: 'Before').save(flush: true).id } + + and: "a request that changes it without a flush" + def interceptor = new GrailsOpenSessionInViewInterceptor() + interceptor.setHibernateDatastore(autoFlush) + WebRequest webRequest = webRequest() + interceptor.preHandle(webRequest) + Session secondarySession = boundSession(secondarySf) + secondarySession.get(InsideTransactionBook, bookId).title = 'After' + + when: "the request goes asynchronous" + interceptor.afterConcurrentHandlingStarted(webRequest) + + then: "nothing is flushed or thrown, and the session is closed" + notThrown(Exception) + titleOf(secondarySf, InsideTransactionBook, bookId) == 'Before' + !secondarySession.open + !TransactionSynchronizationManager.hasResource(secondarySf) + + cleanup: + autoFlush?.close() + } + + def "a request that goes asynchronous releases every session it opened when a flush there throws an Error"() { + given: "a datastore that flushes in AUTO mode and allows updates outside a transaction, with a book on each of two other datasources" + HibernateDatastore autoFlush = new HibernateDatastore(DatastoreUtils.createPropertyResolver(config + [ + 'hibernate.flush.mode': 'AUTO', + 'hibernate.allow_update_outside_transaction': 'true', + 'dataSource.url': 'jdbc:h2:mem:refusedDefaultDb;LOCK_TIMEOUT=10000', + 'dataSources.secondary': [url: 'jdbc:h2:mem:refusedSecondaryDb;LOCK_TIMEOUT=10000'], + 'dataSources.third': [url: 'jdbc:h2:mem:refusedThirdDb;LOCK_TIMEOUT=10000'], + ]), RefusedSecondaryBook, RefusedThirdBook) + SessionFactory secondarySf = autoFlush.getDatastoreForConnection('secondary').sessionFactory + SessionFactory thirdSf = autoFlush.getDatastoreForConnection('third').sessionFactory + Serializable secondaryId = RefusedSecondaryBook.withNewTransaction { new RefusedSecondaryBook(title: 'Before').save(flush: true).id } + Serializable thirdId = RefusedThirdBook.withNewTransaction { new RefusedThirdBook(title: 'Before').save(flush: true).id } + + and: "a request, made asynchronous by Grails, that changes both so that each one's beforeUpdate fails its assert" + def interceptor = new GrailsOpenSessionInViewInterceptor() + interceptor.setHibernateDatastore(autoFlush) + WebRequest webRequest = webRequest() + interceptor.preHandle(webRequest) + Session defaultSession = boundSession(autoFlush.sessionFactory) + Session secondarySession = boundSession(secondarySf) + Session thirdSession = boundSession(thirdSf) + secondarySession.get(RefusedSecondaryBook, secondaryId).title = 'Refused' + thirdSession.get(RefusedThirdBook, thirdId).title = 'Refused' + webRequest.setAttribute('org.grails.ASYNC_STARTED', true, WebRequest.SCOPE_REQUEST) + + when: "the request goes asynchronous" + interceptor.afterConcurrentHandlingStarted(webRequest) + + then: "every session the request opened is unbound and closed" + AssertionError failure = thrown() + [autoFlush.sessionFactory, secondarySf, thirdSf].every { !TransactionSynchronizationManager.hasResource(it) } + !defaultSession.open + !secondarySession.open + !thirdSession.open + + and: "the first failure is thrown, with the second attached" + failure.suppressed.length == 1 + failure.suppressed[0] instanceof AssertionError + + cleanup: + autoFlush?.close() + } + + def "a request that Grails makes asynchronous closes its default session too"() { + given: "the request has opened its sessions" + def interceptor = new GrailsOpenSessionInViewInterceptor() + interceptor.setHibernateDatastore(datastore) + WebRequest webRequest = webRequest() + interceptor.preHandle(webRequest) + Session defaultSession = boundSession(datastore.sessionFactory) + + when: "the action returns a promise or calls startAsync(), which Spring's asynchronous interceptors never see" + webRequest.setAttribute('org.grails.ASYNC_STARTED', true, WebRequest.SCOPE_REQUEST) + interceptor.afterConcurrentHandlingStarted(webRequest) + + then: "the default session is unbound and closed" + !TransactionSynchronizationManager.hasResource(datastore.sessionFactory) + !defaultSession.open + + cleanup: + if (defaultSession?.open) { + defaultSession.close() + } + } + + def "a request that Spring makes asynchronous leaves its default session open for Spring to close"() { + given: "the request has opened its sessions" + def interceptor = new GrailsOpenSessionInViewInterceptor() + interceptor.setHibernateDatastore(datastore) + WebRequest webRequest = webRequest() + interceptor.preHandle(webRequest) + Session defaultSession = boundSession(datastore.sessionFactory) + + when: "the action returns a Callable or DeferredResult, whose processing rebinds the session and closes it" + interceptor.afterConcurrentHandlingStarted(webRequest) + + then: "the default session is unbound, and left open" + !TransactionSynchronizationManager.hasResource(datastore.sessionFactory) + defaultSession.open + + cleanup: + defaultSession?.close() + } + + def "a session of another datasource bound before the request stays bound and open when the request goes asynchronous"() { + given: "a pre-bound session for the secondary datasource" + def interceptor = new GrailsOpenSessionInViewInterceptor() + interceptor.setHibernateDatastore(datastore) + WebRequest webRequest = webRequest() + SessionFactory secondarySf = datastore.getDatastoreForConnection('secondary').sessionFactory + Session preBoundSession = secondarySf.openSession() + SessionHolder preBoundHolder = new SessionHolder(preBoundSession) + TransactionSynchronizationManager.bindResource(secondarySf, preBoundHolder) + interceptor.preHandle(webRequest) + Session defaultSession = boundSession(datastore.sessionFactory) + + when: "the request goes asynchronous" + interceptor.afterConcurrentHandlingStarted(webRequest) + + then: "the pre-bound session is left bound and open" + TransactionSynchronizationManager.getResource(secondarySf).is(preBoundHolder) + preBoundSession.open + + cleanup: if (TransactionSynchronizationManager.hasResource(secondarySf)) { TransactionSynchronizationManager.unbindResource(secondarySf) } preBoundSession.close() + defaultSession?.close() + } + + def "a request that goes asynchronous closes the session of another datasource without a flush"() { + given: "a book of the other datasource" + OsivBook.withTransaction { new OsivBook(title: 'Before').save() } + + and: "a request that changes it without a flush" + def interceptor = new GrailsOpenSessionInViewInterceptor() + interceptor.setHibernateDatastore(datastore) + WebRequest webRequest = webRequest() + SessionFactory secondarySf = datastore.getDatastoreForConnection('secondary').sessionFactory + interceptor.preHandle(webRequest) + Session defaultSession = boundSession(datastore.sessionFactory) + Session secondarySession = boundSession(secondarySf) + OsivBook book = OsivBook.findByTitle('Before') + book.title = 'Changed before going asynchronous' + + expect: "the default flush mode, under which the change is not written when an action returns either" + secondarySession.hibernateFlushMode == FlushMode.COMMIT + + when: "the request goes asynchronous" + interceptor.afterConcurrentHandlingStarted(webRequest) + + then: "the change is not written, and the session is closed" + OsivBook.withNewSession { OsivBook.findByTitle('Before') } != null + OsivBook.withNewSession { OsivBook.findByTitle('Changed before going asynchronous') } == null + !TransactionSynchronizationManager.hasResource(secondarySf) + !secondarySession.open + + cleanup: + defaultSession?.close() + OsivBook.withTransaction { OsivBook.list()*.delete(flush: true) } } def "CRUD operations work on secondary datasource with OSIV"() { @@ -173,6 +450,29 @@ class MultiDataSourceSessionSpec extends Specification { } interceptor.afterCompletion(webRequest, null) } + + private static String titleOf(SessionFactory sessionFactory, Class type, Serializable id) { + Session session = sessionFactory.openSession() + try { + session.get(type, id).title + } + finally { + session.close() + } + } + + private WebRequest webRequest() { + Map attributes = [:] + Stub(WebRequest) { + getAttribute(_ as String, WebRequest.SCOPE_REQUEST) >> { String name, int scope -> attributes[name] } + setAttribute(_ as String, _, WebRequest.SCOPE_REQUEST) >> { String name, Object value, int scope -> attributes[name] = value } + removeAttribute(_ as String, WebRequest.SCOPE_REQUEST) >> { String name, int scope -> attributes.remove(name) } + } + } + + private static Session boundSession(SessionFactory sessionFactory) { + ((SessionHolder) TransactionSynchronizationManager.getResource(sessionFactory))?.session + } } @Entity @@ -190,3 +490,47 @@ class OsivBook { class OsivAuthor { String name } + +@Entity +class OutsideTransactionBook { + String title + + static mapping = { + datasource 'secondary' + } +} + +@Entity +class InsideTransactionBook { + String title + + static mapping = { + datasource 'secondary' + } +} + +@Entity +class RefusedSecondaryBook { + String title + + def beforeUpdate() { + assert title != 'Refused' + } + + static mapping = { + datasource 'secondary' + } +} + +@Entity +class RefusedThirdBook { + String title + + def beforeUpdate() { + assert title != 'Refused' + } + + static mapping = { + datasource 'third' + } +} diff --git a/grails-data-mongodb/core/src/main/groovy/org/grails/datastore/mapping/mongo/MongoDatastore.java b/grails-data-mongodb/core/src/main/groovy/org/grails/datastore/mapping/mongo/MongoDatastore.java index 6136b9292f6..14e8db781fe 100644 --- a/grails-data-mongodb/core/src/main/groovy/org/grails/datastore/mapping/mongo/MongoDatastore.java +++ b/grails-data-mongodb/core/src/main/groovy/org/grails/datastore/mapping/mongo/MongoDatastore.java @@ -86,6 +86,7 @@ import org.grails.datastore.gorm.GormInstanceApi; import org.grails.datastore.gorm.GormValidationApi; import org.grails.datastore.gorm.events.AutoTimestampEventListener; +import org.grails.datastore.gorm.events.AutoTimestampingDatastore; import org.grails.datastore.gorm.events.ConfigurableApplicationEventPublisher; import org.grails.datastore.gorm.events.DefaultApplicationEventPublisher; import org.grails.datastore.gorm.events.DomainEventListener; @@ -147,7 +148,7 @@ * @author Graeme Rocher * @since 1.0 */ -public class MongoDatastore extends AbstractDatastore implements MappingContext.Listener, Closeable, StatelessDatastore, MultipleConnectionSourceCapableDatastore, MultiTenantCapableDatastore, TransactionCapableDatastore, SmartLifecycle { +public class MongoDatastore extends AbstractDatastore implements MappingContext.Listener, Closeable, StatelessDatastore, MultipleConnectionSourceCapableDatastore, MultiTenantCapableDatastore, TransactionCapableDatastore, SmartLifecycle, AutoTimestampingDatastore { public static final String SETTING_DATABASE_NAME = MongoSettings.SETTING_DATABASE_NAME; public static final String SETTING_CONNECTION_STRING = MongoSettings.SETTING_CONNECTION_STRING; @@ -3102,6 +3103,7 @@ public Codec get(Class clazz, CodecRegistry registry) { } } + @Override public AutoTimestampEventListener getAutoTimestampEventListener() { return this.autoTimestampEventListener; } diff --git a/grails-data-mongodb/core/src/test/groovy/org/grails/datastore/gorm/mongo/connections/AutoTimestampEventListenerConnectionsSpec.groovy b/grails-data-mongodb/core/src/test/groovy/org/grails/datastore/gorm/mongo/connections/AutoTimestampEventListenerConnectionsSpec.groovy new file mode 100644 index 00000000000..a8f62c78267 --- /dev/null +++ b/grails-data-mongodb/core/src/test/groovy/org/grails/datastore/gorm/mongo/connections/AutoTimestampEventListenerConnectionsSpec.groovy @@ -0,0 +1,125 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.grails.datastore.gorm.mongo.connections + +import java.util.function.Supplier + +import spock.lang.AutoCleanup + +import org.springframework.context.support.GenericApplicationContext + +import grails.gorm.annotation.CreatedBy +import grails.gorm.annotation.Entity +import grails.gorm.annotation.LastModifiedBy +import org.apache.grails.testing.mongo.AutoStartedMongoSpec +import org.grails.datastore.gorm.events.ConfigurableApplicationContextEventPublisher +import org.grails.datastore.gorm.timestamp.AuditorAware +import org.grails.datastore.mapping.config.Settings +import org.grails.datastore.mapping.mongo.MongoDatastore + +/** + * The listener of every connection, not only that of the default one, sets the auditor properties from the + * application context of the datastore's event publisher, follows the auto-timestamp settings of the configuration, + * and is the one a domain class answers with. + */ +class AutoTimestampEventListenerConnectionsSpec extends AutoStartedMongoSpec { + + private static final Date PAST = new Date(110_000_000_000L) + + @AutoCleanup + GenericApplicationContext context + + @AutoCleanup + MongoDatastore datastore + + @Override + boolean shouldInitializeDatastore() { + false + } + + void cleanup() { + datastore?.mongoClient?.listDatabaseNames()?.findAll { String name -> name.startsWith('timestampConnections') } + ?.each { String name -> datastore.mongoClient.getDatabase(name).drop() } + } + + void 'every connection sets the auditor properties from the application context of the publisher'() { + given: + context = new GenericApplicationContext() + context.registerBean(AuditorAware, { { -> Optional.of('auditor') } as AuditorAware } as Supplier) + context.refresh() + datastore = new MongoDatastore(configuration(), new ConfigurableApplicationContextEventPublisher(context), AuditedMongoNote) + AuditedMongoNote onDefault = new AuditedMongoNote(text: 'default') + AuditedMongoNote onOther = new AuditedMongoNote(text: 'other') + + when: + onDefault.save(flush: true) + AuditedMongoNote.other.withSession { + AuditedMongoNote.other.save(onOther, [flush: true]) + } + + then: + AuditedMongoNote.other.withSession { AuditedMongoNote.other.count() } == 1 + onDefault.createdBy == 'auditor' + onOther.createdBy == 'auditor' + onOther.lastModifiedBy == 'auditor' + !AuditedMongoNote.other.autoTimestampEventListener.is(AuditedMongoNote.autoTimestampEventListener) + } + + void 'every connection keeps a given timestamp when the configuration turns overwriting off'() { + given: + datastore = new MongoDatastore(configuration() + [(Settings.SETTING_AUTO_TIMESTAMP_INSERT_OVERWRITE): false], AuditedMongoNote) + AuditedMongoNote onOther = new AuditedMongoNote(text: 'other', dateCreated: PAST) + + when: + AuditedMongoNote.other.withSession { + AuditedMongoNote.other.save(onOther, [flush: true]) + } + + then: + AuditedMongoNote.other.withNewSession { AuditedMongoNote.other.get(onOther.id).dateCreated } == PAST + onOther.lastUpdated != null + } + + private Map configuration() { + [ + 'grails.mongodb.url' : "mongodb://${mongoHost}:${mongoPort}/timestampConnectionsDefault".toString(), + 'grails.mongodb.connections': [ + other: [url: "mongodb://${mongoHost}:${mongoPort}/timestampConnectionsOther".toString()] + ] + ] as Map + } +} + +@Entity +class AuditedMongoNote { + + String text + Date dateCreated + Date lastUpdated + + @CreatedBy + String createdBy + + @LastModifiedBy + String lastModifiedBy + + static mapping = { + connections 'default', 'other' + } +} diff --git a/grails-data-neo4j/core/src/main/groovy/org/grails/datastore/gorm/neo4j/Neo4jDatastore.java b/grails-data-neo4j/core/src/main/groovy/org/grails/datastore/gorm/neo4j/Neo4jDatastore.java index 8b124f0a2d6..9b33a326161 100644 --- a/grails-data-neo4j/core/src/main/groovy/org/grails/datastore/gorm/neo4j/Neo4jDatastore.java +++ b/grails-data-neo4j/core/src/main/groovy/org/grails/datastore/gorm/neo4j/Neo4jDatastore.java @@ -47,6 +47,7 @@ import org.apache.grails.datastore.core.querytail.QueryTailSettings; import org.grails.datastore.gorm.GormEnhancer; import org.grails.datastore.gorm.events.AutoTimestampEventListener; +import org.grails.datastore.gorm.events.AutoTimestampingDatastore; import org.grails.datastore.gorm.events.ConfigurableApplicationEventPublisher; import org.grails.datastore.gorm.events.DefaultApplicationEventPublisher; import org.grails.datastore.gorm.events.DomainEventListener; @@ -97,7 +98,7 @@ * * @since 1.0 */ -public class Neo4jDatastore extends AbstractDatastore implements Closeable, StatelessDatastore, GraphDatastore, Settings, MultipleConnectionSourceCapableDatastore, MultiTenantCapableDatastore, MessageSourceAware, TransactionCapableDatastore { +public class Neo4jDatastore extends AbstractDatastore implements Closeable, StatelessDatastore, GraphDatastore, Settings, MultipleConnectionSourceCapableDatastore, MultiTenantCapableDatastore, MessageSourceAware, TransactionCapableDatastore, AutoTimestampingDatastore { private static final Logger log = LoggerFactory.getLogger(Neo4jDatastore.class); protected final Driver boltDriver; @@ -151,6 +152,10 @@ public Neo4jDatastore(ConnectionSources c childDatastore = new Neo4jDatastore(singletonConnectionSources, mappingContext, eventPublisher) { @Override protected GormEnhancer initialize(Neo4jConnectionSourceSettings settings) { + // The parent's enhancer serves this connection's entities, but the sessions of this + // connection publish their events with this datastore as the source, and only + // listeners of its own handle those. + registerEntityEventListeners(eventPublisher); return null; } }; @@ -432,13 +437,26 @@ public Neo4jMappingContext getMappingContext() { } protected void registerEventListeners(ConfigurableApplicationEventPublisher eventPublisher) { - eventPublisher.addApplicationListener(new DomainEventListener(this)); - eventPublisher.addApplicationListener(autoTimestampEventListener); + registerEntityEventListeners(eventPublisher); if (multiTenancyMode == MultiTenancySettings.MultiTenancyMode.DISCRIMINATOR) { eventPublisher.addApplicationListener(new MultiTenantEventListener(this)); } } + /** + * Registers the listeners that run the domain class events and set the timestamps of the entities saved through + * this datastore. The datastore of another connection registers only these: the tenant of a discriminator + * property is bound for the datastore of the default connection, and a tenant filter of its own would not see + * it. + * + * @param eventPublisher The publisher of this datastore's events + * @since 9.0 + */ + protected void registerEntityEventListeners(ConfigurableApplicationEventPublisher eventPublisher) { + eventPublisher.addApplicationListener(new DomainEventListener(this)); + eventPublisher.addApplicationListener(autoTimestampEventListener); + } + protected GormEnhancer initialize(Neo4jConnectionSourceSettings settings) { registerEventListeners(this.eventPublisher); @@ -611,6 +629,7 @@ public void setMessageSource(MessageSource messageSource) { } } + @Override public AutoTimestampEventListener getAutoTimestampEventListener() { return this.autoTimestampEventListener; } diff --git a/grails-data-neo4j/core/src/test/groovy/grails/gorm/tests/NamedConnectionsSpec.groovy b/grails-data-neo4j/core/src/test/groovy/grails/gorm/tests/NamedConnectionsSpec.groovy index c8dd2af853f..3ce100158be 100644 --- a/grails-data-neo4j/core/src/test/groovy/grails/gorm/tests/NamedConnectionsSpec.groovy +++ b/grails-data-neo4j/core/src/test/groovy/grails/gorm/tests/NamedConnectionsSpec.groovy @@ -63,12 +63,56 @@ class NamedConnectionsSpec extends Specification { (Settings.SETTING_NEO4J_URL) : "bolt://localhost:${defaultPort}".toString(), (Settings.SETTING_NEO4J_BUILD_INDEX) : false, (Settings.SETTING_CONNECTIONS) : [other: [url: "bolt://localhost:${otherPort}".toString()]] - ], RoutedCompany) + ], RoutedCompany, TimestampedCompany) } void cleanup() { RoutedCompany.withTransaction { RoutedCompany.list()*.delete(flush: true) } RoutedCompany.other.withTransaction { RoutedCompany.other.list().each { RoutedCompany.other.delete(it, [flush: true]) } } + TimestampedCompany.other.withTransaction { TimestampedCompany.other.list().each { TimestampedCompany.other.delete(it, [flush: true]) } } + } + + void "test a save through a named connection runs the events and sets the timestamps"() { + given: + def company = new TimestampedCompany(name: 'draft') + + when: + TimestampedCompany.other.withTransaction { + TimestampedCompany.other.save(company, [flush: true]) + } + + then: + TimestampedCompany.other.count() == 1 + company.name == 'DRAFT' + company.dateCreated != null + company.lastUpdated != null + } + + void "test the listener of a named connection turns off the timestamps of the saves through it"() { + given: + Date past = new Date(110_000_000_000L) + def kept = new TimestampedCompany(name: 'kept', dateCreated: past, lastUpdated: past) + def stamped = new TimestampedCompany(name: 'stamped', dateCreated: past, lastUpdated: past) + def defaultListener = TimestampedCompany.autoTimestampEventListener + def otherListener = TimestampedCompany.other.autoTimestampEventListener + + when: + TimestampedCompany.other.withTransaction { + otherListener.withoutTimestamps { + TimestampedCompany.other.save(kept, [flush: true]) + } + defaultListener.withoutTimestamps { + TimestampedCompany.other.save(stamped, [flush: true]) + } + } + + then: + !otherListener.is(defaultListener) + otherListener.is(datastore.getDatastoreForConnection('other').autoTimestampEventListener) + kept.dateCreated == past + kept.lastUpdated == past + stamped.dateCreated != past + stamped.lastUpdated != past } void "test an entity is saved through a named connection to that connection's server"() { @@ -139,6 +183,23 @@ class RoutedCompanyService { } } +@Entity +class TimestampedCompany implements Neo4jEntity { + + Long id + String name + Date dateCreated + Date lastUpdated + + def beforeInsert() { + name = name.toUpperCase() + } + + static mapping = { + datasource 'ALL' + } +} + @Entity class RoutedCompany implements Neo4jEntity { Long id diff --git a/grails-data-simple/src/main/groovy/org/grails/datastore/mapping/simple/SimpleMapDatastore.java b/grails-data-simple/src/main/groovy/org/grails/datastore/mapping/simple/SimpleMapDatastore.java index 08c4ef2c4b8..5e78a4d0893 100644 --- a/grails-data-simple/src/main/groovy/org/grails/datastore/mapping/simple/SimpleMapDatastore.java +++ b/grails-data-simple/src/main/groovy/org/grails/datastore/mapping/simple/SimpleMapDatastore.java @@ -40,6 +40,7 @@ import org.grails.datastore.gorm.GormStaticApi; import org.grails.datastore.gorm.GormValidationApi; import org.grails.datastore.gorm.events.AutoTimestampEventListener; +import org.grails.datastore.gorm.events.AutoTimestampingDatastore; import org.grails.datastore.gorm.events.ConfigurableApplicationContextEventPublisher; import org.grails.datastore.gorm.events.ConfigurableApplicationEventPublisher; import org.grails.datastore.gorm.events.DefaultApplicationEventPublisher; @@ -81,7 +82,7 @@ * @since 1.0 */ @SuppressWarnings("rawtypes") -public class SimpleMapDatastore extends AbstractDatastore implements Closeable, TransactionCapableDatastore, SingleSessionCapableDatastore, SchemaMultiTenantCapableDatastore, ConnectionSourceSettings>, ConnectionSourcesProvider, ConnectionSourceSettings> { +public class SimpleMapDatastore extends AbstractDatastore implements Closeable, TransactionCapableDatastore, SingleSessionCapableDatastore, SchemaMultiTenantCapableDatastore, ConnectionSourceSettings>, ConnectionSourcesProvider, ConnectionSourceSettings>, AutoTimestampingDatastore { private final Map inmemoryData; private final TenantResolver tenantResolver; protected final GormEnhancer gormEnhancer; @@ -94,6 +95,7 @@ public class SimpleMapDatastore extends AbstractDatastore implements Closeable, protected final Map datastoresByConnectionSource = new LinkedHashMap<>(); protected final boolean failOnError; private final SimpleMapQueryTail queryTail; + private final AutoTimestampEventListener autoTimestampEventListener; public SimpleMapDatastore(ConnectionSources, ConnectionSourceSettings> connectionSources, MappingContext mappingContext, ConfigurableApplicationEventPublisher eventPublisher) { super(mappingContext); @@ -109,6 +111,9 @@ public SimpleMapDatastore(ConnectionSources, ConnectionSourceSe this.tenantResolver = multiTenancy.getTenantResolver(); PropertyResolver config = connectionSources.getBaseConfiguration(); this.failOnError = config.getProperty(Settings.SETTING_FAIL_ON_ERROR, Boolean.class, false); + // Created ahead of the datastores of the other connections, so that it stays the first one the shared mapping + // context lists, as it was when only this datastore had a listener. + this.autoTimestampEventListener = new AutoTimestampEventListener(this); if (!(connectionSources instanceof SingletonConnectionSources)) { Iterable, ConnectionSourceSettings>> allConnectionSources = connectionSources.getAllConnectionSources(); @@ -123,6 +128,10 @@ public SimpleMapDatastore(ConnectionSources, ConnectionSourceSe childDatastore = new SimpleMapDatastore(singletonConnectionSources, mappingContext, eventPublisher) { @Override protected GormEnhancer initialize(ConnectionSourceSettings settings) { + // The parent's enhancer serves this connection's entities, but the sessions of this + // connection publish their events with this datastore as the source, and only + // listeners of its own handle those. + registerEntityEventListeners(eventPublisher); return null; } @@ -291,13 +300,26 @@ private SimpleMapDatastore getDatastoreForQualifier(Class cls, String qua } protected void registerEventListeners(ConfigurableApplicationEventPublisher eventPublisher) { - eventPublisher.addApplicationListener(new DomainEventListener(this)); - eventPublisher.addApplicationListener(new AutoTimestampEventListener(this)); + registerEntityEventListeners(eventPublisher); if (multiTenancyMode == MultiTenancySettings.MultiTenancyMode.DISCRIMINATOR) { eventPublisher.addApplicationListener(new MultiTenantEventListener(this)); } } + /** + * Registers the listeners that run the domain class events and set the timestamps of the entities saved through + * this datastore. The datastore of another connection, or of a schema, registers only these: the tenant of a + * discriminator column is bound for the datastore of the default connection, and a tenant filter of its own + * would not see it. + * + * @param eventPublisher The publisher of this datastore's events + * @since 9.0 + */ + protected void registerEntityEventListeners(ConfigurableApplicationEventPublisher eventPublisher) { + eventPublisher.addApplicationListener(new DomainEventListener(this)); + eventPublisher.addApplicationListener(autoTimestampEventListener); + } + public Map getIndices() { return indices; } @@ -312,6 +334,11 @@ public ApplicationEventPublisher getApplicationEventPublisher() { return this.eventPublisher; } + @Override + public AutoTimestampEventListener getAutoTimestampEventListener() { + return autoTimestampEventListener; + } + /** * @return the query-tail capture of this datastore's connection source, or null when capture is not enabled * @since 9.0 @@ -466,6 +493,9 @@ public void addTenantForSchema(String schemaName) { childDatastore = new SimpleMapDatastore(singletonConnectionSources, mappingContext, eventPublisher) { @Override protected GormEnhancer initialize(ConnectionSourceSettings settings) { + // As for a connection's datastore, the schema's sessions publish their events with this + // datastore as the source. + registerEntityEventListeners(eventPublisher); return null; } }; diff --git a/grails-data-simple/src/test/groovy/org/grails/datastore/mapping/simple/SimpleMapAutoTimestampListenerSpec.groovy b/grails-data-simple/src/test/groovy/org/grails/datastore/mapping/simple/SimpleMapAutoTimestampListenerSpec.groovy new file mode 100644 index 00000000000..ae068f97e71 --- /dev/null +++ b/grails-data-simple/src/test/groovy/org/grails/datastore/mapping/simple/SimpleMapAutoTimestampListenerSpec.groovy @@ -0,0 +1,278 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.grails.datastore.mapping.simple + +import groovy.transform.CompileStatic + +import spock.lang.AutoCleanup +import spock.lang.Specification +import spock.util.environment.RestoreSystemProperties + +import grails.gorm.MultiTenant +import grails.gorm.annotation.Entity +import grails.gorm.multitenancy.Tenants +import grails.gorm.transactions.Transactional +import org.grails.datastore.gorm.events.AutoTimestampEventListener +import org.grails.datastore.mapping.config.Settings +import org.grails.datastore.mapping.core.DatastoreUtils +import org.grails.datastore.mapping.core.Session +import org.grails.datastore.mapping.core.SessionCallback +import org.grails.datastore.mapping.core.connections.ConnectionSource +import org.grails.datastore.mapping.multitenancy.MultiTenancySettings +import org.grails.datastore.mapping.multitenancy.resolvers.SystemPropertyTenantResolver + +/** + * The listener a domain class answers with is the one of the datastore its saves go through. The in-memory + * datastore keeps the unqualified operations of every domain class on the datastore of the default connection, and + * an explicit connection, or a tenant's database or schema, on its own. + */ +@RestoreSystemProperties +class SimpleMapAutoTimestampListenerSpec extends Specification { + + private static final Date PAST = new Date(110_000_000_000L) + + @AutoCleanup + SimpleMapDatastore datastore + + void 'a domain class answers with the listener of the datastore that saves it'() { + given: + datastore = new SimpleMapDatastore(['secondary'], ListenerNote, SecondaryListenerNote) + SimpleMapDatastore secondary = (SimpleMapDatastore) datastore.getDatastoreForConnection('secondary') + + expect: 'unqualified operations stay on the default connection, whatever the class is mapped to' + ListenerNote.autoTimestampEventListener.is(datastore.autoTimestampEventListener) + SecondaryListenerNote.autoTimestampEventListener.is(datastore.autoTimestampEventListener) + + and: 'an explicit connection has a listener of its own' + ListenerNote.secondary.autoTimestampEventListener.is(secondary.autoTimestampEventListener) + SecondaryListenerNote.secondary.autoTimestampEventListener.is(secondary.autoTimestampEventListener) + !secondary.autoTimestampEventListener.is(datastore.autoTimestampEventListener) + } + + void 'the first auto-timestamp listener of the mapping context is still the default connection\'s'() { + given: 'code that looked the listener up through the mapping context, before domain classes answered with it' + datastore = new SimpleMapDatastore(['secondary', 'tertiary'], ListenerNote) + + expect: + datastore.mappingContext.eventListeners.find { it instanceof AutoTimestampEventListener }.is(datastore.autoTimestampEventListener) + } + + void 'a transaction for a connection routes the unqualified lookup to that connection'() { + given: + datastore = new SimpleMapDatastore(['secondary'], ListenerNote) + SimpleMapDatastore secondary = (SimpleMapDatastore) datastore.getDatastoreForConnection('secondary') + + expect: + ListenerNote.secondary.withTransaction { ListenerNote.autoTimestampEventListener }.is(secondary.autoTimestampEventListener) + new SecondaryListenerLookup().lookUp().is(secondary.autoTimestampEventListener) + ListenerNote.autoTimestampEventListener.is(datastore.autoTimestampEventListener) + } + + void 'only the listener of the connection that saves an instance turns its timestamps off'() { + given: + datastore = new SimpleMapDatastore(['secondary'], ListenerNote) + def defaultListener = ListenerNote.autoTimestampEventListener + def secondaryListener = ListenerNote.secondary.autoTimestampEventListener + ListenerNote kept = new ListenerNote(text: 'kept', dateCreated: PAST, lastUpdated: PAST) + ListenerNote overwritten = new ListenerNote(text: 'overwritten', dateCreated: PAST, lastUpdated: PAST) + + when: + ListenerNote.secondary.withTransaction { + secondaryListener.withoutTimestamps { + kept.secondary.save(flush: true) + } + defaultListener.withoutTimestamps { + overwritten.secondary.save(flush: true) + } + } + + then: + kept.dateCreated == PAST + kept.lastUpdated == PAST + overwritten.dateCreated != PAST + overwritten.lastUpdated != PAST + } + + void 'every connection follows the auto-timestamp settings of the configuration'() { + given: + datastore = new SimpleMapDatastore( + DatastoreUtils.createPropertyResolver([ + (Settings.SETTING_AUTO_TIMESTAMP_INSERT_OVERWRITE) : false, + (Settings.SETTING_AUTO_TIMESTAMP_CACHE_ANNOTATIONS): false + ]), + ['secondary'], + ListenerNote + ) + ListenerNote onDefault = new ListenerNote(text: 'default', dateCreated: PAST) + ListenerNote onSecondary = new ListenerNote(text: 'secondary', dateCreated: PAST) + + when: + ListenerNote.withTransaction { + onDefault.save(flush: true) + } + ListenerNote.secondary.withTransaction { + onSecondary.secondary.save(flush: true) + } + + then: + onDefault.dateCreated == PAST + onSecondary.dateCreated == PAST + onSecondary.lastUpdated != null + !ListenerNote.autoTimestampEventListener.cacheAutoTimestampAnnotations + !ListenerNote.secondary.autoTimestampEventListener.cacheAutoTimestampAnnotations + } + + void 'a tenant with a database of its own answers with the listener of that database'() { + given: + datastore = new SimpleMapDatastore( + DatastoreUtils.createPropertyResolver([ + (Settings.SETTING_MULTI_TENANCY_MODE) : MultiTenancySettings.MultiTenancyMode.DATABASE, + (Settings.SETTING_MULTI_TENANT_RESOLVER): new SystemPropertyTenantResolver() + ]), + [ConnectionSource.DEFAULT, 'foo', 'bar'], + TenantListenerNote + ) + def foo = datastore.getDatastoreForConnection('foo').autoTimestampEventListener + def bar = datastore.getDatastoreForConnection('bar').autoTimestampEventListener + + expect: + !foo.is(bar) + Tenants.withId('foo') { TenantListenerNote.autoTimestampEventListener }.is(foo) + Tenants.withId('bar') { TenantListenerNote.autoTimestampEventListener }.is(bar) + TenantListenerNote.withTenant('bar').autoTimestampEventListener.is(bar) + + and: 'statically compiled code reaches it through the operations withTenant returns' + StaticTenantListenerLookup.forTenant('foo').is(foo) + StaticTenantListenerLookup.forTenant('bar').is(bar) + + when: 'the current tenant comes from the resolver' + System.setProperty(SystemPropertyTenantResolver.PROPERTY_NAME, 'foo') + + then: + TenantListenerNote.autoTimestampEventListener.is(foo) + } + + void 'a class that is not multi-tenant answers inside a tenant block with the listener of the datastore that saves it'() { + given: + datastore = new SimpleMapDatastore( + DatastoreUtils.createPropertyResolver([ + (Settings.SETTING_MULTI_TENANCY_MODE) : MultiTenancySettings.MultiTenancyMode.DATABASE, + (Settings.SETTING_MULTI_TENANT_RESOLVER): new SystemPropertyTenantResolver() + ]), + [ConnectionSource.DEFAULT, 'foo'], + TenantListenerNote, ListenerNote + ) + ListenerNote note = new ListenerNote(text: 'kept', dateCreated: PAST, lastUpdated: PAST) + + when: + def listener = Tenants.withId('foo') { + ListenerNote.autoTimestampEventListener.withoutTimestamps { + note.save(flush: true) + } + ListenerNote.autoTimestampEventListener + } + + then: 'the timestamps were turned off for the save' + note.dateCreated == PAST + note.lastUpdated == PAST + + and: 'by the listener of the datastore that holds the saved instance' + SimpleMapDatastore foo = (SimpleMapDatastore) datastore.getDatastoreForConnection('foo') + List holding = [datastore, foo].findAll { SimpleMapDatastore ds -> + DatastoreUtils.execute(ds, { Session session -> session.retrieve(ListenerNote, note.id) } as SessionCallback) != null + } + holding.size() == 1 + listener.is(holding.first().autoTimestampEventListener) + } + + void 'a tenant with a schema added at runtime answers with the listener of that schema'() { + given: + datastore = new SimpleMapDatastore( + DatastoreUtils.createPropertyResolver([ + (Settings.SETTING_MULTI_TENANCY_MODE) : MultiTenancySettings.MultiTenancyMode.SCHEMA, + (Settings.SETTING_MULTI_TENANT_RESOLVER): new SystemPropertyTenantResolver() + ]), + [ConnectionSource.DEFAULT], + TenantListenerNote + ) + datastore.addTenantForSchema('foo') + TenantListenerNote note = new TenantListenerNote(text: 'kept', dateCreated: PAST, lastUpdated: PAST) + + when: + Tenants.withId('foo') { + TenantListenerNote.autoTimestampEventListener.withoutTimestamps { + note.save(flush: true) + } + } + + then: + Tenants.withId('foo') { TenantListenerNote.autoTimestampEventListener }.is(datastore.getDatastoreForConnection('foo').autoTimestampEventListener) + TenantListenerNote.withTenant('foo').autoTimestampEventListener.is(datastore.getDatastoreForConnection('foo').autoTimestampEventListener) + note.dateCreated == PAST + note.lastUpdated == PAST + } +} + +@Entity +class ListenerNote { + + String text + Date dateCreated + Date lastUpdated + + static mapping = { + datasource 'ALL' + } +} + +@CompileStatic +class StaticTenantListenerLookup { + + static AutoTimestampEventListener forTenant(String tenantId) { + TenantListenerNote.withTenant(tenantId).autoTimestampEventListener + } +} + +class SecondaryListenerLookup { + + @Transactional(connection = 'secondary') + AutoTimestampEventListener lookUp() { + ListenerNote.autoTimestampEventListener + } +} + +@Entity +class SecondaryListenerNote { + + String text + Date dateCreated + Date lastUpdated + + static mapping = { + datasource 'secondary' + } +} + +@Entity +class TenantListenerNote implements MultiTenant { + + String text + Date dateCreated + Date lastUpdated +} diff --git a/grails-data-simple/src/test/groovy/org/grails/datastore/mapping/simple/SimpleMapConnectionEventsSpec.groovy b/grails-data-simple/src/test/groovy/org/grails/datastore/mapping/simple/SimpleMapConnectionEventsSpec.groovy new file mode 100644 index 00000000000..d64964cd47e --- /dev/null +++ b/grails-data-simple/src/test/groovy/org/grails/datastore/mapping/simple/SimpleMapConnectionEventsSpec.groovy @@ -0,0 +1,175 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.grails.datastore.mapping.simple + +import spock.lang.AutoCleanup +import spock.lang.Specification +import spock.util.environment.RestoreSystemProperties + +import grails.gorm.MultiTenant +import grails.gorm.annotation.Entity +import grails.gorm.multitenancy.Tenants +import org.grails.datastore.mapping.config.Settings +import org.grails.datastore.mapping.core.DatastoreUtils +import org.grails.datastore.mapping.core.connections.ConnectionSource +import org.grails.datastore.mapping.multitenancy.MultiTenancySettings +import org.grails.datastore.mapping.multitenancy.resolvers.SystemPropertyTenantResolver + +/** + * A save through the datastore of another connection, of a tenant's database or of a schema added at runtime runs + * the domain class events and sets the timestamps, as a save through the default connection does. + */ +@RestoreSystemProperties +class SimpleMapConnectionEventsSpec extends Specification { + + @AutoCleanup + SimpleMapDatastore datastore + + void 'a save through another connection runs the events and sets the timestamps'() { + given: + datastore = new SimpleMapDatastore(['secondary'], ConnectionNote) + ConnectionNote note = new ConnectionNote(text: 'draft') + + when: + ConnectionNote.secondary.withTransaction { + note.secondary.save(flush: true) + } + + then: + ConnectionNote.secondary.withTransaction { ConnectionNote.secondary.count() } == 1 + note.text == 'DRAFT' + note.dateCreated != null + note.lastUpdated != null + } + + void 'with a discriminator column, another connection keeps the tenant handling it had and sets the timestamps'() { + given: 'no tenant from the resolver' + System.setProperty(SystemPropertyTenantResolver.PROPERTY_NAME, '') + datastore = new SimpleMapDatastore( + DatastoreUtils.createPropertyResolver([ + (Settings.SETTING_MULTI_TENANCY_MODE) : MultiTenancySettings.MultiTenancyMode.DISCRIMINATOR, + (Settings.SETTING_MULTI_TENANT_RESOLVER): new SystemPropertyTenantResolver() + ]), + [ConnectionSource.DEFAULT, 'secondary'], + DiscriminatorNote + ) + DiscriminatorNote note = new DiscriminatorNote(text: 'draft', tenantId: 't1') + + when: + List found = Tenants.withId('t1') { + DiscriminatorNote.secondary.withTransaction { + note.secondary.save(flush: true) + DiscriminatorNote.secondary.list() + } + } + + then: + found*.text == ['draft'] + note.dateCreated != null + } + + void 'a save for a tenant with a database of its own runs the events and sets the timestamps'() { + given: + datastore = new SimpleMapDatastore( + DatastoreUtils.createPropertyResolver([ + (Settings.SETTING_MULTI_TENANCY_MODE) : MultiTenancySettings.MultiTenancyMode.DATABASE, + (Settings.SETTING_MULTI_TENANT_RESOLVER): new SystemPropertyTenantResolver() + ]), + [ConnectionSource.DEFAULT, 'foo'], + TenantEventNote + ) + TenantEventNote note = new TenantEventNote(text: 'draft') + + when: + Tenants.withId('foo') { + note.save(flush: true) + } + + then: + Tenants.withId('foo') { TenantEventNote.count() } == 1 + note.text == 'DRAFT' + note.dateCreated != null + note.lastUpdated != null + } + + void 'a save for a tenant with a schema added at runtime runs the events and sets the timestamps'() { + given: + datastore = new SimpleMapDatastore( + DatastoreUtils.createPropertyResolver([ + (Settings.SETTING_MULTI_TENANCY_MODE) : MultiTenancySettings.MultiTenancyMode.SCHEMA, + (Settings.SETTING_MULTI_TENANT_RESOLVER): new SystemPropertyTenantResolver() + ]), + [ConnectionSource.DEFAULT], + TenantEventNote + ) + datastore.addTenantForSchema('foo') + TenantEventNote note = new TenantEventNote(text: 'draft') + + when: + Tenants.withId('foo') { + note.save(flush: true) + } + + then: + Tenants.withId('foo') { TenantEventNote.count() } == 1 + note.text == 'DRAFT' + note.dateCreated != null + note.lastUpdated != null + } +} + +@Entity +class DiscriminatorNote implements MultiTenant { + + String text + String tenantId + Date dateCreated + + static mapping = { + datasource 'ALL' + } +} + +@Entity +class ConnectionNote { + + String text + Date dateCreated + Date lastUpdated + + def beforeInsert() { + text = text.toUpperCase() + } + + static mapping = { + datasource 'ALL' + } +} + +@Entity +class TenantEventNote implements MultiTenant { + + String text + Date dateCreated + Date lastUpdated + + def beforeInsert() { + text = text.toUpperCase() + } +} diff --git a/grails-datamapping-core-test/src/test/groovy/grails/gorm/services/multitenancy/database/DatabasePerTenantSpec.groovy b/grails-datamapping-core-test/src/test/groovy/grails/gorm/services/multitenancy/database/DatabasePerTenantSpec.groovy index cf8ebfd3e94..3da1d1ac41c 100644 --- a/grails-datamapping-core-test/src/test/groovy/grails/gorm/services/multitenancy/database/DatabasePerTenantSpec.groovy +++ b/grails-datamapping-core-test/src/test/groovy/grails/gorm/services/multitenancy/database/DatabasePerTenantSpec.groovy @@ -23,6 +23,7 @@ import spock.lang.Shared import spock.lang.Specification import spock.util.environment.RestoreSystemProperties +import grails.gorm.multitenancy.Tenants import org.grails.datastore.mapping.config.Settings import org.grails.datastore.mapping.core.DatastoreUtils import org.grails.datastore.mapping.core.connections.ConnectionSource @@ -81,6 +82,22 @@ class DatabasePerTenantSpec extends Specification { bookService.countBooks() == 2 bookDataService.countBooks() == 2 } + + void 'withTenant reaches the named tenant when no tenant is current'() { + given: 'a book in the foo database and none in bar' + Tenants.withId('foo') { + new Book(title: 'The Stand').save(flush: true) + } + + and: 'no current tenant' + System.setProperty(SystemPropertyTenantResolver.PROPERTY_NAME, '') + + expect: + Book.withTenant('foo') { Book.count() } == 1 + Book.withTenant('bar') { Book.count() } == 0 + Book.withTenant('foo').count() == 1 + Book.withTenant('bar').count() == 0 + } } diff --git a/grails-datamapping-core-test/src/test/groovy/org/grails/datastore/gorm/CustomAutoTimestampSpec.groovy b/grails-datamapping-core-test/src/test/groovy/org/grails/datastore/gorm/CustomAutoTimestampSpec.groovy index d720cea1932..5fe21047b5d 100644 --- a/grails-datamapping-core-test/src/test/groovy/org/grails/datastore/gorm/CustomAutoTimestampSpec.groovy +++ b/grails-datamapping-core-test/src/test/groovy/org/grails/datastore/gorm/CustomAutoTimestampSpec.groovy @@ -93,8 +93,7 @@ class CustomAutoTimestampSpec extends GrailsDataTckSpec extends Entity { */ @Generated static T withTenant(Serializable tenantId, Closure callable) { - GormRegistry.instance.findStaticApi((Class) this).withTenant(tenantId, callable) + // The tenant is named here, so the lookup must not resolve the current one: findStaticApi does, and for a + // database or schema per tenant it throws TenantNotFoundException when no tenant is current. + GormRegistry.instance.getStaticApi((Class) this).withTenant(tenantId, callable) } /** @@ -68,6 +70,7 @@ trait MultiTenant extends Entity { */ @Generated static GormAllOperations withTenant(Serializable tenantId) { - (GormAllOperations) GormRegistry.instance.findStaticApi((Class) this).withTenant(tenantId) + // The tenant is named here too, so the current one is not resolved; see withTenant(Serializable, Closure). + (GormAllOperations) GormRegistry.instance.getStaticApi((Class) this).withTenant(tenantId) } } diff --git a/grails-datamapping-core/src/main/groovy/grails/gorm/api/GormStaticOperations.groovy b/grails-datamapping-core/src/main/groovy/grails/gorm/api/GormStaticOperations.groovy index b8de3e30a2f..f4eff110baf 100644 --- a/grails-datamapping-core/src/main/groovy/grails/gorm/api/GormStaticOperations.groovy +++ b/grails-datamapping-core/src/main/groovy/grails/gorm/api/GormStaticOperations.groovy @@ -28,6 +28,7 @@ import jakarta.persistence.LockModeType import org.springframework.transaction.TransactionDefinition import grails.gorm.DetachedCriteria +import org.grails.datastore.gorm.events.AutoTimestampEventListener import org.grails.datastore.gorm.finders.FinderMethod import org.grails.datastore.gorm.internal.RefreshLockArguments import org.grails.datastore.mapping.model.PersistentEntity @@ -52,6 +53,21 @@ interface GormStaticOperations { */ List getGormDynamicFinders() + /** + * Returns the listener that sets the timestamp and auditor properties of an instance saved through these + * operations, as {@code Book.withTenant('acme').autoTimestampEventListener} returns the tenant's. Each datastore + * has a listener of its own, and so does each connection and each tenant with a database or schema of its own, + * and a listener only handles the saves made through its own datastore. + * + * @return the listener that handles a save through these operations + * @throws UnsupportedOperationException when these operations cannot look the listener up + * @since 9.0 + */ + @CompileStatic + default AutoTimestampEventListener getAutoTimestampEventListener() { + throw new UnsupportedOperationException("[${getClass().name}] cannot look up the auto-timestamp event listener") + } + /** * * @param callable Callable closure containing detached criteria definition diff --git a/grails-datamapping-core/src/main/groovy/org/grails/datastore/gorm/GormEntity.groovy b/grails-datamapping-core/src/main/groovy/org/grails/datastore/gorm/GormEntity.groovy index b9a40169300..d60b763dff9 100644 --- a/grails-datamapping-core/src/main/groovy/org/grails/datastore/gorm/GormEntity.groovy +++ b/grails-datamapping-core/src/main/groovy/org/grails/datastore/gorm/GormEntity.groovy @@ -29,6 +29,7 @@ import jakarta.persistence.Transient import org.springframework.transaction.TransactionDefinition import grails.gorm.DetachedCriteria +import org.grails.datastore.gorm.events.AutoTimestampEventListener import org.grails.datastore.gorm.finders.FinderMethod import org.grails.datastore.mapping.dirty.checking.DirtyCheckable import org.grails.datastore.mapping.model.MappingContext @@ -506,6 +507,35 @@ trait GormEntity implements GormValidateable, DirtyCheckable, GormEntityApi getGormDynamicFinders() { currentGormStaticApi().gormDynamicFinders } + + /** + * Returns the listener that sets this class's timestamp and auditor properties. Each datastore has a listener of + * its own, and so does each connection and each tenant with a database or schema of its own, so the + * {@code autoTimestampEventListener} bean may not be the one that handles this class. This one is the listener of + * the connection that a {@code save()} made from the same place goes to: the class's default connection, the + * connection of a running {@code Car.secondary.withTransaction { }} block or + * {@code @Transactional(connection = 'secondary')} method, or the current tenant's for a multi-tenant class. + * + *

+     * Car.autoTimestampEventListener.withoutTimestamps {
+     *     car.save(flush: true)
+     * }
+     * 
+ * + * For another connection of a class mapped to several, ask that connection, as in + * {@code Car.secondary.autoTimestampEventListener}. + * + * @return the listener that handles this class + * @throws IllegalStateException when the class's datastore has no such listener + * @throws org.grails.datastore.mapping.multitenancy.exceptions.TenantNotFoundException for a multi-tenant class + * with a database or schema per tenant when no tenant is current, as a save would + * @since 9.0 + */ + @Generated + static AutoTimestampEventListener getAutoTimestampEventListener() { + currentGormStaticApi().autoTimestampEventListener + } + /** * * @param callable Callable closure containing detached criteria definition diff --git a/grails-datamapping-core/src/main/groovy/org/grails/datastore/gorm/GormStaticApi.groovy b/grails-datamapping-core/src/main/groovy/org/grails/datastore/gorm/GormStaticApi.groovy index 845a596e41f..33e9b0dcdef 100644 --- a/grails-datamapping-core/src/main/groovy/org/grails/datastore/gorm/GormStaticApi.groovy +++ b/grails-datamapping-core/src/main/groovy/org/grails/datastore/gorm/GormStaticApi.groovy @@ -38,6 +38,8 @@ import grails.gorm.api.GormStaticOperations import grails.gorm.multitenancy.CurrentTenantHolder import grails.gorm.multitenancy.Tenants import grails.gorm.transactions.GrailsTransactionTemplate +import org.grails.datastore.gorm.events.AutoTimestampEventListener +import org.grails.datastore.gorm.events.AutoTimestampingDatastore import org.grails.datastore.gorm.finders.FinderMethod import org.grails.datastore.gorm.internal.RefreshLockArguments import org.grails.datastore.gorm.transactions.DefaultTransactionTemplateFactory @@ -168,6 +170,39 @@ class GormStaticApi extends AbstractGormApi implements GormAllOperations session.datastore } as SessionCallback)) + } + + /** + * @param ds the datastore that saves the instances of the persistent class + * @return the auto-timestamp listener of the datastore + * @throws IllegalStateException when the datastore has no such listener + * @since 9.0 + */ + protected AutoTimestampEventListener autoTimestampEventListenerOf(Datastore ds) { + AutoTimestampEventListener listener = ds instanceof AutoTimestampingDatastore + ? ((AutoTimestampingDatastore) ds).autoTimestampEventListener + : null + if (listener == null) { + throw new IllegalStateException("The datastore [${ds}] of class [${persistentClass.name}] has no auto-timestamp event listener") + } + listener + } + GormStaticApi forQualifier(String qualifier) { Datastore ds = getDatastore() DatastoreResolver resolver = new DatastoreResolver() { diff --git a/grails-datamapping-core/src/main/groovy/org/grails/datastore/gorm/events/AutoTimestampEventListener.java b/grails-datamapping-core/src/main/groovy/org/grails/datastore/gorm/events/AutoTimestampEventListener.java index 957eefb8a7c..caf7bbbcdbf 100644 --- a/grails-datamapping-core/src/main/groovy/org/grails/datastore/gorm/events/AutoTimestampEventListener.java +++ b/grails-datamapping-core/src/main/groovy/org/grails/datastore/gorm/events/AutoTimestampEventListener.java @@ -38,6 +38,9 @@ import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.context.ApplicationEvent; +import org.springframework.context.ApplicationEventPublisher; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.core.env.PropertyResolver; import org.grails.datastore.gorm.timestamp.AuditorAware; import org.grails.datastore.gorm.timestamp.DefaultTimestampProvider; @@ -45,7 +48,10 @@ import org.grails.datastore.mapping.config.AuditMetadataType; import org.grails.datastore.mapping.config.Entity; import org.grails.datastore.mapping.config.Settings; +import org.grails.datastore.mapping.core.AbstractDatastore; import org.grails.datastore.mapping.core.Datastore; +import org.grails.datastore.mapping.core.connections.ConnectionSources; +import org.grails.datastore.mapping.core.connections.ConnectionSourcesProvider; import org.grails.datastore.mapping.engine.EntityAccess; import org.grails.datastore.mapping.engine.event.AbstractPersistenceEvent; import org.grails.datastore.mapping.engine.event.AbstractPersistenceEventListener; @@ -89,11 +95,31 @@ public class AutoTimestampEventListener extends AbstractPersistenceEventListener private final ThreadLocal disabledLastUpdated = new ThreadLocal<>(); private TimestampProvider timestampProvider = new DefaultTimestampProvider(); - private AuditorAware auditorAware; + private volatile AuditorAware auditorAware; + // Whether auditorAware holds the outcome of a lookup, or a value that was set; until then it is looked up from + // the application context of the datastore when an entity is saved. + private volatile boolean auditorAwareResolved; + + // Guards the writes of auditorAware and auditorAwareResolved, so that an auditor set while a lookup runs is not + // replaced by what the lookup finds. + private final Object auditorAwareLock = new Object(); + + /** + * Creates the listener of the datastore, which reads {@link Settings#SETTING_AUTO_TIMESTAMP_INSERT_OVERWRITE} and + * {@link Settings#SETTING_AUTO_TIMESTAMP_CACHE_ANNOTATIONS} from the datastore's configuration. Unless an + * {@link AuditorAware} is set, or the listener is given an application context, it looks the auditor up in the + * application context of the datastore when an entity is first saved. + * + * @param datastore The datastore whose entities the listener handles + */ public AutoTimestampEventListener(final Datastore datastore) { super(datastore); + // Only a listener that is a Spring bean gets the @Value settings injected, and that is the listener of the + // default connection only; the listener of every other connection, and of a tenant's database or schema, + // takes them from the configuration of its datastore. + applySettings(datastore); MappingContext mappingContext = datastore.getMappingContext(); initForMappingContext(mappingContext); } @@ -106,9 +132,57 @@ protected AutoTimestampEventListener(final MappingContext mappingContext) { @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { + // Spring calls this after it has set the bean's properties, so an auditor the listener was configured with + // is kept when the context has none of its own. + AuditorAware contextAuditorAware = lookUpAuditorAware(applicationContext); + synchronized (auditorAwareLock) { + if (contextAuditorAware != null) { + this.auditorAware = contextAuditorAware; + } + this.auditorAwareResolved = true; + } + } + + private void applySettings(Datastore datastore) { + if (!(datastore instanceof ConnectionSourcesProvider connectionSourcesProvider)) { + return; + } + ConnectionSources connectionSources = connectionSourcesProvider.getConnectionSources(); + PropertyResolver configuration = connectionSources != null ? connectionSources.getBaseConfiguration() : null; + if (configuration != null) { + insertOverwrite = configuration.getProperty(Settings.SETTING_AUTO_TIMESTAMP_INSERT_OVERWRITE, Boolean.class, insertOverwrite); + cacheAutoTimestampAnnotations = configuration.getProperty(Settings.SETTING_AUTO_TIMESTAMP_CACHE_ANNOTATIONS, Boolean.class, cacheAutoTimestampAnnotations); + } + } + + private static AuditorAware lookUpAuditorAware(ApplicationContext applicationContext) { try { - this.auditorAware = applicationContext.getBean(AuditorAware.class); - } catch (BeansException ignore) {} + return applicationContext.getBean(AuditorAware.class); + } catch (BeansException ignore) { + return null; + } + } + + /** + * @return the application context of the datastore, once it is active, from the datastore or else from its + * event publisher, or {@code null} + */ + private ApplicationContext activeApplicationContextOfDatastore() { + ApplicationContext applicationContext = null; + if (datastore instanceof AbstractDatastore abstractDatastore) { + applicationContext = abstractDatastore.getApplicationContext(); + } + if (applicationContext == null && datastore != null) { + ApplicationEventPublisher publisher = datastore.getApplicationEventPublisher(); + if (publisher instanceof ConfigurableApplicationContextEventPublisher contextEventPublisher) { + applicationContext = contextEventPublisher.getApplicationContext(); + } + } + if (applicationContext instanceof ConfigurableApplicationContext configurableApplicationContext && + !configurableApplicationContext.isActive()) { + return null; + } + return applicationContext; } protected void initForMappingContext(MappingContext mappingContext) { @@ -163,8 +237,9 @@ public boolean beforeInsert(PersistentEntity entity, EntityAccess ea) { } // Handle auditor fields - if (auditorAware != null) { - Optional currentAuditor = auditorAware.getCurrentAuditor(); + AuditorAware currentAuditorAware = getAuditorAware(); + if (currentAuditorAware != null) { + Optional currentAuditor = currentAuditorAware.getCurrentAuditor(); if (currentAuditor.isPresent()) { Object auditor = currentAuditor.get(); @@ -209,8 +284,9 @@ public boolean beforeUpdate(PersistentEntity entity, EntityAccess ea) { } // Handle auditor fields - if (auditorAware != null) { - Optional currentAuditor = auditorAware.getCurrentAuditor(); + AuditorAware currentAuditorAware = getAuditorAware(); + if (currentAuditorAware != null) { + Optional currentAuditor = currentAuditorAware.getCurrentAuditor(); if (currentAuditor.isPresent()) { Object auditor = currentAuditor.get(); @@ -307,12 +383,33 @@ public void setTimestampProvider(TimestampProvider timestampProvider) { this.timestampProvider = timestampProvider; } + /** + * @return the auditor that the auditor properties are set from: the one that was set, the one of the application + * context the listener was given, or else the one of the datastore's application context, which is looked up + * once that context is active; {@code null} when there is none + */ public AuditorAware getAuditorAware() { + if (!auditorAwareResolved) { + ApplicationContext applicationContext = activeApplicationContextOfDatastore(); + if (applicationContext != null) { + // The lookup runs outside the lock, and what it finds is kept only if no auditor was set meanwhile. + AuditorAware contextAuditorAware = lookUpAuditorAware(applicationContext); + synchronized (auditorAwareLock) { + if (!auditorAwareResolved) { + auditorAware = contextAuditorAware; + auditorAwareResolved = true; + } + } + } + } return auditorAware; } public void setAuditorAware(AuditorAware auditorAware) { - this.auditorAware = auditorAware; + synchronized (auditorAwareLock) { + this.auditorAware = auditorAware; + this.auditorAwareResolved = true; + } } private static boolean isDisabled(final ThreadLocal disabledTimestamps, final String entityName) { diff --git a/grails-datamapping-core/src/main/groovy/org/grails/datastore/gorm/events/AutoTimestampingDatastore.java b/grails-datamapping-core/src/main/groovy/org/grails/datastore/gorm/events/AutoTimestampingDatastore.java new file mode 100644 index 00000000000..1ab197088f9 --- /dev/null +++ b/grails-datamapping-core/src/main/groovy/org/grails/datastore/gorm/events/AutoTimestampingDatastore.java @@ -0,0 +1,38 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.grails.datastore.gorm.events; + +/** + * A datastore that sets the timestamp and auditor properties of the entities it saves through an + * {@link AutoTimestampEventListener} of its own. Every datastore has its own listener, and so does every connection + * of a datastore with several, and a listener only handles the entities saved through its own datastore. Turning + * timestamps off on one listener therefore leaves the entities saved through any other datastore or connection + * untouched. + * + * @since 9.0 + */ +public interface AutoTimestampingDatastore { + + /** + * @return the listener that sets the timestamps of the entities this datastore saves, or {@code null} when this + * datastore sets none + */ + AutoTimestampEventListener getAutoTimestampEventListener(); + +} diff --git a/grails-datamapping-core/src/main/groovy/org/grails/datastore/gorm/events/DefaultApplicationEventPublisher.groovy b/grails-datamapping-core/src/main/groovy/org/grails/datastore/gorm/events/DefaultApplicationEventPublisher.groovy index b1d534a60ae..4533712620d 100644 --- a/grails-datamapping-core/src/main/groovy/org/grails/datastore/gorm/events/DefaultApplicationEventPublisher.groovy +++ b/grails-datamapping-core/src/main/groovy/org/grails/datastore/gorm/events/DefaultApplicationEventPublisher.groovy @@ -19,6 +19,8 @@ package org.grails.datastore.gorm.events +import java.util.concurrent.CopyOnWriteArrayList + import groovy.transform.CompileStatic import org.springframework.context.ApplicationEvent @@ -35,7 +37,9 @@ import org.springframework.context.event.SmartApplicationListener @CompileStatic class DefaultApplicationEventPublisher implements ConfigurableApplicationEventPublisher { - private List applicationListeners = [] + // Copied on write: a datastore adds the listeners of a schema it adds at runtime, which can happen while an + // event is being published on this thread or another one. A listener added then receives the next event. + private final List applicationListeners = new CopyOnWriteArrayList<>() @Override void publishEvent(ApplicationEvent event) { diff --git a/grails-datamapping-core/src/main/groovy/org/grails/datastore/gorm/multitenancy/TenantDelegatingGormOperations.groovy b/grails-datamapping-core/src/main/groovy/org/grails/datastore/gorm/multitenancy/TenantDelegatingGormOperations.groovy index 00a27844a3c..43fd195f6b3 100644 --- a/grails-datamapping-core/src/main/groovy/org/grails/datastore/gorm/multitenancy/TenantDelegatingGormOperations.groovy +++ b/grails-datamapping-core/src/main/groovy/org/grails/datastore/gorm/multitenancy/TenantDelegatingGormOperations.groovy @@ -26,6 +26,7 @@ import org.springframework.transaction.TransactionDefinition import grails.gorm.DetachedCriteria import grails.gorm.api.GormAllOperations import grails.gorm.multitenancy.Tenants +import org.grails.datastore.gorm.events.AutoTimestampEventListener import org.grails.datastore.gorm.finders.FinderMethod import org.grails.datastore.mapping.core.Datastore import org.grails.datastore.mapping.model.PersistentEntity @@ -210,6 +211,13 @@ class TenantDelegatingGormOperations implements GormAllOperations { return allOperations.gormDynamicFinders } + @Override + AutoTimestampEventListener getAutoTimestampEventListener() { + Tenants.withId(requireMultiTenantCapableDatastore(), tenantId) { + allOperations.autoTimestampEventListener + } + } + @Override DetachedCriteria where(Closure callable) { Tenants.withId(requireMultiTenantCapableDatastore(), tenantId) { diff --git a/grails-datamapping-core/src/test/groovy/org/grails/datastore/gorm/GormStaticApiSpec.groovy b/grails-datamapping-core/src/test/groovy/org/grails/datastore/gorm/GormStaticApiSpec.groovy index ff8f6bb2174..19867d02105 100644 --- a/grails-datamapping-core/src/test/groovy/org/grails/datastore/gorm/GormStaticApiSpec.groovy +++ b/grails-datamapping-core/src/test/groovy/org/grails/datastore/gorm/GormStaticApiSpec.groovy @@ -28,6 +28,7 @@ import grails.gorm.api.GormAllOperations import grails.gorm.api.GormInstanceOperations import grails.gorm.api.GormStaticOperations import grails.gorm.multitenancy.Tenants +import org.grails.datastore.gorm.events.AutoTimestampingDatastore import org.grails.datastore.gorm.events.DefaultApplicationEventPublisher import org.grails.datastore.mapping.config.Settings import org.grails.datastore.mapping.core.Datastore @@ -159,6 +160,68 @@ class GormStaticApiSpec extends Specification { api.getGormDynamicFinders() == [finder] } + void "getAutoTimestampEventListener returns the listener of the api's datastore"() { + given: + def api = new GormStaticApi(GormStaticApiThing, datastore, []) + + expect: + api.autoTimestampEventListener != null + api.autoTimestampEventListener.is(datastore.autoTimestampEventListener) + GormStaticApiThing.autoTimestampEventListener.is(datastore.autoTimestampEventListener) + } + + void "the default auto-timestamp listener of the static operations contract says it cannot be looked up"() { + given: 'an implementation that provides nothing beyond the interface defaults' + GormStaticOperations operations = (GormStaticOperations) Proxy.newProxyInstance( + GormStaticOperations.classLoader, [GormStaticOperations] as Class[], + { Object proxy, Method method, Object[] methodArgs -> + if (method.isDefault()) { + return InvocationHandler.invokeDefault(proxy, method, methodArgs) + } + throw new AssertionError("unexpected call of ${method.name}") + } as InvocationHandler) + + when: + operations.autoTimestampEventListener + + then: + UnsupportedOperationException e = thrown() + e.message.contains('cannot look up the auto-timestamp event listener') + } + + void "getAutoTimestampEventListener rejects a datastore that does not set timestamps through a listener"() { + given: + Datastore plainDatastore = Stub(Datastore) + plainDatastore.getMappingContext() >> datastore.mappingContext + plainDatastore.connect() >> Stub(Session) { getDatastore() >> plainDatastore } + def api = new GormStaticApi(GormStaticApiThing, plainDatastore, []) + + when: + api.autoTimestampEventListener + + then: + IllegalStateException e = thrown() + e.message.contains(GormStaticApiThing.name) + e.message.contains(plainDatastore.toString()) + } + + void "getAutoTimestampEventListener rejects an auto-timestamping datastore that has no listener"() { + given: + Datastore listenerlessDatastore = Stub(Datastore, additionalInterfaces: [AutoTimestampingDatastore]) + listenerlessDatastore.getMappingContext() >> datastore.mappingContext + listenerlessDatastore.connect() >> Stub(Session) { getDatastore() >> listenerlessDatastore } + ((AutoTimestampingDatastore) listenerlessDatastore).getAutoTimestampEventListener() >> null + def api = new GormStaticApi(GormStaticApiThing, listenerlessDatastore, []) + + when: + api.autoTimestampEventListener + + then: + IllegalStateException e = thrown() + e.message.contains(GormStaticApiThing.name) + e.message.contains(listenerlessDatastore.toString()) + } + void "forQualifier creates a new static api bound to the requested qualifier"() { given: def api = new GormStaticApi(GormStaticApiThing, datastore, []) diff --git a/grails-datamapping-core/src/test/groovy/org/grails/datastore/gorm/events/DefaultApplicationEventPublisherSpec.groovy b/grails-datamapping-core/src/test/groovy/org/grails/datastore/gorm/events/DefaultApplicationEventPublisherSpec.groovy index 2c4e5fc689d..6f6d2cf1ae9 100644 --- a/grails-datamapping-core/src/test/groovy/org/grails/datastore/gorm/events/DefaultApplicationEventPublisherSpec.groovy +++ b/grails-datamapping-core/src/test/groovy/org/grails/datastore/gorm/events/DefaultApplicationEventPublisherSpec.groovy @@ -160,4 +160,30 @@ class DefaultApplicationEventPublisherSpec extends Specification { 1 * supported.onApplicationEvent(_) 0 * unsupported.onApplicationEvent(_) } + + void 'a listener can add another listener while an event is published'() { + given: 'a listener that adds one, as a datastore that adds a schema while it saves does' + List received = [] + ApplicationListener added = { ApplicationEvent event -> received << "added:${event.source}".toString() } as ApplicationListener + publisher.addApplicationListener({ ApplicationEvent event -> + received << "adding:${event.source}".toString() + if (event.source == 'first') { + publisher.addApplicationListener(added) + } + } as ApplicationListener) + + when: + publisher.publishEvent(new TestEvent('first')) + publisher.publishEvent(new TestEvent('second')) + + then: 'the added listener receives the events published after it was added' + received == ['adding:first', 'adding:second', 'added:second'] + } + + static class TestEvent extends ApplicationEvent { + + TestEvent(Object source) { + super(source) + } + } } diff --git a/grails-datamapping-core/src/test/groovy/org/grails/datastore/gorm/jdbc/connections/DataSourceConnectionSourceFactorySpec.groovy b/grails-datamapping-core/src/test/groovy/org/grails/datastore/gorm/jdbc/connections/DataSourceConnectionSourceFactorySpec.groovy index 0a9730c393e..843750b726f 100644 --- a/grails-datamapping-core/src/test/groovy/org/grails/datastore/gorm/jdbc/connections/DataSourceConnectionSourceFactorySpec.groovy +++ b/grails-datamapping-core/src/test/groovy/org/grails/datastore/gorm/jdbc/connections/DataSourceConnectionSourceFactorySpec.groovy @@ -18,14 +18,356 @@ */ package org.grails.datastore.gorm.jdbc.connections +import com.zaxxer.hikari.HikariDataSource +import groovy.util.ConfigObject import org.grails.datastore.mapping.core.DatastoreUtils +import org.grails.datastore.mapping.core.exceptions.ConfigurationException import org.grails.datastore.mapping.core.connections.ConnectionSource +import org.springframework.core.convert.converter.Converter +import org.springframework.core.convert.support.DefaultConversionService +import org.springframework.core.env.MapPropertySource +import org.springframework.core.env.StandardEnvironment import org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy import org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy import spock.lang.Specification +import javax.sql.DataSource + + class DataSourceConnectionSourceFactorySpec extends Specification { + void "create binds flattened default data source pool properties from the Spring Environment"() { + given: + def factory = new DataSourceConnectionSourceFactory() + def environment = new StandardEnvironment() + environment.propertySources.addFirst(new MapPropertySource('application.yml', [ + 'dataSource.url': 'jdbc:h2:mem:factoryEnvironmentTest', + 'dataSource.driverClassName': 'org.h2.Driver', + 'dataSource.username': 'sa', + 'dataSource.password': '', + 'dataSource.type': 'com.zaxxer.hikari.HikariDataSource', + 'dataSource.properties.maximumPoolSize': 16, + 'dataSource.properties.minimumIdle': 8, + 'dataSource.properties.connectionTimeout': 2000, + 'dataSource.properties.dataSourceProperties.connectionCollation': 'utf8mb4_unicode_ci' + ])) + + when: + def connectionSource = factory.create(ConnectionSource.DEFAULT, environment) + + then: + connectionSource.settings.url == 'jdbc:h2:mem:factoryEnvironmentTest' + connectionSource.settings.username == 'sa' + connectionSource.settings.password == '' + connectionSource.source instanceof TransactionAwareDataSourceProxy + def lazyDataSource = ((TransactionAwareDataSourceProxy) connectionSource.source).targetDataSource + lazyDataSource instanceof LazyConnectionDataSourceProxy + def dataSource = ((LazyConnectionDataSourceProxy) lazyDataSource).targetDataSource + dataSource instanceof HikariDataSource + dataSource.maximumPoolSize == 16 + dataSource.minimumIdle == 8 + dataSource.connectionTimeout == 2000 + dataSource.dataSourceProperties.getProperty('connectionCollation') == 'utf8mb4_unicode_ci' + + cleanup: + connectionSource?.close() + } + + void "create binds flattened named data source pool properties from the Spring Environment"() { + given: + def factory = new DataSourceConnectionSourceFactory() + def environment = new StandardEnvironment() + environment.propertySources.addFirst(new MapPropertySource('application.yml', [ + 'dataSources.secondary.url': 'jdbc:h2:mem:factoryNamedEnvironmentTest', + 'dataSources.secondary.type': HikariDataSource.name, + 'dataSources.secondary.properties.maximumPoolSize': 24 + ])) + + when: + def connectionSource = factory.create('secondary', environment) + + then: + connectionSource.name == 'secondary' + def dataSource = unwrap(connectionSource.source) + dataSource instanceof HikariDataSource + dataSource.maximumPoolSize == 24 + + cleanup: + connectionSource?.close() + } + + void "create uses the highest precedence Spring property source for pool properties"() { + given: + def factory = new DataSourceConnectionSourceFactory() + def environment = new StandardEnvironment() + environment.propertySources.addFirst(new MapPropertySource('application.yml', [ + 'dataSource.url': 'jdbc:h2:mem:factoryPrecedenceTest', + 'dataSource.type': HikariDataSource.name, + 'dataSource.properties.maximumPoolSize': 16, + 'dataSource.properties.minimumIdle': 8 + ])) + environment.propertySources.addFirst(new MapPropertySource('override', [ + 'dataSource.properties.maximumPoolSize': 32 + ])) + + when: + def connectionSource = factory.create(ConnectionSource.DEFAULT, environment) + + then: + def dataSource = unwrap(connectionSource.source) + dataSource instanceof HikariDataSource + dataSource.maximumPoolSize == 32 + dataSource.minimumIdle == 8 + + cleanup: + connectionSource?.close() + } + + void "create keeps a higher precedence scalar over a lower flattened descendant"() { + given: + def factory = new DataSourceConnectionSourceFactory() + def environment = new StandardEnvironment() + environment.propertySources.addFirst(new MapPropertySource('low', [ + 'dataSource.url': 'jdbc:h2:mem:factoryScalarPrecedenceTest', + 'dataSource.type': HikariDataSource.name, + 'dataSource.properties.dataSourceProperties.foo.x': 'low', + 'dataSource.properties.foo.x': 'low', + 'dataSource.properties.maximumPoolSize.nested': '${unresolvedLowerPoolSize}' + ])) + environment.propertySources.addFirst(new MapPropertySource('high', [ + 'dataSource.properties.dataSourceProperties.foo': 'bar', + 'dataSource.properties.foo': 'bar', + 'dataSource.properties.maximumPoolSize': 50 + ])) + + when: + def connectionSource = factory.create(ConnectionSource.DEFAULT, environment) + + then: + connectionSource.settings.properties == [maximumPoolSize: 50, dataSourceProperties: [foo: 'bar'], foo: 'bar'] + def dataSource = unwrap(connectionSource.source) + dataSource.maximumPoolSize == 50 + dataSource.dataSourceProperties.getProperty('foo') == 'bar' + !dataSource.dataSourceProperties.containsKey('foo.x') + + cleanup: + connectionSource?.close() + } + + void "create does not merge a direct properties map when a flattened descendant exists"() { + given: + def factory = new DataSourceConnectionSourceFactory() + def environment = new StandardEnvironment() + environment.propertySources.addFirst(new MapPropertySource('low', [ + 'dataSource.url': 'jdbc:h2:mem:factoryUnmergedMapTest', + 'dataSource.type': HikariDataSource.name, + 'dataSource.properties': [maximumPoolSize: 20, minimumIdle: 2] + ])) + environment.propertySources.addFirst(new MapPropertySource('high', [ + 'dataSource.properties.maximumPoolSize': 50 + ])) + + when: + def connectionSource = factory.create(ConnectionSource.DEFAULT, environment) + + then: + connectionSource.settings.properties == [maximumPoolSize: 50] + unwrap(connectionSource.source).maximumPoolSize == 50 + + cleanup: + connectionSource?.close() + } + + void "create collects indexed pool properties into a list in numeric order"() { + given: + def factory = new DataSourceConnectionSourceFactory() + def environment = new StandardEnvironment() + environment.propertySources.addFirst(new MapPropertySource('application', [ + 'dataSource.url': 'jdbc:h2:mem:factoryIndexedPropertiesTest', + 'dataSource.type': ListDataSource.name, + 'dataSource.properties.connectionInitSqls[1]': 'SET B=2', + 'dataSource.properties.connectionInitSqls[0]': 'SET A=1' + ])) + + when: + def connectionSource = factory.create(ConnectionSource.DEFAULT, environment) + + then: + connectionSource.settings.properties == [connectionInitSqls: ['SET A=1', 'SET B=2']] + unwrap(connectionSource.source).connectionInitSqls == ['SET A=1', 'SET B=2'] + + cleanup: + connectionSource?.close() + } + + void "create still binds an existing nested properties map from DatastoreUtils"() { + given: + def factory = new DataSourceConnectionSourceFactory() + def dataSourceConfig = new ConfigObject() + dataSourceConfig.putAll([ + url: 'jdbc:h2:mem:factoryNestedMapTest', + type: HikariDataSource, + properties: [maximumPoolSize: 20] + ]) + def applicationConfig = new ConfigObject() + applicationConfig.put('dataSource', dataSourceConfig) + def config = DatastoreUtils.createPropertyResolver(applicationConfig) + + when: + def connectionSource = factory.create(ConnectionSource.DEFAULT, config) + + then: + config.getProperty('dataSource.properties', Map) == [maximumPoolSize: 20] + def dataSource = unwrap(connectionSource.source) + dataSource instanceof HikariDataSource + dataSource.maximumPoolSize == 20 + + cleanup: + connectionSource?.close() + } + + void "create rebuilds flattened properties when the direct map is #directValue and conversion fails is #conversionFails"() { + given: + def factory = new DataSourceConnectionSourceFactory() + def environment = new StandardEnvironment() + if (conversionFails) { + environment.conversionService = new DefaultConversionService() + environment.conversionService.addConverter(String, Map, { String value -> + throw new IllegalArgumentException('Cannot convert value to a map') + } as Converter) + } + environment.propertySources.addFirst(new MapPropertySource('application', [ + 'dataSource.url': 'jdbc:h2:mem:factoryMapFallbackTest', + 'dataSource.type': HikariDataSource.name, + 'dataSource.properties': directValue, + 'dataSource.properties.maximumPoolSize': '${poolSize}', + 'poolSize': 16, + 'dataSource.propertiesFoo.maximumPoolSize': 99, + 'dataSource.properties.dataSourceProperties.connectionCollation': 'utf8mb4_unicode_ci', + 'dataSource.properties.dataSourceProperties.empty': '', + 'dataSource.properties.dataSourceProperties.absent': null + ])) + + when: + def connectionSource = factory.create(ConnectionSource.DEFAULT, environment) + + then: + connectionSource.settings.properties == [ + maximumPoolSize: '16', + dataSourceProperties: [connectionCollation: 'utf8mb4_unicode_ci', empty: ''] + ] + def dataSource = unwrap(connectionSource.source) + dataSource instanceof HikariDataSource + dataSource.maximumPoolSize == 16 + dataSource.dataSourceProperties.getProperty('connectionCollation') == 'utf8mb4_unicode_ci' + dataSource.dataSourceProperties.getProperty('empty') == '' + !dataSource.dataSourceProperties.containsKey('absent') + + cleanup: + connectionSource?.close() + + where: + directValue | conversionFails + null | false + [:] | false + 'not a map' | false + 'not a map' | true + } + + void "create binds flattened pool size when the exact properties path is an unresolved placeholder"() { + given: + def factory = new DataSourceConnectionSourceFactory() + def environment = new StandardEnvironment() + environment.propertySources.addFirst(new MapPropertySource('application', [ + 'dataSource.url': 'jdbc:h2:mem:factoryMissingExactPath', + 'dataSource.type': HikariDataSource.name, + 'dataSource.properties': '${missing}', + 'dataSource.properties.maximumPoolSize': 16 + ])) + + when: + def connectionSource = factory.create(ConnectionSource.DEFAULT, environment) + + then: + connectionSource.settings.properties == [maximumPoolSize: 16] + unwrap(connectionSource.source).maximumPoolSize == 16 + + cleanup: + connectionSource?.close() + } + + void "create does not invoke a string to map converter when a flattened descendant exists"() { + given: + def factory = new DataSourceConnectionSourceFactory() + def environment = new StandardEnvironment() + def invoked = [] + environment.conversionService = new DefaultConversionService() + environment.conversionService.addConverter(String, Map, { String value -> + invoked.add(value) + throw new IllegalArgumentException('Cannot convert value to a map') + } as Converter) + environment.propertySources.addFirst(new MapPropertySource('application', [ + 'dataSource.url': 'jdbc:h2:mem:factoryConverterSkipped', + 'dataSource.type': HikariDataSource.name, + 'dataSource.properties': 'not a map', + 'dataSource.properties.maximumPoolSize': 16 + ])) + + when: + def connectionSource = factory.create(ConnectionSource.DEFAULT, environment) + + then: + invoked.empty + connectionSource.settings.properties == [maximumPoolSize: 16] + unwrap(connectionSource.source).maximumPoolSize == 16 + + cleanup: + connectionSource?.close() + } + + void "create binds a map-valued descendant without forcing it to a string"() { + given: + def factory = new DataSourceConnectionSourceFactory() + def environment = new StandardEnvironment() + environment.propertySources.addFirst(new MapPropertySource('application', [ + 'dataSource.url': 'jdbc:h2:mem:factoryMapDescendantTest', + 'dataSource.type': HikariDataSource.name, + 'dataSource.properties.maximumPoolSize': 16, + 'dataSource.properties.dataSourceProperties': [connectionCollation: 'utf8mb4_unicode_ci', empty: ''] + ])) + + when: + def connectionSource = factory.create(ConnectionSource.DEFAULT, environment) + + then: + connectionSource.settings.properties.maximumPoolSize == 16 + connectionSource.settings.properties.dataSourceProperties == [connectionCollation: 'utf8mb4_unicode_ci', empty: ''] + def dataSource = unwrap(connectionSource.source) + dataSource instanceof HikariDataSource + dataSource.maximumPoolSize == 16 + dataSource.dataSourceProperties.getProperty('connectionCollation') == 'utf8mb4_unicode_ci' + dataSource.dataSourceProperties.getProperty('empty') == '' + + cleanup: + connectionSource?.close() + } + + void "create rejects a non-map properties value when no flattened properties exist"() { + given: + def factory = new DataSourceConnectionSourceFactory() + def environment = new StandardEnvironment() + environment.propertySources.addFirst(new MapPropertySource('application', [ + 'dataSource.url': 'jdbc:h2:mem:factoryInvalidPropertiesTest', + 'dataSource.properties': 'not a map' + ])) + + when: + factory.create(ConnectionSource.DEFAULT, environment) + + then: + thrown(ConfigurationException) + } + void "create resolves settings for the default data source from the 'dataSource' prefix"() { given: def factory = new DataSourceConnectionSourceFactory() @@ -41,6 +383,7 @@ class DataSourceConnectionSourceFactorySpec extends Specification { then: connectionSource.name == ConnectionSource.DEFAULT connectionSource.settings.url == 'jdbc:h2:mem:factoryDefaultTest;DB_CLOSE_DELAY=-1' + connectionSource.settings.properties.isEmpty() connectionSource.source != null cleanup: @@ -112,4 +455,20 @@ class DataSourceConnectionSourceFactorySpec extends Specification { expect: factory.connectionSourcesConfigurationKey == 'dataSources' } + + static class ListDataSource extends HikariDataSource { + + List connectionInitSqls + } + + private static DataSource unwrap(DataSource dataSource) { + DataSource current = dataSource + while (current instanceof TransactionAwareDataSourceProxy || current instanceof LazyConnectionDataSourceProxy) { + current = current instanceof TransactionAwareDataSourceProxy ? + ((TransactionAwareDataSourceProxy) current).targetDataSource : + ((LazyConnectionDataSourceProxy) current).targetDataSource + } + return current + } } + diff --git a/grails-datamapping-core/src/test/groovy/org/grails/datastore/gorm/multitenancy/TenantDelegatingGormOperationsSpec.groovy b/grails-datamapping-core/src/test/groovy/org/grails/datastore/gorm/multitenancy/TenantDelegatingGormOperationsSpec.groovy index a7fd2af87ea..d5200e97e38 100644 --- a/grails-datamapping-core/src/test/groovy/org/grails/datastore/gorm/multitenancy/TenantDelegatingGormOperationsSpec.groovy +++ b/grails-datamapping-core/src/test/groovy/org/grails/datastore/gorm/multitenancy/TenantDelegatingGormOperationsSpec.groovy @@ -20,7 +20,9 @@ package org.grails.datastore.gorm.multitenancy import grails.gorm.api.GormAllOperations import grails.gorm.multitenancy.Tenants +import org.grails.datastore.gorm.events.AutoTimestampEventListener import org.grails.datastore.mapping.core.Datastore +import org.grails.datastore.mapping.keyvalue.mapping.config.KeyValueMappingContext import org.grails.datastore.mapping.multitenancy.MultiTenancySettings import org.grails.datastore.mapping.multitenancy.MultiTenantCapableDatastore import org.springframework.transaction.TransactionDefinition @@ -74,6 +76,20 @@ class TenantDelegatingGormOperationsSpec extends Specification { 0 * delegate.save(_, _) } + void "getAutoTimestampEventListener delegates to the wrapped operations under the bound tenant"() { + given: + def delegate = Mock(GormAllOperations) + def ops = buildOperations(delegate) + def listener = new AutoTimestampEventListener(Stub(Datastore) { getMappingContext() >> new KeyValueMappingContext('test') }) + + when: + def result = ops.autoTimestampEventListener + + then: + 1 * delegate.getAutoTimestampEventListener() >> listener + result.is(listener) + } + void "deleteAll() delegates to the wrapped operations under the bound tenant"() { given: def delegate = Mock(GormAllOperations) diff --git a/grails-datamapping-core/src/test/groovy/org/grails/datastore/gorm/timestamp/AutoTimestampEventListenerSpec.groovy b/grails-datamapping-core/src/test/groovy/org/grails/datastore/gorm/timestamp/AutoTimestampEventListenerSpec.groovy index 743cbe2346f..704011bb080 100644 --- a/grails-datamapping-core/src/test/groovy/org/grails/datastore/gorm/timestamp/AutoTimestampEventListenerSpec.groovy +++ b/grails-datamapping-core/src/test/groovy/org/grails/datastore/gorm/timestamp/AutoTimestampEventListenerSpec.groovy @@ -26,13 +26,20 @@ import java.util.concurrent.ExecutorService import java.util.concurrent.Executors import java.util.concurrent.Future import java.util.concurrent.TimeUnit +import java.util.function.Supplier import groovy.transform.InheritConstructors import spock.lang.Specification +import org.springframework.beans.factory.NoSuchBeanDefinitionException +import org.springframework.beans.factory.config.BeanDefinition +import org.springframework.beans.factory.config.BeanDefinitionCustomizer +import org.springframework.context.support.GenericApplicationContext + import grails.gorm.annotation.CreatedBy import grails.gorm.annotation.LastModifiedBy import org.grails.datastore.gorm.events.AutoTimestampEventListener +import org.grails.datastore.gorm.events.ConfigurableApplicationContextEventPublisher import org.grails.datastore.mapping.config.Property import org.grails.datastore.mapping.core.Datastore import org.grails.datastore.mapping.engine.EntityAccess @@ -633,6 +640,132 @@ class AutoTimestampEventListenerSpec extends Specification { updated == [lastModifiedBy: 'testUser'] } + void "a listener that is not given an auditor looks it up in the application context of its datastore once that is active"() { + given: 'the publisher of the datastore, which the datastores of every connection share, holds the context' + AuditorAware contextAuditor = { -> Optional.of('contextUser') } as AuditorAware + GenericApplicationContext context = new GenericApplicationContext() + context.registerBean(AuditorAware, { contextAuditor } as Supplier) + TestEventListener contextListener = new TestEventListener(Stub(Datastore) { + getMappingContext() >> null + getApplicationEventPublisher() >> new ConfigurableApplicationContextEventPublisher(context) + }) + + expect: 'no auditor while the context is not refreshed' + contextListener.auditorAware == null + + when: + context.refresh() + contextListener.persistentEntityAdded(auditedEntity()) + + then: + contextListener.auditorAware.is(contextAuditor) + appliedOnInsert(contextListener, Audited) == [createdBy: 'contextUser', lastModifiedBy: 'contextUser'] + + cleanup: + context.close() + } + + void "an auditor that is set, or found in the application context given to the listener, is not looked up again"() { + given: + AuditorAware setAuditor = { -> Optional.of('setUser') } as AuditorAware + AuditorAware givenAuditor = { -> Optional.of('givenUser') } as AuditorAware + GenericApplicationContext datastoreContext = new GenericApplicationContext() + datastoreContext.registerBean(AuditorAware, { { -> Optional.of('datastoreUser') } as AuditorAware } as Supplier) + datastoreContext.refresh() + GenericApplicationContext givenContext = new GenericApplicationContext() + givenContext.registerBean(AuditorAware, { givenAuditor } as Supplier) + givenContext.refresh() + Datastore datastore = Stub(Datastore) { + getMappingContext() >> null + getApplicationEventPublisher() >> new ConfigurableApplicationContextEventPublisher(datastoreContext) + } + TestEventListener withSetAuditor = new TestEventListener(datastore) + TestEventListener withGivenContext = new TestEventListener(datastore) + TestEventListener withEmptyContext = new TestEventListener(datastore) + GenericApplicationContext emptyContext = new GenericApplicationContext() + emptyContext.refresh() + + when: + withSetAuditor.auditorAware = setAuditor + withGivenContext.applicationContext = givenContext + withEmptyContext.applicationContext = emptyContext + + then: + withSetAuditor.auditorAware.is(setAuditor) + withGivenContext.auditorAware.is(givenAuditor) + withEmptyContext.auditorAware == null + + cleanup: + [datastoreContext, givenContext, emptyContext]*.close() + } + + void "a listener bean keeps the auditor it is configured with when the application context has none"() { + given: 'Spring sets the bean\'s auditorAware property and then gives it the application context' + AuditorAware configuredAuditor = { -> Optional.of('configuredUser') } as AuditorAware + GenericApplicationContext context = new GenericApplicationContext() + context.registerBean('listener', TestEventListener, { new TestEventListener(Stub(Datastore) { getMappingContext() >> null }) } as Supplier, + { BeanDefinition definition -> definition.propertyValues.add('auditorAware', configuredAuditor) } as BeanDefinitionCustomizer) + + when: + context.refresh() + TestEventListener bean = context.getBean(TestEventListener) + bean.persistentEntityAdded(auditedEntity()) + + then: + bean.auditorAware.is(configuredAuditor) + appliedOnInsert(bean, Audited) == [createdBy: 'configuredUser', lastModifiedBy: 'configuredUser'] + + cleanup: + context.close() + } + + void "a listener bean takes the auditor of the application context over the one it is configured with"() { + given: + AuditorAware configuredAuditor = { -> Optional.of('configuredUser') } as AuditorAware + AuditorAware contextAuditor = { -> Optional.of('contextUser') } as AuditorAware + GenericApplicationContext context = new GenericApplicationContext() + context.registerBean(AuditorAware, { contextAuditor } as Supplier) + context.registerBean('listener', TestEventListener, { new TestEventListener(Stub(Datastore) { getMappingContext() >> null }) } as Supplier, + { BeanDefinition definition -> definition.propertyValues.add('auditorAware', configuredAuditor) } as BeanDefinitionCustomizer) + + when: + context.refresh() + + then: + context.getBean(TestEventListener).auditorAware.is(contextAuditor) + + cleanup: + context.close() + } + + void "an auditor set while the listener looks one up in the application context of its datastore is kept"() { + given: 'a context whose lookup of the auditor blocks until it is released, and then finds none' + BlockingAuditorLookupContext context = new BlockingAuditorLookupContext() + context.refresh() + context.blockAuditorLookup = true + TestEventListener contextListener = new TestEventListener(Stub(Datastore) { + getMappingContext() >> null + getApplicationEventPublisher() >> new ConfigurableApplicationContextEventPublisher(context) + }) + AuditorAware setAuditor = { -> Optional.of('setUser') } as AuditorAware + Thread lookup = new Thread({ contextListener.auditorAware } as Runnable) + + when: 'an auditor is set while the lookup runs' + lookup.start() + assert context.lookupEntered.await(10, TimeUnit.SECONDS) + contextListener.auditorAware = setAuditor + context.release.countDown() + lookup.join(10000) + + then: + !lookup.alive + contextListener.auditorAware.is(setAuditor) + + cleanup: + context.release.countDown() + context.close() + } + private PersistentEntity auditedEntity() { PersistentEntity entity = null PersistentProperty createdBy = auditedProperty('createdBy') { -> entity } @@ -660,8 +793,12 @@ class AutoTimestampEventListenerSpec extends Specification { } private Map appliedOnInsert(Class clazz) { + appliedOnInsert(listener, clazz) + } + + private static Map appliedOnInsert(AutoTimestampEventListener eventListener, Class clazz) { Map applied = new ConcurrentHashMap<>() - listener.beforeInsert(entityFor(clazz), recordingAccess(applied)) + eventListener.beforeInsert(entityFor(clazz), recordingAccess(applied)) applied } @@ -716,3 +853,20 @@ class TestEventListener extends AutoTimestampEventListener { } } } + +class BlockingAuditorLookupContext extends GenericApplicationContext { + + final CountDownLatch lookupEntered = new CountDownLatch(1) + final CountDownLatch release = new CountDownLatch(1) + volatile boolean blockAuditorLookup + + @Override + T getBean(Class requiredType) { + if (blockAuditorLookup && requiredType == AuditorAware) { + lookupEntered.countDown() + release.await(10, TimeUnit.SECONDS) + throw new NoSuchBeanDefinitionException(AuditorAware) + } + super.getBean(requiredType) + } +} diff --git a/grails-datamapping-tck/src/main/groovy/org/apache/grails/data/testing/tck/domains/AllConnectionsTimestampNote.groovy b/grails-datamapping-tck/src/main/groovy/org/apache/grails/data/testing/tck/domains/AllConnectionsTimestampNote.groovy new file mode 100644 index 00000000000..86a9e74f0ba --- /dev/null +++ b/grails-datamapping-tck/src/main/groovy/org/apache/grails/data/testing/tck/domains/AllConnectionsTimestampNote.groovy @@ -0,0 +1,35 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.grails.data.testing.tck.domains + +import grails.persistence.Entity + +@Entity +class AllConnectionsTimestampNote { + + Long id + Long version + String text + Date dateCreated + Date lastUpdated + + static mapping = { + datasource 'ALL' + } +} diff --git a/grails-datamapping-tck/src/main/groovy/org/apache/grails/data/testing/tck/domains/DefaultConnectionTimestampNote.groovy b/grails-datamapping-tck/src/main/groovy/org/apache/grails/data/testing/tck/domains/DefaultConnectionTimestampNote.groovy new file mode 100644 index 00000000000..60634a1d53a --- /dev/null +++ b/grails-datamapping-tck/src/main/groovy/org/apache/grails/data/testing/tck/domains/DefaultConnectionTimestampNote.groovy @@ -0,0 +1,31 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.grails.data.testing.tck.domains + +import grails.persistence.Entity + +@Entity +class DefaultConnectionTimestampNote { + + Long id + Long version + String text + Date dateCreated + Date lastUpdated +} diff --git a/grails-datamapping-tck/src/main/groovy/org/apache/grails/data/testing/tck/domains/SecondaryConnectionTimestampNote.groovy b/grails-datamapping-tck/src/main/groovy/org/apache/grails/data/testing/tck/domains/SecondaryConnectionTimestampNote.groovy new file mode 100644 index 00000000000..14e86b3ea6b --- /dev/null +++ b/grails-datamapping-tck/src/main/groovy/org/apache/grails/data/testing/tck/domains/SecondaryConnectionTimestampNote.groovy @@ -0,0 +1,35 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.grails.data.testing.tck.domains + +import grails.persistence.Entity + +@Entity +class SecondaryConnectionTimestampNote { + + Long id + Long version + String text + Date dateCreated + Date lastUpdated + + static mapping = { + datasource 'secondary' + } +} diff --git a/grails-datamapping-tck/src/main/groovy/org/apache/grails/data/testing/tck/domains/TimestampNote.groovy b/grails-datamapping-tck/src/main/groovy/org/apache/grails/data/testing/tck/domains/TimestampNote.groovy new file mode 100644 index 00000000000..7a12f16c99a --- /dev/null +++ b/grails-datamapping-tck/src/main/groovy/org/apache/grails/data/testing/tck/domains/TimestampNote.groovy @@ -0,0 +1,31 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.grails.data.testing.tck.domains + +import grails.persistence.Entity + +@Entity +class TimestampNote { + + Long id + Long version + String text + Date dateCreated + Date lastUpdated +} diff --git a/grails-datamapping-tck/src/main/groovy/org/apache/grails/data/testing/tck/domains/TimestampNoteConnectionService.groovy b/grails-datamapping-tck/src/main/groovy/org/apache/grails/data/testing/tck/domains/TimestampNoteConnectionService.groovy new file mode 100644 index 00000000000..8e912082212 --- /dev/null +++ b/grails-datamapping-tck/src/main/groovy/org/apache/grails/data/testing/tck/domains/TimestampNoteConnectionService.groovy @@ -0,0 +1,40 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.grails.data.testing.tck.domains + +import grails.gorm.services.Service +import grails.gorm.transactions.Transactional +import org.grails.datastore.gorm.events.AutoTimestampEventListener + +@Service(AllConnectionsTimestampNote) +@Transactional(connection = 'secondary') +abstract class TimestampNoteConnectionService { + + AutoTimestampEventListener listener() { + AllConnectionsTimestampNote.autoTimestampEventListener + } + + AllConnectionsTimestampNote saveWithoutTimestamps(String text, Date timestamp) { + AllConnectionsTimestampNote note = new AllConnectionsTimestampNote(text: text, dateCreated: timestamp, lastUpdated: timestamp) + AllConnectionsTimestampNote.autoTimestampEventListener.withoutTimestamps { + note.save(flush: true) + } + note + } +} diff --git a/grails-datamapping-tck/src/main/groovy/org/apache/grails/data/testing/tck/tests/AutoTimestampEventListenerLookupSpec.groovy b/grails-datamapping-tck/src/main/groovy/org/apache/grails/data/testing/tck/tests/AutoTimestampEventListenerLookupSpec.groovy new file mode 100644 index 00000000000..57e5ff3358a --- /dev/null +++ b/grails-datamapping-tck/src/main/groovy/org/apache/grails/data/testing/tck/tests/AutoTimestampEventListenerLookupSpec.groovy @@ -0,0 +1,58 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.grails.data.testing.tck.tests + +import org.apache.grails.data.testing.tck.base.GrailsDataTckSpec +import org.apache.grails.data.testing.tck.domains.TimestampNote +import org.grails.datastore.gorm.events.AutoTimestampingDatastore + +class AutoTimestampEventListenerLookupSpec extends GrailsDataTckSpec { + + private static final Date PAST = new Date(110_000_000_000L) + + void setupSpec() { + manager.registerDomainClasses(TimestampNote) + } + + void 'a domain class answers with the listener of its datastore'() { + expect: + manager.session.datastore instanceof AutoTimestampingDatastore + TimestampNote.autoTimestampEventListener != null + TimestampNote.autoTimestampEventListener.is(((AutoTimestampingDatastore) manager.session.datastore).autoTimestampEventListener) + } + + void 'the listener of a domain class turns its timestamps off'() { + given: + TimestampNote kept = new TimestampNote(text: 'kept', dateCreated: PAST, lastUpdated: PAST) + TimestampNote stamped = new TimestampNote(text: 'stamped', dateCreated: PAST, lastUpdated: PAST) + + when: + TimestampNote.autoTimestampEventListener.withoutTimestamps { + kept.save(flush: true) + } + stamped.save(flush: true) + manager.session.clear() + + then: + TimestampNote.get(kept.id).dateCreated == PAST + TimestampNote.get(kept.id).lastUpdated == PAST + TimestampNote.get(stamped.id).dateCreated != PAST + TimestampNote.get(stamped.id).lastUpdated != PAST + } +} diff --git a/grails-datamapping-tck/src/main/groovy/org/apache/grails/data/testing/tck/tests/AutoTimestampEventListenerMultiDataSourceSpec.groovy b/grails-datamapping-tck/src/main/groovy/org/apache/grails/data/testing/tck/tests/AutoTimestampEventListenerMultiDataSourceSpec.groovy new file mode 100644 index 00000000000..a335f0e2781 --- /dev/null +++ b/grails-datamapping-tck/src/main/groovy/org/apache/grails/data/testing/tck/tests/AutoTimestampEventListenerMultiDataSourceSpec.groovy @@ -0,0 +1,132 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.grails.data.testing.tck.tests + +import spock.lang.Issue +import spock.lang.Requires + +import org.apache.grails.data.testing.tck.base.GrailsDataTckSpec +import org.apache.grails.data.testing.tck.domains.AllConnectionsTimestampNote +import org.apache.grails.data.testing.tck.domains.DefaultConnectionTimestampNote +import org.apache.grails.data.testing.tck.domains.SecondaryConnectionTimestampNote +import org.apache.grails.data.testing.tck.domains.TimestampNoteConnectionService + +/** + * Every connection has an auto-timestamp listener of its own, and a domain class answers with the listener of the + * connection its saves go through. Which connection that is for the unqualified operations of a class mapped only to + * another connection is the datastore's own: a datastore with a single session, as the in-memory one of unit tests, + * keeps them on the default connection. + */ +@Issue('https://github.com/apache/grails-core/issues/14509') +@Requires({ instance.manager?.supportsMultipleDataSources() }) +class AutoTimestampEventListenerMultiDataSourceSpec extends GrailsDataTckSpec { + + private static final Date PAST = new Date(110_000_000_000L) + + void setup() { + manager.setupMultiDataSource(DefaultConnectionTimestampNote, SecondaryConnectionTimestampNote, AllConnectionsTimestampNote) + } + + void cleanup() { + manager.cleanupMultiDataSource() + } + + void 'each connection answers with a listener of its own'() { + given: + def defaultListener = AllConnectionsTimestampNote.autoTimestampEventListener + def secondaryListener = AllConnectionsTimestampNote.secondary.autoTimestampEventListener + + expect: + !defaultListener.is(secondaryListener) + DefaultConnectionTimestampNote.autoTimestampEventListener.is(defaultListener) + AllConnectionsTimestampNote.secondary.withTransaction { AllConnectionsTimestampNote.autoTimestampEventListener }.is(secondaryListener) + } + + void 'a method transactional on a connection answers with that connection\'s listener'() { + given: + TimestampNoteConnectionService service = manager.getServiceForConnection(TimestampNoteConnectionService, 'secondary') + def secondaryListener = AllConnectionsTimestampNote.secondary.autoTimestampEventListener + + when: + AllConnectionsTimestampNote note = service.saveWithoutTimestamps('transactional', PAST) + + then: + service.listener().is(secondaryListener) + !service.listener().is(AllConnectionsTimestampNote.autoTimestampEventListener) + AllConnectionsTimestampNote.secondary.withNewTransaction { AllConnectionsTimestampNote.secondary.get(note.id).dateCreated } == PAST + AllConnectionsTimestampNote.secondary.withNewTransaction { AllConnectionsTimestampNote.secondary.get(note.id).lastUpdated } == PAST + } + + void 'the listener of each domain class turns off the timestamps of its own saves'() { + given: + DefaultConnectionTimestampNote car = new DefaultConnectionTimestampNote(text: 'car', dateCreated: PAST, lastUpdated: PAST) + SecondaryConnectionTimestampNote person = new SecondaryConnectionTimestampNote(text: 'person', dateCreated: PAST, lastUpdated: PAST) + + when: + DefaultConnectionTimestampNote.withNewTransaction { + DefaultConnectionTimestampNote.autoTimestampEventListener.withoutTimestamps { + car.save(flush: true) + } + } + SecondaryConnectionTimestampNote.withNewTransaction { + SecondaryConnectionTimestampNote.autoTimestampEventListener.withoutTimestamps { + person.save(flush: true) + } + } + + then: + DefaultConnectionTimestampNote.withNewTransaction { DefaultConnectionTimestampNote.get(car.id).dateCreated } == PAST + DefaultConnectionTimestampNote.withNewTransaction { DefaultConnectionTimestampNote.get(car.id).lastUpdated } == PAST + SecondaryConnectionTimestampNote.withNewTransaction { SecondaryConnectionTimestampNote.get(person.id).dateCreated } == PAST + SecondaryConnectionTimestampNote.withNewTransaction { SecondaryConnectionTimestampNote.get(person.id).lastUpdated } == PAST + } + + void 'the listener of one connection does not reach a save through another'() { + given: + def defaultListener = AllConnectionsTimestampNote.autoTimestampEventListener + AllConnectionsTimestampNote note = new AllConnectionsTimestampNote(text: 'shared', dateCreated: PAST, lastUpdated: PAST) + + when: + AllConnectionsTimestampNote.secondary.withNewTransaction { + defaultListener.withoutTimestamps { + note.secondary.save(flush: true) + } + } + + then: + AllConnectionsTimestampNote.secondary.withNewTransaction { AllConnectionsTimestampNote.secondary.get(note.id).dateCreated } != PAST + AllConnectionsTimestampNote.secondary.withNewTransaction { AllConnectionsTimestampNote.secondary.get(note.id).lastUpdated } != PAST + } + + void 'a domain class on several connections turns off the timestamps of the connection it names'() { + given: + AllConnectionsTimestampNote note = new AllConnectionsTimestampNote(text: 'shared', dateCreated: PAST, lastUpdated: PAST) + + when: + AllConnectionsTimestampNote.secondary.withNewTransaction { + AllConnectionsTimestampNote.secondary.autoTimestampEventListener.withoutTimestamps { + note.secondary.save(flush: true) + } + } + + then: + AllConnectionsTimestampNote.secondary.withNewTransaction { AllConnectionsTimestampNote.secondary.get(note.id).dateCreated } == PAST + AllConnectionsTimestampNote.secondary.withNewTransaction { AllConnectionsTimestampNote.secondary.get(note.id).lastUpdated } == PAST + } +} diff --git a/grails-datasource/build.gradle b/grails-datasource/build.gradle index 26e5072f136..fd65f5a955a 100644 --- a/grails-datasource/build.gradle +++ b/grails-datasource/build.gradle @@ -58,6 +58,7 @@ dependencies { compileOnly 'jakarta.servlet:jakarta.servlet-api' testImplementation 'com.h2database:h2' + testImplementation 'com.zaxxer:HikariCP' testImplementation 'jakarta.servlet:jakarta.servlet-api' testImplementation 'org.springframework:spring-test' testImplementation 'org.apache.groovy:groovy-json', { diff --git a/grails-datasource/src/test/groovy/org/grails/plugins/datasource/DataSourceEnvironmentYamlSpec.groovy b/grails-datasource/src/test/groovy/org/grails/plugins/datasource/DataSourceEnvironmentYamlSpec.groovy new file mode 100644 index 00000000000..948542ae57c --- /dev/null +++ b/grails-datasource/src/test/groovy/org/grails/plugins/datasource/DataSourceEnvironmentYamlSpec.groovy @@ -0,0 +1,215 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.grails.plugins.datasource + +import java.nio.file.Files +import java.nio.file.Path + +import javax.sql.DataSource + +import com.zaxxer.hikari.HikariDataSource + +import org.springframework.core.env.StandardEnvironment +import org.springframework.core.io.ByteArrayResource +import org.springframework.core.io.FileSystemResource +import org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy +import org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy + +import grails.util.Environment +import org.grails.config.yaml.YamlPropertySourceLoader +import org.grails.core.cfg.GroovyConfigPropertySourceLoader +import org.grails.datastore.gorm.jdbc.connections.DataSourceConnectionSourceFactory +import org.grails.datastore.mapping.core.connections.ConnectionSource + +import spock.lang.Specification + +class DataSourceEnvironmentYamlSpec extends Specification { + + private static final String APPLICATION_YML = '''\ +dataSource: + pooled: true + driverClassName: org.h2.Driver + username: sa + password: '' + type: com.zaxxer.hikari.HikariDataSource + properties: + maximumPoolSize: 4 + minimumIdle: 1 + connectionTimeout: 1000 +environments: + development: + dataSource: + dbCreate: create-drop + url: jdbc:h2:mem:devDb;DB_CLOSE_DELAY=-1 + properties: + maximumPoolSize: 16 + minimumIdle: 8 + connectionTimeout: 2000 + test: + dataSource: + dbCreate: update + url: jdbc:h2:mem:testDb;DB_CLOSE_DELAY=-1 + properties: + maximumPoolSize: 2 + minimumIdle: 1 + production: + dataSource: + dbCreate: none + url: jdbc:h2:mem:prodDb;DB_CLOSE_DELAY=-1 + properties: + maximumPoolSize: 32 + minimumIdle: 4 + connectionTimeout: 5000 +''' + + private String previousEnvironment + + def setup() { + previousEnvironment = System.getProperty(Environment.KEY) + } + + def cleanup() { + if (previousEnvironment == null) { + System.clearProperty(Environment.KEY) + } + else { + System.setProperty(Environment.KEY, previousEnvironment) + } + Environment.reset() + } + + void "application.yml #environment block supplies the dataSource properties"() { + given: + System.setProperty(Environment.KEY, environment) + Environment.reset() + def connectionSource = createFromApplicationYaml() + def dataSource = unwrap(connectionSource.source) + + expect: + connectionSource.settings.url == url + dataSource instanceof HikariDataSource + dataSource.maximumPoolSize == maximumPoolSize + dataSource.minimumIdle == minimumIdle + dataSource.connectionTimeout == connectionTimeout + + cleanup: + connectionSource?.close() + + where: + environment | url | maximumPoolSize | minimumIdle | connectionTimeout + 'development' | 'jdbc:h2:mem:devDb;DB_CLOSE_DELAY=-1' | 16 | 8 | 2000 + 'test' | 'jdbc:h2:mem:testDb;DB_CLOSE_DELAY=-1' | 2 | 1 | 1000 + 'production' | 'jdbc:h2:mem:prodDb;DB_CLOSE_DELAY=-1' | 32 | 4 | 5000 + } + + void "unset grails.env uses the development dataSource properties"() { + given: + System.clearProperty(Environment.KEY) + Environment.reset() + def connectionSource = createFromApplicationYaml() + def dataSource = unwrap(connectionSource.source) + + expect: + connectionSource.settings.url == 'jdbc:h2:mem:devDb;DB_CLOSE_DELAY=-1' + dataSource instanceof HikariDataSource + dataSource.maximumPoolSize == 16 + dataSource.minimumIdle == 8 + dataSource.connectionTimeout == 2000 + + cleanup: + connectionSource?.close() + } + + void "application.yml binds a list under dataSource properties"() { + given: + def yaml = """\ +dataSource: + url: jdbc:h2:mem:yamlListPropertiesTest + type: ${ListDataSource.name} + properties: + connectionInitSqls: + - SET A=1 + - SET B=2 +""" + def loaded = new YamlPropertySourceLoader().load( + 'application.yml', new ByteArrayResource(yaml.getBytes('UTF-8'))) + def environment = new StandardEnvironment() + environment.propertySources.addFirst(loaded.first()) + + when: + def connectionSource = new DataSourceConnectionSourceFactory().create(ConnectionSource.DEFAULT, environment) + + then: + connectionSource.settings.properties.connectionInitSqls == ['SET A=1', 'SET B=2'] + unwrap(connectionSource.source).connectionInitSqls == ['SET A=1', 'SET B=2'] + + cleanup: + connectionSource?.close() + } + + void "application.groovy dotted properties configure the connection pool"() { + given: + def directory = Files.createTempDirectory(Path.of('build'), 'datasource-groovy-').toFile() + def application = new File(directory, 'application.groovy') + application.setText('''\ +dataSource.url = 'jdbc:h2:mem:groovyDottedPropertiesTest' +dataSource.type = 'com.zaxxer.hikari.HikariDataSource' +dataSource.properties.maximumPoolSize = 100 +''', 'UTF-8') + def loaded = new GroovyConfigPropertySourceLoader().load( + 'application.groovy', new FileSystemResource(application)) + def environment = new StandardEnvironment() + environment.propertySources.addFirst(loaded.first()) + + when: + def connectionSource = new DataSourceConnectionSourceFactory().create(ConnectionSource.DEFAULT, environment) + + then: + connectionSource.settings.properties.maximumPoolSize == 100 + unwrap(connectionSource.source).maximumPoolSize == 100 + + cleanup: + connectionSource?.close() + directory?.deleteDir() + } + + static class ListDataSource extends HikariDataSource { + + List connectionInitSqls + } + + private static createFromApplicationYaml() { + def loaded = new YamlPropertySourceLoader().load( + 'application.yml', + new ByteArrayResource(APPLICATION_YML.getBytes('UTF-8'))) + def environment = new StandardEnvironment() + environment.propertySources.addFirst(loaded.first()) + new DataSourceConnectionSourceFactory().create(ConnectionSource.DEFAULT, environment) + } + + private static DataSource unwrap(DataSource dataSource) { + DataSource current = dataSource + while (current instanceof TransactionAwareDataSourceProxy || current instanceof LazyConnectionDataSourceProxy) { + current = current instanceof TransactionAwareDataSourceProxy ? + ((TransactionAwareDataSourceProxy) current).targetDataSource : + ((LazyConnectionDataSourceProxy) current).targetDataSource + } + return current + } +} diff --git a/grails-datastore-core/src/main/groovy/org/grails/datastore/mapping/config/ConfigurationBuilder.groovy b/grails-datastore-core/src/main/groovy/org/grails/datastore/mapping/config/ConfigurationBuilder.groovy index 02118c4ea89..f7d51167c39 100644 --- a/grails-datastore-core/src/main/groovy/org/grails/datastore/mapping/config/ConfigurationBuilder.groovy +++ b/grails-datastore-core/src/main/groovy/org/grails/datastore/mapping/config/ConfigurationBuilder.groovy @@ -32,7 +32,10 @@ import groovy.util.logging.Slf4j import org.springframework.core.convert.ConversionFailedException import org.springframework.core.convert.ConverterNotFoundException +import org.springframework.core.env.ConfigurableEnvironment +import org.springframework.core.env.EnumerablePropertySource import org.springframework.core.env.PropertyResolver +import org.springframework.core.env.PropertySource import org.springframework.util.ClassUtils import org.springframework.util.ReflectionUtils @@ -258,7 +261,7 @@ abstract class ConfigurationBuilder { } if (newBuilder instanceof Map) { - Map subMap = propertyResolver.getProperty(propertyPath, Map, Collections.emptyMap()) + Map subMap = resolveMapProperty(propertyPath, Collections.emptyMap()) if (!subMap.isEmpty()) { ((Map) newBuilder).putAll(subMap) } @@ -395,7 +398,9 @@ abstract class ConfigurationBuilder { def value try { - value = propertyResolver.getProperty(propertyPathForArg, argType, fallBackValue) + value = argType == Map + ? resolveMapProperty(propertyPathForArg, (Map) fallBackValue) + : propertyResolver.getProperty(propertyPathForArg, argType, fallBackValue) } catch (ConversionFailedException e) { value = handleConversionException(e, argType, propertyPathForArg, fallBackValue) } catch (ConverterNotFoundException e) { @@ -425,6 +430,168 @@ abstract class ConfigurationBuilder { } + private Map resolveMapProperty(String propertyPath, Map fallBackValue) { + if (propertyResolver instanceof DatastoreEnvironment || !(propertyResolver instanceof ConfigurableEnvironment)) { + return readDirectMap(propertyPath, fallBackValue) + } + ConfigurableEnvironment environment = (ConfigurableEnvironment) propertyResolver + String prefix = propertyPath + '.' + Map result = new LinkedHashMap<>() + Set> containers = Collections.newSetFromMap(new IdentityHashMap, Boolean>()) + containers.add(result) + boolean stored = false + for (PropertySource propertySource : environment.propertySources) { + if (!(propertySource instanceof EnumerablePropertySource)) { + continue + } + Set> builtLists = Collections.newSetFromMap(new IdentityHashMap, Boolean>()) + for (String name : ((EnumerablePropertySource) propertySource).propertyNames) { + if (name == null || !name.startsWith(prefix)) { + continue + } + String relativeName = name.substring(prefix.length()) + if (relativeName.isEmpty() || pathBlocked(result, builtLists, relativeName)) { + continue + } + Object value = readEnvironmentValue(environment, name) + if (value == null) { + continue + } + putFlattenedName(result, containers, builtLists, relativeName, value) + stored = true + } + } + if (!stored) { + // A map stored at the exact path is used only when no flattened name exists. + return readDirectMap(propertyPath, fallBackValue) + } + return result + } + + /** + * Read the exact path. ConversionFailedException and ConverterNotFoundException propagate + * from this call, which a raw environment reaches only after the walk stored nothing. + */ + private Map readDirectMap(String propertyPath, Map fallBackValue) { + Map directValue = propertyResolver.getProperty(propertyPath, Map) + if (directValue != null && !directValue.isEmpty()) { + return directValue + } + return directValue != null ? directValue : fallBackValue + } + + /** + * Read the highest precedence entry, preserving maps and lists. String values are resolved + * through the environment so its unresolvable-placeholder setting is kept. + */ + private static Object readEnvironmentValue(ConfigurableEnvironment environment, String name) { + for (PropertySource propertySource : environment.propertySources) { + Object value = propertySource.getProperty(name) + if (value == null) { + continue + } + if (!(value instanceof String)) { + return value + } + String resolved = environment.getProperty(name) + return resolved != null ? resolved : value + } + return null + } + + /** + * True when this path is already stored, or an ancestor holds a non-map. + * A stored map is not a blocking ancestor of a missing leaf. + * Only a list built from the numeric indexes of the current source accepts a later index, + * so a higher source's list replaces a lower one whole. + */ + private static boolean pathBlocked(Map result, Set> builtLists, String name) { + int bracket = indexedBracket(name) + String listPath = bracket > 0 ? name.substring(0, bracket) : name + String[] segments = listPath.split('\\.') + Map current = result + for (int i = 0; i < segments.length - 1; i++) { + if (!current.containsKey(segments[i])) { + return false + } + Object child = current.get(segments[i]) + if (!(child instanceof Map)) { + return true + } + current = (Map) child + } + String leaf = segments[segments.length - 1] + if (!current.containsKey(leaf)) { + return false + } + if (bracket < 0) { + return true + } + Object existing = current.get(leaf) + if (!(existing instanceof List) || !builtLists.contains((List) existing)) { + return true + } + int index = Integer.parseInt(name.substring(bracket + 1, name.length() - 1)) + List values = (List) existing + return index < values.size() && values.get(index) != null + } + + private static void putFlattenedName(Map result, Set> containers, + Set> builtLists, String name, Object value) { + int bracket = indexedBracket(name) + String listPath = bracket > 0 ? name.substring(0, bracket) : name + String[] segments = listPath.split('\\.') + Map current = result + for (int i = 0; i < segments.length - 1; i++) { + Object child = current.get(segments[i]) + if (child instanceof Map && !containers.contains((Map) child)) { + Map copied = new LinkedHashMap<>((Map) child) + containers.add(copied) + current.put(segments[i], copied) + child = copied + } + if (!(child instanceof Map) || !containers.contains((Map) child)) { + Map created = new LinkedHashMap<>() + containers.add(created) + current.put(segments[i], created) + child = created + } + current = (Map) child + } + String leaf = segments[segments.length - 1] + if (bracket < 0) { + current.put(leaf, value) + return + } + int index = Integer.parseInt(name.substring(bracket + 1, name.length() - 1)) + Object existing = current.get(leaf) + List values + if (existing instanceof List && builtLists.contains((List) existing)) { + values = (List) existing + } + else { + values = new ArrayList<>() + builtLists.add(values) + } + while (values.size() <= index) { + values.add(null) + } + values.set(index, value) + current.put(leaf, values) + } + + private static int indexedBracket(String name) { + int bracket = name.lastIndexOf('[') + if (bracket <= 0 || !name.endsWith(']')) { + return -1 + } + if (name.lastIndexOf('.') > bracket) { + return -1 + } + String indexText = name.substring(bracket + 1, name.length() - 1) + return indexText.matches('\\d+') ? bracket : -1 + } + protected Object newChildBuilderForFallback(Object childBuilder, Object fallbackConfig) { return childBuilder } diff --git a/grails-datastore-core/src/test/groovy/org/grails/datastore/mapping/config/ConfigurationBuilderSpec.groovy b/grails-datastore-core/src/test/groovy/org/grails/datastore/mapping/config/ConfigurationBuilderSpec.groovy index af787366710..41aaac0c2b1 100644 --- a/grails-datastore-core/src/test/groovy/org/grails/datastore/mapping/config/ConfigurationBuilderSpec.groovy +++ b/grails-datastore-core/src/test/groovy/org/grails/datastore/mapping/config/ConfigurationBuilderSpec.groovy @@ -19,6 +19,9 @@ package org.grails.datastore.mapping.config +import groovy.transform.builder.Builder +import groovy.transform.builder.SimpleStrategy + import org.grails.datastore.mapping.core.DatastoreUtils import org.grails.datastore.mapping.core.exceptions.ConfigurationException import org.grails.datastore.mapping.core.connections.ConnectionSourceSettings @@ -29,6 +32,10 @@ import org.springframework.core.convert.TypeDescriptor import org.grails.datastore.mapping.multitenancy.TenantResolver import org.grails.datastore.mapping.multitenancy.resolvers.FixedTenantResolver import org.springframework.core.env.PropertyResolver +import org.springframework.core.env.PropertySource +import org.springframework.core.env.MapPropertySource +import org.springframework.core.env.StandardEnvironment +import org.springframework.core.env.SystemEnvironmentPropertySource import org.springframework.util.ReflectionUtils import spock.lang.Specification @@ -45,6 +52,256 @@ import java.util.concurrent.TimeUnit */ class ConfigurationBuilderSpec extends Specification { + void "Test discovered dotted map properties use higher relaxed environment values"() { + given: + def environment = new StandardEnvironment() + environment.propertySources.addFirst(new MapPropertySource('low', [ + 'dataSource.properties.maximumPoolSize': 10 + ])) + environment.propertySources.addFirst(new SystemEnvironmentPropertySource('high', [ + DATASOURCE_PROPERTIES_MAXIMUMPOOLSIZE: '50' + ])) + + expect: + new MapConfigurationBuilder(environment).build().properties == [maximumPoolSize: '50'] + } + + void "Test discovered dotted map properties use higher non-enumerable values without conversion"() { + given: + def environment = new StandardEnvironment() + def values = [maximumPoolSize: '${pool.size}', options: [enabled: true], connectionInitSqls: ['SET A=1', 'SET B=2']] + environment.propertySources.addFirst(new MapPropertySource('low', [ + 'dataSource.properties.maximumPoolSize': 10, + 'dataSource.properties.options': [enabled: false], + 'dataSource.properties.connectionInitSqls': ['LOW'], + 'pool.size': '50' + ])) + environment.propertySources.addFirst(new PropertySource('high', values) { + @Override + Object getProperty(String name) { + return name.startsWith('dataSource.properties.') ? getSource().get(name.substring('dataSource.properties.'.length())) : null + } + }) + + when: + def configuration = new MapConfigurationBuilder(environment).build() + + then: + configuration.properties == [maximumPoolSize: '50', options: [enabled: true], connectionInitSqls: ['SET A=1', 'SET B=2']] + configuration.properties.connectionInitSqls.is(values.connectionInitSqls) + configuration.properties.options.is(values.options) + values.maximumPoolSize == '${pool.size}' + } + + void "Test existing maps preserve literal dotted keys at #propertyPath"() { + given: + def options = [ + 'dbms.memory.pagecache.size': '512M', + nested: ['dbms.memory.heap.max_size': '1G'] + ] + def environment = new StandardEnvironment() + environment.propertySources.addFirst(new MapPropertySource('configuration', [(propertyPath): options])) + + when: + def properties = new MapConfigurationBuilder(environment).build().properties + def stored = nested ? properties.options : properties + + then: + stored == options + stored['dbms.memory.pagecache.size'] == '512M' + stored.nested == ['dbms.memory.heap.max_size': '1G'] + options == ['dbms.memory.pagecache.size': '512M', nested: ['dbms.memory.heap.max_size': '1G']] + + where: + propertyPath | nested + 'dataSource.properties' | false + 'dataSource.properties.options' | true + } + + void "Test string placeholders follow the environment policy"() { + given: + def resolving = new StandardEnvironment() + resolving.propertySources.addFirst(new MapPropertySource('defined', [ + literal: 'ok', + 'dataSource.properties.connectionInitSql': 'SELECT ${literal}' + ])) + def lenient = new StandardEnvironment() + lenient.setIgnoreUnresolvableNestedPlaceholders(true) + lenient.propertySources.addFirst(new MapPropertySource('open', [ + 'dataSource.properties.connectionInitSql': 'SELECT ${literal}' + ])) + + expect: + new MapConfigurationBuilder(resolving).build().properties.connectionInitSql == 'SELECT ok' + new MapConfigurationBuilder(lenient).build().properties.connectionInitSql == 'SELECT ${literal}' + } + + void "Test Spring Environment rebuilds map-backed builder settings with source precedence"() { + given: + def environment = new StandardEnvironment() + environment.propertySources.addFirst(new MapPropertySource('low', [ + 'grails.gorm.hibernate.flush.mode.nested': 'low', + 'grails.gorm.hibernate.options': [size: 20, inherited: true], + 'grails.gorm.hibernate.local': 'replaced by nested map', + 'grails.gorm.hibernate.local.enabled': true + ])) + environment.propertySources.addFirst(new MapPropertySource('high', [ + 'grails.gorm.hibernate.configClass': 'configured', + 'grails.gorm.hibernate.flush.mode': 'HIGH', + 'grails.gorm.hibernate.options.size': 50, + 'grails.gorm.hibernate.connectionInitSqls[1]': 'SET B=2', + 'grails.gorm.hibernate.connectionInitSqls[0]': 'SET A=1' + ])) + + when: + def configuration = new SpringMapConfigurationBuilder(environment).build() + + then: + configuration.hibernate.getConfigClass() == 'configured' + configuration.hibernate['flush'] == [mode: 'HIGH'] + configuration.hibernate['options'] == [size: 50] + configuration.hibernate['local'] == 'replaced by nested map' + configuration.hibernate['connectionInitSqls'] == ['SET A=1', 'SET B=2'] + } + + void "Test a higher map value keeps a missing lower flattened leaf without mutating the source"() { + given: + def options = [enabled: true] + def environment = new StandardEnvironment() + environment.propertySources.addFirst(new MapPropertySource('low', [ + 'dataSource.properties.options.timeout': 1000 + ])) + environment.propertySources.addFirst(new MapPropertySource('high', [ + 'dataSource.properties.options': options + ])) + + when: + def properties = new MapConfigurationBuilder(environment).build().properties + + then: + properties.options == [enabled: true, timeout: 1000] + !properties.options.is(options) + options == [enabled: true] + } + + void "Test a higher source list blocks a lower index and an unresolved placeholder"() { + given: + def sqls = ['HIGH'] + def emptySqls = [] + def environment = new StandardEnvironment() + environment.propertySources.addFirst(new MapPropertySource('low', [ + 'dataSource.properties.sqls[1]': 'LOW', + 'dataSource.properties.emptySqls[1]': '${missing}' + ])) + environment.propertySources.addFirst(new MapPropertySource('high', [ + 'dataSource.properties.sqls': sqls, + 'dataSource.properties.emptySqls': emptySqls, + 'dataSource.properties.built[1]': 'SET B=2', + 'dataSource.properties.built[0]': 'SET A=1' + ])) + + when: + def properties = new MapConfigurationBuilder(environment).build().properties + + then: + properties.sqls.is(sqls) + properties.emptySqls.is(emptySqls) + properties.built == ['SET A=1', 'SET B=2'] + sqls == ['HIGH'] + emptySqls.isEmpty() + } + + void "Test a higher source indexed list replaces a longer lower list"() { + given: + def environment = new StandardEnvironment() + environment.propertySources.addFirst(new MapPropertySource('low', [ + 'dataSource.properties.connectionInitSqls[0]': 'SET A=1', + 'dataSource.properties.connectionInitSqls[1]': 'SET B=2', + 'dataSource.properties.connectionInitSqls[2]': 'SET C=3' + ])) + environment.propertySources.addFirst(new MapPropertySource('high', [ + 'dataSource.properties.connectionInitSqls[0]': 'SET X=9' + ])) + + when: + def properties = new MapConfigurationBuilder(environment).build().properties + + then: + properties.connectionInitSqls == ['SET X=9'] + } + + void "Test a higher map leaf is not replaced by a lower map or unresolved placeholder"() { + given: + def options = [enabled: true] + def pool = [size: 4] + def environment = new StandardEnvironment() + environment.propertySources.addFirst(new MapPropertySource('low', [ + 'dataSource.properties.options': [enabled: false], + 'dataSource.properties.options.enabled': '${missing}', + 'dataSource.properties.pool': '${missing}' + ])) + environment.propertySources.addFirst(new MapPropertySource('high', [ + 'dataSource.properties.options': options, + 'dataSource.properties.pool': pool + ])) + + when: + def properties = new MapConfigurationBuilder(environment).build().properties + + then: + properties.options.is(options) + properties.pool.is(pool) + options == [enabled: true] + pool == [size: 4] + } + + void "Test a lower circular placeholder does not replace a higher map"() { + given: + def environment = new StandardEnvironment() + environment.propertySources.addFirst(new MapPropertySource('low', [ + 'dataSource.properties': '${loop}', + loop: '${loop}' + ])) + environment.propertySources.addFirst(new MapPropertySource('high', [ + 'dataSource.properties': [x: 50] + ])) + + expect: + new MapConfigurationBuilder(environment).build().properties == [x: 50] + } + + void "Test DatastoreUtils resolver retains existing map-backed builder binding"() { + given: + String systemKey = 'grails.gorm.hibernate.minimumIdle' + String previousIdle = System.setProperty(systemKey, '3') + def source = [ + 'grails.gorm.hibernate.configClass': 'configured', + 'grails.gorm.hibernate.flush.mode': 'AUTO', + 'grails.gorm.hibernate.connectionInitSqls': ['SET A=1', 'SET B=2'] + ] + def resolver = DatastoreUtils.createPropertyResolver(source) + def original = new LinkedHashMap(resolver.getProperty('grails.gorm.hibernate', Map)) + + when: + def configuration = new SpringMapConfigurationBuilder(resolver).build() + + then: + configuration.hibernate.getConfigClass() == 'configured' + configuration.hibernate['flush.mode'] == 'AUTO' + configuration.hibernate['connectionInitSqls'] == ['SET A=1', 'SET B=2'] + configuration.hibernate['minimumIdle'] == null + resolver.getProperty('grails.gorm.hibernate', Map) == original + source.size() == 3 + + cleanup: + if (previousIdle == null) { + System.clearProperty(systemKey) + } + else { + System.setProperty(systemKey, previousIdle) + } + } + void "Test configuration builder with getter and setter"() { given:"A configuration" def map = [ @@ -560,6 +817,58 @@ class ConfigurationBuilderSpec extends Specification { configuration.mapBacked.keySet().every { it instanceof String } } + static class MapConfigurationBuilder extends ConfigurationBuilder { + + MapConfigurationBuilder(PropertyResolver propertyResolver) { + super(propertyResolver, 'dataSource') + } + + @Override + protected MapConfiguration createBuilder() { + return new MapConfiguration() + } + + @Override + protected MapConfiguration toConfiguration(MapConfiguration builder) { + return builder + } + } + + @Builder(builderStrategy = SimpleStrategy, prefix = '') + static class MapConfiguration { + + Map properties + } + + static class SpringMapConfigurationBuilder extends ConfigurationBuilder { + + SpringMapConfigurationBuilder(PropertyResolver propertyResolver) { + super(propertyResolver, Settings.PREFIX) + } + + @Override + protected SpringMapConfig createBuilder() { + return new SpringMapConfig() + } + + @Override + protected SpringMapConfig toConfiguration(SpringMapConfig builder) { + return builder + } + } + + @Builder(builderStrategy = SimpleStrategy, prefix = '') + static class SpringMapConfig { + + SpringMapSettings hibernate + } + + @Builder(builderStrategy = SimpleStrategy, prefix = '') + static class SpringMapSettings extends LinkedHashMap { + + String configClass + } + static class TestConfigurationBuilder extends ConfigurationBuilder { TestConfigurationBuilder(PropertyResolver propertyResolver) { @@ -771,3 +1080,4 @@ class ConfigurationBuilderSpec extends Specification { } } } + diff --git a/grails-datastore-web/src/main/groovy/org/grails/datastore/mapping/web/support/OpenSessionInViewInterceptor.java b/grails-datastore-web/src/main/groovy/org/grails/datastore/mapping/web/support/OpenSessionInViewInterceptor.java index dfea96d6f76..88710966939 100644 --- a/grails-datastore-web/src/main/groovy/org/grails/datastore/mapping/web/support/OpenSessionInViewInterceptor.java +++ b/grails-datastore-web/src/main/groovy/org/grails/datastore/mapping/web/support/OpenSessionInViewInterceptor.java @@ -26,8 +26,8 @@ import org.springframework.transaction.support.TransactionSynchronizationManager; import org.springframework.ui.ModelMap; +import org.springframework.web.context.request.AsyncWebRequestInterceptor; import org.springframework.web.context.request.WebRequest; -import org.springframework.web.context.request.WebRequestInterceptor; import org.grails.datastore.mapping.core.Datastore; import org.grails.datastore.mapping.core.DatastoreUtils; @@ -36,12 +36,19 @@ /** * A {@link org.springframework.web.context.request.WebRequestInterceptor} instance that - * handles opening a Datastore session within the scope of a request + * handles opening a Datastore session within the scope of a request. + * + *

A request that finds a session already bound, such as an include within a request that opened one, uses that + * session and leaves it to the one that opened it. When a request goes asynchronous, the session it opened is + * flushed and closed as when the request completes, so that it does not stay bound to the thread for the next + * request that thread serves.

*/ -public class OpenSessionInViewInterceptor implements WebRequestInterceptor { +public class OpenSessionInViewInterceptor implements AsyncWebRequestInterceptor { private static final Log LOG = LogFactory.getLog(OpenSessionInViewInterceptor.class); + private static final String PARTICIPATE_SUFFIX = ".PARTICIPATE"; + Datastore datastore; FlushModeType flushMode = FlushModeType.AUTO; @@ -55,6 +62,10 @@ public Datastore getDatastore() { public void preHandle(WebRequest webRequest) throws Exception { if (hasSessionBound()) { + // a session is already open for this request: use it, and leave it to the one that opened it + String participateAttributeName = getParticipateAttributeName(); + Integer count = (Integer) webRequest.getAttribute(participateAttributeName, WebRequest.SCOPE_REQUEST); + webRequest.setAttribute(participateAttributeName, count != null ? count + 1 : 1, WebRequest.SCOPE_REQUEST); return; } @@ -77,13 +88,7 @@ public void postHandle(WebRequest webRequest, ModelMap modelMap) throws Exceptio SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager.getResource(getDatastore()); LOG.debug("Flushing single Datastore Session in OpenSessionInViewInterceptor"); - final Session session = sessionHolder.getSession(); - - if (session.getFlushMode() == FlushModeType.AUTO) { - if (session.hasTransaction()) { - session.flush(); - } - } + flushIfNecessary(sessionHolder.getSession()); } protected boolean hasSessionBound() { @@ -91,7 +96,7 @@ protected boolean hasSessionBound() { } public void afterCompletion(WebRequest webRequest, Exception e) throws Exception { - if (!hasSessionBound()) { + if (decrementParticipateCount(webRequest) || !hasSessionBound()) { return; } @@ -101,4 +106,56 @@ public void afterCompletion(WebRequest webRequest, Exception e) throws Exception LOG.debug("Closing single Datastore Session in OpenSessionInViewInterceptor"); DatastoreUtils.closeSession(sessionHolder.getSession()); } + + /** + * Called instead of {@link #postHandle} and {@link #afterCompletion} on the thread that started asynchronous + * handling. Flushes the session this interceptor opened, as {@link #postHandle} would, and closes it, so that the + * thread goes back to its pool without a session bound. + */ + public void afterConcurrentHandlingStarted(WebRequest webRequest) { + if (decrementParticipateCount(webRequest) || !hasSessionBound()) { + return; + } + + SessionHolder sessionHolder = + (SessionHolder) TransactionSynchronizationManager.getResource(getDatastore()); + Session session = sessionHolder.getSession(); + try { + LOG.debug("Flushing single Datastore Session in OpenSessionInViewInterceptor as asynchronous handling starts"); + flushIfNecessary(session); + } + finally { + TransactionSynchronizationManager.unbindResource(getDatastore()); + LOG.debug("Closing single Datastore Session in OpenSessionInViewInterceptor as asynchronous handling starts"); + DatastoreUtils.closeSession(session); + } + } + + private void flushIfNecessary(Session session) { + if (session.getFlushMode() == FlushModeType.AUTO && session.hasTransaction()) { + session.flush(); + } + } + + /** + * @return {@code true} when this request used a session that was already open, which is then left as it is + */ + private boolean decrementParticipateCount(WebRequest webRequest) { + String participateAttributeName = getParticipateAttributeName(); + Integer count = (Integer) webRequest.getAttribute(participateAttributeName, WebRequest.SCOPE_REQUEST); + if (count == null) { + return false; + } + if (count > 1) { + webRequest.setAttribute(participateAttributeName, count - 1, WebRequest.SCOPE_REQUEST); + } + else { + webRequest.removeAttribute(participateAttributeName, WebRequest.SCOPE_REQUEST); + } + return true; + } + + private String getParticipateAttributeName() { + return OpenSessionInViewInterceptor.class.getName() + '.' + System.identityHashCode(getDatastore()) + PARTICIPATE_SUFFIX; + } } diff --git a/grails-datastore-web/src/test/groovy/org/grails/datastore/mapping/web/support/OpenSessionInViewInterceptorSpec.groovy b/grails-datastore-web/src/test/groovy/org/grails/datastore/mapping/web/support/OpenSessionInViewInterceptorSpec.groovy new file mode 100644 index 00000000000..f08533283dd --- /dev/null +++ b/grails-datastore-web/src/test/groovy/org/grails/datastore/mapping/web/support/OpenSessionInViewInterceptorSpec.groovy @@ -0,0 +1,142 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.grails.datastore.mapping.web.support + +import jakarta.persistence.FlushModeType + +import org.grails.datastore.mapping.core.Datastore +import org.grails.datastore.mapping.core.DatastoreUtils +import org.grails.datastore.mapping.core.Session +import org.grails.datastore.mapping.transactions.SessionHolder +import org.springframework.mock.web.MockHttpServletRequest +import org.springframework.transaction.support.TransactionSynchronizationManager +import org.springframework.web.context.request.ServletWebRequest +import spock.lang.Specification + +/** + * The session of a request is opened by the first interceptor call that finds none bound, used by an include within + * that request, and closed when the request completes or when it goes asynchronous. + */ +class OpenSessionInViewInterceptorSpec extends Specification { + + Datastore datastore = Mock() + Session session = Mock() + OpenSessionInViewInterceptor interceptor = new OpenSessionInViewInterceptor(datastore: datastore) + ServletWebRequest request = new ServletWebRequest(new MockHttpServletRequest('GET', '/books')) + + void setup() { + datastore.connect() >> session + session.datastore >> datastore + session.flushMode >> FlushModeType.AUTO + session.hasTransaction() >> true + } + + void cleanup() { + TransactionSynchronizationManager.resourceMap.keySet().toList().each { key -> + TransactionSynchronizationManager.unbindResource(key) + } + } + + private Session boundSession() { + ((SessionHolder) TransactionSynchronizationManager.getResource(datastore))?.session + } + + void 'a request without a session opens one, flushes it after the action and closes it when it completes'() { + when: + interceptor.preHandle(request) + + then: + boundSession().is(session) + + when: + interceptor.postHandle(request, null) + + then: + 1 * session.flush() + + when: + interceptor.afterCompletion(request, null) + + then: + 1 * session.disconnect() + boundSession() == null + } + + void 'an include uses the session of the request and leaves it bound and open'() { + given: 'the request has opened its session' + interceptor.preHandle(request) + + when: 'an include within it starts and completes' + interceptor.preHandle(request) + interceptor.postHandle(request, null) + interceptor.afterCompletion(request, null) + + then: + 0 * session.disconnect() + boundSession().is(session) + + when: 'the request completes' + interceptor.afterCompletion(request, null) + + then: + 1 * session.disconnect() + boundSession() == null + } + + void 'a request that goes asynchronous flushes and closes its session and leaves the thread without one'() { + given: + interceptor.preHandle(request) + + when: + interceptor.afterConcurrentHandlingStarted(request) + + then: + 1 * session.flush() + 1 * session.disconnect() + boundSession() == null + } + + void 'the session is closed and unbound when its flush fails as the request goes asynchronous'() { + given: + interceptor.preHandle(request) + + when: + interceptor.afterConcurrentHandlingStarted(request) + + then: + 1 * session.flush() >> { throw new IllegalStateException('flush failed') } + thrown(IllegalStateException) + 1 * session.disconnect() + boundSession() == null + } + + void 'a session that was bound before the request stays bound and open when the request goes asynchronous'() { + given: 'a session bound by the caller' + DatastoreUtils.bindSession(session) + + when: + interceptor.preHandle(request) + interceptor.afterConcurrentHandlingStarted(request) + + then: + 0 * session.flush() + 0 * session.disconnect() + boundSession().is(session) + } +} diff --git a/grails-doc/src/en/guide/async/asyncRequests.adoc b/grails-doc/src/en/guide/async/asyncRequests.adoc index 4d8990cafeb..7467dbbeaf2 100644 --- a/grails-doc/src/en/guide/async/asyncRequests.adoc +++ b/grails-doc/src/en/guide/async/asyncRequests.adoc @@ -31,6 +31,8 @@ The raw `AsyncController.startAsync()` API and its Grails-specific Servlet wrapp Both `WebPromises.task { }` and promises returned from actions honor `spring.mvc.async.request-timeout` (for example, `30s`). When unset, the Servlet container's default applies (normally 30 seconds on Tomcat). This replaces the previous infinite timeout for `WebPromises`. A timeout produces HTTP 503 (Service Unavailable), without logging an application-error stack trace. An application can customize its response with a `503` URL mapping, optionally restricted to `org.springframework.web.context.request.async.AsyncRequestTimeoutException`. The timeout ends the response lifecycle; it does not cancel the underlying task. Tasks must not continue writing to the response after timeout. +NOTE: With GORM for MongoDB, what an action saves is written when the request goes asynchronous, as when the action returns. A failure to write it then only reaches the log, though, and the response still succeeds. So an action that needs to handle a database failure itself should flush, or commit, before it returns a `Promise` or calls `startAsync()`. + ==== Async Models diff --git a/grails-doc/src/en/guide/conf/config/builtInOptions.adoc b/grails-doc/src/en/guide/conf/config/builtInOptions.adoc index e75cb409351..8d4385c2b36 100644 --- a/grails-doc/src/en/guide/conf/config/builtInOptions.adoc +++ b/grails-doc/src/en/guide/conf/config/builtInOptions.adoc @@ -35,6 +35,7 @@ On the runtime front, i.e. `grails-app/conf/application.yml`, there are quite a * `grails.views.gsp.encoding` - The file encoding used for GSP source files (default: 'utf-8'). * `grails.mime.file.extensions` - Whether to use the file extension to dictate the mime type in link:theWebLayer.html#contentNegotiation[Content Negotiation] (default: true). * `grails.mime.types` - A map of supported mime types used for link:theWebLayer.html#contentNegotiation[Content Negotiation]. +* `grails.mime.negotiation.spring` - Whether `request.format`, `response.format`, `withFormat` and the format `respond` writes in are resolved with Spring MVC's content negotiation manager, as the formats `grails.mime.types` gives the media types it resolves, rather than by the Grails rules; the `format` parameter, URL extensions and the `grails.mime` settings of the `Accept` header then also decide what Spring MVC writes for an action (default: false; true from Grails 10). See <>. * `grails.serverURL` - A string specifying the server URL portion of absolute links, including server name e.g. grails.serverURL="https://my.yourportal.com". See link:{gspTagsRef}createLink.html[createLink]. Also used by redirects. * `grails.views.gsp.layout.preprocess` - Determines whether layout preprocessing happens. Disabling this slows down page rendering, but if you need Grails' layout implementation to parse the generated HTML from a GSP view, then disabling it is the right option. Don't worry if you don't understand this advanced property: leave it set to true. * `grails.reload.excludes` and `grails.reload.includes` - Configuring these directives determines the reload behavior for project specific source files. Each directive takes a list of strings that are the class names for project source files that should be excluded from reloading behavior or included accordingly when running the application in development with the `bootRun` task. If the `grails.reload.includes` directive is configured, then only the classes in that list will be reloaded. diff --git a/grails-doc/src/en/guide/introduction/whatsNew.adoc b/grails-doc/src/en/guide/introduction/whatsNew.adoc index 94e52dc8599..592aec42553 100644 --- a/grails-doc/src/en/guide/introduction/whatsNew.adoc +++ b/grails-doc/src/en/guide/introduction/whatsNew.adoc @@ -167,7 +167,14 @@ an action annotated `@ResponseBody`, a `CompletableFuture` it returns included. selects the properties `respond` writes through Spring MVC. See xref:theWebLayer#springResponses[Returning Spring MVC Responses]. -See xref:upgrading#upgrading90x[Upgrading from Grails 8 to Grails 9], sections 4 to 11 and 13 to 16. +With `grails.mime.negotiation.spring: true`, which Grails 10 makes the default, `request.format`, `response.format`, +`withFormat` and the format `respond` writes in are resolved with Spring MVC's content negotiation manager, so that the +`spring.mvc.contentnegotiation.*` settings apply to them, and the `format` parameter, URL extensions and the +`grails.mime` settings of the `Accept` header decide what Spring MVC writes for an action too, such as a +`ResponseEntity` it returns. See <>. + +See xref:upgrading#upgrading90x[Upgrading from Grails 8 to Grails 9], sections 6 to 13 and 15 to 18, and +<>. ==== Hibernate 7 Criteria Queries Can Be Extended @@ -593,6 +600,23 @@ GORM for Hibernate 7 now starts tracking changes to an entity when it is persist As part of this change, persistence event listeners on Hibernate 7 receive a `PersistEvent` (`eventType` `Persist`) for every explicit or flush-time cascaded persist and a `MergeEvent` (`eventType` `Merge`) for every merge. Neither event can be cancelled. Hibernate 5 continues to publish `SaveOrUpdateEvent` when an entity is saved, and publishes no event for a merge. A listener that switches over `event.eventType` with a default branch that throws should handle the new values. See the Events and Auto Timestamping section of the GORM for link:{hibernate7Guide}index.html#eventsAutoTimestamping[Hibernate 7] guide. +==== A Domain Class Answers with Its Auto-Timestamp Listener + +Every datastore has an `AutoTimestampEventListener` of its own, and so does every data source of an application with several and every tenant with a database or schema of its own. A listener only handles the saves made through its own datastore, so the `autoTimestampEventListener` bean, which belongs to the default data source, cannot turn off the timestamps of a domain class saved to another one. A domain class now answers with the listener that handles its saves: + +[source,groovy] +---- +// Car is mapped to the default data source and Person to the 'people' data source +Car.autoTimestampEventListener.withoutTimestamps { + car.save(flush: true) +} +Person.autoTimestampEventListener.withoutTimestamps { + person.save(flush: true) +} +---- + +The lookup follows the data source a save made from the same place goes to. For a `Book` mapped to several data sources, inside a `Book.secondary.withTransaction { }` block, or a method annotated `@Transactional(connection = 'secondary')`, it is the `secondary` data source's listener, and `Book.secondary.autoTimestampEventListener` names that data source anywhere. For a multi-tenant class it is the current tenant's. This works with GORM for Hibernate 7, Hibernate 5, MongoDB and Neo4j, and in unit tests. See the Events and Auto Timestamping section of the GORM for link:{hibernate7Guide}index.html#eventsAutoTimestamping[Hibernate 7] guide. + ==== Default Security Response Headers Servlet web applications now send a baseline set of browser hardening headers on every response: diff --git a/grails-doc/src/en/guide/theWebLayer/contentNegotiation.adoc b/grails-doc/src/en/guide/theWebLayer/contentNegotiation.adoc index e878767b4e7..77317acd7f5 100644 --- a/grails-doc/src/en/guide/theWebLayer/contentNegotiation.adoc +++ b/grails-doc/src/en/guide/theWebLayer/contentNegotiation.adoc @@ -263,6 +263,96 @@ Note the inclusion of the `format` variable in the path. If you do not wish to u +[[springFormatNegotiation]] +==== Resolving Formats with Spring MVC Content Negotiation + +Grails resolves `request.format`, `response.format`, `withFormat` and the format `respond` writes in by the rules this +section describes, while Spring MVC's content negotiation manager decides the type of what Spring MVC writes for an +action: a `ResponseEntity`, a `ProblemDetail` or another value an action returns (see +<>), a problem `respond` writes, and the JSON `respond` writes with +Spring MVC. The two can answer one request differently. Set `grails.mime.negotiation.spring` to `true` to resolve the +Grails formats with Spring MVC's manager too: + +[source,yaml] +.grails-app/conf/application.yml +---- +grails: + mime: + negotiation: + spring: true +---- + +Grails {grailsMajorVersion} keeps the Grails rules by default, and Grails 10 makes the setting the default. With the +setting: + +* The `Accept` header is read by Spring MVC's manager, with its strategies, so the `spring.mvc.contentnegotiation.*` +properties apply to the Grails formats as they do to Spring MVC, and its media types are taken in the order Spring MVC +takes them: by quality, then a more specific type ahead of a less specific one, then in the order the header lists them, +except that a type with the `+xml` suffix comes ahead of the XML type of equal quality, as the Grails rules take it. +`Accept: */*, application/json` is the `json` format, where the Grails rules give `all`, and +`Accept: application/xml, application/atom+xml` is `atom`, as they give. +* Each media type is the format `grails.mime.types`, or a `MimeTypeProvider` bean such as a renderer, gives it. A type +with the `+json` or `+xml` suffix that no configured type names, such as `application/merge-patch+json` or +`application/problem+xml`, is the `json` or the `xml` format, as Spring MVC's JSON and XML converters write such types. +* `*/*` is the `all` format, also where `grails.mime.types` does not list it. So is another wildcard, such as `text/*`, +for which `withFormat` runs its `*` block or else the first block of a format with a type compatible with the wildcard, +and `respond` answers in the first of the action's formats with such a type, as Spring MVC answers a wildcard with the +first of its candidates that is compatible with it. +* A request that accepts no configured type, such as one that sends only `Accept: image/png`, or whose `Accept` header +Spring cannot read, has no format: `response.format` and `response.mimeType` are `null`, `respond` answers +`406 Not Acceptable`, and `withFormat` runs its `*` block, or no block. The Grails rules answer such a request as one +that accepts any type, with the `all` format. +* The `format` parameter and a URL extension name the format, ahead of the `Accept` header, as before. The first value +of the parameter counts, as for Spring MVC; a format no type is configured for is not acceptable, where the Grails rules +answer in the first configured format, but what Spring MVC writes for a format `grails.mime.types` does not list and +`spring.mvc.contentnegotiation.media-types` maps is in the type that maps it, while `respond` answers it with +`406 Not Acceptable`; and the `all` format, which is any type whether or not `grails.mime.types` lists +it, leaves the format to the `Accept` header. Where `spring.mvc.contentnegotiation.favor-parameter` has Spring MVC read +the `format` parameter too, Grails reads it in its place for a request a URL mapping handles, so `?format=all` is left +to the `Accept` header there as well, while a Spring MVC endpoint reads it as Spring MVC does. +* `grails.mime.use.accept.header` and `grails.mime.disable.accept.header.*` ignore the `Accept` header as before. +* The `format` parameter, the URL extension and the `Accept` header settings also decide what Spring MVC writes for a +request a URL mapping handles, so that it is in the format Grails resolves for the request: an action that returns a +`ResponseEntity` of a map answers `/book/show/1.json` in JSON whatever the `Accept` header asks for, and +`/book/show/1.html` with `406 Not Acceptable`, since no message converter writes a map as HTML. +* `request.format` reads the `Content-Type` header as Spring reads it, as one media type, which is the format the +`Accept` header would make it: a body sent as `application/merge-patch+json` is of the `json` format, and data binding +reads it as JSON. A type no configured type stands for, or that Spring cannot read, is of no format, and a request +without a `Content-Type` is of the first configured format, as before. + +A Spring MVC endpoint, such as an `@RestController` or an actuator endpoint, and Spring Security, whose filters negotiate +before a URL mapping has matched the request, negotiate as before: the setting changes no answer Spring MVC gives a request +no URL mapping handles. Each rule of Grails' negotiation becomes: + +[cols="2,3"] +|=== +|Grails rule |With `grails.mime.negotiation.spring` + +|The `format` parameter and URL extensions select the format +|Kept, ahead of Spring MVC's strategies, and also for what Spring MVC writes for the request, which a format only +`spring.mvc.contentnegotiation.media-types` maps decides too. A format neither maps is not acceptable, and the `all` +format leaves the choice to the `Accept` header. + +|`grails.mime.use.accept.header` and `grails.mime.disable.accept.header.*` ignore the `Accept` header +|Kept, also for what Spring MVC writes for the request. + +|`text/xml` and `application/xml` are merged into one `xml` type +|Dropped: both are the `xml` format, which comes in Spring MVC's order. + +|A type with the `+xml` suffix comes ahead of `application/xml` of equal quality +|Kept, also for what Spring MVC writes for the request. + +|The problem types of RFC 9457 are the `json` and `xml` formats +|Extended to any type with the `+json` or `+xml` suffix that no configured type names. + +|A request that accepts no configured type is answered as one that accepts any type, and one that names a format no +type is configured for in the first configured format +|Dropped: such a request, and one whose `Accept` header Spring cannot read, is not acceptable. +|=== + +<>, in the guide to +upgrading from Grails 8 to Grails 9, lists how each answer changes. + ==== Testing Content Negotiation diff --git a/grails-doc/src/en/guide/upgrading/upgrading70x.adoc b/grails-doc/src/en/guide/upgrading/upgrading70x.adoc index e01c067dc1e..85825bb3775 100644 --- a/grails-doc/src/en/guide/upgrading/upgrading70x.adoc +++ b/grails-doc/src/en/guide/upgrading/upgrading70x.adoc @@ -1324,3 +1324,30 @@ As of Grails 7.0.18, with `grails.plugin.springsecurity.rest.token.storage.jwt.u * For an encrypted JWT, `JwtService.parse` and the `accessTokenJwt` property of an `AccessToken` now return a `grails.plugin.springsecurity.rest.token.generation.jwt.NestedJwt` instead of a `com.nimbusds.jwt.EncryptedJWT`. Its claims are those of the signed JWT inside, its header is that of the encrypted JWT, and it serializes to the token. * With encrypted JWTs enabled, a signed JWT is rejected as an invalid token, and with signed JWTs, an encrypted JWT is. Previously such a token failed with a `NullPointerException`. * An encrypted JWT whose content encryption key is encrypted with `RSA1_5` is rejected unless `grails.plugin.springsecurity.rest.token.generation.jwt.jweAlgorithm` is `RSA1_5`. The plugin passes that setting to the new `jweAlgorithm` property of `JwtService`. A `jwtService` bean that an application defines itself accepts `RSA1_5` as before, unless the application sets that property too. + +==== More GSP tags HTML-encode the attribute values they write + +These tags now HTML-encode the values they write into attributes, as `` and `` already did and as these tags already encoded their other attributes: + +* ``, `` and ``: the URL in `action` and `formaction`, and the hidden fields that a `RequestDataValueProcessor` adds to a form. +* ``: `name`, `value`, `form`, and the `id` taken from the name, on the checkbox and on its hidden field. +* `` and ``: `value`, the `action` in the button's name, and `src`. +* ``: `name`, `id` and `selectDateClass`, on the labels, the hidden field and each select. +* `` and ``: `name`, and on `` the `id` taken from it. +* ``: the key of `noSelection`. +* ``: `type`. +* ``: the values of `attrs` that are not closures. +* ``: `uri`, and ``: `src`. +* ``: `class`, on the current step. +* ``: `title`, which it writes as the text of the column's link. A text from `titleKey` is written as the message source returns it. Mark a `title` that holds markup with `raw()`. +* ``: `detailsClass`, `stackClass`, `filenameClass`, `snippetClass`, `lineClass`, `lineNumberClass` and `lineErrorClass`. + +The browser decodes the attribute, so a value without a character reference reaches it unchanged. The markup changes, though: an `&` in the URL of a form is written as `+&+`, as `` writes it in an `href`. + +A value encoded with `encodeAsHTML()` before it reaches the tag is written once, not encoded again. That holds for a String, for a value captured from a GSP body with ``, and for the output of a tag such as `fieldValue()`, also when a `RequestDataValueProcessor` returns the value unchanged. When the processor changes the value, the tag encodes what the processor returns, so a value encoded beforehand is encoded twice, as `` already did. There is one more exception: `` and `` build their URL from `uri`, `base`, `controller` and their other link attributes and only then encode it, so a value encoded beforehand is encoded twice there, as it already was in the `href` of ``. Pass those values unencoded. A `url` attribute that the tag writes as it is given is written once. A character reference written in the page, as in `+name="a&b"+`, is encoded as well: write the character itself. + +A value marked with `raw()` is still written as given by the tags that wrote it so before, and so are the text and the `default` of `message()`, which comes back marked raw. Where a `RequestDataValueProcessor` is registered, `` and the other field tags encode what the processor returns, as before, and `` encodes such a value in any case, as before. Do not pass request data through `raw()` or as the `default` of `message()`. + +`` now encodes its `value` once. It encoded it twice, so a value with `&`, `<`, `>`, `"` or `'` was submitted with character references in it, such as `+R&D+` for `R&D`. + +A value encoded beforehand is no longer encoded twice in the `value` of ``, `` and the other field tags when a `RequestDataValueProcessor` returns it unchanged, nor in an option of ``, with or without a processor. `` and `` write a `class` that was encoded beforehand once next to the classes they add, such as `step` and `sortable`; they encoded it twice. diff --git a/grails-doc/src/en/guide/upgrading/upgrading80x.adoc b/grails-doc/src/en/guide/upgrading/upgrading80x.adoc index 2462e23aafe..7df0f0b7b79 100644 --- a/grails-doc/src/en/guide/upgrading/upgrading80x.adoc +++ b/grails-doc/src/en/guide/upgrading/upgrading80x.adoc @@ -5007,3 +5007,30 @@ As of Grails 8.0.1, with `grails.plugin.springsecurity.rest.token.storage.jwt.us * For an encrypted JWT, `JwtService.parse` and the `accessTokenJwt` property of an `AccessToken` now return a `grails.plugin.springsecurity.rest.token.generation.jwt.NestedJwt` instead of a `com.nimbusds.jwt.EncryptedJWT`. Its claims are those of the signed JWT inside, its header is that of the encrypted JWT, and it serializes to the token. * With encrypted JWTs enabled, a signed JWT is rejected as an invalid token, and with signed JWTs, an encrypted JWT is. Previously such a token failed with a `NullPointerException`. * An encrypted JWT whose content encryption key is encrypted with `RSA1_5` is rejected unless `grails.plugin.springsecurity.rest.token.generation.jwt.jweAlgorithm` is `RSA1_5`. The plugin passes that setting to the new `jweAlgorithm` property of `JwtService`. A `jwtService` bean that an application defines itself accepts `RSA1_5` as before, unless the application sets that property too. + +==== More GSP tags HTML-encode the attribute values they write + +These tags now HTML-encode the values they write into attributes, as `` and `` already did and as these tags already encoded their other attributes: + +* ``, `` and ``: the URL in `action` and `formaction`, and the hidden fields that a `RequestDataValueProcessor` adds to a form. +* ``: `name`, `value`, `form`, and the `id` taken from the name, on the checkbox and on its hidden field. +* ``: `value`, the `action` in the button's name, and `src`. +* ``: `name`, `id` and `selectDateClass`, on the labels, the hidden field and each select. +* `` and ``: `name`, and on `` the `id` taken from it. +* ``: the key of `noSelection`. +* ``: `type`. +* ``: the values of `attrs` that are not closures. +* ``: `uri`, and ``: `src`. +* ``: `class`, on the current step. +* ``: `title`, which it writes as the text of the column's link. A text from `titleKey` is written as the message source returns it. Mark a `title` that holds markup with `raw()`. +* ``: `detailsClass`, `stackClass`, `filenameClass`, `snippetClass`, `lineClass`, `lineNumberClass` and `lineErrorClass`. + +The browser decodes the attribute, so a value without a character reference reaches it unchanged. The markup changes, though: an `&` in the URL of a form is written as `+&+`, as `` writes it in an `href`. + +A value encoded with `encodeAsHTML()` before it reaches the tag is written once, not encoded again. That holds for a String, for a value captured from a GSP body with ``, and for the output of a tag such as `fieldValue()`, also when a `RequestDataValueProcessor` returns the value unchanged. When the processor changes the value, the tag encodes what the processor returns, so a value encoded beforehand is encoded twice, as `` already did. There is one more exception: `` and `` build their URL from `uri`, `base`, `controller` and their other link attributes and only then encode it, so a value encoded beforehand is encoded twice there, as it already was in the `href` of ``. Pass those values unencoded. A `url` attribute that the tag writes as it is given is written once. A character reference written in the page, as in `+name="a&b"+`, is encoded as well: write the character itself. + +A value marked with `raw()` is still written as given by the tags that wrote it so before, and so are the text and the `default` of `message()`, which comes back marked raw. Where a `RequestDataValueProcessor` is registered, `` and the other field tags encode what the processor returns, as before, and `` encodes such a value in any case, as before. Do not pass request data through `raw()` or as the `default` of `message()`. + +`` now encodes its `value` once. It encoded it twice, so a value with `&`, `<`, `>`, `"` or `'` was submitted with character references in it, such as `+R&D+` for `R&D`. + +A value encoded beforehand is no longer encoded twice in the `value` of ``, `` and the other field tags when a `RequestDataValueProcessor` returns it unchanged, nor in an option of ``, with or without a processor. `` and `` write a `class` that was encoded beforehand once next to the classes they add, such as `step` and `sortable`; they encoded it twice. diff --git a/grails-doc/src/en/guide/upgrading/upgrading90x.adoc b/grails-doc/src/en/guide/upgrading/upgrading90x.adoc index 91ad70e5588..e9f6a256b45 100644 --- a/grails-doc/src/en/guide/upgrading/upgrading90x.adoc +++ b/grails-doc/src/en/guide/upgrading/upgrading90x.adoc @@ -131,6 +131,11 @@ The route is now set before the Grails interceptors run, so that a failure in on An interceptor whose `before()` returns `false`, to render a 403 or to redirect, stopped the request before its route was set, so the request was reported with a `uri` of `UNKNOWN`, or `REDIRECTION` for a redirect. It is now reported under the route of the action, so a dashboard or alert keyed on those `uri` values sees those requests move. +* *On Undertow, a request reads as its own dispatch again once a forward returns.* +Undertow left a request reading as a forward once a forward to a path had returned, as Tomcat and Jetty do not. +Spring Framework 7.1's observation filter then took an asynchronous request that had forwarded, for example to an error controller, for a plain one, and reported its failure twice. +`grails-undertow` now restores the dispatcher type when the forward returns, so a filter that reads `getDispatcherType()` once its chain has returned sees `REQUEST` or `ASYNC` rather than `FORWARD`, as it does on Tomcat and Jetty. + * *`http.server.requests.active` is no longer registered by default.* Spring Boot 4.2 makes this long task timer opt-in. A dashboard or alert that reads it sets `management.metrics.observations.include-active-long-task-timer` to `true`, which replaces `management.metrics.observations.ignored-meters`. @@ -1483,3 +1488,124 @@ With GORM for MongoDB, a class that is not multi-tenant and was saved inside a t * When the instances are shared by every tenant and no multi-tenant class references them, move the documents from the tenants' databases into the default database. Data services choose their transaction manager in code generated at compile time, so a data service compiled against an earlier release keeps following the tenant until it is compiled again. + +==== 26. Unit Tests Run Domain Class Events for Every Data Source and Tenant + +In a unit test that implements `DataTest` or `DomainUnitTest`, a save through a data source other than the default one, as `book.secondary.save()` makes, and a save for a tenant with a database or a schema of its own, now run the domain class's events, such as `beforeInsert`, and set its `dateCreated` and `lastUpdated` properties, as they do in the application. In Grails 8 they ran neither. Unit tests still do not set the `@CreatedBy` and `@LastModifiedBy` properties, on any data source, even when the test defines an `AuditorAware` bean, as in Grails 8: the in-memory datastore is not given the application context, so note 28 does not apply to them. + +===== Required Actions + +* A test that expected such a save to keep the `dateCreated` and `lastUpdated` it was given saves inside a `withoutTimestamps { }` block of that data source's listener, such as `Book.secondary.autoTimestampEventListener`. +* A test that expected a domain class event not to run on such a save expects what the event does. +* The in-memory datastore of unit tests now follows `grails.gorm.events.autoTimestampInsertOverwrite` and `grails.gorm.autoTimestampCacheAnnotations` on every data source, the default one included, as the application does. In an application whose configuration sets `autoTimestampInsertOverwrite: false`, a unit test that expected a save to overwrite the `dateCreated` or `lastUpdated` it was given expects them kept. + +==== 27. A Domain Class Has an `autoTimestampEventListener` Property + +Every domain class now has a static `autoTimestampEventListener` property, the listener that sets its timestamp and auditor properties (see the Events and Auto Timestamping section of the GORM for link:{hibernate7Guide}index.html#eventsAutoTimestamping[Hibernate 7] guide). A domain class that declares a property of that name of another type no longer compiles: the compiler reports that the return type of its `getAutoTimestampEventListener()` is incompatible with the one in `GormEntity`. A property of that name typed `AutoTimestampEventListener`, such as an injected bean, compiles but hides the static property, so `Book.autoTimestampEventListener` throws `MissingPropertyException`. + +===== Required Actions + +* Rename a property of a domain class named `autoTimestampEventListener`. Code that used an injected `autoTimestampEventListener` bean in a domain class reads `Book.autoTimestampEventListener` instead, which is the listener of the data source the class's saves go to. + +==== 28. Every Data Source Sets the Auditor Properties and Follows the Auto-Timestamp Settings + +In an application with several data sources, or with a database or a schema per tenant, a save through a data source other than the default one: + +* sets the `@CreatedBy` and `@LastModifiedBy` properties from the application's `AuditorAware`. In Grails 8 only a save through the default data source set them. +* follows `grails.gorm.events.autoTimestampInsertOverwrite` and `grails.gorm.autoTimestampCacheAnnotations`. In Grails 8 it overwrote a `dateCreated` or `lastUpdated` that an instance was saved with, whatever `autoTimestampInsertOverwrite` said. +* with GORM for Neo4j, runs the domain class's events, such as `beforeInsert`, and sets `dateCreated` and `lastUpdated`. In Grails 8 a save through another Neo4j connection ran neither. + +===== Required Actions + +For most applications **no changes are required**. Check the domain classes saved through a data source other than the default one whose auditor properties, given timestamps or, with Neo4j, events were expected to be left alone. + +==== 29. JLine 4 + +Grails 9 manages JLine 4 (`org.jline:jline`), the line Groovy 6's `groovysh` is built on, in place of JLine 3. The link:{versionsRef}Grails%20BOM.html[Grails BOM dependency table] has the exact version. JLine is the terminal library behind the interactive prompts of the `grails` command line tools and of `grails shell`, and behind `GrailsConsole`, which application and plugin commands write to. The line reader, terminal and completer APIs keep their `org.jline` packages. + +===== Required Actions + +* Recompile code of your own that uses JLine, such as an `org.jline.reader.Completer` for a command, against JLine 4. JLine 4 removed APIs that JLine 3 had deprecated, so code that uses them no longer compiles. For example, use `org.jline.builtins.Completers.FileNameCompleter` in place of `org.jline.reader.impl.completer.FileNameCompleter`, and `Terminal.inputEncoding()` and `outputEncoding()` in place of `stdinEncoding()`, `stdoutEncoding()` and `stderrEncoding()`. `TerminalBuilder.codepage(int)` and the `TerminalBuilder.PROP_JNA` and `PROP_JANSI` constants are gone too. +* JLine 4 has no JNA or Jansi terminal provider. Remove a dependency on `org.jline:jline-terminal-jna` or `org.jline:jline-terminal-jansi`: neither has a JLine 4 version, and JLine finds the terminal through its remaining providers. + +picocli's `info.picocli:picocli-shell-jline3` runs on JLine 4, but picocli has no JLine 4 version of it, and its `PicocliCommands` implements `org.jline.console.CommandRegistry`, which JLine 4 deprecates. JLine's own picocli bridge, `org.jline:jline-picocli`, is built for JLine 4. To move to it, use it at the JLine version the Grails BOM manages. + +==== 30. Command Lines of Application Commands + +The command line parser of the `grails` command line tool and of `runCommand`, which reads the command line of an application or plugin command, reads two kinds of input differently: + +* An empty argument after an option without a value is that option's value, an empty one. `--name "" value` used to give the option `name` the value `value`; now `name` is empty, and `value` is an argument. +* The text inside a pair of double quotes is trimmed, as an argument without quotes always was. `" a "` used to reach the command as ` a `, and now reaches it as `a`. + +It also reads three kinds of input it used to fail on or cut short: + +* `--name=`, an option with an empty value, threw an `ArrayIndexOutOfBoundsException`, and is now the option `name` with an empty value. +* An argument that is a single `"` threw a `StringIndexOutOfBoundsException`, and is now passed on as it is. +* The value of an option is everything after the first `=`: `--filter=status=active` used to give `status`, and now gives `status=active`. + +===== Required Actions + +* Check commands that read an option followed by an empty argument, or arguments with spaces inside quotes, and expect the values above. +* Remove workarounds for the three failures, such as passing `--filter=status%3Dactive` and decoding it in the command. + +[[upgradingSpringFormatNegotiation]] +==== 31. Opt Into Spring MVC Content Negotiation for Grails Formats + +Set `grails.mime.negotiation.spring: true` to resolve `request.format`, `response.format`, `withFormat` and the format +`respond` writes in with Spring MVC's content negotiation manager, and to have the `format` parameter, URL extensions and +the `grails.mime` settings of the `Accept` header decide what Spring MVC writes for an action too (see +<>). Grails 9 keeps its own rules by +default, and Grails 10 makes the setting the default, so an application should check these answers before then. + +[source,yaml] +.grails-app/conf/application.yml +---- +grails: + mime: + negotiation: + spring: true +---- + +The setting changes these answers, which the framework's tests record by running the same requests both ways: + +* *The order of the `Accept` header.* Spring MVC takes a more specific type ahead of a less specific one of equal +quality, where the Grails rules keep the order of the header: `Accept: */*, application/json` is the `json` format, +where it is `all` without the setting, and `withFormat` runs the `json` block rather than its first. A type with the +`+xml` suffix still comes ahead of the XML type of equal quality: `Accept: application/xml, application/atom+xml` is +`atom` either way, and Spring MVC now writes what an action returns in that order too. +* *Types with the `+json` and `+xml` suffixes.* A type no configured type names, such as `application/merge-patch+json`, +`application/soap+xml` or, without a `hal` format, `application/hal+json`, is the `json` or `xml` format, where the +Grails rules read only the problem types of RFC 9457 so and answer any other as `all`. In `Content-Type` too: a body sent +as such a type is of the `json` format, where its `request.format` is otherwise `null`, and data binding reads it as JSON. +* *A request that accepts no configured type.* Such a request, as one that sends only `Accept: image/png`, and one whose +`Accept` header Spring cannot read, such as `Accept: application/json;q=abc`, is not acceptable: `response.format` and +`response.mimeType` are `null`, `respond` answers `406 Not Acceptable` and `withFormat` runs its `*` block, or no block, +where the Grails rules answer as for a client that accepts any type, with the `all` format, or read the header +leniently. Code that reads `response.format` or `response.mimeType` must allow for `null`. +* *The `format` parameter and URL extensions.* A format no type is configured for is not acceptable, to `respond`, which +answers `406`, and to what Spring MVC writes, where the Grails rules answer in the first configured format; +`withFormat` answers it from the `Accept` header, as for a format it has no block for. A format that +`spring.mvc.contentnegotiation.media-types` maps, such as `plain: text/plain`, decides what Spring MVC writes, as +without the setting where `spring.mvc.contentnegotiation.favor-parameter` is on, while `respond` still answers `406` +for it. The `all` format leaves the format to the `Accept` header: `?format=all` with `Accept: application/json` is `json`, where it is otherwise `all`. The +first value of the parameter counts. +* *What Spring MVC writes for an action.* The `format` parameter and URL extensions now decide the type of a +`ResponseEntity`, a `ProblemDetail` or another value Spring MVC writes for a request a URL mapping handles, where Spring +MVC read only the `Accept` header: `/book/show/1.json` is answered in JSON whatever the `Accept` header asks for, and +`/book/show/1.html` of an action that returns a map with `406`, since no message converter writes a map as HTML. +`grails.mime.use.accept.header: false` and `grails.mime.disable.accept.header.*` now ignore the `Accept` header for it +too. +* *Without the `all` format.* Where `grails.mime.types` does not list `all`, a request without an `Accept` header is of +the `all` format, as one that sends `Accept: */*` is, where the Grails rules give the first configured format, so +`withFormat` runs its `*` block, or its first block, rather than the block of that format. A type of quality `0` comes +after any other, so `Accept: application/json;q=0, */*;q=0.1` is `all`, where it is otherwise `json`. `?format=all` +leaves the format to the `Accept` header there too, where the Grails rules give the first configured format. +* *Wildcards.* A wildcard such as `text/*` is the `all` format as before, but `withFormat` runs the first block of a +format with a type compatible with it, and `respond` answers in the first such format of the action, where the Grails +rules run the first block and answer in the first format: `withFormat { pdf {...}; html {...} }` runs the `html` block for +`Accept: text/*`. +* *A `Content-Type` Spring cannot read.* A `Content-Type` Spring rejects, such as `application/json;charset=` or a list +of types, is of no format, where the Grails rules read it leniently as `json`. + +Other requests resolve as before, among them those of browsers and those that name one configured type. A Spring MVC +endpoint, and Spring Security's filters before a URL mapping has matched a request, negotiate as before. diff --git a/grails-doc/src/en/ref/Tags - GSP/sortableColumn.adoc b/grails-doc/src/en/ref/Tags - GSP/sortableColumn.adoc index dc5584f76fd..a996e165c4b 100644 --- a/grails-doc/src/en/ref/Tags - GSP/sortableColumn.adoc +++ b/grails-doc/src/en/ref/Tags - GSP/sortableColumn.adoc @@ -57,8 +57,8 @@ Attributes * `property` - name of the property relating to the field * `defaultOrder` (optional) - default order for the property; choose between 'asc' (default if not provided) and 'desc' -* `title` (optional) - title caption for the column -* `titleKey` (optional) - title key to use for the column, resolved against the message source +* `title` (optional) - title caption for the column. It is HTML-encoded; mark a caption that holds markup with `raw()`. +* `titleKey` (optional) - title key to use for the column, resolved against the message source. The message is written as the message source returns it. * `params` (optional) - a Map containing request parameters * `action` (optional) - the name of the action to use in the link; if not specified the `list` action will be used * `namespace` (optional) - the namespace of the controller to use in the link. If omitted, the namespace is inferred from the target controller; you only need to set it when more than one controller shares the name. Use `namespace=""` to target a non-namespaced controller. diff --git a/grails-forge/gradle/wrapper/gradle-wrapper.properties b/grails-forge/gradle/wrapper/gradle-wrapper.properties index b60350078ef..a46e1bafcb5 100644 --- a/grails-forge/gradle/wrapper/gradle-wrapper.properties +++ b/grails-forge/gradle/wrapper/gradle-wrapper.properties @@ -2,7 +2,7 @@ # and grails-forge/grails-forge-core/grails-app/views/forge/feature/build/gradle/templates/gradleWrapperProperties.gsp distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-9.9.1-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-9.9.2-bin.zip networkTimeout=10000 retries=0 retryBackOffMs=500 diff --git a/grails-forge/grails-forge-cli/build.gradle b/grails-forge/grails-forge-cli/build.gradle index 7e324417c1f..aab6c01bf02 100644 --- a/grails-forge/grails-forge-cli/build.gradle +++ b/grails-forge/grails-forge-cli/build.gradle @@ -98,11 +98,10 @@ dependencies { compileOnly 'org.apache.grails.views:grails-web-gsp' compileOnly 'jakarta.servlet:jakarta.servlet-api' implementation "info.picocli:picocli:$picocliVersion" - implementation "info.picocli:picocli-shell-jline3:$picocliVersion" implementation 'jakarta.inject:jakarta.inject-api' // InteractiveShell drives JLine directly, and since the ansi stream install was dropped it also - // relies on JLine's terminal for virtual-terminal processing; pin it rather than inheriting - // whatever version picocli-shell-jline3 happens to bring + // relies on JLine's terminal for virtual-terminal processing. CommandLineCompleter connects it to + // picocli's completion itself: picocli's own bridge, picocli-shell-jline3, is built for JLine 3. implementation 'org.jline:jline' implementation 'org.yaml:snakeyaml' runtimeOnly 'org.slf4j:slf4j-nop' diff --git a/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/CommandLineCompleter.groovy b/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/CommandLineCompleter.groovy new file mode 100644 index 00000000000..f33d061bef6 --- /dev/null +++ b/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/CommandLineCompleter.groovy @@ -0,0 +1,51 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.grails.forge.cli + +import groovy.transform.CompileStatic + +import org.jline.reader.Candidate +import org.jline.reader.Completer +import org.jline.reader.LineReader +import org.jline.reader.ParsedLine +import picocli.AutoComplete +import picocli.CommandLine.Model.CommandSpec + +/** + * Completes the word under the cursor at the interactive prompt from the picocli commands, their + * subcommands and their options. + */ +@CompileStatic +class CommandLineCompleter implements Completer { + + private final CommandSpec spec + + CommandLineCompleter(CommandSpec spec) { + this.spec = Objects.requireNonNull(spec, 'spec') + } + + @Override + void complete(LineReader reader, ParsedLine line, List candidates) { + List suggestions = [] + AutoComplete.complete(spec, line.words().toArray(new String[0]), line.wordIndex(), 0, line.cursor(), suggestions) + suggestions.each { CharSequence suggestion -> + candidates.add(new Candidate(suggestion.toString())) + } + } +} diff --git a/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/InteractiveShell.groovy b/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/InteractiveShell.groovy index 7300eb20f0a..8d86582208c 100644 --- a/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/InteractiveShell.groovy +++ b/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/InteractiveShell.groovy @@ -29,7 +29,6 @@ import org.jline.reader.impl.DefaultParser import org.jline.terminal.Terminal import org.jline.terminal.TerminalBuilder import picocli.CommandLine -import picocli.shell.jline3.PicocliJLineCompleter import java.util.function.BiFunction import java.util.function.Consumer @@ -63,11 +62,10 @@ class InteractiveShell { void start() { try { - PicocliJLineCompleter picocliCommands = new PicocliJLineCompleter(commandLine.getCommandSpec()) Terminal terminal = TerminalBuilder.terminal() LineReader reader = LineReaderBuilder.builder() .terminal(terminal) - .completer(picocliCommands) + .completer(new CommandLineCompleter(commandLine.commandSpec)) .parser(new DefaultParser()) .variable(LineReader.LIST_MAX, 50) // max tab completion candidates .build() diff --git a/grails-forge/grails-forge-cli/src/test/groovy/org/grails/forge/cli/CommandLineCompleterSpec.groovy b/grails-forge/grails-forge-cli/src/test/groovy/org/grails/forge/cli/CommandLineCompleterSpec.groovy new file mode 100644 index 00000000000..2139ee616aa --- /dev/null +++ b/grails-forge/grails-forge-cli/src/test/groovy/org/grails/forge/cli/CommandLineCompleterSpec.groovy @@ -0,0 +1,89 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.grails.forge.cli + +import org.jline.reader.Candidate +import org.jline.reader.LineReader +import org.jline.reader.LineReaderBuilder +import org.jline.reader.impl.DefaultParser +import org.jline.terminal.Terminal +import org.jline.terminal.TerminalBuilder +import org.springframework.context.ConfigurableApplicationContext +import picocli.CommandLine +import spock.lang.AutoCleanup +import spock.lang.Shared +import spock.lang.Specification + +class CommandLineCompleterSpec extends Specification { + + @Shared + @AutoCleanup + ConfigurableApplicationContext context = Application.builder().run() + + @Shared + CommandLine commandLine = ForgeCli.createCommandLine(context, true) + + @Shared + @AutoCleanup + Terminal terminal = TerminalBuilder.builder() + .type(Terminal.TYPE_DUMB) + .streams(new ByteArrayInputStream(new byte[0]), new ByteArrayOutputStream()) + .build() + + void 'an empty prompt offers every command and the options of the root command'() { + expect: + complete('').containsAll(['create-app', 'create-webapp', 'create-restapi', 'create-plugin', 'create-web-plugin', + '--help', '--version']) + } + + void 'a partly typed command is offered as whole words, which the prompt narrows to the typed prefix'() { + when: + List offered = complete('create-a') + + then: 'whole command names, not the rest of the word' + offered.contains('create-app') + !offered.contains('pp') + } + + void 'an option typed after a command is completed from that command, not the root command'() { + when: + List offered = complete('create-app --fe') + + then: + offered.containsAll(['--features', '--servlet', '--jdk', '--gorm']) + !offered.contains('create-plugin') + } + + void 'the completer needs a command to complete from'() { + when: + new CommandLineCompleter(null) + + then: + NullPointerException e = thrown() + e.message == 'spec' + } + + private List complete(String typed) { + LineReader reader = LineReaderBuilder.builder().terminal(terminal).build() + List candidates = [] + new CommandLineCompleter(commandLine.commandSpec) + .complete(reader, new DefaultParser().parse(typed, typed.length()), candidates) + candidates*.value() + } +} diff --git a/grails-forge/grails-forge-core/grails-app/views/forge/feature/build/gradle/templates/gradleWrapperProperties.gsp b/grails-forge/grails-forge-core/grails-app/views/forge/feature/build/gradle/templates/gradleWrapperProperties.gsp index d5d279d8d66..53690689668 100644 --- a/grails-forge/grails-forge-core/grails-app/views/forge/feature/build/gradle/templates/gradleWrapperProperties.gsp +++ b/grails-forge/grails-forge-core/grails-app/views/forge/feature/build/gradle/templates/gradleWrapperProperties.gsp @@ -31,7 +31,7 @@ under the License. %{-- Keep gradle version synced with .sdkmanrc, gradle.properties (gradleToolingApiVersion), all gradle-wrapper.properties files --}% distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-9.9.1-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-9.9.2-bin.zip networkTimeout=10000 validateDistributionUrl=true zipStoreBase=GRADLE_USER_HOME diff --git a/grails-gradle/gradle/wrapper/gradle-wrapper.properties b/grails-gradle/gradle/wrapper/gradle-wrapper.properties index b60350078ef..a46e1bafcb5 100644 --- a/grails-gradle/gradle/wrapper/gradle-wrapper.properties +++ b/grails-gradle/gradle/wrapper/gradle-wrapper.properties @@ -2,7 +2,7 @@ # and grails-forge/grails-forge-core/grails-app/views/forge/feature/build/gradle/templates/gradleWrapperProperties.gsp distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-9.9.1-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-9.9.2-bin.zip networkTimeout=10000 retries=0 retryBackOffMs=500 diff --git a/grails-gradle/model/src/main/groovy/org/grails/build/parsing/CommandLineParser.java b/grails-gradle/model/src/main/groovy/org/grails/build/parsing/CommandLineParser.java index 0e4d522e0d0..f654d78170e 100644 --- a/grails-gradle/model/src/main/groovy/org/grails/build/parsing/CommandLineParser.java +++ b/grails-gradle/model/src/main/groovy/org/grails/build/parsing/CommandLineParser.java @@ -192,10 +192,17 @@ private void parseInternal(DefaultCommandLine cl, String[] args, boolean firstAr for (String arg : args) { if (arg == null) continue; String trimmed = arg.trim(); - if (trimmed != null && trimmed.length() > 0) { - if (trimmed.charAt(0) == '"' && trimmed.charAt(trimmed.length() - 1) == '"') { - trimmed = trimmed.substring(1, trimmed.length() - 1); + if (trimmed.length() > 1 && trimmed.charAt(0) == '"' && trimmed.charAt(trimmed.length() - 1) == '"') { + trimmed = trimmed.substring(1, trimmed.length() - 1).trim(); + } + if (trimmed.isEmpty()) { + // an empty word after an option without a value is that option's value, not nothing + if (lastWasOption != null) { + cl.addUndeclaredOption(lastWasOption, ""); + lastWasOption = null; } + } + else { if (trimmed.charAt(0) == '-') { lastWasOption = processOption(cl, trimmed); } @@ -258,11 +265,12 @@ protected String processOption(DefaultCommandLine cl, String arg) { arg = (arg.charAt(1) == '-' ? arg.substring(2, arg.length()) : arg.substring(1, arg.length())).trim(); - if (arg.contains("=")) { - String[] split = arg.split("="); - String name = split[0].trim(); + int equals = arg.indexOf('='); + if (equals >= 0) { + // the value is everything after the first equals sign, which may contain more of them + String name = arg.substring(0, equals).trim(); validateOptionName(name); - String value = split[1].trim(); + String value = arg.substring(equals + 1).trim(); if (declaredOptions.containsKey(name)) { cl.addDeclaredOption(name, declaredOptions.get(name), value); } diff --git a/grails-gradle/model/src/test/groovy/org/grails/build/parsing/CommandLineParserSpec.groovy b/grails-gradle/model/src/test/groovy/org/grails/build/parsing/CommandLineParserSpec.groovy index e4a52ea0940..14d810202c8 100644 --- a/grails-gradle/model/src/test/groovy/org/grails/build/parsing/CommandLineParserSpec.groovy +++ b/grails-gradle/model/src/test/groovy/org/grails/build/parsing/CommandLineParserSpec.groovy @@ -406,4 +406,62 @@ class CommandLineParserSpec extends Specification { then: thrown ParseException } + + void "An option value keeps every equals sign after the first, for #arguments"() { + when: + def parser = new CommandLineParser() + parser.addOption('filter', 'A declared option') + def cl = parser.parse(arguments as String[]) + + then: + cl.commandName == 'report' + cl.optionValue(name) == value + cl.remainingArgs == ['first'] + + where: + arguments || name | value + ['report', '--filter=a=b', 'first'] || 'filter' | 'a=b' + ['report', '--other=x=y=z', 'first'] || 'other' | 'x=y=z' + ['report', '--filter==b', 'first'] || 'filter' | '=b' + ['report', '--filter=', 'first'] || 'filter' | '' + ['report', 'first', '--filter=url=/a?b=c'] || 'filter' | 'url=/a?b=c' + } + + void "parseString keeps every equals sign of a quoted option value"() { + when: + def cl = new CommandLineParser().parseString("report '--filter=a=b c' first") + + then: + cl.optionValue('filter') == 'a=b c' + cl.remainingArgs == ['first'] + } + + void "An argument of quotes or spaces alone is read without failing, for #arguments"() { + when: + def cl = new CommandLineParser().parse(arguments as String[]) + + then: + cl.commandName == 'report' + cl.remainingArgs == remaining + + where: + arguments || remaining + ['report', '"', 'first'] || ['"', 'first'] + ['report', '""', 'first'] || ['first'] + ['report', '" "', 'first'] || ['first'] + ['report', ' ', 'first'] || ['first'] + ['report', '"two words"', 'first'] || ['two words', 'first'] + } + + void "An empty word after an option is its empty value, and the next word stays an argument, for #arguments"() { + when: + def cl = new CommandLineParser().parse(arguments as String[]) + + then: + cl.optionValue('other') == '' + cl.remainingArgs == ['first'] + + where: + arguments << [['report', '--other', '', 'first'], ['report', '--other', '""', 'first'], ['report', '--other', ' ', 'first']] + } } diff --git a/grails-gsp/grails-web-gsp-taglib/src/main/groovy/org/grails/plugins/web/taglib/RenderTagLib.groovy b/grails-gsp/grails-web-gsp-taglib/src/main/groovy/org/grails/plugins/web/taglib/RenderTagLib.groovy index cf0f3bca8ae..5594804c606 100644 --- a/grails-gsp/grails-web-gsp-taglib/src/main/groovy/org/grails/plugins/web/taglib/RenderTagLib.groovy +++ b/grails-gsp/grails-web-gsp-taglib/src/main/groovy/org/grails/plugins/web/taglib/RenderTagLib.groovy @@ -92,7 +92,7 @@ class RenderTagLib implements TagLibrary { def currentOut = out int statusCode = request.getAttribute('jakarta.servlet.error.status_code') as int currentOut << """

Error ${prettyPrintStatus(statusCode)}

-
+
URI
${htmlEncoder.encode(WebUtils.getForwardURI(request) ?: request.getAttribute('jakarta.servlet.error.request_uri'))}
""" @@ -109,7 +109,7 @@ class RenderTagLib implements TagLibrary { def trace = errorsViewStackTracePrinter.prettyPrint(exception.cause ?: exception, attrs) if (StringUtils.hasText(trace.trim())) { currentOut << '

Trace

' - currentOut << """
"""
+            currentOut << """
"""
             currentOut << htmlEncoder.encode(trace)
             currentOut << '
' } diff --git a/grails-gsp/plugin/src/main/groovy/org/grails/plugins/web/taglib/ApplicationTagLib.groovy b/grails-gsp/plugin/src/main/groovy/org/grails/plugins/web/taglib/ApplicationTagLib.groovy index df1c971a33a..9ddf9c98b80 100644 --- a/grails-gsp/plugin/src/main/groovy/org/grails/plugins/web/taglib/ApplicationTagLib.groovy +++ b/grails-gsp/plugin/src/main/groovy/org/grails/plugins/web/taglib/ApplicationTagLib.groovy @@ -353,7 +353,7 @@ class ApplicationTagLib implements ApplicationContextAware, InitializingBean, Gr // Allow attrs to overwrite any constants attrs.each { typeInfo.remove(it.key) } - out << writer(processedUrl(uri, request), typeInfo, attrs) + out << writer(processedUrl(uri, request).encodeAsHTML(), typeInfo, attrs) out << '\r\n' } @@ -428,7 +428,7 @@ class ApplicationTagLib implements ApplicationContextAware, InitializingBean, Gr v() writer << '"' } else { - writer << " $k=\"$v\"" + writer << " $k=\"${v.encodeAsHTML()}\"" } } writer << '>' diff --git a/grails-gsp/plugin/src/main/groovy/org/grails/plugins/web/taglib/FormTagLib.groovy b/grails-gsp/plugin/src/main/groovy/org/grails/plugins/web/taglib/FormTagLib.groovy index 520d8c08a3d..c3f7bc83b84 100644 --- a/grails-gsp/plugin/src/main/groovy/org/grails/plugins/web/taglib/FormTagLib.groovy +++ b/grails-gsp/plugin/src/main/groovy/org/grails/plugins/web/taglib/FormTagLib.groovy @@ -46,6 +46,8 @@ import org.grails.buffer.GrailsPrintWriter import org.grails.core.artefact.DomainClassArtefactHandler import org.grails.encoder.CodecLookup import org.grails.encoder.Encoder +import org.grails.encoder.EncodingStateRegistry +import org.grails.encoder.EncodingStateRegistryLookupHolder import org.grails.plugins.web.GrailsTagDateHelper import org.grails.taglib.TagOutput import org.grails.web.servlet.mvc.SynchronizerTokensHolder @@ -218,7 +220,7 @@ class FormTagLib implements ApplicationContextAware, InitializingBean, TagLibrar attrs.value = processFormFieldValueIfNecessary(attrs.name, attrs.value, attrs.type) - out << "' } @@ -276,25 +278,28 @@ class FormTagLib implements ApplicationContextAware, InitializingBean, TagLibrar value = processFormFieldValueIfNecessary(name, value, 'checkbox') hiddenValue = processFormFieldValueIfNecessary("_${name}", hiddenValue, 'hidden') + // The hidden field name is built from the encoded name, so that a name encoded before it reached the tag + // is not encoded a second time in one of the two fields. The encoded name holds no other dots than the name. + String htmlName = encodeAttributeValue(name).toString() def hiddenFieldName - if (name.indexOf('.') == -1) { - hiddenFieldName = "_${name}" + if (htmlName.indexOf('.') == -1) { + hiddenFieldName = "_${htmlName}" } else { - def lastDot = name.lastIndexOf('.') - hiddenFieldName = name[0..lastDot] + '_' - if (lastDot + 1 != name.length()) { - hiddenFieldName += name[(lastDot + 1)..-1] + def lastDot = htmlName.lastIndexOf('.') + hiddenFieldName = htmlName[0..lastDot] + '_' + if (lastDot + 1 != htmlName.length()) { + hiddenFieldName += htmlName[(lastDot + 1)..-1] } } out << " writer << "\n" } + hiddenFields.each { key, value -> writer << "\n" } } /** @@ -602,7 +616,7 @@ class FormTagLib implements ApplicationContextAware, InitializingBean, TagLibrar link = requestDataValueProcessor.processAction(request, link, request.method) } - out << link + out << encodeUrl(link, linkAttrs.url) out << '" ' attrs.keySet().removeAll(LinkGenerator.LINK_ATTRIBUTES) @@ -645,16 +659,16 @@ class FormTagLib implements ApplicationContextAware, InitializingBean, TagLibrar def value = attrs.remove('value') def action = attrs.remove('action') ?: value //Change this button to use requestDataValueProcessor - value = processFormFieldValueIfNecessary("_action_${action}", "${value}", 'image') + value = processFormFieldValueIfNecessary("_action_${action}", value, 'image') booleanToAttribute(attrs, 'disabled') - out << "Day") - out.println("") - out.println("") - out.println("") - out.println("") + def htmlName = encodeAttributeValue(name) + def htmlId = encodeAttributeValue(id) + out.println("") + out.println("") + out.println("") + out.println("") + out.println("") // Change this hidden to use requestDataValueProcessor def dateStructValue = processFormFieldValueIfNecessary("${name}", 'date.struct', 'hidden') - out.println("") + out.println("") // create day select if (precision >= PRECISION_RANKINGS['day']) { - out.println("') } // create month select if (precision >= PRECISION_RANKINGS['month']) { - out.println("') @@ -851,7 +867,7 @@ class FormTagLib implements ApplicationContextAware, InitializingBean, TagLibrar // create year select if (precision >= PRECISION_RANKINGS['year']) { - out.println("') } // do hour select if (precision >= PRECISION_RANKINGS['hour']) { - out.println(" :') @@ -912,7 +928,7 @@ class FormTagLib implements ApplicationContextAware, InitializingBean, TagLibrar // do minute select if (precision >= PRECISION_RANKINGS['minute']) { - out.println("') } @@ -945,7 +961,7 @@ class FormTagLib implements ApplicationContextAware, InitializingBean, TagLibrar private def renderNoSelectionOptionImpl(out, noSelectionKey, noSelectionValue, value) { // If a label for the '--Please choose--' first item is supplied, write it out - out << "" + out << "" } /** @@ -1453,7 +1469,7 @@ class FormTagLib implements ApplicationContextAware, InitializingBean, TagLibrar catch (ignored) { } } - keyValue = processFormFieldValueIfNecessary(selectName, "${keyValue}", 'option') + keyValue = processFormFieldValueIfNecessary(selectName, keyValue, 'option') writer << "value=\"${keyValue.toString().encodeAsHTML()}\" " if (dataAttrsMap) { @@ -1506,10 +1522,10 @@ class FormTagLib implements ApplicationContextAware, InitializingBean, TagLibrar booleanToAttribute(attrs, 'readonly') def checked = attrs.remove('checked') ? true : false - value = processFormFieldValueIfNecessary(name, "${value?.toString()?.encodeAsHTML()}", 'radio') - out << " def it = new Expando() def radioWriter = new FastStringWriter() - radioWriter << " encoders = registry.getEncodingStateFor(value.toString()).encoders + // Asked of the HTML codec, because the identifier of the Raw codec declares itself equivalent to every codec + encoders != null && encoders.any { Encoder encoder -> htmlEncoder.codecIdentifier.isEquivalent(encoder.codecIdentifier) } } /** diff --git a/grails-gsp/plugin/src/main/groovy/org/grails/plugins/web/taglib/UrlMappingTagLib.groovy b/grails-gsp/plugin/src/main/groovy/org/grails/plugins/web/taglib/UrlMappingTagLib.groovy index 7f88909cff0..439b6b71705 100644 --- a/grails-gsp/plugin/src/main/groovy/org/grails/plugins/web/taglib/UrlMappingTagLib.groovy +++ b/grails-gsp/plugin/src/main/groovy/org/grails/plugins/web/taglib/UrlMappingTagLib.groovy @@ -92,8 +92,18 @@ class UrlMappingTagLib implements TagLibrary { } } - private Map appendClass(Map attrs, String cssClass) { - attrs['class'] = [attrs['class'] ?: '', cssClass].join(' ').trim() + /** + * Joins CSS classes, encoding each one that is not encoded yet, and marks the result as encoded, so that a class + * the caller has already encoded is written once when the tag adds a class of its own. + */ + private static String joinEncodedClasses(Encoder htmlEncoder, Object... cssClasses) { + String joined = cssClasses.findAll { it }.collect { htmlEncoder.encode(it) }.join(' ') + htmlEncoder.markEncoded(joined) + joined + } + + private static Map appendEncodedClass(Map attrs, String cssClass, Encoder htmlEncoder) { + attrs['class'] = joinEncodedClasses(htmlEncoder, attrs['class'], cssClass) attrs } @@ -124,6 +134,7 @@ class UrlMappingTagLib implements TagLibrary { def paginate(Map attrs) { attrs = (TypeConvertingMap) attrs def writer = out + Encoder htmlEncoder = codecLookup.lookupEncoder('HTML') if (attrs.total == null) { throwTagError('Tag [paginate] is missing required attribute [total]') } @@ -186,14 +197,14 @@ class UrlMappingTagLib implements TagLibrary { // display previous link when not on firststep unless omitPrev is true if (currentstep > firststep && !attrs.boolean('omitPrev')) { linkParams.offset = offset - max - writer << callLink(appendClass(new LinkedHashMap(linkTagAttrs), 'prevLink')) { + writer << callLink(appendEncodedClass(new LinkedHashMap(linkTagAttrs), 'prevLink', htmlEncoder)) { (attrs.prev ?: messageSource.getMessage('paginate.prev', null, messageSource.getMessage('default.paginate.prev', null, 'Previous', locale), locale)) } } // display steps when steps are enabled and laststep is not firststep if (steps && laststep > firststep) { - Map stepAttrs = appendClass(new LinkedHashMap(linkTagAttrs), 'step') + Map stepAttrs = appendEncodedClass(new LinkedHashMap(linkTagAttrs), 'step', htmlEncoder) // determine begin and endstep paging variables int beginstep = currentstep - (Math.round(maxsteps / 2.0d) as int) + (maxsteps % 2) @@ -222,9 +233,10 @@ class UrlMappingTagLib implements TagLibrary { } // display paginate steps + String currentStepClass = joinEncodedClasses(htmlEncoder, attrs.get('class'), activeClass) (beginstep..endstep).each { int i -> if (currentstep == i) { - writer << "${i}" + writer << "${i}" } else { linkParams.offset = (i - 1) * max @@ -246,7 +258,7 @@ class UrlMappingTagLib implements TagLibrary { // display next link when not on laststep unless omitNext is true if (currentstep < laststep && !attrs.boolean('omitNext')) { linkParams.offset = offset + max - writer << callLink(appendClass(new LinkedHashMap(linkTagAttrs), 'nextLink')) { + writer << callLink(appendEncodedClass(new LinkedHashMap(linkTagAttrs), 'nextLink', htmlEncoder)) { (attrs.next ? attrs.next : messageSource.getMessage('paginate.next', null, messageSource.getMessage('default.paginate.next', null, 'Next', locale), locale)) } } @@ -315,9 +327,10 @@ class UrlMappingTagLib implements TagLibrary { if (params.offset) linkParams.offset = params.offset // determine and add sorting order for this column to link params - attrs['class'] = (attrs['class'] ? "${attrs['class']} sortable" : 'sortable') + Encoder htmlEncoder = codecLookup.lookupEncoder('HTML') + attrs['class'] = joinEncodedClasses(htmlEncoder, attrs['class'], 'sortable') if (property == sort) { - attrs['class'] = (attrs['class'] as String) + ' sorted ' + order + attrs['class'] = joinEncodedClasses(htmlEncoder, attrs['class'], 'sorted', order) if (order == 'asc') { linkParams.order = 'desc' } @@ -329,20 +342,21 @@ class UrlMappingTagLib implements TagLibrary { linkParams.order = defaultOrder } - // determine column title - String title = attrs.remove('title') as String + // determine column title: the title attribute is encoded, as the attributes of the tag are, and a text from + // the message source is written as the message source returns it + Object titleAttribute = attrs.remove('title') String titleKey = attrs.remove('titleKey') as String Object mapping = attrs.remove('mapping') + Object title = titleAttribute != null ? htmlEncoder.encode(titleAttribute) : null if (titleKey) { - if (!title) title = titleKey def messageSource = grailsAttributes.messageSource def locale = RequestContextUtils.getLocale(request) - title = messageSource.getMessage(titleKey, null, title, locale) + String message = messageSource.getMessage(titleKey, null, null, locale) + title = message != null ? message : (title ?: htmlEncoder.encode(titleKey)) } writer << ' writer << k writer << '="' diff --git a/grails-gsp/plugin/src/test/groovy/org/grails/web/taglib/TagAttributeEncodingSpec.groovy b/grails-gsp/plugin/src/test/groovy/org/grails/web/taglib/TagAttributeEncodingSpec.groovy new file mode 100644 index 00000000000..a91a227f9a5 --- /dev/null +++ b/grails-gsp/plugin/src/test/groovy/org/grails/web/taglib/TagAttributeEncodingSpec.groovy @@ -0,0 +1,420 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.grails.web.taglib + +import jakarta.servlet.http.HttpServletRequest + +import org.springframework.core.io.FileSystemResource +import org.springframework.web.servlet.support.RequestDataValueProcessor +import spock.lang.Specification + +import grails.testing.web.taglib.TagLibUnitTest +import org.grails.core.io.StaticResourceLocator +import org.grails.plugins.web.DefaultGrailsTagDateHelper +import org.grails.plugins.web.taglib.ApplicationTagLib +import org.grails.plugins.web.taglib.FormTagLib +import org.grails.plugins.web.taglib.RenderTagLib +import org.grails.plugins.web.taglib.UrlMappingTagLib +import org.grails.plugins.web.taglib.ValidationTagLib +import org.grails.web.errors.ErrorsViewStackTracePrinter + +class TagAttributeEncodingSpec extends Specification implements TagLibUnitTest { + + private static final String VALUE = 'a"b' || '
' || '' || '' || '' || '' || '' || '' || '' || ' value="' + ENCODED + '" ' + 'g:checkBox' | 'form' | '' || '' + 'g:actionSubmitImage' | 'value' | '' || '' || '' || '' || ' class="' + ENCODED + '">' + 'g:select' | 'noSelection key' | '' || '' + 'g:radio' | 'name' | '' || '${it.radio}' || '' + 'g:field' | 'type' | '' || 'x' || 'x' + 'g:external' | 'uri of a script' | '' || '