-
-
Notifications
You must be signed in to change notification settings - Fork 962
Support for Gorm Entities with same name, but different packages #15036
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
jdaugherty
merged 25 commits into
apache:7.0.x
from
codeconsole:7.0.x-genericServiceEnhancements
Sep 15, 2025
Merged
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
d0387fb
Support for Gorm Entities with same name, but different packages
codeconsole 720098d
remove no longer used Holders import
codeconsole 269ab5a
add back imports removed when adding the apache header
codeconsole 5cf8da8
Remove redundant import
codeconsole 308ecae
fix formatting
codeconsole 5831e10
formatting
codeconsole e43f42b
move AopUtils import statement
codeconsole ff20f72
import order
codeconsole 870b17b
Spring imports should be in a group before Grails imports.
codeconsole 8071f6f
Merge branch '7.0.x' into 7.0.x-genericServiceEnhancements
codeconsole 730289e
Merge branch '7.0.x' into 7.0.x-genericServiceEnhancements
codeconsole c55e93b
Simplify RestfulServiceController to only resolve services of GormSer…
codeconsole aa8d98d
Use static compilation
codeconsole f0dc6af
Merge branch '7.0.x' into 7.0.x-genericServiceEnhancements
jamesfredley 4a5978c
Merge branch '7.0.x' into 7.0.x-genericServiceEnhancements
codeconsole 4703236
Don't use cache in development mode.
codeconsole fd903c1
Merge branch '7.0.x' into 7.0.x-genericServiceEnhancements
codeconsole c5d8adc
Add scaffolding test app
jdaugherty e48f971
test - scaffolding spec - Add UserControllerSpec
jdaugherty a1eb2a3
Not needed
codeconsole 78860e0
Missing service
codeconsole 111821b
remove devtools causing error
codeconsole d052c99
chore: add missing license headers
jdaugherty 45f14d6
test: expand test coverage
jdaugherty 16837b5
fix: add missing bom
jdaugherty File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
grails-scaffolding/src/main/groovy/grails/plugin/scaffolding/DomainServiceLocator.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/* | ||
* 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.plugin.scaffolding; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.ConcurrentMap; | ||
|
||
import org.springframework.context.ApplicationContext; | ||
|
||
import grails.util.Environment; | ||
import grails.util.Holders; | ||
import org.grails.datastore.gorm.GormEntity; | ||
|
||
/** | ||
* Resolves the appropriate service bean for a given domain class by: | ||
* - Scanning only beans of type GormService | ||
* - Matching via: ((GormEntity<?>) service.getResource()).instanceOf(domainClass) | ||
* | ||
* Keeps a single shared cache for the whole app. | ||
*/ | ||
public final class DomainServiceLocator { | ||
|
||
private static final ConcurrentMap<Class<?>, GormService<?>> CACHE = new ConcurrentHashMap<>(); | ||
jdaugherty marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
private DomainServiceLocator() {} | ||
|
||
/** Resolve (and cache) a service bean for the given domain class. */ | ||
public static <T extends GormEntity<T>> GormService<T> resolve(Class<T> domainClass) { | ||
if (!Environment.isDevelopmentMode()) { | ||
@SuppressWarnings("unchecked") | ||
GormService<T> cached = (GormService<T>) CACHE.get(domainClass); | ||
if (cached != null) return cached; | ||
} | ||
|
||
GormService<T> found = findService(domainClass); | ||
if (!Environment.isDevelopmentMode()) { | ||
CACHE.put(domainClass, found); | ||
} | ||
return found; | ||
} | ||
|
||
/** Clear cache (useful in tests/dev reloads). */ | ||
public static void clear() { | ||
CACHE.clear(); | ||
} | ||
|
||
private static <T extends GormEntity<T>> GormService<T> findService(Class<T> domainClass) { | ||
ApplicationContext ctx = Holders.getGrailsApplication().getMainContext(); | ||
jdaugherty marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
String[] names = ctx.getBeanNamesForType(GormService.class); | ||
GormService<T> match = null; | ||
List<String> matchingBeanNames = new ArrayList<>(); | ||
|
||
for (String name : names) { | ||
GormService<?> gs = (GormService<?>) ctx.getBean(name); | ||
Object resource = gs.getResource(); | ||
if (resource instanceof GormEntity) { | ||
GormEntity<?> ge = (GormEntity<?>) resource; | ||
if (ge.instanceOf(domainClass)) { | ||
matchingBeanNames.add(name); | ||
if (match != null) { | ||
throw new IllegalStateException( | ||
"Multiple GormService beans match domain " + domainClass.getName() + | ||
": " + matchingBeanNames | ||
); | ||
} | ||
@SuppressWarnings("unchecked") | ||
GormService<T> svc = (GormService<T>) gs; | ||
match = svc; | ||
} | ||
} | ||
} | ||
|
||
if (match == null) { | ||
throw new IllegalStateException( | ||
"No GormService bean found for domain " + domainClass.getName() + | ||
" using resource.instanceOf(..). Scanned " + names.length + " GormService beans." | ||
); | ||
} | ||
|
||
return match; | ||
} | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
plugins { | ||
id 'groovy' | ||
id 'org.apache.grails.gradle.grails-gsp' | ||
id 'org.apache.grails.gradle.grails-web' | ||
id 'cloud.wondrify.asset-pipeline' | ||
} | ||
|
||
version = "0.0.1" | ||
group = "com.example" | ||
|
||
dependencies { | ||
console "org.apache.grails:grails-console" | ||
implementation platform(project(':grails-bom')) | ||
implementation "org.springframework.boot:spring-boot-starter-logging" | ||
implementation "org.springframework.boot:spring-boot-starter-validation" | ||
implementation "org.springframework.boot:spring-boot-autoconfigure" | ||
implementation "org.springframework.boot:spring-boot-starter" | ||
implementation "org.springframework.boot:spring-boot-starter-oauth2-client" | ||
implementation "org.apache.grails:grails-core" | ||
implementation "org.springframework.boot:spring-boot-starter-actuator" | ||
implementation "org.springframework.boot:spring-boot-starter-tomcat" | ||
implementation "org.apache.grails:grails-web-boot" | ||
implementation "org.apache.grails:grails-logging" | ||
implementation "org.apache.grails:grails-rest-transforms" | ||
implementation "org.apache.grails:grails-databinding" | ||
implementation "org.apache.grails:grails-services" | ||
implementation "org.apache.grails:grails-url-mappings" | ||
implementation "org.apache.grails:grails-layout" | ||
implementation "org.apache.grails:grails-interceptors" | ||
implementation "org.apache.grails:grails-scaffolding" | ||
implementation "org.apache.grails:grails-data-hibernate5" | ||
implementation "org.apache.grails:grails-gsp" | ||
integrationTestImplementation testFixtures("org.apache.grails:grails-geb") | ||
profile "org.apache.grails.profiles:web" | ||
runtimeOnly "org.fusesource.jansi:jansi" | ||
runtimeOnly "com.h2database:h2" | ||
runtimeOnly "com.zaxxer:HikariCP" | ||
runtimeOnly "cloud.wondrify:asset-pipeline-grails" | ||
testAndDevelopmentOnly platform(project(':grails-bom')) | ||
testAndDevelopmentOnly "org.webjars.npm:bootstrap" | ||
testAndDevelopmentOnly "org.webjars.npm:bootstrap-icons" | ||
testAndDevelopmentOnly "org.webjars.npm:jquery" | ||
testImplementation "org.apache.grails:grails-testing-support-datamapping" | ||
testImplementation "org.spockframework:spock-core" | ||
testImplementation "org.apache.grails:grails-testing-support-web" | ||
} | ||
|
||
apply { | ||
from rootProject.layout.projectDirectory.file('gradle/functional-test-config.gradle') | ||
from rootProject.layout.projectDirectory.file('gradle/java-config.gradle') | ||
from rootProject.layout.projectDirectory.file('gradle/test-webjar-asset-config.gradle') | ||
from rootProject.layout.projectDirectory.file('gradle/grails-extension-gradle-config.gradle') | ||
} |
27 changes: 27 additions & 0 deletions
27
grails-test-examples/scaffolding/grails-app/assets/images/advancedgrails.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+6.87 KB
...-test-examples/scaffolding/grails-app/assets/images/apple-touch-icon-retina.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+3 KB
grails-test-examples/scaffolding/grails-app/assets/images/apple-touch-icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 19 additions & 0 deletions
19
grails-test-examples/scaffolding/grails-app/assets/images/documentation.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+5.43 KB
grails-test-examples/scaffolding/grails-app/assets/images/favicon.ico
Binary file not shown.
26 changes: 26 additions & 0 deletions
26
...st-examples/scaffolding/grails-app/assets/images/grails-cupsonly-logo-white.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.