diff --git a/jetty-core/jetty-util/src/main/java/org/eclipse/jetty/util/resource/EmptyResource.java b/jetty-core/jetty-util/src/main/java/org/eclipse/jetty/util/resource/EmptyResource.java deleted file mode 100644 index 4d549131e64..00000000000 --- a/jetty-core/jetty-util/src/main/java/org/eclipse/jetty/util/resource/EmptyResource.java +++ /dev/null @@ -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 list() - { - return null; - } - - @Override - public Resource resolve(String subUriPath) throws IOException - { - return this; - } -} diff --git a/jetty-core/jetty-util/src/main/java/org/eclipse/jetty/util/resource/ResourceCollection.java b/jetty-core/jetty-util/src/main/java/org/eclipse/jetty/util/resource/ResourceCollection.java index fd718a80c81..542f6942949 100644 --- a/jetty-core/jetty-util/src/main/java/org/eclipse/jetty/util/resource/ResourceCollection.java +++ b/jetty-core/jetty-util/src/main/java/org/eclipse/jetty/util/resource/ResourceCollection.java @@ -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); diff --git a/jetty-ee10/jetty-ee10-annotations/src/test/java/org/eclipse/jetty/ee10/annotations/TestAnnotationDecorator.java b/jetty-ee10/jetty-ee10-annotations/src/test/java/org/eclipse/jetty/ee10/annotations/TestAnnotationDecorator.java index 4429bbb3a29..4e5ce96ae24 100644 --- a/jetty-ee10/jetty-ee10-annotations/src/test/java/org/eclipse/jetty/ee10/annotations/TestAnnotationDecorator.java +++ b/jetty-ee10/jetty-ee10-annotations/src/test/java/org/eclipse/jetty/ee10/annotations/TestAnnotationDecorator.java @@ -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; } diff --git a/jetty-ee10/jetty-ee10-webapp/src/main/java/org/eclipse/jetty/ee10/webapp/MetaData.java b/jetty-ee10/jetty-ee10-webapp/src/main/java/org/eclipse/jetty/ee10/webapp/MetaData.java index 32a8a773320..7f98cc9b4c6 100644 --- a/jetty-ee10/jetty-ee10-webapp/src/main/java/org/eclipse/jetty/ee10/webapp/MetaData.java +++ b/jetty-ee10/jetty-ee10-webapp/src/main/java/org/eclipse/jetty/ee10/webapp/MetaData.java @@ -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 _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 list = _annotations.computeIfAbsent(enclosingResource, k -> new ArrayList<>()); @@ -490,7 +486,7 @@ public class MetaData } List 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 diff --git a/jetty-ee10/jetty-ee10-webapp/src/main/java/org/eclipse/jetty/ee10/webapp/MetaInfConfiguration.java b/jetty-ee10/jetty-ee10-webapp/src/main/java/org/eclipse/jetty/ee10/webapp/MetaInfConfiguration.java index 42cb5d5684c..25872038ccf 100644 --- a/jetty-ee10/jetty-ee10-webapp/src/main/java/org/eclipse/jetty/ee10/webapp/MetaInfConfiguration.java +++ b/jetty-ee10/jetty-ee10-webapp/src/main/java/org/eclipse/jetty/ee10/webapp/MetaInfConfiguration.java @@ -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 * diff --git a/jetty-ee10/jetty-ee10-webapp/src/test/java/org/eclipse/jetty/ee10/webapp/TestMetaData.java b/jetty-ee10/jetty-ee10-webapp/src/test/java/org/eclipse/jetty/ee10/webapp/TestMetaData.java index 24a2c8490f7..61942e88eac 100644 --- a/jetty-ee10/jetty-ee10-webapp/src/test/java/org/eclipse/jetty/ee10/webapp/TestMetaData.java +++ b/jetty-ee10/jetty-ee10-webapp/src/test/java/org/eclipse/jetty/ee10/webapp/TestMetaData.java @@ -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)); diff --git a/jetty-ee9/jetty-ee9-annotations/src/test/java/org/eclipse/jetty/ee9/annotations/TestAnnotationDecorator.java b/jetty-ee9/jetty-ee9-annotations/src/test/java/org/eclipse/jetty/ee9/annotations/TestAnnotationDecorator.java index 683c01c42bc..54defad371a 100644 --- a/jetty-ee9/jetty-ee9-annotations/src/test/java/org/eclipse/jetty/ee9/annotations/TestAnnotationDecorator.java +++ b/jetty-ee9/jetty-ee9-annotations/src/test/java/org/eclipse/jetty/ee9/annotations/TestAnnotationDecorator.java @@ -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; } diff --git a/jetty-ee9/jetty-ee9-webapp/src/main/java/org/eclipse/jetty/ee9/webapp/MetaData.java b/jetty-ee9/jetty-ee9-webapp/src/main/java/org/eclipse/jetty/ee9/webapp/MetaData.java index 96691419345..04a091e41d3 100644 --- a/jetty-ee9/jetty-ee9-webapp/src/main/java/org/eclipse/jetty/ee9/webapp/MetaData.java +++ b/jetty-ee9/jetty-ee9-webapp/src/main/java/org/eclipse/jetty/ee9/webapp/MetaData.java @@ -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 _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 list = _annotations.computeIfAbsent(enclosingResource, k -> new ArrayList<>()); @@ -490,7 +486,7 @@ public class MetaData } List 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 diff --git a/jetty-ee9/jetty-ee9-webapp/src/main/java/org/eclipse/jetty/ee9/webapp/MetaInfConfiguration.java b/jetty-ee9/jetty-ee9-webapp/src/main/java/org/eclipse/jetty/ee9/webapp/MetaInfConfiguration.java index a3b0e611502..cb3d88c1455 100644 --- a/jetty-ee9/jetty-ee9-webapp/src/main/java/org/eclipse/jetty/ee9/webapp/MetaInfConfiguration.java +++ b/jetty-ee9/jetty-ee9-webapp/src/main/java/org/eclipse/jetty/ee9/webapp/MetaInfConfiguration.java @@ -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 * diff --git a/jetty-ee9/jetty-ee9-webapp/src/test/java/org/eclipse/jetty/ee9/webapp/TestMetaData.java b/jetty-ee9/jetty-ee9-webapp/src/test/java/org/eclipse/jetty/ee9/webapp/TestMetaData.java index 43ae99195db..46b7f63e466 100644 --- a/jetty-ee9/jetty-ee9-webapp/src/test/java/org/eclipse/jetty/ee9/webapp/TestMetaData.java +++ b/jetty-ee9/jetty-ee9-webapp/src/test/java/org/eclipse/jetty/ee9/webapp/TestMetaData.java @@ -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));