Skip to content

Commit

Permalink
Initial support for legacy war deployments
Browse files Browse the repository at this point in the history
  • Loading branch information
stuartwdouglas committed Sep 18, 2018
1 parent c81e38b commit 76f9126
Show file tree
Hide file tree
Showing 15 changed files with 400 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.jboss.shamrock.deployment;

import java.nio.file.Path;
import java.util.Collection;
import java.util.Set;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.jboss.shamrock.deployment;

import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;

public class ArchiveContextBuilder {

private List<Path> additionalApplicationArchives = new ArrayList<>();

public ArchiveContextBuilder addAdditionalApplicationArchive(Path path) {
additionalApplicationArchives.add(path);
return this;
}

List<Path> getAdditionalApplicationArchives() {
return additionalApplicationArchives;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.jboss.shamrock.deployment;

import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@ public class BeanArchiveProcessor implements ResourceProcessor {
public void process(ArchiveContext archiveContext, ProcessorContext processorContext) throws Exception {
List<IndexView> indexes = new ArrayList<>();
for(ApplicationArchive archive : archiveContext.getAllApplicationArchives()) {
//TODO: this should not really be in core
if(archive.getChildPath("META-INF/beans.xml") != null) {
indexes.add(archive.getIndex());
} else if(archive.getChildPath("WEB-INF/beans.xml") != null) {
//TODO: how to handle WEB-INF?
indexes.add(archive.getIndex());
}
}
beanArchiveIndex.setIndex(CompositeIndex.create(indexes));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import java.util.Map;
import java.util.ServiceLoader;
import java.util.Set;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Function;
import java.util.logging.Level;
Expand Down Expand Up @@ -74,8 +73,9 @@ public class BuildTimeGenerator {
private final boolean useStaticInit;
private final List<Function<String, Function<ClassVisitor, ClassVisitor>>> bytecodeTransformers = new ArrayList<>();
private final Set<String> applicationArchiveMarkers;
private final ArchiveContextBuilder archiveContextBuilder;

public BuildTimeGenerator(ClassOutput classOutput, ClassLoader cl, boolean useStaticInit) {
public BuildTimeGenerator(ClassOutput classOutput, ClassLoader cl, boolean useStaticInit, ArchiveContextBuilder contextBuilder) {
this.useStaticInit = useStaticInit;
Iterator<ShamrockSetup> loader = ServiceLoader.load(ShamrockSetup.class, cl).iterator();
SetupContextImpl setupContext = new SetupContextImpl();
Expand All @@ -90,6 +90,7 @@ public BuildTimeGenerator(ClassOutput classOutput, ClassLoader cl, boolean useSt
this.injection = new DeploymentProcessorInjection(setupContext.injectionProviders);
this.classLoader = cl;
this.applicationArchiveMarkers = new HashSet<>(setupContext.applicationArchiveMarkers);
this.archiveContextBuilder = contextBuilder;
}

public List<Function<String, Function<ClassVisitor, ClassVisitor>>> getBytecodeTransformers() {
Expand Down Expand Up @@ -129,10 +130,10 @@ public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOEx
}
});
Index appIndex = indexer.complete();
List<ApplicationArchive> applicationArchives = ApplicationArchiveLoader.scanForOtherIndexes(classLoader, config, applicationArchiveMarkers, root);

List<ApplicationArchive> applicationArchives = ApplicationArchiveLoader.scanForOtherIndexes(classLoader, config, applicationArchiveMarkers, root, archiveContextBuilder.getAdditionalApplicationArchives());

ArchiveContext context = new ArchiveContextImpl(new ApplicationArchiveImpl(appIndex, root, null), applicationArchives, config);

ProcessorContextImpl processorContext = new ProcessorContextImpl();
processorContext.addResource("META-INF/microprofile-config.properties");
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class ApplicationArchiveLoader {
private static final String INDEX_DEPENDENCIES = "index-dependencies";
private static final String INDEX_JAR = "index-jar";

public static List<ApplicationArchive> scanForOtherIndexes(ClassLoader classLoader, BuildConfig config, Set<String> applicationArchiveFiles, Path appRoot) throws IOException {
public static List<ApplicationArchive> scanForOtherIndexes(ClassLoader classLoader, BuildConfig config, Set<String> applicationArchiveFiles, Path appRoot, List<Path> additionalApplicationArchives) throws IOException {

Set<Path> dependenciesToIndex = new HashSet<>();

Expand All @@ -49,6 +49,8 @@ public static List<ApplicationArchive> scanForOtherIndexes(ClassLoader classLoad
//we don't index the application root, this is handled elsewhere
dependenciesToIndex.remove(appRoot);

dependenciesToIndex.addAll(additionalApplicationArchives);

return indexPaths(dependenciesToIndex, classLoader);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.lang.reflect.Method;
import java.nio.file.Path;

import org.jboss.shamrock.deployment.ArchiveContextBuilder;
import org.jboss.shamrock.deployment.BuildTimeGenerator;

/**
Expand All @@ -17,9 +18,11 @@ public class RuntimeRunner implements Runnable, Closeable {
private final Path target;
private final RuntimeClassLoader loader;
private Closeable closeTask;
private final ArchiveContextBuilder archiveContextBuilder;

public RuntimeRunner(ClassLoader classLoader, Path target, Path frameworkClassesPath) {
public RuntimeRunner(ClassLoader classLoader, Path target, Path frameworkClassesPath, ArchiveContextBuilder archiveContextBuilder) {
this.target = target;
this.archiveContextBuilder = archiveContextBuilder;
this.loader = new RuntimeClassLoader(classLoader, target, frameworkClassesPath);
}

Expand All @@ -33,7 +36,7 @@ public void close() throws IOException {
@Override
public void run() {
try {
BuildTimeGenerator buildTimeGenerator = new BuildTimeGenerator(loader, loader, false);
BuildTimeGenerator buildTimeGenerator = new BuildTimeGenerator(loader, loader, false, archiveContextBuilder);
buildTimeGenerator.run(target);
loader.accept(buildTimeGenerator.getBytecodeTransformers());
Class<?> mainClass = loader.findClass(BuildTimeGenerator.MAIN_CLASS);
Expand Down
97 changes: 97 additions & 0 deletions legacy/launcher/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.jboss.shamrock</groupId>
<artifactId>shamrock-legacy</artifactId>
<version>1.0.0.Alpha1-SNAPSHOT</version>
<relativePath>../</relativePath>
</parent>

<artifactId>shamrock-legacy-launcher</artifactId>

<dependencies>
<dependency>
<groupId>org.jboss.shamrock</groupId>
<artifactId>shamrock-core-deployment</artifactId>
</dependency>
<dependency>
<groupId>org.jboss.shamrock</groupId>
<artifactId>shamrock-legacy-runner</artifactId>
</dependency>

<dependency>
<groupId>org.jboss.shamrock</groupId>
<artifactId>shamrock-undertow-deployment</artifactId>
</dependency>

<dependency>
<groupId>org.jboss.shamrock</groupId>
<artifactId>shamrock-arc-deployment</artifactId>
</dependency>
<dependency>
<groupId>org.jboss.shamrock</groupId>
<artifactId>shamrock-transactions-deployment</artifactId>
</dependency>
<dependency>
<groupId>org.jboss.shamrock</groupId>
<artifactId>shamrock-jaxrs-deployment</artifactId>
</dependency>
<dependency>
<groupId>org.jboss.shamrock</groupId>
<artifactId>shamrock-rest-client-deployment</artifactId>
</dependency>
<dependency>
<groupId>org.jboss.shamrock</groupId>
<artifactId>shamrock-health-deployment</artifactId>
</dependency>
<dependency>
<groupId>org.jboss.shamrock</groupId>
<artifactId>shamrock-jpa-deployment</artifactId>
</dependency>
<dependency>
<groupId>org.jboss.shamrock</groupId>
<artifactId>shamrock-metrics-deployment</artifactId>
</dependency>
<dependency>
<groupId>org.jboss.shamrock</groupId>
<artifactId>shamrock-openapi-deployment</artifactId>
</dependency>

</dependencies>

<build>
<finalName>shamrock-legacy</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>org.jboss.shamrock.legacy.launcher.Main</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<phase>install</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package org.jboss.shamrock.legacy.launcher;

import java.io.File;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.List;

/**
* For some reason when using class-path entries something seems broken in java service loaders, as getResources()
* will only every return a single resource from the app class loader.
* <p>
* To get around this the legacy integration is broken up into a launcher and the runner, with this launcher
* just setting up class loading.
* <p>
* This has the advatange than anything added to he lib dir will automatically be added to the class path.
*/
public class Main {

public static void main(String... args) {
String classFileName = Main.class.getName().replace(".", "/") + ".class";
URL location = Main.class.getClassLoader().getResource(classFileName);
File libDir;
if (location.getProtocol().equals("jar")) {
String loc = location.getPath().substring(5, location.getPath().lastIndexOf('!'));
File file = new File(loc);
libDir = new File(file.getParentFile(), "lib");
} else if (location.getProtocol().equals("file")) {

String loc = location.getPath().substring(0, location.getPath().length() - classFileName.length());
File file = new File(loc);
libDir = new File(file.getParentFile(), "lib");
} else {
throw new RuntimeException("Unable to determine lib dir location from URL: " + location);
}
if (!libDir.isDirectory()) {
throw new RuntimeException("Could not find lib dir " + libDir);
}
try {
List<URL> urls = new ArrayList<>();
for (File i : libDir.listFiles()) {
urls.add(i.toURL());
}
ClassLoader old = Thread.currentThread().getContextClassLoader();
URLClassLoader ucl = new URLClassLoader(urls.toArray(new URL[urls.size()]));
try {
Thread.currentThread().setContextClassLoader(ucl);
Class<?> main = ucl.loadClass("org.jboss.shamrock.legacy.Main");
Method run = main.getDeclaredMethod("main", String[].class);
run.invoke(null, (Object) args);
} finally {
Thread.currentThread().setContextClassLoader(old);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}

}
20 changes: 20 additions & 0 deletions legacy/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>shamrock-parent</artifactId>
<groupId>org.jboss.shamrock</groupId>
<version>1.0.0.Alpha1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>shamrock-legacy</artifactId>
<packaging>pom</packaging>
<modules>
<module>launcher</module>
<module>runner</module>
</modules>


</project>
22 changes: 22 additions & 0 deletions legacy/runner/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.jboss.shamrock</groupId>
<artifactId>shamrock-legacy</artifactId>
<version>1.0.0.Alpha1-SNAPSHOT</version>
<relativePath>../</relativePath>
</parent>

<artifactId>shamrock-legacy-runner</artifactId>

<dependencies>
<dependency>
<groupId>org.jboss.shamrock</groupId>
<artifactId>shamrock-core-deployment</artifactId>
</dependency>
</dependencies>
</project>
Loading

0 comments on commit 76f9126

Please sign in to comment.