jdaugherty commented on code in PR #15036: URL: https://github.com/apache/grails-core/pull/15036#discussion_r2330943789
########## grails-scaffolding/src/main/groovy/grails/plugin/scaffolding/DomainServiceLocator.java: ########## @@ -0,0 +1,98 @@ +/* + * 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.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<>(); Review Comment: GormService will be a bean, what if someone has reloading enabled? Won't this then break their ability to reload? One solution would be to move this to a bean itself. ########## grails-scaffolding/src/main/groovy/grails/plugin/scaffolding/DomainServiceLocator.java: ########## @@ -0,0 +1,98 @@ +/* + * 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.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 { Review Comment: We should add a functional test for scaffolding - we should be able to easily add 2 examples of this scenario to prove this works. ########## grails-scaffolding/src/main/groovy/grails/plugin/scaffolding/DomainServiceLocator.java: ########## @@ -0,0 +1,98 @@ +/* + * 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.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) { + @SuppressWarnings("unchecked") + GormService<T> cached = (GormService<T>) CACHE.get(domainClass); + if (cached != null) return cached; + + GormService<T> found = findService(domainClass); + 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(); Review Comment: If we're going to rely on beans, why is this class itself not a bean? Is it used earlier in the init process? ########## grails-scaffolding/src/main/groovy/grails/plugin/scaffolding/RestfulServiceController.groovy: ########## @@ -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> { Review Comment: By changing a definition of this class that's widely extended, this may potentially break every subclass and require recompilation. We've stated we would not make these changes after RC2. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
