Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PAYARA-3662] fixing glassfish-web.xml contextroot overwrite #3831

Merged
merged 5 commits into from
Mar 19, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
* limitations under the License.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes to this file can be ignored

*/
// Modified 2017 Payara Foundation

package fish.payara.micro.boot.loader;

import java.io.File;
Expand All @@ -29,124 +28,133 @@
import fish.payara.micro.boot.loader.archive.JarFileArchive;

/**
* Base class for launchers that can start an application with a fully configured
* classpath backed by one or more {@link Archive}s.
* Base class for launchers that can start an application with a fully
* configured classpath backed by one or more {@link Archive}s.
*
* @author Phillip Webb
* @author Dave Syer
*/
public abstract class Launcher {

/**
* Launch the application. This method is the initial entry point that should be
* called by a subclass {@code public static void main(String[] args)} method.
* @param args the incoming arguments
* @throws Exception if the application fails to launch
*/
protected Object launch(String method, String[] args) throws Exception {
boolean explode = true;
String unpackDir = null;
for (int i = 0; i < args.length; i++) {
if ("--nested".equals(args[i].toLowerCase())) {
explode = false;
} else if ("--unpackdir".equals(args[i].toLowerCase()) || "--rootdir".equals(args[i].toLowerCase())) {
if (args.length >= (i+1)) {
unpackDir = args[i+1];
}
/**
* Launch the application. This method is the initial entry point that
* should be called by a subclass
* {@code public static void main(String[] args)} method.
*
* @param args the incoming arguments
* @throws Exception if the application fails to launch
*/
protected Object launch(String method, String[] args) throws Exception {
boolean explode = true;
String unpackDir = null;
for (int i = 0; i < args.length; i++) {
if ("--nested".equals(args[i].toLowerCase())) {
explode = false;
} else if ("--unpackdir".equals(args[i].toLowerCase()) || "--rootdir".equals(args[i].toLowerCase())) {
if (args.length >= (i + 1)) {
unpackDir = args[i + 1];
}
}

ClassLoader classLoader;
if (!explode) {
classLoader = createClassLoader(getClassPathArchives());
fish.payara.micro.boot.loader.jar.JarFile.registerUrlProtocolHandler();
}

ClassLoader classLoader;
if (!explode) {
classLoader = createClassLoader(getClassPathArchives());
fish.payara.micro.boot.loader.jar.JarFile.registerUrlProtocolHandler();
} else {
if (unpackDir != null) {
classLoader = new ExplodedURLClassloader(new File(unpackDir));
} else {
if (unpackDir != null) {
classLoader = new ExplodedURLClassloader(new File(unpackDir));
} else {
classLoader = new ExplodedURLClassloader();
}
classLoader = new ExplodedURLClassloader();
}
return launch(method, args, getMainClass(), classLoader);
}
}
return launch(method, args, getMainClass(), classLoader);
}

/**
* Create a classloader for the specified archives.
* @param archives the archives
* @return the classloader
* @throws Exception if the classloader cannot be created
*/
protected ClassLoader createClassLoader(List<Archive> archives) throws Exception {
List<URL> urls = new ArrayList<URL>(archives.size());
for (Archive archive : archives) {
urls.add(archive.getUrl());
}
return createClassLoader(urls.toArray(new URL[urls.size()]));
}
/**
* Create a classloader for the specified archives.
*
* @param archives the archives
* @return the classloader
* @throws Exception if the classloader cannot be created
*/
protected ClassLoader createClassLoader(List<Archive> archives) throws Exception {
List<URL> urls = new ArrayList<URL>(archives.size());
for (Archive archive : archives) {
urls.add(archive.getUrl());
}
return createClassLoader(urls.toArray(new URL[urls.size()]));
}

/**
* Create a classloader for the specified URLs.
* @param urls the URLs
* @return the classloader
* @throws Exception if the classloader cannot be created
*/
protected ClassLoader createClassLoader(URL[] urls) throws Exception {
return new LaunchedURLClassLoader(urls, getClass().getClassLoader());
}
/**
* Create a classloader for the specified URLs.
*
* @param urls the URLs
* @return the classloader
* @throws Exception if the classloader cannot be created
*/
protected ClassLoader createClassLoader(URL[] urls) throws Exception {
return new LaunchedURLClassLoader(urls, getClass().getClassLoader());
}

/**
* Launch the application given the archive file and a fully configured classloader.
* @param args the incoming arguments
* @param mainClass the main class to run
* @param classLoader the classloader
* @throws Exception if the launch fails
*/
protected Object launch(String method, String[] args, String mainClass, ClassLoader classLoader)
throws Exception {
Thread.currentThread().setContextClassLoader(classLoader);
return createMainMethodRunner(mainClass, method, args, classLoader).run();
}
/**
* Launch the application given the archive file and a fully configured
* classloader.
*
* @param args the incoming arguments
* @param mainClass the main class to run
* @param classLoader the classloader
* @throws Exception if the launch fails
*/
protected Object launch(String method, String[] args, String mainClass, ClassLoader classLoader)
throws Exception {
Thread.currentThread().setContextClassLoader(classLoader);
return createMainMethodRunner(mainClass, method, args, classLoader).run();
}

/**
* Create the {@code MainMethodRunner} used to launch the application.
* @param mainClass the main class
* @param args the incoming arguments
* @param classLoader the classloader
* @return the main method runner
*/
protected MainMethodRunner createMainMethodRunner(String mainClass, String method, String[] args,
ClassLoader classLoader) {
return new MainMethodRunner(mainClass, args, method);
}
/**
* Create the {@code MainMethodRunner} used to launch the application.
*
* @param mainClass the main class
* @param args the incoming arguments
* @param classLoader the classloader
* @return the main method runner
*/
protected MainMethodRunner createMainMethodRunner(String mainClass, String method, String[] args,
ClassLoader classLoader) {
return new MainMethodRunner(mainClass, args, method);
}

/**
* Returns the main class that should be launched.
* @return the name of the main class
* @throws Exception if the main class cannot be obtained
*/
protected abstract String getMainClass() throws Exception;
/**
* Returns the main class that should be launched.
*
* @return the name of the main class
* @throws Exception if the main class cannot be obtained
*/
protected abstract String getMainClass() throws Exception;

/**
* Returns the archives that will be used to construct the class path.
* @return the class path archives
* @throws Exception if the class path archives cannot be obtained
*/
protected abstract List<Archive> getClassPathArchives() throws Exception;
/**
* Returns the archives that will be used to construct the class path.
*
* @return the class path archives
* @throws Exception if the class path archives cannot be obtained
*/
protected abstract List<Archive> getClassPathArchives() throws Exception;

protected final Archive createArchive() throws Exception {
ProtectionDomain protectionDomain = getClass().getProtectionDomain();
CodeSource codeSource = protectionDomain.getCodeSource();
URI location = (codeSource == null ? null : codeSource.getLocation().toURI());
String path = (location == null ? null : location.getSchemeSpecificPart());
if (path == null) {
throw new IllegalStateException("Unable to determine code source archive");
}
File root = new File(path);
if (!root.exists()) {
throw new IllegalStateException(
"Unable to determine code source archive from " + root);
}
return new JarFileArchive(root);
}
protected final Archive createArchive() throws Exception {
ProtectionDomain protectionDomain = getClass().getProtectionDomain();
CodeSource codeSource = protectionDomain.getCodeSource();
URI location = (codeSource == null ? null : codeSource.getLocation().toURI());
String path = (location == null ? null : location.getSchemeSpecificPart());
if (path == null) {
throw new IllegalStateException("Unable to determine code source archive");
}
File root = new File(path);
if (!root.exists()) {
throw new IllegalStateException(
"Unable to determine code source archive from " + root);
}
return new JarFileArchive(root);
}

}
Loading