-
Notifications
You must be signed in to change notification settings - Fork 460
/
ResourceRootIndexer.java
96 lines (86 loc) · 3.98 KB
/
ResourceRootIndexer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*
* Copyright The WildFly Authors
* SPDX-License-Identifier: Apache-2.0
*/
package org.jboss.as.server.deployment.annotation;
import java.io.InputStream;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.jboss.as.server.logging.ServerLogger;
import org.jboss.as.server.deployment.Attachments;
import org.jboss.as.server.deployment.DeploymentUnitProcessingException;
import org.jboss.as.server.deployment.module.ResourceRoot;
import org.jboss.as.server.moduleservice.ModuleIndexBuilder;
import org.jboss.jandex.Index;
import org.jboss.jandex.IndexReader;
import org.jboss.jandex.Indexer;
import org.jboss.vfs.VFSUtils;
import org.jboss.vfs.VirtualFile;
import org.jboss.vfs.VirtualFileFilter;
import org.jboss.vfs.VisitorAttributes;
import org.jboss.vfs.util.SuffixMatchFilter;
/**
* Utility class for indexing a resource root
*/
public class ResourceRootIndexer {
/**
* Creates and attaches the annotation index to a resource root, if it has not already been attached
*/
public static void indexResourceRoot(final ResourceRoot resourceRoot) throws DeploymentUnitProcessingException {
if (resourceRoot.getAttachment(Attachments.ANNOTATION_INDEX) != null) {
return;
}
VirtualFile indexFile = resourceRoot.getRoot().getChild(ModuleIndexBuilder.INDEX_LOCATION);
if (indexFile.exists()) {
try {
IndexReader reader = new IndexReader(indexFile.openStream());
resourceRoot.putAttachment(Attachments.ANNOTATION_INDEX, reader.read());
ServerLogger.DEPLOYMENT_LOGGER.tracef("Found and read index at: %s", indexFile);
return;
} catch (Exception e) {
ServerLogger.DEPLOYMENT_LOGGER.cannotLoadAnnotationIndex(indexFile.getPathName(), e.toString());
}
}
// if this flag is present and set to false then do not index the resource
Boolean shouldIndexResource = resourceRoot.getAttachment(Attachments.INDEX_RESOURCE_ROOT);
if (shouldIndexResource != null && !shouldIndexResource) {
return;
}
final List<String> indexIgnorePathList = resourceRoot.getAttachment(Attachments.INDEX_IGNORE_PATHS);
final Set<String> indexIgnorePaths;
if (indexIgnorePathList != null && !indexIgnorePathList.isEmpty()) {
indexIgnorePaths = new HashSet<String>(indexIgnorePathList);
} else {
indexIgnorePaths = null;
}
final VirtualFile virtualFile = resourceRoot.getRoot();
final Indexer indexer = new Indexer();
try {
final VisitorAttributes visitorAttributes = new VisitorAttributes();
visitorAttributes.setLeavesOnly(true);
visitorAttributes.setRecurseFilter(new VirtualFileFilter() {
public boolean accepts(VirtualFile file) {
return indexIgnorePaths == null || !indexIgnorePaths.contains(file.getPathNameRelativeTo(virtualFile));
}
});
final List<VirtualFile> classChildren = virtualFile.getChildren(new SuffixMatchFilter(".class", visitorAttributes));
for (VirtualFile classFile : classChildren) {
InputStream inputStream = null;
try {
inputStream = classFile.openStream();
indexer.index(inputStream);
} catch (Exception e) {
ServerLogger.DEPLOYMENT_LOGGER.cannotIndexClass(classFile.getPathNameRelativeTo(virtualFile), virtualFile.getPathName(), e);
} finally {
VFSUtils.safeClose(inputStream);
}
}
final Index index = indexer.complete();
resourceRoot.putAttachment(Attachments.ANNOTATION_INDEX, index);
ServerLogger.DEPLOYMENT_LOGGER.tracef("Generated index for archive %s", virtualFile);
} catch (Throwable t) {
throw ServerLogger.ROOT_LOGGER.deploymentIndexingFailed(t);
}
}
}