jdaugherty commented on code in PR #16000: URL: https://github.com/apache/grails-core/pull/16000#discussion_r3610604331
########## grails-core/src/main/groovy/grails/boot/config/ArtefactIndexReader.java: ########## @@ -0,0 +1,83 @@ +/* + * 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.boot.config; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.net.URL; +import java.nio.charset.StandardCharsets; +import java.util.Collection; +import java.util.LinkedHashSet; +import java.util.Set; + +import grails.io.IOUtils; + +/** + * Reads the optional application artefact index. + * + * <p>The index is UTF-8 text at {@value #RESOURCE_NAME}, with one fully qualified + * class name per nonempty line. Any unreadable or unresolvable entry rejects the + * complete index so callers can use their normal classpath scan.</p> + */ +final class ArtefactIndexReader { + + static final String RESOURCE_NAME = "META-INF/grails/artefacts.idx"; + + private ArtefactIndexReader() { + } + + static Collection<Class> read(Class<?> applicationClass, Collection<String> packageNames) { + try { + URL resource = new URL(IOUtils.findRootResource(applicationClass), RESOURCE_NAME); + Set<Class> classes = new LinkedHashSet<>(); + return readResource(resource, applicationClass.getClassLoader(), packageNames, classes) ? classes : null; + } catch (IOException ignored) { + return null; + } + } + + private static boolean readResource(URL resource, ClassLoader classLoader, Collection<String> packageNames, Set<Class> classes) { + try (InputStream inputStream = resource.openStream(); + BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8))) { + String className; + while ((className = reader.readLine()) != null) { + if (className.isEmpty()) { Review Comment: Rejecting the entire index on an empty line is stricter than it needs to be — a trailing blank line is the most common artifact of text-file generation and concatenation (the spec's own `writeIndex` has to `.trim()` to avoid it). Skipping blank lines (`continue`) keeps the strict-reject behavior for genuinely malformed content while tolerating the boring case. If strictness is intentional as a whole-file integrity signal, the javadoc should say the producer must not emit blank lines, including trailing ones. ########## grails-core/src/main/groovy/grails/boot/config/ArtefactIndexReader.java: ########## @@ -0,0 +1,83 @@ +/* + * 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.boot.config; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.net.URL; +import java.nio.charset.StandardCharsets; +import java.util.Collection; +import java.util.LinkedHashSet; +import java.util.Set; + +import grails.io.IOUtils; + +/** + * Reads the optional application artefact index. + * + * <p>The index is UTF-8 text at {@value #RESOURCE_NAME}, with one fully qualified + * class name per nonempty line. Any unreadable or unresolvable entry rejects the + * complete index so callers can use their normal classpath scan.</p> + */ +final class ArtefactIndexReader { + + static final String RESOURCE_NAME = "META-INF/grails/artefacts.idx"; + + private ArtefactIndexReader() { + } + + static Collection<Class> read(Class<?> applicationClass, Collection<String> packageNames) { Review Comment: Two operational concerns for production startup code: 1. **Silence.** Both outcomes are invisible — no log when the index is used, none when it's rejected. A corrupted index silently degrades to slow scanning and nobody finds out; a working index can't be confirmed either. Suggest debug-level logs for "index used (N entries)" and "index rejected, falling back". 2. **Stale-index hazard.** A valid but *incomplete* index is the dangerous case: developer adds an artefact, index isn't regenerated, and the new class is silently absent from the application — no error, no fallback. The producer side will need a freshness guarantee (or the index should carry a hash/marker the reader can validate), and a kill-switch system property to force scanning would be a cheap escape hatch worth adding in the seed. ########## grails-core/src/main/groovy/grails/boot/config/ArtefactIndexReader.java: ########## @@ -0,0 +1,83 @@ +/* + * 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.boot.config; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.net.URL; +import java.nio.charset.StandardCharsets; +import java.util.Collection; +import java.util.LinkedHashSet; +import java.util.Set; + +import grails.io.IOUtils; + +/** + * Reads the optional application artefact index. + * + * <p>The index is UTF-8 text at {@value #RESOURCE_NAME}, with one fully qualified + * class name per nonempty line. Any unreadable or unresolvable entry rejects the + * complete index so callers can use their normal classpath scan.</p> + */ +final class ArtefactIndexReader { + + static final String RESOURCE_NAME = "META-INF/grails/artefacts.idx"; + + private ArtefactIndexReader() { + } + + static Collection<Class> read(Class<?> applicationClass, Collection<String> packageNames) { + try { + URL resource = new URL(IOUtils.findRootResource(applicationClass), RESOURCE_NAME); + Set<Class> classes = new LinkedHashSet<>(); + return readResource(resource, applicationClass.getClassLoader(), packageNames, classes) ? classes : null; + } catch (IOException ignored) { Review Comment: `IOUtils.findRootResource` throws **IllegalStateException**, not IOException, when the class resource can't be resolved (`targetClass.getResource(...)` returning null — e.g. an application class from a classloader that doesn't expose `.class` resources). That escapes this catch and would fail startup, where today the same situation just scans. Since the whole contract of this reader is "never make things worse than the fallback," this should catch that too (e.g. `catch (IOException | RuntimeException)`). ########## grails-core/src/main/groovy/grails/boot/config/ArtefactIndexReader.java: ########## @@ -0,0 +1,83 @@ +/* + * 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.boot.config; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.net.URL; +import java.nio.charset.StandardCharsets; +import java.util.Collection; +import java.util.LinkedHashSet; +import java.util.Set; + +import grails.io.IOUtils; + +/** + * Reads the optional application artefact index. + * + * <p>The index is UTF-8 text at {@value #RESOURCE_NAME}, with one fully qualified + * class name per nonempty line. Any unreadable or unresolvable entry rejects the + * complete index so callers can use their normal classpath scan.</p> + */ +final class ArtefactIndexReader { + + static final String RESOURCE_NAME = "META-INF/grails/artefacts.idx"; + + private ArtefactIndexReader() { + } + + static Collection<Class> read(Class<?> applicationClass, Collection<String> packageNames) { + try { + URL resource = new URL(IOUtils.findRootResource(applicationClass), RESOURCE_NAME); + Set<Class> classes = new LinkedHashSet<>(); + return readResource(resource, applicationClass.getClassLoader(), packageNames, classes) ? classes : null; + } catch (IOException ignored) { + return null; + } + } + + private static boolean readResource(URL resource, ClassLoader classLoader, Collection<String> packageNames, Set<Class> classes) { + try (InputStream inputStream = resource.openStream(); + BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8))) { + String className; + while ((className = reader.readLine()) != null) { + if (className.isEmpty()) { + return false; + } + if (isInPackage(className, packageNames)) { + classes.add(classLoader.loadClass(className)); + } + } + return true; + } catch (IOException | ClassNotFoundException | LinkageError ignored) { + return false; + } + } + + private static boolean isInPackage(String className, Collection<String> packageNames) { Review Comment: Worth documenting the semantic differences from the `ClassPathScanner` path this replaces, since the future index producer has to compensate for them: - The scanner only returns classes carrying an annotation whose name starts with `grails.` (default `annotationFilter`); the reader trusts every listed entry with no annotation check, so a hand-edited or buggy index can inject arbitrary classes into the artefact set. - The scanner skips `DEFAULT_IGNORED_ROOT_PACKAGES` (`com`, `org`, `net`, …) even when explicitly passed as packageNames; the reader honors them. Both are fine if the producer mirrors scan semantics exactly, but that contract currently lives nowhere — a sentence in the class javadoc would pin it. -- 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]
