Issue #11572 add check for isDeployable when hot deploy file changes (#11576)

* Issue #11572 add check for isDeployable when hot deploy file changes
This commit is contained in:
Jan Bartel 2024-03-27 09:59:36 +01:00 committed by GitHub
parent 9defd5d416
commit 92cb3cbe7e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 82 additions and 1 deletions

View File

@ -501,7 +501,7 @@ public class ContextProvider extends ScanningAppProvider
!Files.exists(path.getParent().resolve(basename + ".WAR")); !Files.exists(path.getParent().resolve(basename + ".WAR"));
} }
// deploy if there is not a .war for of the same basename // deploy if it is a .war and there is not a .xml for of the same basename
if (FileID.isWebArchive(path)) if (FileID.isWebArchive(path))
{ {
// if a .xml file exists for it // if a .xml file exists for it
@ -519,4 +519,11 @@ public class ContextProvider extends ScanningAppProvider
if (isDeployable(path)) if (isDeployable(path))
super.pathAdded(path); super.pathAdded(path);
} }
@Override
protected void pathChanged(Path path) throws Exception
{
if (isDeployable(path))
super.pathChanged(path);
}
} }

View File

@ -16,12 +16,14 @@ package org.eclipse.jetty.deploy.providers;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.attribute.FileTime;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
import org.eclipse.jetty.deploy.AppProvider; import org.eclipse.jetty.deploy.AppProvider;
import org.eclipse.jetty.deploy.DeploymentManager; import org.eclipse.jetty.deploy.DeploymentManager;
import org.eclipse.jetty.deploy.test.XmlConfiguredJetty; import org.eclipse.jetty.deploy.test.XmlConfiguredJetty;
import org.eclipse.jetty.server.handler.ContextHandler;
import org.eclipse.jetty.toolchain.test.FS; import org.eclipse.jetty.toolchain.test.FS;
import org.eclipse.jetty.toolchain.test.jupiter.WorkDir; import org.eclipse.jetty.toolchain.test.jupiter.WorkDir;
import org.eclipse.jetty.toolchain.test.jupiter.WorkDirExtension; import org.eclipse.jetty.toolchain.test.jupiter.WorkDirExtension;
@ -34,6 +36,10 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import static org.awaitility.Awaitility.await; import static org.awaitility.Awaitility.await;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNotSame;
import static org.junit.jupiter.api.Assertions.assertSame;
/** /**
* Similar in scope to {@link ContextProviderStartupTest}, except is concerned with the modification of existing * Similar in scope to {@link ContextProviderStartupTest}, except is concerned with the modification of existing
@ -56,6 +62,11 @@ public class ContextProviderRuntimeUpdatesTest
FS.ensureDirExists(resourceBase); FS.ensureDirExists(resourceBase);
jetty.setProperty("test.bar.resourceBase", resourceBase.toUri().toASCIIString()); jetty.setProperty("test.bar.resourceBase", resourceBase.toUri().toASCIIString());
Path tmpBase = jetty.getJettyBasePath().resolve("tmp");
FS.ensureDirExists(tmpBase);
jetty.setProperty("test.tmpBase", tmpBase.toFile().getAbsolutePath());
System.err.println(tmpBase.toFile().getAbsolutePath());
Files.writeString(resourceBase.resolve("text.txt"), "This is the resourceBase text"); Files.writeString(resourceBase.resolve("text.txt"), "This is the resourceBase text");
Path resourceBaseAlt = jetty.getJettyBasePath().resolve("resourceBase-alt"); Path resourceBaseAlt = jetty.getJettyBasePath().resolve("resourceBase-alt");
@ -110,6 +121,42 @@ public class ContextProviderRuntimeUpdatesTest
await().atMost(5, TimeUnit.SECONDS).until(() -> _scans.get() > scan); await().atMost(5, TimeUnit.SECONDS).until(() -> _scans.get() > scan);
} }
/**
* Test that if a war file has a context xml sibling, it will only be redeployed when the
* context xml changes, not the war.
*/
@Test
public void testSelectiveDeploy(WorkDir workDir) throws Exception
{
Path testdir = workDir.getEmptyPathDir();
createJettyBase(testdir);
startJetty();
Path webappsDir = jetty.getJettyBasePath().resolve("webapps");
Files.createFile(webappsDir.resolve("simple.war"));
jetty.copyWebapp("simple.xml", "simple.xml");
waitForDirectoryScan();
jetty.assertContextHandlerExists("/simple");
ContextHandler contextHandler = jetty.getContextHandler("/simple");
assertNotNull(contextHandler);
assertEquals(jetty.getJettyBasePath().resolve("tmp").toFile().getAbsolutePath(), contextHandler.getTempDirectory().getAbsolutePath());
//touch the context xml and check the context handler was redeployed
jetty.copyWebapp("simple.xml", "simple.xml");
waitForDirectoryScan();
ContextHandler contextHandler2 = jetty.getContextHandler("/simple");
assertNotNull(contextHandler2);
assertNotSame(contextHandler, contextHandler2);
//touch the war file and check the context handler was NOT redeployed
Thread.sleep(1000L); //ensure at least a millisecond has passed
Files.setLastModifiedTime(webappsDir.resolve("simple.war"), FileTime.fromMillis(System.currentTimeMillis()));
waitForDirectoryScan();
ContextHandler contextHandler3 = jetty.getContextHandler("/simple");
assertNotNull(contextHandler3);
assertSame(contextHandler2, contextHandler3);
}
/** /**
* Simple webapp deployment after startup of server. * Simple webapp deployment after startup of server.
* *

View File

@ -216,6 +216,23 @@ public class XmlConfiguredJetty
} }
} }
public ContextHandler getContextHandler(String contextPath)
{
ContextHandler contextHandler = null;
for (Handler handler : _contexts.getHandlers())
{
if (handler instanceof ContextHandler ch)
{
if (ch.getContextPath().equals(contextPath))
{
contextHandler = ch;
break;
}
}
}
return contextHandler;
}
private void copyFile(String type, Path srcFile, Path destFile) throws IOException private void copyFile(String type, Path srcFile, Path destFile) throws IOException
{ {
assertThat(srcFile, PathMatchers.isRegularFile()); assertThat(srcFile, PathMatchers.isRegularFile());

View File

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://eclipse.dev/jetty/configure_10_0.dtd">
<Configure class="org.eclipse.jetty.server.handler.ContextHandler">
<Set name="contextPath">/simple</Set>
<Set name="tempDirectory"><Property name="test.tmpBase" /></Set>
<Set name="baseResourceAsString"><Property name="test.bar.resourceBase" /></Set>
<Set name="handler">
<New class="org.eclipse.jetty.server.handler.ResourceHandler" />
</Set>
</Configure>