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

Jetty 12 : QuickStart Generation uses Path instead of Resource #8612

Closed
Show file tree
Hide file tree
Changes from all 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 @@ -76,7 +76,7 @@ private void generate() throws MojoExecutionException
{
try
{
QuickStartGenerator generator = new QuickStartGenerator(effectiveWebXml, webApp);
QuickStartGenerator generator = new QuickStartGenerator(effectiveWebXml.toPath(), webApp);
generator.generate();
}
catch (Exception e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import org.eclipse.jetty.server.ShutdownMonitor;
import org.eclipse.jetty.server.handler.ContextHandler;
import org.eclipse.jetty.util.component.AbstractLifeCycle;
import org.eclipse.jetty.util.resource.ResourceFactory;

/**
* JettyEmbedded
Expand Down Expand Up @@ -280,7 +279,7 @@ private void configure() throws Exception
Path qs = webApp.getTempDirectory().toPath().resolve("quickstart-web.xml");
if (Files.exists(qs) && Files.isRegularFile(qs))
{
webApp.setAttribute(QuickStartConfiguration.QUICKSTART_WEB_XML, ResourceFactory.of(webApp).newResource(qs));
webApp.setAttribute(QuickStartConfiguration.QUICKSTART_WEB_XML, qs);
webApp.addConfiguration(new MavenQuickStartConfiguration());
webApp.setAttribute(QuickStartConfiguration.MODE, Mode.QUICKSTART);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,9 @@ public void doStart()
throws Exception
{
//Run the webapp to create the quickstart file and properties file
generator = new QuickStartGenerator(forkWebXml, webApp);
generator = new QuickStartGenerator(forkWebXml.toPath(), webApp);
generator.setContextXml(contextXml);
generator.setWebAppPropsFile(webAppPropsFile);
generator.setWebAppPropsFile(webAppPropsFile.toPath());
generator.setServer(server);
generator.generate();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,12 @@

package org.eclipse.jetty.ee10.maven.plugin;

import java.io.File;
import java.nio.file.Path;

import org.eclipse.jetty.ee10.annotations.AnnotationConfiguration;
import org.eclipse.jetty.ee10.quickstart.QuickStartConfiguration;
import org.eclipse.jetty.ee10.quickstart.QuickStartConfiguration.Mode;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.util.resource.ResourceFactory;
import org.eclipse.jetty.util.thread.QueuedThreadPool;

/**
Expand All @@ -31,9 +30,9 @@
*/
public class QuickStartGenerator
{
private File quickstartXml;
private MavenWebAppContext webApp;
private File webAppPropsFile;
private final Path quickstartXml;
private final MavenWebAppContext webApp;
private Path webAppPropsFile;
private String contextXml;
private boolean prepared = false;
private Server server;
Expand All @@ -43,10 +42,10 @@ public class QuickStartGenerator
* @param quickstartXml the file to generate quickstart into
* @param webApp the webapp for which to generate quickstart
*/
public QuickStartGenerator(File quickstartXml, MavenWebAppContext webApp)
public QuickStartGenerator(Path quickstartXml, MavenWebAppContext webApp) throws Exception
{
this.quickstartXml = quickstartXml;
this.webApp = webApp;
this.webApp = webApp == null ? new MavenWebAppContext() : webApp;
}

/**
Expand All @@ -60,7 +59,7 @@ public MavenWebAppContext getWebApp()
/**
* @return the quickstartXml
*/
public File getQuickstartXml()
public Path getQuickstartXml()
{
return quickstartXml;
}
Expand All @@ -81,15 +80,15 @@ public void setServer(Server server)
this.server = server;
}

public File getWebAppPropsFile()
public Path getWebAppPropsFile()
{
return webAppPropsFile;
}

/**
* @param webAppPropsFile properties file describing the webapp
*/
public void setWebAppPropsFile(File webAppPropsFile)
public void setWebAppPropsFile(Path webAppPropsFile)
{
this.webAppPropsFile = webAppPropsFile;
}
Expand All @@ -115,13 +114,10 @@ public void setContextXml(String contextXml)
private void prepareWebApp()
throws Exception
{
if (webApp == null)
webApp = new MavenWebAppContext();

//set the webapp up to do very little other than generate the quickstart-web.xml
webApp.addConfiguration(new MavenQuickStartConfiguration());
webApp.setAttribute(QuickStartConfiguration.MODE, Mode.GENERATE);
webApp.setAttribute(QuickStartConfiguration.QUICKSTART_WEB_XML, ResourceFactory.of(webApp).newResource(quickstartXml.toPath()));
webApp.setAttribute(QuickStartConfiguration.QUICKSTART_WEB_XML, quickstartXml);
webApp.setAttribute(QuickStartConfiguration.ORIGIN_ATTRIBUTE, "o");
webApp.setCopyWebDir(false);
webApp.setCopyWebInf(false);
Expand Down Expand Up @@ -175,7 +171,7 @@ public void generate()

//save config of the webapp BEFORE we stop
if (webAppPropsFile != null)
WebAppPropertyConverter.toProperties(webApp, webAppPropsFile, contextXml);
WebAppPropertyConverter.toProperties(webApp, webAppPropsFile.toFile(), contextXml);
}
finally
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,17 @@

package org.eclipse.jetty.ee10.maven.plugin;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.toolchain.test.MavenTestingUtils;
import org.eclipse.jetty.toolchain.test.jupiter.WorkDir;
import org.eclipse.jetty.util.resource.ResourceFactory;
import org.junit.jupiter.api.Test;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.greaterThan;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
Expand All @@ -27,24 +32,32 @@
*/
public class TestQuickStartGenerator
{
public WorkDir workDir;

@Test
public void testGenerator() throws Exception
{
Path tmpDir = workDir.getEmptyPathDir();

MavenWebAppContext webApp = new MavenWebAppContext();
webApp.setContextPath("/shouldbeoverridden");
webApp.setBaseResource(MavenTestingUtils.getTestResourcePathDir("root"));
File quickstartFile = new File(MavenTestingUtils.getTargetTestingDir(), "quickstart-web.xml");
Path rootDir = MavenTestingUtils.getTargetPath("test-classes/root");
assertTrue(Files.exists(rootDir));
assertTrue(Files.isDirectory(rootDir));
webApp.setBaseResource(ResourceFactory.root().newResource(rootDir));

Path quickstartFile = tmpDir.resolve("quickstart-web.xml");
QuickStartGenerator generator = new QuickStartGenerator(quickstartFile, webApp);
generator.setContextXml(MavenTestingUtils.getTestResourceFile("embedder-context.xml").getAbsolutePath());
generator.setContextXml(MavenTestingUtils.getTargetFile("test-classes/embedder-context.xml").getAbsolutePath());
generator.setServer(new Server());
MavenTestingUtils.getTargetTestingDir().mkdirs();
File propsFile = new File(MavenTestingUtils.getTargetTestingDir(), "webapp.props");
propsFile.createNewFile();

Path propsFile = tmpDir.resolve("webapp.props");
Files.createFile(propsFile);
generator.setWebAppPropsFile(propsFile);
generator.generate();
assertTrue(propsFile.exists());
assertTrue(propsFile.length() > 0);
assertTrue(quickstartFile.exists());
assertTrue(quickstartFile.length() > 0);
assertTrue(Files.exists(propsFile));
assertThat(Files.size(propsFile), greaterThan(0L));
assertTrue(Files.exists(quickstartFile));
assertThat(Files.size(quickstartFile), greaterThan(0L));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashSet;
import java.util.Set;

Expand Down Expand Up @@ -99,8 +100,9 @@ public void preConfigure(WebAppContext context) throws Exception
throw new IllegalStateException("Bad Quickstart location");

//look for quickstart-web.xml in WEB-INF of webapp
Resource quickStartWebXml = getQuickStartWebXml(context);
LOG.debug("quickStartWebXml={} exists={}", quickStartWebXml, quickStartWebXml.exists());
Path quickStartWebXml = getQuickStartWebXml(context);
if (LOG.isDebugEnabled())
LOG.debug("quickStartWebXml={}", quickStartWebXml);

//Get the mode
Object o = context.getAttribute(MODE);
Expand All @@ -114,7 +116,7 @@ public void preConfigure(WebAppContext context) throws Exception
{
case GENERATE:
{
if (quickStartWebXml.exists())
if (Files.exists(quickStartWebXml))
LOG.info("Regenerating {}", quickStartWebXml);
else
LOG.info("Generating {}", quickStartWebXml);
Expand All @@ -128,7 +130,7 @@ public void preConfigure(WebAppContext context) throws Exception
}
case AUTO:
{
if (quickStartWebXml.exists())
if (Files.exists(quickStartWebXml))
{
quickStart(context);
}
Expand All @@ -141,7 +143,7 @@ public void preConfigure(WebAppContext context) throws Exception
break;
}
case QUICKSTART:
if (quickStartWebXml.exists())
if (Files.exists(quickStartWebXml))
quickStart(context);
else
throw new IllegalStateException("No " + quickStartWebXml);
Expand All @@ -159,7 +161,8 @@ protected void configure(QuickStartGeneratorConfiguration generator, WebAppConte
if (attr != null)
generator.setOriginAttribute(attr.toString());

generator.setQuickStartWebXml((Resource)context.getAttribute(QUICKSTART_WEB_XML));
Path quickStartWebXml = getQuickStartWebXml(context);
generator.setQuickStartWebXml(quickStartWebXml);
}

@Override
Expand All @@ -181,7 +184,8 @@ public void configure(WebAppContext context) throws Exception
//add a decorator that will find introspectable annotations
context.getObjectFactory().addDecorator(new AnnotationDecorator(context)); //this must be the last Decorator because they are run in reverse order!

LOG.debug("configured {}", this);
if (LOG.isDebugEnabled())
LOG.debug("configured {}", this);
}
}

Expand Down Expand Up @@ -211,7 +215,11 @@ protected void quickStart(WebAppContext context)
_quickStart = true;
context.setConfigurations(context.getConfigurations().stream()
.filter(c -> !__replacedConfigurations.contains(c.replaces()) && !__replacedConfigurations.contains(c.getClass())).toList().toArray(new Configuration[]{}));
context.getMetaData().setWebDescriptor(new WebDescriptor((Resource)context.getAttribute(QUICKSTART_WEB_XML)));
Path quickStartWebXml = getQuickStartWebXml(context);
if (!Files.exists(quickStartWebXml))
throw new IllegalStateException("Quickstart doesn't exist: " + quickStartWebXml);
Resource quickStartWebResource = context.getResourceFactory().newResource(quickStartWebXml);
context.getMetaData().setWebDescriptor(new WebDescriptor(quickStartWebResource));
context.getContext().getServletContext().setEffectiveMajorVersion(context.getMetaData().getWebDescriptor().getMajorVersion());
context.getContext().getServletContext().setEffectiveMinorVersion(context.getMetaData().getWebDescriptor().getMinorVersion());
}
Expand All @@ -223,41 +231,62 @@ protected void quickStart(WebAppContext context)
* @return the Resource for the quickstart-web.xml
* @throws Exception if unable to find the quickstart xml
*/
public Resource getQuickStartWebXml(WebAppContext context) throws Exception
public static Path getQuickStartWebXml(WebAppContext context) throws IOException
{
Object attr = context.getAttribute(QUICKSTART_WEB_XML);
if (attr instanceof Resource)
return (Resource)attr;
if (attr instanceof Path)
return (Path)attr;

Resource webInf = context.getWebInf();
if (webInf == null || !webInf.exists())
{
Files.createDirectories(context.getBaseResource().getPath().resolve("WEB-INF"));
webInf = context.getWebInf();
}
Path webInfDir = getWebInfPath(context);
Path qstartFile = webInfDir.resolve("quickstart-web.xml");

Resource qstart;
if (attr == null || StringUtil.isBlank(attr.toString()))
{
// TODO: should never return from WEB-INF/lib/foo.jar!/WEB-INF/quickstart-web.xml
// TODO: should also never return from a META-INF/versions/#/WEB-INF/quickstart-web.xml location
qstart = webInf.resolve("quickstart-web.xml");
}
else
if (attr != null && StringUtil.isNotBlank(attr.toString()))
{
Resource resource;
String attrValue = attr.toString();
try
{
// Try a relative resolution
qstart = _resourceFactory.newResource(webInf.getPath().resolve(attr.toString()));
resource = context.getResourceFactory().newResource(webInfDir.resolve(attrValue));
}
catch (Throwable th)
{
// try as a resource
qstart = _resourceFactory.newResource(attr.toString());
resource = context.getResourceFactory().newResource(attrValue);
}
if (resource != null)
{
Path attrPath = resource.getPath();
if (attrPath != null)
{
if (LOG.isDebugEnabled())
LOG.debug("Using quickstart attribute {} value of {}", attr, attrValue);
qstartFile = attrPath;
}
}
context.setAttribute(QUICKSTART_WEB_XML, qstart);
}
context.setAttribute(QUICKSTART_WEB_XML, qstart);
return qstart;
if (LOG.isDebugEnabled())
LOG.debug("Using quickstart location: {}", qstartFile);
context.setAttribute(QUICKSTART_WEB_XML, qstartFile);
return qstartFile;
}

private static Path getWebInfPath(WebAppContext context) throws IOException
{
Path webInfDir = null;
Resource webInf = context.getWebInf();
if (webInf != null)
{
webInfDir = webInf.getPath();
}

if (webInfDir == null)
{
Path baseResourcePath = context.getBaseResource().getPath();
webInfDir = baseResourcePath.resolve("WEB-INF");
if (!Files.exists(webInfDir))
Files.createDirectories(webInfDir);
}
return webInfDir;
}
}
Loading