get rid of EmptyResource

Signed-off-by: Ludovic Orban <lorban@bitronix.be>
This commit is contained in:
Ludovic Orban 2022-07-26 17:56:37 +02:00
parent 4856a7d065
commit 0c0c333970
10 changed files with 46 additions and 206 deletions

View File

@ -1,150 +0,0 @@
//
// ========================================================================
// Copyright (c) 1995-2022 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//
package org.eclipse.jetty.util.resource;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URI;
import java.nio.ByteBuffer;
import java.nio.channels.ReadableByteChannel;
import java.nio.file.Path;
import java.util.List;
/**
* EmptyResource
*
* Represents a resource that does does not refer to any file, url, jar etc.
*/
public class EmptyResource extends Resource
{
public static final Resource INSTANCE = new EmptyResource();
private static final ReadableByteChannel EOF_READABLE_BYTE_CHANNEL = new ReadableByteChannel()
{
@Override
public int read(ByteBuffer dst)
{
return -1;
}
@Override
public boolean isOpen()
{
return false;
}
@Override
public void close()
{
}
};
private static final InputStream EOF_INPUT_STREAM = new InputStream()
{
@Override
public int read()
{
return -1;
}
};
private EmptyResource()
{
}
@Override
public boolean isContainedIn(Resource r) throws MalformedURLException
{
return false;
}
@Override
public boolean exists()
{
return false;
}
@Override
public boolean isDirectory()
{
return false;
}
@Override
public long lastModified()
{
return 0;
}
@Override
public long length()
{
return 0;
}
@Override
public URI getURI()
{
return null;
}
@Override
public Path getPath()
{
return null;
}
@Override
public String getName()
{
return null;
}
@Override
public InputStream newInputStream() throws IOException
{
return EOF_INPUT_STREAM;
}
@Override
public ReadableByteChannel newReadableByteChannel() throws IOException
{
return EOF_READABLE_BYTE_CHANNEL;
}
@Override
public boolean delete() throws SecurityException
{
return false;
}
@Override
public boolean renameTo(Resource dest) throws SecurityException
{
return false;
}
@Override
public List<String> list()
{
return null;
}
@Override
public Resource resolve(String subUriPath) throws IOException
{
return this;
}
}

View File

@ -264,11 +264,7 @@ public class ResourceCollection extends Resource
}
if (resources == null)
{
if (addedResource != null)
return addedResource; // This will not exist
return EmptyResource.INSTANCE;
}
return addedResource; // This will not exist
if (resources.size() == 1)
return resources.get(0);

View File

@ -13,6 +13,8 @@
package org.eclipse.jetty.ee10.annotations;
import java.nio.file.Path;
import org.eclipse.jetty.ee10.plus.annotation.LifeCycleCallbackCollection;
import org.eclipse.jetty.ee10.servlet.ServletHolder;
import org.eclipse.jetty.ee10.servlet.Source;
@ -20,7 +22,7 @@ import org.eclipse.jetty.ee10.webapp.MetaData;
import org.eclipse.jetty.ee10.webapp.WebAppContext;
import org.eclipse.jetty.ee10.webapp.WebDescriptor;
import org.eclipse.jetty.util.DecoratedObjectFactory;
import org.eclipse.jetty.util.resource.EmptyResource;
import org.eclipse.jetty.util.resource.Resource;
import org.eclipse.jetty.xml.XmlParser;
import org.junit.jupiter.api.Test;
@ -36,7 +38,7 @@ public class TestAnnotationDecorator
{
public TestWebDescriptor(MetaData.Complete metadata)
{
super(EmptyResource.INSTANCE);
super(Resource.newResource(Path.of(".")));
_metaDataComplete = metadata;
}

View File

@ -22,7 +22,6 @@ import java.util.Map;
import java.util.Objects;
import jakarta.servlet.ServletContext;
import org.eclipse.jetty.util.resource.EmptyResource;
import org.eclipse.jetty.util.resource.Resource;
import org.eclipse.jetty.util.thread.AutoLock;
import org.slf4j.Logger;
@ -39,7 +38,6 @@ public class MetaData
public static final String VALIDATE_XML = "org.eclipse.jetty.webapp.validateXml";
public static final String ORDERED_LIBS = "jakarta.servlet.context.orderedLibs";
public static final Resource NON_FRAG_RESOURCE = EmptyResource.INSTANCE;
private final AutoLock _lock = new AutoLock();
protected Map<String, OriginInfo> _origins = new HashMap<>();
@ -366,7 +364,7 @@ public class MetaData
{
//if no resource associated with an annotation map it to empty resource - these
//annotations will always be processed first
Resource enclosingResource = EmptyResource.INSTANCE;
Resource enclosingResource = null;
Resource resource = annotation.getResource();
if (resource != null)
{
@ -381,9 +379,7 @@ public class MetaData
if (enclosingResource == null)
enclosingResource = getEnclosingResource(_orderedContainerResources, resource);
//Couldn't find a parent resource in any of the known resources, map it to the empty resource
if (enclosingResource == null)
enclosingResource = EmptyResource.INSTANCE;
//Couldn't find a parent resource in any of the known resources, map it to null
}
List<DiscoveredAnnotation> list = _annotations.computeIfAbsent(enclosingResource, k -> new ArrayList<>());
@ -490,7 +486,7 @@ public class MetaData
}
List<Resource> resources = new ArrayList<>();
resources.add(EmptyResource.INSTANCE); //always apply annotations with no resource first
resources.add(null); //always apply annotations with no resource first
resources.addAll(_orderedContainerResources); //next all annotations from container path
resources.addAll(_webInfClasses); //next everything from web-inf classes
resources.addAll(getWebInfResources(isOrdered())); //finally annotations (in order) from webinf path

View File

@ -45,7 +45,6 @@ import java.util.stream.Collectors;
import org.eclipse.jetty.util.IO;
import org.eclipse.jetty.util.PatternMatcher;
import org.eclipse.jetty.util.StringUtil;
import org.eclipse.jetty.util.resource.EmptyResource;
import org.eclipse.jetty.util.resource.Resource;
import org.eclipse.jetty.util.resource.ResourceCollection;
import org.slf4j.Logger;
@ -465,7 +464,7 @@ public class MetaInfConfiguration extends AbstractConfiguration
if (cache != null && cache.containsKey(target))
{
resourcesDir = cache.get(target);
if (resourcesDir == EmptyResource.INSTANCE)
if (isEmptyResource(resourcesDir))
{
if (LOG.isDebugEnabled())
LOG.debug("{} cached as containing no META-INF/resources", target);
@ -495,11 +494,6 @@ public class MetaInfConfiguration extends AbstractConfiguration
_mountedResources.add(mount);
}
if (!resourcesDir.exists() || !resourcesDir.isDirectory())
{
resourcesDir = EmptyResource.INSTANCE;
}
if (cache != null)
{
Resource old = cache.putIfAbsent(target, resourcesDir);
@ -509,7 +503,7 @@ public class MetaInfConfiguration extends AbstractConfiguration
LOG.debug("{} META-INF/resources cache updated", target);
}
if (resourcesDir == EmptyResource.INSTANCE)
if (isEmptyResource(resourcesDir))
{
return;
}
@ -528,6 +522,11 @@ public class MetaInfConfiguration extends AbstractConfiguration
dirs.add(resourcesDir);
}
private static boolean isEmptyResource(Resource resourcesDir)
{
return !resourcesDir.exists() || !resourcesDir.isDirectory();
}
/**
* Scan for META-INF/web-fragment.xml file in the given jar.
*
@ -543,7 +542,7 @@ public class MetaInfConfiguration extends AbstractConfiguration
if (cache != null && cache.containsKey(jar))
{
webFrag = cache.get(jar);
if (webFrag == EmptyResource.INSTANCE)
if (isEmptyFragment(webFrag))
{
if (LOG.isDebugEnabled())
LOG.debug("{} cached as containing no META-INF/web-fragment.xml", jar);
@ -566,10 +565,6 @@ public class MetaInfConfiguration extends AbstractConfiguration
URI uri = jar.getURI();
webFrag = Resource.newResource(uriJarPrefix(uri, "!/META-INF/web-fragment.xml"));
}
if (!webFrag.exists() || webFrag.isDirectory())
{
webFrag = EmptyResource.INSTANCE;
}
if (cache != null)
{
@ -581,7 +576,7 @@ public class MetaInfConfiguration extends AbstractConfiguration
LOG.debug("{} META-INF/web-fragment.xml cache updated", jar);
}
if (webFrag == EmptyResource.INSTANCE)
if (isEmptyFragment(webFrag))
return;
}
@ -596,6 +591,11 @@ public class MetaInfConfiguration extends AbstractConfiguration
LOG.debug("{} added to context", webFrag);
}
private static boolean isEmptyFragment(Resource webFrag)
{
return !webFrag.exists() || webFrag.isDirectory();
}
/**
* Discover META-INF/*.tld files in the given jar
*

View File

@ -21,7 +21,6 @@ import java.util.List;
import org.acme.webapp.TestAnnotation;
import org.eclipse.jetty.toolchain.test.MavenTestingUtils;
import org.eclipse.jetty.util.IO;
import org.eclipse.jetty.util.resource.EmptyResource;
import org.eclipse.jetty.util.resource.Resource;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
@ -170,7 +169,7 @@ public class TestMetaData
assertThat(list, hasSize(1));
//test an annotation that didn't have an associated resource
list = wac.getMetaData()._annotations.get(EmptyResource.INSTANCE);
list = wac.getMetaData()._annotations.get(null);
assertThat(list, contains(annotationC));
assertThat(list, hasSize(1));

View File

@ -13,6 +13,8 @@
package org.eclipse.jetty.ee9.annotations;
import java.nio.file.Path;
import org.eclipse.jetty.ee9.plus.annotation.LifeCycleCallbackCollection;
import org.eclipse.jetty.ee9.servlet.ServletHolder;
import org.eclipse.jetty.ee9.servlet.Source;
@ -20,7 +22,7 @@ import org.eclipse.jetty.ee9.webapp.MetaData;
import org.eclipse.jetty.ee9.webapp.WebAppContext;
import org.eclipse.jetty.ee9.webapp.WebDescriptor;
import org.eclipse.jetty.util.DecoratedObjectFactory;
import org.eclipse.jetty.util.resource.EmptyResource;
import org.eclipse.jetty.util.resource.Resource;
import org.eclipse.jetty.xml.XmlParser;
import org.junit.jupiter.api.Test;
@ -36,7 +38,7 @@ public class TestAnnotationDecorator
{
public TestWebDescriptor(MetaData.Complete metadata)
{
super(EmptyResource.INSTANCE);
super(Resource.newResource(Path.of(".")));
_metaDataComplete = metadata;
}

View File

@ -22,7 +22,6 @@ import java.util.Map;
import java.util.Objects;
import jakarta.servlet.ServletContext;
import org.eclipse.jetty.util.resource.EmptyResource;
import org.eclipse.jetty.util.resource.Resource;
import org.eclipse.jetty.util.thread.AutoLock;
import org.slf4j.Logger;
@ -39,7 +38,6 @@ public class MetaData
public static final String VALIDATE_XML = "org.eclipse.jetty.ee9.webapp.validateXml";
public static final String ORDERED_LIBS = "jakarta.servlet.context.orderedLibs";
public static final Resource NON_FRAG_RESOURCE = EmptyResource.INSTANCE;
private final AutoLock _lock = new AutoLock();
protected Map<String, OriginInfo> _origins = new HashMap<>();
@ -366,7 +364,7 @@ public class MetaData
{
//if no resource associated with an annotation map it to empty resource - these
//annotations will always be processed first
Resource enclosingResource = EmptyResource.INSTANCE;
Resource enclosingResource = null;
Resource resource = annotation.getResource();
if (resource != null)
{
@ -381,9 +379,7 @@ public class MetaData
if (enclosingResource == null)
enclosingResource = getEnclosingResource(_orderedContainerResources, resource);
//Couldn't find a parent resource in any of the known resources, map it to the empty resource
if (enclosingResource == null)
enclosingResource = EmptyResource.INSTANCE;
//Couldn't find a parent resource in any of the known resources, map it to null
}
List<DiscoveredAnnotation> list = _annotations.computeIfAbsent(enclosingResource, k -> new ArrayList<>());
@ -490,7 +486,7 @@ public class MetaData
}
List<Resource> resources = new ArrayList<>();
resources.add(EmptyResource.INSTANCE); //always apply annotations with no resource first
resources.add(null); //always apply annotations with no resource first
resources.addAll(_orderedContainerResources); //next all annotations from container path
resources.addAll(_webInfClasses); //next everything from web-inf classes
resources.addAll(getWebInfResources(isOrdered())); //finally annotations (in order) from webinf path

View File

@ -45,7 +45,6 @@ import java.util.stream.Collectors;
import org.eclipse.jetty.util.IO;
import org.eclipse.jetty.util.PatternMatcher;
import org.eclipse.jetty.util.StringUtil;
import org.eclipse.jetty.util.resource.EmptyResource;
import org.eclipse.jetty.util.resource.Resource;
import org.eclipse.jetty.util.resource.ResourceCollection;
import org.slf4j.Logger;
@ -460,7 +459,7 @@ public class MetaInfConfiguration extends AbstractConfiguration
if (cache != null && cache.containsKey(target))
{
resourcesDir = cache.get(target);
if (resourcesDir == EmptyResource.INSTANCE)
if (isEmptyResource(resourcesDir))
{
if (LOG.isDebugEnabled())
LOG.debug("{} cached as containing no META-INF/resources", target);
@ -490,11 +489,6 @@ public class MetaInfConfiguration extends AbstractConfiguration
_mountedResources.add(mount);
}
if (!resourcesDir.exists() || !resourcesDir.isDirectory())
{
resourcesDir = EmptyResource.INSTANCE;
}
if (cache != null)
{
Resource old = cache.putIfAbsent(target, resourcesDir);
@ -504,7 +498,7 @@ public class MetaInfConfiguration extends AbstractConfiguration
LOG.debug("{} META-INF/resources cache updated", target);
}
if (resourcesDir == EmptyResource.INSTANCE)
if (isEmptyResource(resourcesDir))
{
return;
}
@ -523,6 +517,11 @@ public class MetaInfConfiguration extends AbstractConfiguration
dirs.add(resourcesDir);
}
private static boolean isEmptyResource(Resource resourcesDir)
{
return !resourcesDir.exists() || !resourcesDir.isDirectory();
}
/**
* Scan for META-INF/web-fragment.xml file in the given jar.
*
@ -538,7 +537,7 @@ public class MetaInfConfiguration extends AbstractConfiguration
if (cache != null && cache.containsKey(jar))
{
webFrag = cache.get(jar);
if (webFrag == EmptyResource.INSTANCE)
if (isEmptyFragment(webFrag))
{
if (LOG.isDebugEnabled())
LOG.debug("{} cached as containing no META-INF/web-fragment.xml", jar);
@ -561,14 +560,10 @@ public class MetaInfConfiguration extends AbstractConfiguration
URI uri = jar.getURI();
webFrag = Resource.newResource(uriJarPrefix(uri, "!/META-INF/web-fragment.xml"));
}
if (!webFrag.exists() || webFrag.isDirectory())
{
webFrag = EmptyResource.INSTANCE;
}
if (cache != null)
{
//web-fragment.xml doesn't exist: put token in cache to signal we've seen the jar
//web-fragment.xml doesn't exist: put token in cache to signal we've seen the jar
Resource old = cache.putIfAbsent(jar, webFrag);
if (old != null)
webFrag = old;
@ -576,7 +571,7 @@ public class MetaInfConfiguration extends AbstractConfiguration
LOG.debug("{} META-INF/web-fragment.xml cache updated", jar);
}
if (webFrag == EmptyResource.INSTANCE)
if (isEmptyFragment(webFrag))
return;
}
@ -591,6 +586,11 @@ public class MetaInfConfiguration extends AbstractConfiguration
LOG.debug("{} added to context", webFrag);
}
private static boolean isEmptyFragment(Resource webFrag)
{
return !webFrag.exists() || webFrag.isDirectory();
}
/**
* Discover META-INF/*.tld files in the given jar
*

View File

@ -21,7 +21,6 @@ import java.util.List;
import org.acme.webapp.TestAnnotation;
import org.eclipse.jetty.toolchain.test.MavenTestingUtils;
import org.eclipse.jetty.util.IO;
import org.eclipse.jetty.util.resource.EmptyResource;
import org.eclipse.jetty.util.resource.Resource;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
@ -170,7 +169,7 @@ public class TestMetaData
assertThat(list, hasSize(1));
//test an annotation that didn't have an associated resource
list = wac.getMetaData()._annotations.get(EmptyResource.INSTANCE);
list = wac.getMetaData()._annotations.get(null);
assertThat(list, contains(annotationC));
assertThat(list, hasSize(1));