Skip to content
Merged
Show file tree
Hide file tree
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 Aug 29, 2025
720098d
remove no longer used Holders import
codeconsole Aug 29, 2025
269ab5a
add back imports removed when adding the apache header
codeconsole Aug 29, 2025
5cf8da8
Remove redundant import
codeconsole Aug 30, 2025
308ecae
fix formatting
codeconsole Aug 30, 2025
5831e10
formatting
codeconsole Aug 30, 2025
e43f42b
move AopUtils import statement
codeconsole Aug 31, 2025
ff20f72
import order
codeconsole Aug 31, 2025
870b17b
Spring imports should be in a group before Grails imports.
codeconsole Aug 31, 2025
8071f6f
Merge branch '7.0.x' into 7.0.x-genericServiceEnhancements
codeconsole Aug 31, 2025
730289e
Merge branch '7.0.x' into 7.0.x-genericServiceEnhancements
codeconsole Sep 3, 2025
c55e93b
Simplify RestfulServiceController to only resolve services of GormSer…
codeconsole Sep 3, 2025
aa8d98d
Use static compilation
codeconsole Sep 3, 2025
f0dc6af
Merge branch '7.0.x' into 7.0.x-genericServiceEnhancements
jamesfredley Sep 4, 2025
4a5978c
Merge branch '7.0.x' into 7.0.x-genericServiceEnhancements
codeconsole Sep 8, 2025
4703236
Don't use cache in development mode.
codeconsole Sep 11, 2025
fd903c1
Merge branch '7.0.x' into 7.0.x-genericServiceEnhancements
codeconsole Sep 12, 2025
c5d8adc
Add scaffolding test app
jdaugherty Sep 12, 2025
e48f971
test - scaffolding spec - Add UserControllerSpec
jdaugherty Sep 12, 2025
a1eb2a3
Not needed
codeconsole Sep 12, 2025
78860e0
Missing service
codeconsole Sep 12, 2025
111821b
remove devtools causing error
codeconsole Sep 14, 2025
d052c99
chore: add missing license headers
jdaugherty Sep 15, 2025
45f14d6
test: expand test coverage
jdaugherty Sep 15, 2025
16837b5
fix: add missing bom
jdaugherty Sep 15, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();

// 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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ package grails.plugin.scaffolding
import grails.artefact.Artefact
import grails.gorm.transactions.ReadOnly
import grails.rest.RestfulController
import grails.util.Holders
import org.grails.datastore.gorm.GormEntityApi

@Artefact('Controller')
Expand All @@ -34,7 +33,7 @@ class RestfulServiceController<T> extends RestfulController<T> {
}

protected def getService() {
Holders.grailsApplication.getMainContext().getBean(resourceName + 'Service')
DomainServiceLocator.resolve(resource)
}

protected T queryForResource(Serializable id) {
Expand Down
Loading