Skip to content

Commit

Permalink
#27 TomcatWebServer: minor refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
mvysny committed Sep 2, 2024
1 parent 76556e3 commit 1389091
Showing 1 changed file with 12 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.github.mvysny.vaadinboot.common;

import com.github.mvysny.vaadinboot.VaadinBoot;
import org.apache.catalina.Context;
import org.apache.catalina.WebResourceRoot;
import org.apache.catalina.loader.WebappLoader;
Expand All @@ -14,12 +13,8 @@

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
Expand All @@ -40,7 +35,10 @@ public class TomcatWebServer implements WebServer {

private volatile Context context;

private volatile File resourcesJarOrFolder;
/**
* The outcome of {@link Env#findResourcesJarOrFolder(URL)}.
*/
protected volatile File resourcesJarOrFolder;

@NotNull
public Context getContext() {
Expand Down Expand Up @@ -111,27 +109,23 @@ protected Context createWebAppContext(@NotNull VaadinBootBase<?> configuration)
ctx.getLoader().setDelegate(true);

final WebResourceRoot root = new StandardRoot(ctx);
final File mainJarFile = addStaticWebapp(root);
enableClasspathScanning(root, mainJarFile);
addStaticWebapp(root);
enableClasspathScanning(root);
ctx.setResources(root);
return ctx;
}

@NotNull
protected File addStaticWebapp(@NotNull WebResourceRoot root) throws IOException {
final URL webapp = Env.findWebRoot();
final File mainJarFile = Env.findResourcesJarOrFolder(webapp);
if (mainJarFile.isDirectory()) {
protected void addStaticWebapp(@NotNull WebResourceRoot root) throws IOException {
if (resourcesJarOrFolder.isDirectory()) {
root.addPreResources(new DirResourceSet(root, "/",
mainJarFile.getAbsolutePath(), "/webapp"));
resourcesJarOrFolder.getAbsolutePath(), "/webapp"));
} else {
root.addPreResources(new JarResourceSet(root, "/",
mainJarFile.getAbsolutePath(), "/webapp"));
resourcesJarOrFolder.getAbsolutePath(), "/webapp"));
}
return mainJarFile;
}

private static void enableClasspathScanning(@NotNull WebResourceRoot root, @NotNull File mainJarFile) {
protected void enableClasspathScanning(@NotNull WebResourceRoot root) {
// we need to add your app's classes to Tomcat to enable classpath scanning, in order to
// auto-discover app @WebServlet and @WebListener.
if (Env.isDevelopmentEnvironment) {
Expand All @@ -147,7 +141,7 @@ private static void enableClasspathScanning(@NotNull WebResourceRoot root, @NotN
root.addPreResources(new DirResourceSet(root, "/WEB-INF/classes", additionWebInfClasses.getAbsolutePath(), "/"));
} else {
// the main jar file contains not only the static `webapp` but also the classes of the project.
root.addPreResources(new JarResourceSet(root, "/WEB-INF/classes", mainJarFile.getAbsolutePath(), "/"));
root.addPreResources(new JarResourceSet(root, "/WEB-INF/classes", resourcesJarOrFolder.getAbsolutePath(), "/"));
}
}
}

0 comments on commit 1389091

Please sign in to comment.