-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
134 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
core/src/main/java/net/cactusthorn/config/core/loader/ClasspathJarManifestLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package net.cactusthorn.config.core.loader; | ||
|
||
import java.util.jar.Attributes; | ||
import java.util.jar.JarFile; | ||
import java.util.jar.Manifest; | ||
|
||
import static net.cactusthorn.config.core.util.ApiMessages.msg; | ||
import static net.cactusthorn.config.core.util.ApiMessages.Key.*; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.net.URI; | ||
import java.net.URL; | ||
import java.util.Collections; | ||
import java.util.Enumeration; | ||
import java.util.Map; | ||
import java.util.logging.Logger; | ||
import java.util.stream.Collectors; | ||
|
||
public class ClasspathJarManifestLoader implements Loader { | ||
|
||
private static final Logger LOG = Logger.getLogger(ClasspathJarManifestLoader.class.getName()); | ||
|
||
private static final String SUB_PREFIX = "jar:manifest?"; | ||
private static final String PREFIX = "classpath:" + SUB_PREFIX; | ||
|
||
@Override public boolean accept(URI uri) { | ||
return uri.toString().startsWith(PREFIX); | ||
} | ||
|
||
@Override public Map<String, String> load(URI uri, ClassLoader classLoader) { | ||
|
||
String param = uri.getSchemeSpecificPart().substring(SUB_PREFIX.length()); | ||
String[] parts = param.split("=", 2); | ||
String name = parts[0]; | ||
String value = parts.length > 1 ? parts[1] : null; | ||
|
||
try { | ||
Enumeration<URL> urls = getClass().getClassLoader().getResources(JarFile.MANIFEST_NAME); | ||
while (urls.hasMoreElements()) { | ||
URL url = urls.nextElement(); | ||
try (InputStream in = url.openStream()) { | ||
|
||
Manifest manifest = new Manifest(in); | ||
Attributes attributes = manifest.getMainAttributes(); | ||
String attribute = attributes.getValue(name); | ||
if (attribute != null && (value == null || value.equals(attribute))) { | ||
return attributes.entrySet().stream() | ||
.collect(Collectors.toMap(e -> e.getKey().toString(), e -> e.getValue().toString())); | ||
} | ||
} | ||
} | ||
} catch (IOException e) { | ||
LOG.info(msg(CANT_LOAD_RESOURCE, uri.toString(), e.toString())); | ||
return Collections.emptyMap(); | ||
} | ||
|
||
if (value != null) { | ||
LOG.info(msg(MANIFEST_NOT_FOUND_1, name, value)); | ||
return Collections.emptyMap(); | ||
} | ||
LOG.info(msg(MANIFEST_NOT_FOUND_2, name)); | ||
return Collections.emptyMap(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
core/src/test/java/net/cactusthorn/config/core/loader/ClasspathJarManifestLoaderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package net.cactusthorn.config.core.loader; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
import java.net.URI; | ||
import java.util.Map; | ||
import java.util.logging.Handler; | ||
import java.util.logging.Level; | ||
import java.util.logging.LogManager; | ||
import java.util.logging.Logger; | ||
|
||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class ClasspathJarManifestLoaderTest { | ||
|
||
private static final Loader LOADER = new ClasspathJarManifestLoader(); | ||
private static final ClassLoader CL = ClasspathJarManifestLoaderTest.class.getClassLoader(); | ||
|
||
@BeforeAll static void setUpLogger() { | ||
Logger rootLogger = LogManager.getLogManager().getLogger(""); | ||
rootLogger.setLevel(Level.FINE); | ||
// switch off default Handlers to do not get anything in console | ||
for (Handler h : rootLogger.getHandlers()) { | ||
h.setLevel(Level.OFF); | ||
} | ||
} | ||
|
||
@Test public void accept() { | ||
assertTrue(LOADER.accept(URI.create("classpath:jar:manifest?a=b"))); | ||
} | ||
|
||
@Test public void notAccept() { | ||
assertFalse(LOADER.accept(URI.create("classpath:a.xml#ISO-8859-1"))); | ||
} | ||
|
||
@Test public void notFoundNameValue() { | ||
LOADER.load(URI.create("classpath:jar:manifest?a=b"), CL); | ||
} | ||
|
||
@Test public void notFoundName() { | ||
LOADER.load(URI.create("classpath:jar:manifest?a"), CL); | ||
} | ||
|
||
@Test public void load() { | ||
Map<String, String> result = LOADER.load(URI.create("classpath:jar:manifest?Bundle-Name=JUnit%20Jupiter%20API"), CL); | ||
assertEquals("junit-jupiter-api", result.get("Implementation-Title")); | ||
} | ||
|
||
@Test public void loadOnlyName() { | ||
Map<String, String> result = LOADER.load(URI.create("classpath:jar:manifest?Bundle-Name"), CL); | ||
assertFalse(result.isEmpty()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters