Skip to content

Commit 6ca251b

Browse files
committed
Make resource naming consitent with RestfulController
1 parent 4f6ff89 commit 6ca251b

File tree

2 files changed

+23
-26
lines changed

2 files changed

+23
-26
lines changed

grails-scaffolding/src/main/groovy/grails/plugin/scaffolding/DomainServiceLocator.java

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
/**
3434
* Resolves the appropriate service bean for a given domain class by:
3535
* - Scanning only beans of type GormService
36-
* - Matching via: ((GormEntity<?>) service.getResource()).instanceOf(domainClass)
36+
* - Matching via: domainClass.isAssignableFrom(service.getResource())
3737
*
3838
* Keeps a single shared cache for the whole app.
3939
*/
@@ -72,28 +72,25 @@ private static <T extends GormEntity<T>> GormService<T> findService(Class<T> dom
7272

7373
for (String name : names) {
7474
GormService<?> gs = (GormService<?>) ctx.getBean(name);
75-
Object resource = gs.getResource();
76-
if (resource instanceof GormEntity) {
77-
GormEntity<?> ge = (GormEntity<?>) resource;
78-
if (ge.instanceOf(domainClass)) {
79-
matchingBeanNames.add(name);
80-
if (match != null) {
81-
throw new IllegalStateException(
82-
"Multiple GormService beans match domain " + domainClass.getName() +
83-
": " + matchingBeanNames
84-
);
85-
}
86-
@SuppressWarnings("unchecked")
87-
GormService<T> svc = (GormService<T>) gs;
88-
match = svc;
75+
Class<?> resourceClass = gs.getResource();
76+
if (resourceClass != null && domainClass.isAssignableFrom(resourceClass)) {
77+
matchingBeanNames.add(name);
78+
if (match != null) {
79+
throw new IllegalStateException(
80+
"Multiple GormService beans match domain " + domainClass.getName() +
81+
": " + matchingBeanNames
82+
);
8983
}
84+
@SuppressWarnings("unchecked")
85+
GormService<T> svc = (GormService<T>) gs;
86+
match = svc;
9087
}
9188
}
9289

9390
if (match == null) {
9491
throw new IllegalStateException(
9592
"No GormService bean found for domain " + domainClass.getName() +
96-
" using resource.instanceOf(..). Scanned " + names.length + " GormService beans."
93+
". Scanned " + names.length + " GormService beans."
9794
);
9895
}
9996

grails-scaffolding/src/main/groovy/grails/plugin/scaffolding/GormService.groovy

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,37 +35,37 @@ import org.grails.datastore.gorm.GormEntityApi
3535
@CompileStatic
3636
class GormService<T extends GormEntity<T>> {
3737

38-
private GormAllOperations<T> resource
39-
Class<T> resourceClass
38+
private GormAllOperations<T> gormStaticApi
39+
Class<T> resource
4040
String resourceName
4141
String resourceClassName
4242
boolean readOnly
4343

4444
GormService(Class<T> resource, boolean readOnly) {
45+
this.resource = resource
4546
this.readOnly = readOnly
46-
resourceClass = resource
4747
resourceClassName = resource.simpleName
4848
resourceName = GrailsNameUtils.getPropertyName(resource)
4949
}
5050

51-
protected GormAllOperations<T> getResource() {
52-
if (resource == null) {
51+
protected GormAllOperations<T> getGormStaticApi() {
52+
if (gormStaticApi == null) {
5353
// Lazy initialization - happens on first access when GORM is ready
54-
resource = GormEnhancer.findStaticApi(resourceClass) as GormAllOperations<T>
54+
gormStaticApi = GormEnhancer.findStaticApi(resource) as GormAllOperations<T>
5555
}
56-
return resource
56+
return gormStaticApi
5757
}
5858

5959
T get(Serializable id) {
60-
getResource().get(id)
60+
getGormStaticApi().get(id)
6161
}
6262

6363
List<T> list(Map args) {
64-
getResource().list(args)
64+
getGormStaticApi().list(args)
6565
}
6666

6767
Long count(Map args) {
68-
getResource().count()
68+
getGormStaticApi().count()
6969
}
7070

7171
@Transactional

0 commit comments

Comments
 (0)