-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: the version of hook should not be stored in Java code, but read… (
#50) feat: the version of hook should not be stored in Java code, but read from jar manifest, Bundle-Version manifest entry can be auto-generated using maven or manually handled by adding to manifest.md.
- Loading branch information
Showing
3 changed files
with
73 additions
and
4 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
58 changes: 58 additions & 0 deletions
58
src/main/java/ch/sbb/polarion/extension/interceptor/util/HookManifestUtils.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,58 @@ | ||
package ch.sbb.polarion.extension.interceptor.util; | ||
|
||
import ch.sbb.polarion.extension.interceptor.model.ActionHook; | ||
import lombok.experimental.UtilityClass; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.io.IOException; | ||
import java.net.URISyntaxException; | ||
import java.net.URL; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.security.CodeSource; | ||
import java.util.jar.Attributes; | ||
import java.util.jar.JarFile; | ||
import java.util.jar.Manifest; | ||
|
||
@UtilityClass | ||
@SuppressWarnings("unused") | ||
public class HookManifestUtils { | ||
|
||
public static final String BUNDLE_VERSION = "Bundle-Version"; | ||
|
||
public static @Nullable String getHookVersion(@NotNull Class<? extends ActionHook> hookClass) { | ||
try { | ||
Manifest manifest = loadManifestByClass(hookClass); | ||
if (manifest != null) { | ||
Attributes attributes = manifest.getMainAttributes(); | ||
return attributes.getValue(BUNDLE_VERSION); | ||
} else { | ||
return null; | ||
} | ||
} catch (IOException | URISyntaxException e) { | ||
throw new IllegalStateException(e); | ||
} | ||
} | ||
|
||
public static @Nullable Manifest loadManifestByClass(@NotNull Class<? extends ActionHook> hookClass) throws IOException, URISyntaxException { | ||
CodeSource codeSource = hookClass.getProtectionDomain().getCodeSource(); | ||
|
||
if (codeSource != null) { | ||
URL location = codeSource.getLocation(); | ||
Path jarPath = Paths.get(location.toURI()); | ||
|
||
if (Files.isRegularFile(jarPath)) { | ||
try (JarFile jarFile = new JarFile(jarPath.toFile())) { | ||
return jarFile.getManifest(); | ||
} | ||
} else { | ||
throw new IllegalArgumentException("The class %s is not loaded from a JAR file.".formatted(hookClass.getName())); | ||
} | ||
} else { | ||
throw new IllegalArgumentException("Could not determine the code source location."); | ||
} | ||
} | ||
|
||
} |
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