下面列出了怎么用org.jboss.shrinkwrap.api.exporter.ZipExporter的API类实例代码及写法,或者点击链接到github查看源代码。
private void createShrinkWrapDomain() {
ClassLoader originalCl = Thread.currentThread().getContextClassLoader();
try {
if (isFatJar()) {
Module appModule = Module.getBootModuleLoader().loadModule(APPLICATION_MODULE_NAME);
Thread.currentThread().setContextClassLoader(appModule.getClassLoader());
}
Domain domain = ShrinkWrap.getDefaultDomain();
domain.getConfiguration().getExtensionLoader().addOverride(ZipExporter.class, ZipExporterImpl.class);
domain.getConfiguration().getExtensionLoader().addOverride(ZipImporter.class, ZipImporterImpl.class);
domain.getConfiguration().getExtensionLoader().addOverride(ExplodedExporter.class, ExplodedExporterImpl.class);
domain.getConfiguration().getExtensionLoader().addOverride(ExplodedImporter.class, ExplodedImporterImpl.class);
domain.getConfiguration().getExtensionLoader().addOverride(JavaArchive.class, JavaArchiveImpl.class);
domain.getConfiguration().getExtensionLoader().addOverride(WebArchive.class, WebArchiveImpl.class);
} catch (Exception e) {
SwarmMessages.MESSAGES.shrinkwrapDomainSetupFailed(e);
} finally {
Thread.currentThread().setContextClassLoader(originalCl);
}
}
public void repackageWar(File file) throws IOException {
if (!filterWebInfLib) {
return;
}
this.log.info("Repackaging .war: " + file);
Path backupPath = get(file);
move(file, backupPath, this.log);
Archive original = ShrinkWrap.create(JavaArchive.class);
try (InputStream inputStream = Files.newInputStream(backupPath)) {
original.as(ZipImporter.class).importFrom(inputStream);
}
Archive repackaged = new WebInfLibFilteringArchive(original, this.dependencyManager);
repackaged.as(ZipExporter.class).exportTo(file, true);
this.log.info("Repackaged .war: " + file);
}
@Test
public void test_load_plugins_folder_contains_jar_file() throws Exception {
final File pluginsFolder = temporaryFolder.newFolder("extension");
final File pluginFolder = temporaryFolder.newFolder("extension", "plugin1");
final File file = new File(pluginFolder, "extension.jar");
FileUtils.writeStringToFile(pluginFolder.toPath().resolve("hivemq-extension.xml").toFile(),
validPluginXML1,
Charset.defaultCharset());
final JavaArchive javaArchive = ShrinkWrap.create(JavaArchive.class).
addAsServiceProviderAndClasses(ExtensionMain.class, TestExtensionMainImpl.class);
javaArchive.as(ZipExporter.class).exportTo(file);
when(classServiceLoader.load(eq(ExtensionMain.class), any(ClassLoader.class))).thenReturn(new ArrayList<>());
pluginLoader.loadPlugins(pluginsFolder.toPath(), false, ExtensionMain.class);
//Let's verify that the extension was loaded
verify(classServiceLoader).load(any(Class.class), any(ClassLoader.class));
}
@Override
public ArtifactSpec resolve(ArtifactSpec spec) throws Exception {
File resolved = resolvedArtifacts.get(spec);
if (resolved == null) {
Archive archive = artifacts.get(spec);
if (archive != null) {
resolved = File.createTempFile(spec.artifactId(), ".jar");
resolved.delete();
resolved.deleteOnExit();
archive.as(ZipExporter.class).exportTo(resolved);
this.resolvedArtifacts.put(spec, resolved);
}
}
spec.file = resolved;
return spec;
}
/*******************************
* loadSinglePlugin(...) Tests *
*******************************/
@Test(timeout = 5000)
public void test_load_single_plugin_load_and_instantiate_enabled() throws Throwable {
final File pluginFolder1 = temporaryFolder.newFolder("extension", "plugin1");
FileUtils.writeStringToFile(pluginFolder1.toPath().resolve("hivemq-extension.xml").toFile(),
validPluginXML1,
Charset.defaultCharset());
final File file = new File(pluginFolder1, "extension.jar");
final JavaArchive javaArchive = ShrinkWrap.create(JavaArchive.class).
addAsServiceProviderAndClasses(ExtensionMain.class, TestExtensionMainImpl.class);
javaArchive.as(ZipExporter.class).exportTo(file);
final HiveMQExtension hiveMQExtension = realPluginLoader.loadSinglePlugin(
pluginFolder1.toPath(),
HiveMQPluginXMLReader.getPluginEntityFromXML(pluginFolder1.toPath(), true).get(),
ExtensionMain.class);
assertNotNull(hiveMQExtension);
hiveMQExtension.start(super.getTestPluginStartInput(), super.getTestPluginStartOutput());
assertTrue(hiveMQExtension.isEnabled());
}
@Test
public void testFractionMatchingWebXML() throws Exception {
JARArchive archive = ShrinkWrap.create(JARArchive.class);
archive.addAsResource("WEB-INF/web.xml");
FractionUsageAnalyzer analyzer = new FractionUsageAnalyzer();
final File out = Files.createTempFile(archive.getName(), ".war").toFile();
out.deleteOnExit();
archive.as(ZipExporter.class).exportTo(out, true);
analyzer.source(out);
assertThat(analyzer.detectNeededFractions()
.stream()
.filter(fd -> fd.getArtifactId().equals("security"))
.count()).isEqualTo(1);
}
private DisconnectOutboundInterceptor getIsolatedOutboundInterceptor(final @NotNull String name) throws Exception {
final JavaArchive javaArchive = ShrinkWrap.create(JavaArchive.class)
.addClass("com.hivemq.extensions.handler.DisconnectOutboundInterceptorHandlerTest$" + name);
final File jarFile = temporaryFolder.newFile();
javaArchive.as(ZipExporter.class).exportTo(jarFile, true);
final IsolatedPluginClassloader
cl =
new IsolatedPluginClassloader(new URL[]{jarFile.toURI().toURL()}, this.getClass().getClassLoader());
final Class<?> interceptorClass =
cl.loadClass("com.hivemq.extensions.handler.DisconnectOutboundInterceptorHandlerTest$" + name);
return (DisconnectOutboundInterceptor) interceptorClass.newInstance();
}
private DisconnectInboundInterceptor getIsolatedInboundInterceptor(final @NotNull String name) throws Exception {
final JavaArchive javaArchive = ShrinkWrap.create(JavaArchive.class)
.addClass("com.hivemq.extensions.handler.DisconnectInboundInterceptorHandlerTest$" + name);
final File jarFile = temporaryFolder.newFile();
javaArchive.as(ZipExporter.class).exportTo(jarFile, true);
final IsolatedPluginClassloader
cl =
new IsolatedPluginClassloader(new URL[]{jarFile.toURI().toURL()}, this.getClass().getClassLoader());
final Class<?> interceptorClass =
cl.loadClass("com.hivemq.extensions.handler.DisconnectInboundInterceptorHandlerTest$" + name);
return (DisconnectInboundInterceptor) interceptorClass.newInstance();
}
private @NotNull UnsubscribeInboundInterceptor getIsolatedInboundInterceptor(final @NotNull String name)
throws Exception {
final JavaArchive javaArchive = ShrinkWrap.create(JavaArchive.class)
.addClass("com.hivemq.extensions.handler.UnsubscribeInboundInterceptorHandlerTest$" + name);
final File jarFile = temporaryFolder.newFile();
javaArchive.as(ZipExporter.class).exportTo(jarFile, true);
final IsolatedPluginClassloader
cl =
new IsolatedPluginClassloader(new URL[]{jarFile.toURI().toURL()}, this.getClass().getClassLoader());
final Class<?> interceptorClass =
cl.loadClass("com.hivemq.extensions.handler.UnsubscribeInboundInterceptorHandlerTest$" + name);
return (UnsubscribeInboundInterceptor) interceptorClass.newInstance();
}
private SubackOutboundInterceptor getIsolatedOutboundInterceptor(final @NotNull String name) throws Exception {
final JavaArchive javaArchive = ShrinkWrap.create(JavaArchive.class)
.addClass("com.hivemq.extensions.handler.SubackOutboundInterceptorHandlerTest$" + name);
final File jarFile = temporaryFolder.newFile();
javaArchive.as(ZipExporter.class).exportTo(jarFile, true);
final IsolatedPluginClassloader
cl =
new IsolatedPluginClassloader(new URL[]{jarFile.toURI().toURL()}, this.getClass().getClassLoader());
final Class<?> interceptorClass =
cl.loadClass("com.hivemq.extensions.handler.SubackOutboundInterceptorHandlerTest$" + name);
return (SubackOutboundInterceptor) interceptorClass.newInstance();
}
@Test
public void testFractionMatching() throws Exception {
JARArchive archive = ShrinkWrap.create(JARArchive.class);
archive.addAsResource("META-INF/persistence.xml");
FractionUsageAnalyzer analyzer = new FractionUsageAnalyzer();
File out = Files.createTempFile(archive.getName(), ".war").toFile();
archive.as(ZipExporter.class).exportTo(out, true);
analyzer.source(out);
assertThat(analyzer.detectNeededFractions()
.stream()
.filter(fd -> fd.getArtifactId().equals("jpa"))
.count()).isEqualTo(1);
out.delete();
}
@NotNull
private PubackInboundInterceptor getInboundInterceptor(@NotNull final String name) throws Exception {
final JavaArchive javaArchive = ShrinkWrap.create(JavaArchive.class)
.addClass("com.hivemq.extensions.handler.PubackInterceptorHandlerTest$" + name);
final File jarFile = temporaryFolder.newFile();
javaArchive.as(ZipExporter.class).exportTo(jarFile, true);
//This classloader contains the classes from the jar file
final IsolatedPluginClassloader
cl =
new IsolatedPluginClassloader(new URL[]{jarFile.toURI().toURL()}, this.getClass().getClassLoader());
final Class<?> interceptorClass =
cl.loadClass("com.hivemq.extensions.handler.PubackInterceptorHandlerTest$" + name);
return (PubackInboundInterceptor) interceptorClass.newInstance();
}
@Test
public void testFractionMatching() throws Exception {
JARArchive archive = ShrinkWrap.create(JARArchive.class);
archive.addClass(MyTopicMDB.class);
FractionUsageAnalyzer analyzer = new FractionUsageAnalyzer();
final File out = Files.createTempFile(archive.getName(), ".war").toFile();
out.deleteOnExit();
archive.as(ZipExporter.class).exportTo(out, true);
analyzer.source(out);
assertThat(analyzer.detectNeededFractions()
.stream()
.filter(fd -> fd.getArtifactId().equals("messaging"))
.count()).isEqualTo(1);
}
@Test
public void testFractionMatching() throws Exception {
JARArchive archive = ShrinkWrap.create(JARArchive.class);
archive.addAsResource(EmptyAsset.INSTANCE,"WEB-INF/beans.xml");
FractionUsageAnalyzer analyzer = new FractionUsageAnalyzer();
final File out = Files.createTempFile(archive.getName(), ".war").toFile();
out.deleteOnExit();
archive.as(ZipExporter.class).exportTo(out, true);
analyzer.source(out);
assertThat(analyzer.detectNeededFractions()
.stream()
.filter(fd -> fd.getArtifactId().equals("cdi"))
.count()).isEqualTo(1);
}
@NotNull
private PubrecInboundInterceptor getInboundInterceptor(@NotNull final String name) throws Exception {
final JavaArchive javaArchive = ShrinkWrap.create(JavaArchive.class)
.addClass("com.hivemq.extensions.handler.PubrecInterceptorHandlerTest$" + name);
final File jarFile = temporaryFolder.newFile();
javaArchive.as(ZipExporter.class).exportTo(jarFile, true);
//This classloader contains the classes from the jar file
final IsolatedPluginClassloader
cl =
new IsolatedPluginClassloader(new URL[]{jarFile.toURI().toURL()}, this.getClass().getClassLoader());
final Class<?> interceptorClass =
cl.loadClass("com.hivemq.extensions.handler.PubrecInterceptorHandlerTest$" + name);
return (PubrecInboundInterceptor) interceptorClass.newInstance();
}
@NotNull
private PubrecOutboundInterceptor getOutboundInterceptor(@NotNull final String name) throws Exception {
final JavaArchive javaArchive = ShrinkWrap.create(JavaArchive.class)
.addClass("com.hivemq.extensions.handler.PubrecInterceptorHandlerTest$" + name);
final File jarFile = temporaryFolder.newFile();
javaArchive.as(ZipExporter.class).exportTo(jarFile, true);
//This classloader contains the classes from the jar file
final IsolatedPluginClassloader
cl =
new IsolatedPluginClassloader(new URL[]{jarFile.toURI().toURL()}, this.getClass().getClassLoader());
final Class<?> interceptorClass =
cl.loadClass("com.hivemq.extensions.handler.PubrecInterceptorHandlerTest$" + name);
return (PubrecOutboundInterceptor) interceptorClass.newInstance();
}
@NotNull
private PubrelInboundInterceptor getInboundInterceptor(@NotNull final String name) throws Exception {
final JavaArchive javaArchive = ShrinkWrap.create(JavaArchive.class)
.addClass("com.hivemq.extensions.handler.PubrelInterceptorHandlerTest$" + name);
final File jarFile = temporaryFolder.newFile();
javaArchive.as(ZipExporter.class).exportTo(jarFile, true);
//This classloader contains the classes from the jar file
final IsolatedPluginClassloader
cl =
new IsolatedPluginClassloader(new URL[]{jarFile.toURI().toURL()}, this.getClass().getClassLoader());
final Class<?> interceptorClass =
cl.loadClass("com.hivemq.extensions.handler.PubrelInterceptorHandlerTest$" + name);
return (PubrelInboundInterceptor) interceptorClass.newInstance();
}
@NotNull
private PubrelOutboundInterceptor getOutboundInterceptor(@NotNull final String name) throws Exception {
final JavaArchive javaArchive = ShrinkWrap.create(JavaArchive.class)
.addClass("com.hivemq.extensions.handler.PubrelInterceptorHandlerTest$" + name);
final File jarFile = temporaryFolder.newFile();
javaArchive.as(ZipExporter.class).exportTo(jarFile, true);
//This classloader contains the classes from the jar file
final IsolatedPluginClassloader
cl =
new IsolatedPluginClassloader(new URL[]{jarFile.toURI().toURL()}, this.getClass().getClassLoader());
final Class<?> interceptorClass =
cl.loadClass("com.hivemq.extensions.handler.PubrelInterceptorHandlerTest$" + name);
return (PubrelOutboundInterceptor) interceptorClass.newInstance();
}
private AuthorizerProvider getTestAuthorizerProvider(final String name, final CountDownLatch countDownLatch)
throws Exception {
final JavaArchive javaArchive = ShrinkWrap.create(JavaArchive.class)
.addClass("com.hivemq.extensions.handler.testextensions." + name)
.addClass("com.hivemq.extensions.handler.testextensions." + name + "$1");
final File jarFile = temporaryFolder.newFile();
javaArchive.as(ZipExporter.class).exportTo(jarFile, true);
//This classloader contains the classes from the jar file
final IsolatedPluginClassloader cl =
new IsolatedPluginClassloader(new URL[]{jarFile.toURI().toURL()}, this.getClass().getClassLoader());
final Class<?> providerClass = cl.loadClass("com.hivemq.extensions.handler.testextensions." + name);
final AuthorizerProvider testProvider =
(AuthorizerProvider) providerClass.getDeclaredConstructor(CountDownLatch.class)
.newInstance(countDownLatch);
return testProvider;
}
private @NotNull PingReqInboundInterceptor getIsolatedInboundInterceptor(final @NotNull String name) throws Exception {
final JavaArchive javaArchive = ShrinkWrap.create(JavaArchive.class)
.addClass("com.hivemq.extensions.handler.PingInterceptorHandlerTest$" + name);
final File jarFile = temporaryFolder.newFile();
javaArchive.as(ZipExporter.class).exportTo(jarFile, true);
final IsolatedPluginClassloader
cl =
new IsolatedPluginClassloader(new URL[]{jarFile.toURI().toURL()}, this.getClass().getClassLoader());
final Class<?> interceptorClass =
cl.loadClass("com.hivemq.extensions.handler.PingInterceptorHandlerTest$" + name);
final PingReqInboundInterceptor interceptor =
(PingReqInboundInterceptor) interceptorClass.newInstance();
return interceptor;
}
private @NotNull PingRespOutboundInterceptor getIsolatedOutboundInterceptor(final @NotNull String name)
throws Exception {
final JavaArchive javaArchive = ShrinkWrap.create(JavaArchive.class)
.addClass("com.hivemq.extensions.handler.PingInterceptorHandlerTest$" + name);
final File jarFile = temporaryFolder.newFile();
javaArchive.as(ZipExporter.class).exportTo(jarFile, true);
final IsolatedPluginClassloader
cl =
new IsolatedPluginClassloader(new URL[]{jarFile.toURI().toURL()}, this.getClass().getClassLoader());
final Class<?> interceptorClass =
cl.loadClass("com.hivemq.extensions.handler.PingInterceptorHandlerTest$" + name);
final PingRespOutboundInterceptor interceptor =
(PingRespOutboundInterceptor) interceptorClass.newInstance();
return interceptor;
}
@NotNull
private PubcompInboundInterceptor getInboundInterceptor(@NotNull final String name) throws Exception {
final JavaArchive javaArchive = ShrinkWrap.create(JavaArchive.class)
.addClass("com.hivemq.extensions.handler.PubcompInterceptorHandlerTest$" + name);
final File jarFile = temporaryFolder.newFile();
javaArchive.as(ZipExporter.class).exportTo(jarFile, true);
//This classloader contains the classes from the jar file
final IsolatedPluginClassloader
cl =
new IsolatedPluginClassloader(new URL[]{jarFile.toURI().toURL()}, this.getClass().getClassLoader());
final Class<?> interceptorClass =
cl.loadClass("com.hivemq.extensions.handler.PubcompInterceptorHandlerTest$" + name);
return (PubcompInboundInterceptor) interceptorClass.newInstance();
}
@NotNull
private PubcompOutboundInterceptor getOutboundInterceptor(@NotNull final String name) throws Exception {
final JavaArchive javaArchive = ShrinkWrap.create(JavaArchive.class)
.addClass("com.hivemq.extensions.handler.PubcompInterceptorHandlerTest$" + name);
final File jarFile = temporaryFolder.newFile();
javaArchive.as(ZipExporter.class).exportTo(jarFile, true);
//This classloader contains the classes from the jar file
final IsolatedPluginClassloader
cl =
new IsolatedPluginClassloader(new URL[]{jarFile.toURI().toURL()}, this.getClass().getClassLoader());
final Class<?> interceptorClass =
cl.loadClass("com.hivemq.extensions.handler.PubcompInterceptorHandlerTest$" + name);
return (PubcompOutboundInterceptor) interceptorClass.newInstance();
}
public byte[] addContent(Archive<?> archive) throws IOException, URISyntaxException {
try {
MessageDigest messageDigest = MessageDigest.getInstance("SHA-1");
byte[] sha1Bytes;
messageDigest.reset();
BufferedInputStream bis = new BufferedInputStream(archive.as(ZipExporter.class).exportAsInputStream());
byte[] bytes = new byte[8192];
int read;
while ((read = bis.read(bytes)) > -1) {
messageDigest.update(bytes, 0, read);
}
sha1Bytes = messageDigest.digest();
String key = toKey(sha1Bytes);
this.fs.addArchive(archive.getName(), archive);
this.index.put(key, this.fsMount.getChild(archive.getName()).toURI());
return sha1Bytes;
} catch (NoSuchAlgorithmException e) {
throw new IOException(e);
}
}
static void exportToQuarkusDeploymentPath(JavaArchive archive) throws IOException {
String exportPath = System.getProperty("quarkus.deploymentExportPath");
if (exportPath == null) {
return;
}
File exportDir = new File(exportPath);
if (exportDir.exists()) {
if (!exportDir.isDirectory()) {
throw new IllegalStateException("Export path is not a directory: " + exportPath);
}
try (Stream<Path> stream = Files.walk(exportDir.toPath())) {
stream.sorted(Comparator.reverseOrder()).map(Path::toFile)
.forEach(File::delete);
}
} else if (!exportDir.mkdirs()) {
throw new IllegalStateException("Export path could not be created: " + exportPath);
}
File exportFile = new File(exportDir, archive.getName());
archive.as(ZipExporter.class).exportTo(exportFile);
}
@Test
public void testDetectEmptyWarAsUndertow() throws Exception {
JavaArchive archive = ShrinkWrap.create(JavaArchive.class, "test.war");
archive.add(EmptyAsset.INSTANCE, "nothing");
FractionUsageAnalyzer analyzer = new FractionUsageAnalyzer();
final File out = Files.createTempFile(archive.getName(), ".war").toFile();
archive.as(ZipExporter.class).exportTo(out, true);
analyzer.source(out);
assertThat(analyzer.detectNeededFractions()
.stream()
.filter(fd -> fd.getArtifactId().equals("undertow"))
.count())
.isEqualTo(1);
out.delete();
}
@Override
public Archive<?> generateDeployment(TestDeployment testDeployment, Collection<ProtocolArchiveProcessor> collection) {
Archive<?> applicationArchive = testDeployment.getApplicationArchive();
boolean isClassPath = ClassPath.isRepresentedBy(applicationArchive);
for (Archive<?> auxiliaryArchive : testDeployment.getAuxiliaryArchives()) {
if (isClassPath) {
applicationArchive.add(auxiliaryArchive, ClassPath.ROOT_ARCHIVE_PATH, ZipExporter.class);
} else {
applicationArchive.merge(auxiliaryArchive);
}
}
return applicationArchive;
}
protected File export(JavaArchive archive) throws IOException {
File tmpFile = File.createTempFile("boostrap-archive", ".jar");
tmpFile.deleteOnExit();
tmpFile.delete();
archive.as(ZipExporter.class).exportTo(tmpFile);
return tmpFile;
}
@Test
public void test_load_plugins_folder_contain_wrong_id_in_xml() throws Exception {
final File pluginsFolder = temporaryFolder.newFolder("extension");
final File pluginFolder = temporaryFolder.newFolder("extension", "plugin1");
final File file = new File(pluginFolder, "extension.jar");
FileUtils.writeStringToFile(pluginFolder.toPath().resolve("hivemq-extension.xml").toFile(),
invalidPluginXML,
Charset.defaultCharset());
final JavaArchive javaArchive = ShrinkWrap.create(JavaArchive.class).
addAsServiceProviderAndClasses(ExtensionMain.class, TestExtensionMainImpl.class);
javaArchive.as(ZipExporter.class).exportTo(file);
final ImmutableList<HiveMQPluginEvent> pluginEvents =
pluginLoader.loadPlugins(pluginsFolder.toPath(), false, ExtensionMain.class);
assertEquals(0, pluginEvents.size());
//Let's verify that the extension was not loaded
verify(classServiceLoader, never()).load(any(Class.class), any(ClassLoader.class));
final File[] listFiles = pluginFolder.listFiles();
assertNotNull(listFiles);
assertEquals(2, listFiles.length);
boolean disabledFileFound = false;
for (final File listFile : listFiles) {
if (listFile != null && listFile.getName().equals("DISABLED")) {
disabledFileFound = true;
}
}
assertFalse(disabledFileFound);
}
@Test
public void test_load_plugins_folder_contains_two_plugin_folders() throws Exception {
final File pluginsFolder = temporaryFolder.newFolder("extension");
final File pluginFolder1 = temporaryFolder.newFolder("extension", "plugin1");
final File pluginFolder2 = temporaryFolder.newFolder("extension", "plugin2");
final File file = new File(pluginFolder1, "extension.jar");
final File file2 = new File(pluginFolder2, "extension.jar");
final JavaArchive javaArchive = ShrinkWrap.create(JavaArchive.class).
addAsServiceProviderAndClasses(ExtensionMain.class, TestExtensionMainImpl.class);
FileUtils.writeStringToFile(pluginFolder1.toPath().resolve("hivemq-extension.xml").toFile(),
validPluginXML1,
Charset.defaultCharset());
FileUtils.writeStringToFile(pluginFolder2.toPath().resolve("hivemq-extension.xml").toFile(),
validPluginXML2,
Charset.defaultCharset());
javaArchive.as(ZipExporter.class).exportTo(file);
javaArchive.as(ZipExporter.class).exportTo(file2);
when(classServiceLoader.load(eq(ExtensionMain.class), any(ClassLoader.class))).thenReturn(new ArrayList<>());
pluginLoader.loadPlugins(pluginsFolder.toPath(), false, ExtensionMain.class);
//Let's verify that the extensions were loaded
verify(classServiceLoader, times(2)).load(any(Class.class), any(ClassLoader.class));
}