Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
Expand Up @@ -60,8 +60,14 @@ final class ApplicationArtefactScanner {
* @return The classes that constitute the Grails application
*/
static Collection<Class> scanApplicationClasses(Class<?> applicationClass, Collection<String> packageNames) {
Collection<Class> classes = new HashSet<>()
classes.addAll(new ClassPathScanner().scan(applicationClass, packageNames))
Collection<Class> classes = new LinkedHashSet<>()
Collection<Class> indexedClasses = ArtefactIndexReader.read(applicationClass, packageNames)
if (indexedClasses == null) {
classes.addAll(new ClassPathScanner().scan(applicationClass, packageNames))
}
else {
classes.addAll(indexedClasses)
}
classes.addAll(loadTransformedClasses(applicationClass.classLoader))
return classes
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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>
*/
Comment on lines +39 to +56
final class ArtefactIndexReader {

static final String RESOURCE_NAME = "META-INF/grails/artefacts.idx";

private ArtefactIndexReader() {
}

static Collection<Class> read(Class<?> applicationClass, Collection<String> packageNames) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

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) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)).

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

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) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

for (String packageName : packageNames) {
if (packageName != null && (packageName.isEmpty() ? !className.contains(".") : className.startsWith(packageName + "."))) {
return true;
}
}
return false;
}
}
Loading
Loading