-
-
Notifications
You must be signed in to change notification settings - Fork 970
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 10 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
247 changes: 247 additions & 0 deletions
247
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,247 @@ | ||
| /* | ||
| * 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.lang.reflect.Field; | ||
| import java.lang.reflect.Method; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.concurrent.ConcurrentHashMap; | ||
| import java.util.concurrent.ConcurrentMap; | ||
|
|
||
| import org.springframework.aop.support.AopUtils; | ||
| import org.springframework.context.ApplicationContext; | ||
|
|
||
| import grails.util.GrailsNameUtils; | ||
| import grails.util.Holders; | ||
| import org.grails.datastore.gorm.GormEntity; | ||
|
|
||
| /** | ||
| * Resolves the appropriate *Service bean for a given domain class by: | ||
| * 1) Fast-path bean name guesses ("<simpleName>Service", "<packageLeaf><SimpleName>Service"), | ||
| * validated by resource.instanceOf(domainClass). | ||
| * 2) Scanning beans that extend GormService<?> (directly or indirectly) and matching via resource.instanceOf(..). | ||
| * 3) Tie-breakers: prefer guessed bean names, then same package-leaf as the domain. | ||
| * | ||
| * Keeps a single shared cache for the whole app. | ||
| */ | ||
| public final class DomainServiceLocator { | ||
|
|
||
| private static final ConcurrentMap<Class<?>, Object> CACHE = new ConcurrentHashMap<>(); | ||
|
|
||
| private DomainServiceLocator() {} | ||
|
|
||
| /** Resolve (and cache) a service bean for the given domain class. */ | ||
| public static Object resolve(Class<?> domainClass) { | ||
| return CACHE.computeIfAbsent(domainClass, DomainServiceLocator::findService); | ||
| } | ||
|
|
||
| /** Clear cache (useful in tests/dev reloads). */ | ||
| public static void clear() { CACHE.clear(); } | ||
|
|
||
| // --------------------------------------------------------------------- | ||
| // Core resolution | ||
| // --------------------------------------------------------------------- | ||
|
|
||
| private static Object findService(Class<?> domainClass) { | ||
| ApplicationContext ctx = Holders.getGrailsApplication().getMainContext(); | ||
jdaugherty marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // Fast-path guesses: <simpleName>Service, <packageLeaf><SimpleName>Service | ||
| String simpleNameBean = GrailsNameUtils.getPropertyName(domainClass.getSimpleName()) + "Service"; | ||
| String altName = null; | ||
| Package pkg = domainClass.getPackage(); | ||
| if (pkg != null) { | ||
| String[] parts = pkg.getName().split("\\."); | ||
| String leaf = parts[parts.length - 1]; | ||
| altName = GrailsNameUtils.getPropertyName(leaf + domainClass.getSimpleName()) + "Service"; | ||
| } | ||
|
|
||
| // 1) Try guessed bean names first (validate via resource.instanceOf(domainClass)) | ||
| if (ctx.containsBean(simpleNameBean)) { | ||
| Object bean = ctx.getBean(simpleNameBean); | ||
| if (serviceMatchesDomain(bean, domainClass)) return bean; | ||
| } | ||
| if (altName != null && ctx.containsBean(altName)) { | ||
| Object bean = ctx.getBean(altName); | ||
| if (serviceMatchesDomain(bean, domainClass)) return bean; | ||
| } | ||
|
|
||
| // 2) Collect all beans that ultimately extend GormService<?> | ||
| List<Object> candidates = collectGormServiceCandidates(ctx); | ||
|
|
||
| // Filter by match (resource.instanceOf(..)) | ||
| List<Object> matches = new ArrayList<>(); | ||
| for (Object cand : candidates) { | ||
| if (serviceMatchesDomain(cand, domainClass)) matches.add(cand); | ||
| } | ||
|
|
||
| if (matches.size() == 1) return matches.get(0); | ||
| if (matches.isEmpty()) { | ||
| // Optional last-resort: scan any *Service bean and try matching | ||
| List<Object> fallback = scanAllServiceBeansAndMatch(ctx, domainClass); | ||
| if (fallback.size() == 1) return fallback.get(0); | ||
| if (fallback.isEmpty()) { | ||
| throw new IllegalStateException( | ||
| "No service found for domain " + domainClass.getName() + | ||
| " (checked bean name and resource.instanceOf(..) across GormService subclasses)." | ||
| ); | ||
| } | ||
| return pickFromMultiple(fallback, simpleNameBean, altName, domainClass); | ||
| } | ||
|
|
||
| // Multiple matches: tie-breakers | ||
| return pickFromMultiple(matches, simpleNameBean, altName, domainClass); | ||
| } | ||
|
|
||
| // --------------------------------------------------------------------- | ||
| // Candidate collection & matching | ||
| // --------------------------------------------------------------------- | ||
|
|
||
| /** Collect beans that extend GormService<?> (directly or via subclasses like GormMongoService<?>). */ | ||
| private static List<Object> collectGormServiceCandidates(ApplicationContext ctx) { | ||
| List<Object> candidates = new ArrayList<>(); | ||
|
|
||
| // Fast path: any bean assignable to GormService | ||
| String[] names = ctx.getBeanNamesForType(GormService.class); | ||
| for (String n : names) candidates.add(ctx.getBean(n)); | ||
| if (!candidates.isEmpty()) return candidates; | ||
|
|
||
| // Fallback: scan all *Service beans, keep those whose class ultimately extends GormService | ||
| for (String name : ctx.getBeanDefinitionNames()) { | ||
| if (!name.endsWith("Service")) continue; | ||
| Object bean = ctx.getBean(name); | ||
| if (extendsGormService(AopUtils.getTargetClass(bean))) candidates.add(bean); | ||
| } | ||
| return candidates; | ||
| } | ||
|
|
||
| /** True if the given class or any superclass equals GormService. */ | ||
| private static boolean extendsGormService(Class<?> type) { | ||
| Class<?> c = type; | ||
| while (c != null && c != Object.class) { | ||
| if (GormService.class.equals(c)) return true; | ||
| c = c.getSuperclass(); | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| /** Robust domain match: use service.resource.instanceOf(domainClass). */ | ||
| private static boolean serviceMatchesDomain(Object bean, Class<?> domainClass) { | ||
| Object resource = extractResource(bean); | ||
| if (resource instanceof GormEntity) { | ||
| @SuppressWarnings("unchecked") | ||
| GormEntity<?> ge = (GormEntity<?>) resource; | ||
| return ge.instanceOf(domainClass); | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| /** Try to read service.resource (Groovy property → getResource()) or direct field access on target class. */ | ||
| private static Object extractResource(Object bean) { | ||
| try { | ||
| Method m = bean.getClass().getMethod("getResource"); | ||
| return m.invoke(bean); | ||
| } catch (NoSuchMethodException e) { | ||
| Class<?> target = AopUtils.getTargetClass(bean); | ||
| Field f = findField(target, "resource"); | ||
| if (f != null) { | ||
| try { | ||
| f.setAccessible(true); | ||
| return f.get(bean); | ||
| } catch (Throwable ignore) { /* fall through */ } | ||
| } | ||
| } catch (Throwable ignore) { | ||
| // ignore and treat as not found | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| /** Find a declared field in the class hierarchy. */ | ||
| private static Field findField(Class<?> type, String name) { | ||
| Class<?> c = type; | ||
| while (c != null && c != Object.class) { | ||
| try { | ||
| return c.getDeclaredField(name); | ||
| } catch (NoSuchFieldException e) { | ||
| c = c.getSuperclass(); | ||
| } | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| /** Last-resort scan across all *Service beans using the same resource-based match. */ | ||
| private static List<Object> scanAllServiceBeansAndMatch(ApplicationContext ctx, Class<?> domainClass) { | ||
| List<Object> matches = new ArrayList<>(); | ||
| for (String name : ctx.getBeanDefinitionNames()) { | ||
| if (!name.endsWith("Service")) continue; | ||
| Object bean = ctx.getBean(name); | ||
| if (serviceMatchesDomain(bean, domainClass)) matches.add(bean); | ||
| } | ||
| return matches; | ||
| } | ||
|
|
||
| // --------------------------------------------------------------------- | ||
| // Tie-breakers | ||
| // --------------------------------------------------------------------- | ||
|
|
||
| private static Object pickFromMultiple(List<Object> matches, String simpleNameBean, String altName, Class<?> domainClass) { | ||
| ApplicationContext ctx = Holders.getGrailsApplication().getMainContext(); | ||
|
|
||
| // Prefer bean whose name equals a fast-path guess (compare target classes) | ||
| for (String guess : new String[]{simpleNameBean, altName}) { | ||
| if (guess == null) continue; | ||
| if (ctx.containsBean(guess)) { | ||
| Object bean = ctx.getBean(guess); | ||
| Class<?> guessTarget = AopUtils.getTargetClass(bean); | ||
| for (Object m : matches) { | ||
| if (AopUtils.getTargetClass(m).equals(guessTarget)) return m; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Prefer same package *leaf* as the domain (e.g., "admin" for org.website.admin.User) | ||
| String domainLeaf = packageLeaf(domainClass); | ||
| if (domainLeaf != null) { | ||
| List<Object> sameLeaf = new ArrayList<>(); | ||
| for (Object m : matches) { | ||
| String leaf = packageLeaf(AopUtils.getTargetClass(m)); | ||
| if (domainLeaf.equals(leaf)) sameLeaf.add(m); | ||
| } | ||
| if (sameLeaf.size() == 1) return sameLeaf.get(0); | ||
| } | ||
|
|
||
| throw new IllegalStateException("Multiple services match domain " + domainClass.getName() + | ||
| ": " + classNames(matches)); | ||
| } | ||
|
|
||
| private static String packageLeaf(Class<?> clazz) { | ||
| Package p = clazz.getPackage(); | ||
| if (p == null) return null; | ||
| String name = p.getName(); | ||
| int idx = name.lastIndexOf('.'); | ||
| return (idx >= 0) ? name.substring(idx + 1) : name; | ||
| } | ||
|
|
||
| private static String classNames(List<Object> beans) { | ||
| List<String> names = new ArrayList<>(beans.size()); | ||
| for (Object b : beans) names.add(AopUtils.getTargetClass(b).getName()); | ||
| return names.toString(); | ||
| } | ||
| } | ||
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
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.