Skip to content
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 @@ -35,6 +35,7 @@
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.jar.Manifest;

import org.eclipse.jetty.io.IOResources;
import org.eclipse.jetty.util.ClassMatcher;
import org.eclipse.jetty.util.ClassVisibilityChecker;
import org.eclipse.jetty.util.FileID;
Expand Down Expand Up @@ -96,7 +97,9 @@ public interface Context extends ClassVisibilityChecker
* @param urlOrPath The URL or path to convert
* @return The Resource for the URL/path
* @throws IOException The Resource could not be created.
* @deprecated use ResourceFactory.of(component).newResource(String) properly at webapp initialization time only.
Comment thread
joakime marked this conversation as resolved.
Outdated
*/
@Deprecated(since = "12.1.11", forRemoval = true)
Resource newResource(String urlOrPath) throws IOException;

/**
Expand Down Expand Up @@ -637,14 +640,17 @@ private void definePackageIfNecessary(String className, URL url) throws IOExcept
if (externalForm.startsWith("jar:file:") && externalForm.contains("!/"))
{
URI jarURI = URIUtil.unwrapContainer(new URI(externalForm));
Resource manifestResource = getContext().newResource(URIUtil.uriJarPrefix(jarURI, "!/META-INF/MANIFEST.MF").toASCIIString());
if (manifestResource.exists())
try (ResourceFactory.Closeable resourceFactory = ResourceFactory.closeable())
{
try (InputStream is = manifestResource.newInputStream())
Resource manifestResource = resourceFactory.newResource(URIUtil.uriJarPrefix(jarURI, "!/META-INF/MANIFEST.MF").toASCIIString());
if (Resources.isReadableFile(manifestResource))
{
Manifest manifest = new Manifest(is);
definePackage(packageName, manifest, jarURI.toURL());
return;
try (InputStream is = IOResources.asInputStream(manifestResource))
{
Manifest manifest = new Manifest(is);
definePackage(packageName, manifest, jarURI.toURL());
return;
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -771,4 +771,11 @@ interface Closeable extends ResourceFactory, java.io.Closeable
interface LifeCycle extends org.eclipse.jetty.util.component.LifeCycle, ResourceFactory, Dumpable
{
}

interface Tracking
Comment thread
joakime marked this conversation as resolved.
Outdated
{
int getTrackingCount();

List<Resource> getTrackedResources();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.nio.file.FileSystem;
import java.nio.file.Path;
import java.nio.file.ProviderNotFoundException;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

Expand Down Expand Up @@ -124,12 +125,7 @@ static boolean isSupported(String str)
return RESOURCE_FACTORIES.getBest(str) != null;
}

interface Tracking
{
int getTrackingCount();
}

static class Closeable implements ResourceFactory.Closeable, Tracking
static class Closeable implements ResourceFactory.Closeable, ResourceFactory.Tracking
{
private boolean closed = false;
private final CompositeResourceFactory _compositeResourceFactory = new CompositeResourceFactory();
Expand All @@ -153,9 +149,15 @@ public int getTrackingCount()
{
return _compositeResourceFactory.mounted.size();
}

@Override
public List<Resource> getTrackedResources()
{
return Collections.unmodifiableList(_compositeResourceFactory.mounted);
}
}

static class LifeCycle extends AbstractLifeCycle implements ResourceFactory.LifeCycle
static class LifeCycle extends AbstractLifeCycle implements ResourceFactory.LifeCycle, ResourceFactory.Tracking
{
private final CompositeResourceFactory _compositeResourceFactory = new CompositeResourceFactory();

Expand All @@ -181,6 +183,17 @@ public void dump(Appendable out, String indent) throws IOException
.toList();
Dumpable.dumpObjects(out, indent, this, new DumpableCollection("newResourceReferences", referencedUris));
}

public int getTrackingCount()
{
return _compositeResourceFactory.mounted.size();
}

@Override
public List<Resource> getTrackedResources()
{
return Collections.unmodifiableList(_compositeResourceFactory.mounted);
}
}

static class CompositeResourceFactory implements ResourceFactory
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ public void testJarMountNonExistent(WorkDir workDir) throws IOException
Resource resBadFile = resourceFactory.newResource(jarUri.toASCIIString() + "bad/file.txt");
assertNull(resBadFile);

if (resourceFactory instanceof ResourceFactoryInternals.Tracking tracking)
if (resourceFactory instanceof ResourceFactory.Tracking tracking)
{
assertThat(tracking.getTrackingCount(), is(0));
}
Expand Down Expand Up @@ -266,7 +266,7 @@ public void testMountsForSameJar(WorkDir workDir) throws IOException
Resource twoTxt = resourceFactory.newResource(jarUri.toASCIIString() + "datainf/two.txt");
assertTrue(Resources.isReadableFile(twoTxt));

if (resourceFactory instanceof ResourceFactoryInternals.Tracking tracking)
if (resourceFactory instanceof ResourceFactory.Tracking tracking)
{
assertThat(tracking.getTrackingCount(), is(2));
}
Expand Down Expand Up @@ -305,20 +305,20 @@ public void testMountsForSameJarDifferentResourceFactories(WorkDir workDir) thro
Resource twoTxt = resourceFactory2.newResource(jarUri.toASCIIString() + "datainf/two.txt");
assertTrue(Resources.isReadableFile(twoTxt));

if (resourceFactory1 instanceof ResourceFactoryInternals.Tracking tracking)
if (resourceFactory1 instanceof ResourceFactory.Tracking tracking)
{
assertThat(tracking.getTrackingCount(), is(2));
}

if (resourceFactory2 instanceof ResourceFactoryInternals.Tracking tracking)
if (resourceFactory2 instanceof ResourceFactory.Tracking tracking)
{
assertThat(tracking.getTrackingCount(), is(1));
}

// Close Resource Factory 1
resourceFactory1.close();

if (resourceFactory1 instanceof ResourceFactoryInternals.Tracking tracking)
if (resourceFactory1 instanceof ResourceFactory.Tracking tracking)
{
assertThat(tracking.getTrackingCount(), is(0));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
import org.eclipse.jetty.util.ExceptionUtil;
import org.eclipse.jetty.util.URIUtil;
import org.eclipse.jetty.util.resource.Resource;
import org.eclipse.jetty.util.resource.ResourceFactory;
import org.eclipse.jetty.util.resource.Resources;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -233,9 +234,10 @@ public void init() throws ServletException
{
try
{
baseResource = URIUtil.isRelative(rb) ? baseResource.resolve(rb) : contextHandler.newResource(rb);
ResourceFactory resourceFactory = ResourceFactory.of(contextHandler);
baseResource = URIUtil.isRelative(rb) ? baseResource.resolve(rb) : resourceFactory.newResource(rb);
if (baseResource.isAlias())
baseResource = contextHandler.newResource(baseResource.getRealURI());
baseResource = resourceFactory.newResource(baseResource.getRealURI());
}
catch (Exception e)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -798,7 +798,9 @@ public Resource getResource(String pathInContext) throws MalformedURLException
* @param url the url to convert to a Resource
* @return the Resource for that url
* @throws IOException if unable to create a Resource from the URL
* @deprecated use ResourceFactory.of(component).newResource(URL) properly at webapp initialization time only.
Comment thread
joakime marked this conversation as resolved.
Outdated
*/
@Deprecated(since = "12.1.11", forRemoval = true)
public Resource newResource(URL url) throws IOException
{
return ResourceFactory.of(this).newResource(url);
Expand All @@ -809,7 +811,9 @@ public Resource newResource(URL url) throws IOException
*
* @param uri the URI to convert to a Resource
* @return the Resource for that URI
* @deprecated use ResourceFactory.of(component).newResource(URI) properly at webapp initialization time only.
*/
@Deprecated(since = "12.1.11", forRemoval = true)
public Resource newResource(URI uri)
{
return ResourceFactory.of(this).newResource(uri);
Expand All @@ -820,7 +824,9 @@ public Resource newResource(URI uri)
*
* @param urlOrPath The URL or path to convert
* @return The Resource for the URL/path
* @deprecated use ResourceFactory.of(component).newResource(String) properly at webapp initialization time only.
*/
@Deprecated(since = "12.1.11", forRemoval = true)
public Resource newResource(String urlOrPath)
{
return ResourceFactory.of(this).newResource(urlOrPath);
Expand Down Expand Up @@ -2882,6 +2888,16 @@ else if (path.charAt(0) != '/')

@Override
public URL getResource(String path) throws MalformedURLException
{
Resource resource = getJettyResource(path);
if (resource != null)
return resource.getURI().toURL();

// No hits
return null;
}

private Resource getJettyResource(String path)
{
try
{
Expand All @@ -2904,17 +2920,14 @@ public URL getResource(String path) throws MalformedURLException
{
// return first
if (Resources.exists(r))
return r.getURI().toURL();
}
return r;
}
catch (MalformedURLException e)
{
throw e;
}
catch (Throwable e)
catch (Throwable throwable)
{
// catch IOException, RuntimeException, and things like java.nio.fileInvalidPathException here.
throw (MalformedURLException)new MalformedURLException(path).initCause(e);
if (LOG.isDebugEnabled())
LOG.debug("Unable to get Jetty Resource for path: {}", path, throwable);
return null;
}

// No hits
Expand All @@ -2926,22 +2939,18 @@ public InputStream getResourceAsStream(String path)
{
try
{
URL url = getResource(path);
if (url == null)
return null;
Resource r = ResourceFactory.of(ServletContextHandler.this).newResource(url);
// Cannot serve directories as an InputStream
if (r.isDirectory())
return null;
return IOResources.asInputStream(r);
Resource resource = getJettyResource(path);
if (Resources.isReadableFile(resource))
return IOResources.asInputStream(resource);
}
catch (Throwable e)
{
// catch RuntimeException and things like java.nio.fileInvalidPathException here.
if (LOG.isTraceEnabled())
LOG.trace("IGNORED", e);
return null;
}
// not found
return null;
Comment thread
lorban marked this conversation as resolved.
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1620,11 +1620,11 @@ protected Resource getResourceForTempDirName()

if (resource == null)
{
if (getWar() == null || getWar().length() == 0)
if (getWar() == null || getWar().isEmpty())
throw new IllegalStateException("No resourceBase or war set for context");

// Use name of given resource in the temporary dirname
resource = newResource(getWar());
resource = ResourceFactory.of(this).newResource(getWar());
Comment thread
joakime marked this conversation as resolved.
}
return resource;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public void unpack(WebAppContext context) throws IOException
{
String war = context.getWar();
if (StringUtil.isNotBlank(war))
webApp = context.newResource(war);
webApp = ResourceFactory.of(context).newResource(war);
else
webApp = context.getBaseResource();

Expand All @@ -172,7 +172,7 @@ public void unpack(WebAppContext context) throws IOException
URI realURI = webApp.getRealURI();
if (LOG.isDebugEnabled())
LOG.debug("{} anti-aliased to {}", webApp, realURI);
Resource realWebApp = context.newResource(realURI);
Resource realWebApp = ResourceFactory.of(context).newResource(realURI);
if (Resources.exists(realWebApp))
webApp = realWebApp;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
package org.eclipse.jetty.ee10.webapp;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;

import org.eclipse.jetty.ee10.servlet.ErrorPageErrorHandler;
import org.eclipse.jetty.util.StringUtil;
import org.eclipse.jetty.util.resource.Resource;
import org.eclipse.jetty.util.resource.ResourceFactory;
import org.eclipse.jetty.util.resource.Resources;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -61,7 +61,7 @@ public void preConfigure(WebAppContext context) throws Exception
}
}
if (Resources.missing(dftResource))
dftResource = context.newResource(defaultsDescriptor);
dftResource = ResourceFactory.of(context).newResource(defaultsDescriptor);
}
if (Resources.isReadableFile(dftResource))
context.getMetaData().setDefaultsDescriptor(new DefaultsDescriptor(dftResource));
Expand All @@ -83,7 +83,7 @@ public void preConfigure(WebAppContext context) throws Exception
{
Resource orideResource = context.getResourceFactory().newClassLoaderResource(overrideDescriptor);
if (Resources.missing(orideResource))
orideResource = context.newResource(overrideDescriptor);
orideResource = ResourceFactory.of(context).newResource(overrideDescriptor);
if (Resources.isReadableFile(orideResource))
context.getMetaData().addOverrideDescriptor(new OverrideDescriptor(orideResource));
}
Expand All @@ -99,12 +99,12 @@ public void configure(WebAppContext context) throws Exception
context.getMetaData().addDescriptorProcessor(new StandardDescriptorProcessor());
}

protected Resource findWebXml(WebAppContext context) throws IOException, MalformedURLException
protected Resource findWebXml(WebAppContext context) throws IOException
{
String descriptor = context.getDescriptor();
if (descriptor != null)
{
Resource web = context.newResource(descriptor);
Resource web = ResourceFactory.of(context).newResource(descriptor);
if (web != null && !web.isDirectory())
return web;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
import org.eclipse.jetty.util.ExceptionUtil;
import org.eclipse.jetty.util.URIUtil;
import org.eclipse.jetty.util.resource.Resource;
import org.eclipse.jetty.util.resource.ResourceFactory;
import org.eclipse.jetty.util.resource.Resources;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -238,9 +239,10 @@ public void init() throws ServletException
{
try
{
baseResource = URIUtil.isRelative(rb) ? baseResource.resolve(rb) : contextHandler.newResource(rb);
ResourceFactory resourceFactory = ResourceFactory.of(contextHandler);
baseResource = URIUtil.isRelative(rb) ? baseResource.resolve(rb) : resourceFactory.newResource(rb);
if (baseResource.isAlias())
baseResource = contextHandler.newResource(baseResource.getRealURI());
baseResource = resourceFactory.newResource(baseResource.getRealURI());
}
catch (Exception e)
{
Expand Down
Loading
Loading