Skip to content
This repository has been archived by the owner on Jun 7, 2021. It is now read-only.

Commit

Permalink
Refactor the Layout class a bit, and add some tests. tcrawley.
Browse files Browse the repository at this point in the history
  • Loading branch information
bobmcwhirter committed Nov 11, 2015
1 parent 5dd8ef2 commit 7ef89ab
Show file tree
Hide file tree
Showing 9 changed files with 328 additions and 76 deletions.
57 changes: 53 additions & 4 deletions bootstrap/pom.xml
Expand Up @@ -59,19 +59,68 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<executions>
<execution>
<id>add-test-source</id>
<phase>generate-test-sources</phase>
<goals>
<goal>add-test-source</goal>
</goals>
<configuration>
<sources>
<source>src/it/java</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<executions>
<execution>
<id>integration-test</id>
<phase>integration-test</phase>
<goals>
<goal>integration-test</goal>
</goals>
<configuration>
<testSourceDirectory>src/it/java</testSourceDirectory>
<failIfNoTests>true</failIfNoTests>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
<groupId>org.jboss.modules</groupId>
<artifactId>jboss-modules</artifactId>
</dependency>
<dependency>
<groupId>org.jboss.modules</groupId>
<artifactId>jboss-modules</artifactId>
</dependency>
<dependency>
<groupId>org.jboss.spec.javax.sql</groupId>
<artifactId>jboss-javax-sql-api_7.0_spec</artifactId>
<version>${version.org.jboss.spec.javax.sql.jboss-javax-sql-api_7.0_spec}</version>
</dependency>
<dependency>
<groupId>org.jboss.shrinkwrap</groupId>
<artifactId>shrinkwrap-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.shrinkwrap</groupId>
<artifactId>shrinkwrap-spi</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.shrinkwrap</groupId>
<artifactId>shrinkwrap-impl-base</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

</project>
116 changes: 116 additions & 0 deletions bootstrap/src/it/java/org/wildfly/swarm/bootstrap/LayoutIT.java
@@ -0,0 +1,116 @@
package org.wildfly.swarm.bootstrap;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.CodeSource;
import java.util.Arrays;
import java.util.jar.Attributes;
import java.util.jar.JarFile;
import java.util.jar.Manifest;

import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.ByteArrayAsset;
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
import org.jboss.shrinkwrap.api.exporter.ZipExporter;
import org.jboss.shrinkwrap.api.importer.ZipImporter;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.Test;
import org.wildfly.swarm.bootstrap.util.Layout;

import static org.fest.assertions.Assertions.assertThat;

/**
* @author Bob McWhirter
*/
public class LayoutIT {

@Test
public void testIsUberJar() throws Exception {
JavaArchive archive = createBootstrapArchive();

archive.addAsManifestResource(EmptyAsset.INSTANCE, "wildfly-swarm.properties");

ClassLoader cl = createClassLoader(archive);
Class<?> layoutClass = cl.loadClass(Layout.class.getName());

Method getInstance = layoutClass.getMethod("getInstance");
Object layout = getInstance.invoke(layoutClass);

Method isUberJar = layoutClass.getMethod("isUberJar");
Object result = isUberJar.invoke(layout);

assertThat(result).isEqualTo(true);
}

@Test
public void testGetManifest() throws Exception {
JavaArchive archive = createBootstrapArchive();

archive.addAsManifestResource(EmptyAsset.INSTANCE, "wildfly-swarm.properties");

Manifest manifest = new Manifest();

manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
manifest.getMainAttributes().put(new Attributes.Name("Wildfly-Swarm-Main-Class"), "MyMainClass");

ByteArrayOutputStream out = new ByteArrayOutputStream();
manifest.write(out);
out.close();
archive.addAsManifestResource(new ByteArrayAsset(out.toByteArray()), "MANIFEST.MF");

ClassLoader cl = createClassLoader(archive);
Class<?> layoutClass = cl.loadClass(Layout.class.getName());

Method getInstance = layoutClass.getMethod("getInstance");
Object layout = getInstance.invoke(layoutClass);

Method isUberJar = layoutClass.getMethod("isUberJar");
Object result = isUberJar.invoke(layout);

assertThat(result).isEqualTo(true);

Method getManifest = layoutClass.getMethod("getManifest");
Manifest fetchedManifest = (Manifest) getManifest.invoke(layout);

assertThat(fetchedManifest).isNotNull();
assertThat(fetchedManifest.getMainAttributes().get(new Attributes.Name("Wildfly-Swarm-Main-Class"))).isEqualTo("MyMainClass");
}

protected ClassLoader createClassLoader(JavaArchive archive) throws IOException {
File tmpFile = export(archive);
return new URLClassLoader(new URL[]{tmpFile.toURI().toURL()}, null);
}

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;
}

protected JavaArchive createBootstrapArchive() throws IOException {
JavaArchive archive = ShrinkWrap.create(JavaArchive.class);
archive.as(ZipImporter.class).importFrom(new JarFile(findBootstrapJar()));
return archive;
}

protected File findBootstrapJar() {
Path targetDir = Paths.get("target");

File[] children = targetDir.toFile().listFiles();
for (File child : children) {
if (child.getName().startsWith("wildfly-swarm-bootstrap") && child.getName().endsWith(".jar") && !child.getName().endsWith("-sources.jar")) {
return child;
}
}

return null;
}
}
Expand Up @@ -34,7 +34,7 @@ public static void main(String[] args) throws Throwable {
System.setProperty("boot.module.loader", BootModuleLoader.class.getName());

String mainClassName = null;
Manifest manifest = Layout.getManifest();
Manifest manifest = Layout.getInstance().getManifest();

if (manifest != null) {
mainClassName = (String) manifest.getMainAttributes().get(new Attributes.Name("Wildfly-Swarm-Main-Class"));
Expand Down
Expand Up @@ -20,6 +20,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
Expand Down Expand Up @@ -58,7 +59,7 @@ public ModuleSpec findModule(ModuleIdentifier identifier, ModuleLoader delegateL
ModuleSpec.Builder builder = ModuleSpec.build(identifier);

try {
if (Layout.isFatJar()) {
if (Layout.getInstance().isUberJar()) {
gatherJarsFromJar(builder);
} else {
ClassLoader cl = ClassLoader.getSystemClassLoader();
Expand Down Expand Up @@ -88,6 +89,8 @@ public ModuleSpec findModule(ModuleIdentifier identifier, ModuleLoader delegateL
}
} catch (IOException e) {
throw new ModuleLoadException(e);
} catch (URISyntaxException e) {
throw new ModuleLoadException(e);
}

builder.addDependency(DependencySpec.createModuleDependencySpec(ModuleIdentifier.create("org.jboss.modules")));
Expand Down
Expand Up @@ -19,6 +19,7 @@
import org.wildfly.swarm.bootstrap.util.Layout;

import java.io.*;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
Expand All @@ -45,20 +46,22 @@ public ModuleSpec findModule(ModuleIdentifier identifier, ModuleLoader delegateL
ModuleSpec.Builder builder = ModuleSpec.build(identifier);

try {
if (Layout.isFatJar()) {
if (Layout.getInstance().isUberJar()) {
gatherJarsFromJar(builder);
}

builder.addDependency(DependencySpec.createLocalDependencySpec());
builder.addDependency(DependencySpec.createModuleDependencySpec(ModuleIdentifier.create("org.jboss.modules")));
builder.addDependency(DependencySpec.createModuleDependencySpec(ModuleIdentifier.create("org.jboss.msc")));
builder.addDependency(DependencySpec.createModuleDependencySpec(ModuleIdentifier.create("org.jboss.shrinkwrap")));
builder.addDependency(DependencySpec.createModuleDependencySpec(ModuleIdentifier.create("javax.api")));

return builder.create();
} catch (IOException e) {
throw new ModuleLoadException(e);
} catch (URISyntaxException e) {
throw new ModuleLoadException(e);
}

builder.addDependency(DependencySpec.createLocalDependencySpec());
builder.addDependency(DependencySpec.createModuleDependencySpec(ModuleIdentifier.create("org.jboss.modules")));
builder.addDependency(DependencySpec.createModuleDependencySpec(ModuleIdentifier.create("org.jboss.msc")));
builder.addDependency(DependencySpec.createModuleDependencySpec(ModuleIdentifier.create("org.jboss.shrinkwrap")));
builder.addDependency(DependencySpec.createModuleDependencySpec(ModuleIdentifier.create("javax.api")));

return builder.create();
}

protected void gatherJarsFromJar(ModuleSpec.Builder builder) throws IOException {
Expand Down
Expand Up @@ -17,6 +17,7 @@

import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;

import org.jboss.modules.Environment;
import org.jboss.modules.ModuleFinder;
Expand All @@ -37,36 +38,42 @@ public class ClasspathModuleFinder implements ModuleFinder {
public ModuleSpec findModule(ModuleIdentifier identifier, ModuleLoader delegateLoader) throws ModuleLoadException {
final String path = "modules/" + identifier.getName().replace('.', '/') + "/" + identifier.getSlot() + "/module.xml";

ClassLoader cl = Layout.getBootstrapClassLoader();
InputStream in = cl.getResourceAsStream(path);

if (in == null && cl != ClasspathModuleFinder.class.getClassLoader()) {
in = ClasspathModuleFinder.class.getClassLoader().getResourceAsStream(path);
}
try {
ClassLoader cl = Layout.getInstance().getBootstrapClassLoader();
InputStream in = cl.getResourceAsStream(path);

if (in == null) {
return null;
}
if (in == null && cl != ClasspathModuleFinder.class.getClassLoader()) {
in = ClasspathModuleFinder.class.getClassLoader().getResourceAsStream(path);
}

ModuleSpec moduleSpec = null;
try {
moduleSpec = ModuleXmlParserBridge.parseModuleXml(new ModuleXmlParserBridge.ResourceRootFactoryBridge() {
@Override
public ResourceLoader createResourceLoader(final String rootPath, final String loaderPath, final String loaderName) throws IOException {
return Environment.getModuleResourceLoader(rootPath, loaderPath, loaderName);
}
}, "/", in, path.toString(), delegateLoader, identifier);
if (in == null) {
return null;
}

} catch (IOException e) {
throw new ModuleLoadException(e);
} finally {
ModuleSpec moduleSpec = null;
try {
in.close();
moduleSpec = ModuleXmlParserBridge.parseModuleXml(new ModuleXmlParserBridge.ResourceRootFactoryBridge() {
@Override
public ResourceLoader createResourceLoader(final String rootPath, final String loaderPath, final String loaderName) throws IOException {
return Environment.getModuleResourceLoader(rootPath, loaderPath, loaderName);
}
}, "/", in, path.toString(), delegateLoader, identifier);

} catch (IOException e) {
throw new ModuleLoadException(e);
} finally {
try {
in.close();
} catch (IOException e) {
throw new ModuleLoadException(e);
}
}
return moduleSpec;
} catch (IOException e) {
throw new ModuleLoadException(e);
} catch (URISyntaxException e) {
throw new ModuleLoadException(e);
}
return moduleSpec;

}
}

0 comments on commit 7ef89ab

Please sign in to comment.