Skip to content

Commit 09054e8

Browse files
authored
Merge pull request #131 from mercyblitz/dev
Test coverage
2 parents c8d4e4b + 96bcdd2 commit 09054e8

116 files changed

Lines changed: 2671 additions & 803 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

microsphere-annotation-processor/src/test/java/io/microsphere/annotation/processor/ConfigurationPropertyProcessingTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
package io.microsphere.annotation.processor;
1818

1919
import io.microsphere.annotation.ConfigurationProperty;
20-
import io.microsphere.classloading.ManifestArtifactResolver;
20+
import io.microsphere.classloading.ManifestArtifactResourceResolver;
2121
import org.junit.jupiter.api.Test;
2222

2323
import javax.lang.model.element.AnnotationMirror;
@@ -42,12 +42,12 @@ public class ConfigurationPropertyProcessingTest extends AbstractAnnotationProce
4242

4343
@Override
4444
protected void addCompiledClasses(Set<Class<?>> compiledClasses) {
45-
compiledClasses.add(ManifestArtifactResolver.class);
45+
compiledClasses.add(ManifestArtifactResourceResolver.class);
4646
}
4747

4848
@Test
4949
public void test() {
50-
TypeElement type = getTypeElement(ManifestArtifactResolver.class);
50+
TypeElement type = getTypeElement(ManifestArtifactResourceResolver.class);
5151
List<VariableElement> fields = getDeclaredFields(type);
5252
assertNotNull(fields);
5353
for (VariableElement field : fields) {

microsphere-java-core/src/main/java/io/microsphere/classloading/AbstractArtifactResolver.java

Lines changed: 0 additions & 64 deletions
This file was deleted.
Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package io.microsphere.classloading;
18+
19+
import io.microsphere.annotation.Nullable;
20+
import io.microsphere.logging.Logger;
21+
22+
import java.io.File;
23+
import java.io.FileInputStream;
24+
import java.io.IOException;
25+
import java.io.InputStream;
26+
import java.net.URL;
27+
import java.util.List;
28+
import java.util.Set;
29+
import java.util.jar.JarEntry;
30+
import java.util.jar.JarFile;
31+
32+
import static io.microsphere.collection.CollectionUtils.first;
33+
import static io.microsphere.collection.ListUtils.first;
34+
import static io.microsphere.io.FileUtils.resolveRelativePath;
35+
import static io.microsphere.io.IOUtils.close;
36+
import static io.microsphere.io.scanner.SimpleFileScanner.INSTANCE;
37+
import static io.microsphere.logging.LoggerFactory.getLogger;
38+
import static io.microsphere.net.URLUtils.resolveArchiveFile;
39+
import static io.microsphere.util.Assert.assertNotNull;
40+
import static io.microsphere.util.ClassLoaderUtils.getDefaultClassLoader;
41+
import static io.microsphere.util.jar.JarUtils.filter;
42+
43+
/**
44+
* Abstract {@link ArtifactResourceResolver} class
45+
*
46+
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
47+
* @see ArtifactResourceResolver
48+
* @since 1.0.0
49+
*/
50+
public abstract class AbstractArtifactResourceResolver implements ArtifactResourceResolver {
51+
52+
protected final Logger logger = getLogger(getClass());
53+
54+
protected final ClassLoader classLoader;
55+
56+
protected final int priority;
57+
58+
protected AbstractArtifactResourceResolver(int priority) {
59+
this(getDefaultClassLoader(), priority);
60+
}
61+
62+
protected AbstractArtifactResourceResolver(ClassLoader classLoader, int priority) {
63+
assertNotNull(classLoader, () -> "The 'classLoader' must not be null");
64+
this.classLoader = classLoader;
65+
this.priority = priority;
66+
}
67+
68+
@Override
69+
public final Artifact resolve(URL resourceURL) {
70+
if (resourceURL == null) {
71+
return null;
72+
}
73+
74+
File archiveFile = resolveArchiveFile(resourceURL);
75+
InputStream artifactMetadataData = null;
76+
Artifact artifact = null;
77+
try {
78+
if (archiveFile == null) {
79+
if (logger.isTraceEnabled()) {
80+
logger.trace("The resourceURL['{}'] can't be resolved to be an archive file", resourceURL);
81+
}
82+
artifactMetadataData = readArtifactMetadataDataFromResource(resourceURL, classLoader);
83+
} else {
84+
artifactMetadataData = readArtifactMetadataDataFromArchiveFile(archiveFile);
85+
}
86+
if (artifactMetadataData != null) {
87+
artifact = resolve(resourceURL, artifactMetadataData, classLoader);
88+
}
89+
} catch (IOException e) {
90+
if (logger.isErrorEnabled()) {
91+
logger.error("The Artifact can't be resolved from the resource URL : {}", resourceURL, e);
92+
}
93+
} finally {
94+
// close the InputStream
95+
close(artifactMetadataData);
96+
}
97+
return artifact;
98+
}
99+
100+
@Nullable
101+
protected InputStream readArtifactMetadataDataFromResource(URL resourceURL, ClassLoader classLoader) throws IOException {
102+
return null;
103+
}
104+
105+
@Nullable
106+
protected InputStream readArtifactMetadataDataFromArchiveFile(File archiveFile) throws IOException {
107+
InputStream artifactMetadataData = null;
108+
if (archiveFile.isFile()) {
109+
artifactMetadataData = readArtifactMetadataDataFromFile(archiveFile);
110+
} else if (archiveFile.isDirectory()) {
111+
artifactMetadataData = readArtifactMetadataDataFromDirectory(archiveFile);
112+
}
113+
return artifactMetadataData;
114+
}
115+
116+
@Nullable
117+
protected InputStream readArtifactMetadataDataFromFile(File archiveFile) throws IOException {
118+
JarFile jarFile = new JarFile(archiveFile);
119+
JarEntry jarEntry = findArtifactMetadataEntry(jarFile);
120+
if (jarEntry == null) {
121+
if (logger.isTraceEnabled()) {
122+
logger.trace("The artifact metadata entry can't be resolved from the JarFile[path: '{}']", archiveFile);
123+
}
124+
return null;
125+
}
126+
return jarFile.getInputStream(jarEntry);
127+
}
128+
129+
@Nullable
130+
protected InputStream readArtifactMetadataDataFromDirectory(File directory) throws IOException {
131+
File artifactMetadataFile = findArtifactMetadata(directory);
132+
if (artifactMetadataFile == null) {
133+
if (logger.isTraceEnabled()) {
134+
logger.trace("The artifact metadata file can't be found in the directory[path: '{}']", directory);
135+
}
136+
return null;
137+
}
138+
return new FileInputStream(artifactMetadataFile);
139+
}
140+
141+
protected JarEntry findArtifactMetadataEntry(JarFile jarFile) throws IOException {
142+
List<JarEntry> entries = filter(jarFile, this::isArtifactMetadataEntry);
143+
return first(entries);
144+
}
145+
146+
protected File findArtifactMetadata(File directory) throws IOException {
147+
Set<File> files = INSTANCE.scan(directory, true, file -> isArtifactMetadataFile(directory, file));
148+
return first(files);
149+
}
150+
151+
protected boolean isArtifactMetadataEntry(JarEntry jarEntry) {
152+
return isArtifactMetadata(jarEntry.getName());
153+
}
154+
155+
protected boolean isArtifactMetadataFile(File directory, File file) {
156+
String path = resolveRelativePath(directory, file);
157+
return isArtifactMetadata(path);
158+
}
159+
160+
protected boolean isArtifactMetadata(String relativePath) {
161+
return false;
162+
}
163+
164+
protected abstract Artifact resolve(URL resourceURL, InputStream artifactMetadataData, ClassLoader classLoader) throws IOException;
165+
166+
@Override
167+
public final int getPriority() {
168+
return priority;
169+
}
170+
171+
@Override
172+
public final String toString() {
173+
final StringBuilder sb = new StringBuilder(getClass().getSimpleName());
174+
sb.append('{');
175+
sb.append("classLoader=").append(classLoader);
176+
sb.append(", priority=").append(priority);
177+
sb.append('}');
178+
return sb.toString();
179+
}
180+
}

microsphere-java-core/src/main/java/io/microsphere/classloading/Artifact.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import java.util.Objects;
99
import java.util.function.Function;
1010

11+
import static java.util.Objects.hash;
12+
1113
/**
1214
* Artifact entity
1315
*
@@ -111,7 +113,7 @@ public boolean equals(Object o) {
111113

112114
@Override
113115
public int hashCode() {
114-
return Objects.hash(artifactId, version, location);
116+
return hash(artifactId, version, location);
115117
}
116118

117119
@Override

microsphere-java-core/src/main/java/io/microsphere/classloading/ArtifactDetector.java

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,13 @@
22

33
import io.microsphere.annotation.Nullable;
44
import io.microsphere.logging.Logger;
5-
import io.microsphere.util.ClassUtils;
65

76
import java.net.URL;
87
import java.util.Iterator;
98
import java.util.LinkedHashSet;
109
import java.util.LinkedList;
1110
import java.util.List;
1211
import java.util.Set;
13-
import java.util.StringJoiner;
14-
import java.util.stream.Collectors;
1512

1613
import static io.microsphere.collection.CollectionUtils.isEmpty;
1714
import static io.microsphere.logging.LoggerFactory.getLogger;
@@ -37,19 +34,19 @@ public class ArtifactDetector {
3734

3835
final ClassLoader classLoader;
3936

40-
private final List<ArtifactResolver> artifactResolvers;
37+
private final List<ArtifactResourceResolver> artifactResourceResolvers;
4138

4239
public ArtifactDetector() {
4340
this(null);
4441
}
4542

4643
public ArtifactDetector(@Nullable ClassLoader classLoader) {
4744
ClassLoader actualClassLoader = classLoader == null ? getClassLoader(getClass()) : classLoader;
48-
List<ArtifactResolver> artifactResolvers = loadServicesList(ArtifactResolver.class, actualClassLoader, true);
45+
this.artifactResourceResolvers = loadServicesList(ArtifactResourceResolver.class, actualClassLoader, true);
4946
this.classLoader = actualClassLoader;
50-
this.artifactResolvers = artifactResolvers;
5147
if (logger.isTraceEnabled()) {
52-
logger.trace("ClassLoader[argument : {} , actual : {}] , ArtifactResolver List : {}", classLoader, actualClassLoader, getArtifactResolverClassNames(artifactResolvers));
48+
logger.trace("ClassLoader[argument : {} , actual : {}] , ArtifactResolver List : {}",
49+
classLoader, actualClassLoader, this.artifactResourceResolvers);
5350
}
5451
}
5552

@@ -68,13 +65,16 @@ protected List<Artifact> detect(Set<URL> classPathURLs) {
6865
}
6966

7067
List<Artifact> artifactList = new LinkedList<>();
71-
for (ArtifactResolver artifactResolver : artifactResolvers) {
72-
Set<Artifact> artifactSet = artifactResolver.resolve(classPathURLs);
73-
for (Artifact artifact : artifactSet) {
74-
artifactList.add(artifact);
75-
classPathURLs.remove(artifact.getLocation());
68+
for (URL resourceURL : classPathURLs) {
69+
for (ArtifactResourceResolver artifactResourceResolver : artifactResourceResolvers) {
70+
Artifact artifact = artifactResourceResolver.resolve(resourceURL);
71+
if (artifact != null) {
72+
artifactList.add(artifact);
73+
break;
74+
}
7675
}
7776
}
77+
7878
return unmodifiableList(artifactList);
7979
}
8080

@@ -85,12 +85,8 @@ protected Set<URL> getClassPathURLs(boolean includedJdkLibraries) {
8585
removeJdkClassPathURLs(classPathURLs);
8686
}
8787
if (logger.isTraceEnabled()) {
88-
StringJoiner stringJoiner = new StringJoiner(System.lineSeparator());
89-
for (URL classPathURL : classPathURLs) {
90-
stringJoiner.add(classPathURL.toString());
91-
}
9288
logger.trace("ClassLoader[{}] covers the URLs[expected: {}, actual: {}], class-path : {}",
93-
classLoader, urls.size(), classPathURLs.size(), stringJoiner);
89+
classLoader, urls.size(), classPathURLs.size(), classPathURLs);
9490
}
9591
return classPathURLs;
9692
}
@@ -108,11 +104,4 @@ private void removeJdkClassPathURLs(Set<URL> classPathURLs) {
108104
}
109105
}
110106

111-
private List<String> getArtifactResolverClassNames(List<ArtifactResolver> artifactResolvers) {
112-
return artifactResolvers.stream()
113-
.map(Object::getClass)
114-
.map(ClassUtils::getTypeName)
115-
.collect(Collectors.toList());
116-
}
117-
118107
}

0 commit comments

Comments
 (0)