Skip to content
Merged
Show file tree
Hide file tree
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 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
3 changes: 3 additions & 0 deletions gradle/test-webjar-asset-config.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@ assets {
excludes = [
'webjars/jquery/**',
'webjars/bootstrap/**',
'webjars/bootstrap-icons/**'
]
includes = [
'webjars/jquery/*/dist/jquery.js',
'webjars/bootstrap/*/dist/js/bootstrap.bundle.js',
'webjars/bootstrap/*/dist/css/bootstrap.css',
'webjars/bootstrap-icons/*/font/bootstrap-icons.css',
'webjars/bootstrap-icons/*/font/fonts/*',
]
}
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<>();

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();

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;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -19,45 +19,52 @@

package grails.plugin.scaffolding

import groovy.transform.CompileStatic
import grails.artefact.Artefact
import grails.gorm.transactions.ReadOnly
import grails.rest.RestfulController
import grails.util.Holders
import org.grails.datastore.gorm.GormEntityApi
import org.grails.datastore.gorm.GormEntity

@Artefact('Controller')
@ReadOnly
class RestfulServiceController<T> extends RestfulController<T> {
@CompileStatic
class RestfulServiceController<T extends GormEntity<T>> extends RestfulController<T> {

RestfulServiceController(Class<T> resource, boolean readOnly) {
super(resource, readOnly)
}

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

@Override
protected T queryForResource(Serializable id) {
getService().get(id)
}

@Override
protected List<T> listAllResources(Map params) {
getService().list(params)
}

@Override
protected Integer countResources() {
getService().count()
}

@Override
protected T saveResource(T resource) {
getService().save(resource)
}

@Override
protected T updateResource(T resource) {
getService().save(resource)
}

@Override
protected void deleteResource(T resource) {
getService().delete(((GormEntityApi) resource).ident())
getService().delete(resource.ident())
}
}
72 changes: 72 additions & 0 deletions grails-test-examples/scaffolding/build.gradle
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')
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Loading