From a2732e3cf6af300335469b98221522268878751a Mon Sep 17 00:00:00 2001 From: Jesse McConnell Date: Fri, 31 May 2013 14:37:02 -0500 Subject: [PATCH 1/5] [Bug 401027] javadoc JMX annotations --- .../jetty/util/annotation/ManagedAttribute.java | 10 ++++++++++ .../jetty/util/annotation/ManagedObject.java | 8 ++++++++ .../jetty/util/annotation/ManagedOperation.java | 5 +++++ .../org/eclipse/jetty/util/annotation/Name.java | 17 +++++++++++++++++ 4 files changed, 40 insertions(+) diff --git a/jetty-util/src/main/java/org/eclipse/jetty/util/annotation/ManagedAttribute.java b/jetty-util/src/main/java/org/eclipse/jetty/util/annotation/ManagedAttribute.java index cc12b3c0c35..df1cdd5d763 100644 --- a/jetty-util/src/main/java/org/eclipse/jetty/util/annotation/ManagedAttribute.java +++ b/jetty-util/src/main/java/org/eclipse/jetty/util/annotation/ManagedAttribute.java @@ -36,6 +36,16 @@ import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; +/** + * The @ManagedAttribute annotation is used to indicate that a given method + * exposes a JMX attribute. This annotation is placed always on the reader + * method of a given attribute. Unless it is marked as read-only in the + * configuration of the annotation a corresponding setter is looked for + * following normal naming conventions. For example if this annotation is + * on a method called getFoo() then a method called setFoo() would be looked + * for and if found wired automatically into the jmx attribute. + * + */ @Retention(RetentionPolicy.RUNTIME) @Documented @Target( { ElementType.METHOD } ) diff --git a/jetty-util/src/main/java/org/eclipse/jetty/util/annotation/ManagedObject.java b/jetty-util/src/main/java/org/eclipse/jetty/util/annotation/ManagedObject.java index a323dc4cb2a..b51f2afe3c9 100644 --- a/jetty-util/src/main/java/org/eclipse/jetty/util/annotation/ManagedObject.java +++ b/jetty-util/src/main/java/org/eclipse/jetty/util/annotation/ManagedObject.java @@ -24,6 +24,14 @@ import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; +/** + * The @ManagedObject annotation is used on a class at the top level to + * indicate that it should be exposed as an mbean. It has only one attribute + * to it which is used as the description of the MBean. Should multiple + * @ManagedObject annotations be found in the chain of influence then the + * first description is used. + * + */ @Retention(RetentionPolicy.RUNTIME) @Documented @Target( { ElementType.TYPE } ) diff --git a/jetty-util/src/main/java/org/eclipse/jetty/util/annotation/ManagedOperation.java b/jetty-util/src/main/java/org/eclipse/jetty/util/annotation/ManagedOperation.java index e65e0ba19e0..b9ff0257b60 100644 --- a/jetty-util/src/main/java/org/eclipse/jetty/util/annotation/ManagedOperation.java +++ b/jetty-util/src/main/java/org/eclipse/jetty/util/annotation/ManagedOperation.java @@ -36,6 +36,11 @@ import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; +/** + * The @ManagedOperation annotation is used to indicate that a given method + * should be considered a JMX operation. + * + */ @Retention(RetentionPolicy.RUNTIME) @Documented @Target( { ElementType.METHOD } ) diff --git a/jetty-util/src/main/java/org/eclipse/jetty/util/annotation/Name.java b/jetty-util/src/main/java/org/eclipse/jetty/util/annotation/Name.java index cfd631abd2f..5d7f1b74328 100644 --- a/jetty-util/src/main/java/org/eclipse/jetty/util/annotation/Name.java +++ b/jetty-util/src/main/java/org/eclipse/jetty/util/annotation/Name.java @@ -24,11 +24,28 @@ import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; +/** + * This annotation is used to describe variables in method + * signatures so that when rendered into tools like JConsole + * it is clear what the parameters are. For example: + * + * public void doodle(@Name(value="doodle", description="A description of the argument") String doodle) + * + */ @Retention(RetentionPolicy.RUNTIME) @Documented @Target( { ElementType.PARAMETER } ) public @interface Name { + /** + * the name of the parameter + * @return + */ String value(); + + /** + * the description of the parameter + * @return + */ String description() default ""; } From d581dac2872e7dbb81462559f157ffba7fb8a474 Mon Sep 17 00:00:00 2001 From: Jan Bartel Date: Mon, 3 Jun 2013 12:34:37 +1000 Subject: [PATCH 2/5] 409684 Ids and properties not set for execution of jetty xml config files with mvn plugin --- .../jetty/maven/plugin/AbstractJettyMojo.java | 20 +++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/jetty-maven-plugin/src/main/java/org/eclipse/jetty/maven/plugin/AbstractJettyMojo.java b/jetty-maven-plugin/src/main/java/org/eclipse/jetty/maven/plugin/AbstractJettyMojo.java index 558899c1819..4e976f42118 100644 --- a/jetty-maven-plugin/src/main/java/org/eclipse/jetty/maven/plugin/AbstractJettyMojo.java +++ b/jetty-maven-plugin/src/main/java/org/eclipse/jetty/maven/plugin/AbstractJettyMojo.java @@ -449,12 +449,28 @@ public abstract class AbstractJettyMojo extends AbstractMojo { if (getJettyXmlFiles() == null) return; - + + XmlConfiguration last = null; for ( File xmlFile : getJettyXmlFiles() ) { getLog().info( "Configuring Jetty from xml configuration file = " + xmlFile.getCanonicalPath() ); XmlConfiguration xmlConfiguration = new XmlConfiguration(Resource.toURL(xmlFile)); - xmlConfiguration.configure(this.server); + + //chain ids from one config file to another + if (last == null) + xmlConfiguration.getIdMap().put("Server", this.server); + else + xmlConfiguration.getIdMap().putAll(last.getIdMap()); + + //Set the system properties each time in case the config file set a new one + Enumeration ensysprop = System.getProperties().propertyNames(); + while (ensysprop.hasMoreElements()) + { + String name = (String)ensysprop.nextElement(); + xmlConfiguration.getProperties().put(name,System.getProperty(name)); + } + last = xmlConfiguration; + xmlConfiguration.configure(); } } From 952f8442e5f07bde3f747a799d4bc56a42a1a0bf Mon Sep 17 00:00:00 2001 From: Greg Wilkins Date: Mon, 3 Jun 2013 14:15:55 +1000 Subject: [PATCH 3/5] 408529 Etags set in 304 response Improved default servlet setting of etags --- .../eclipse/jetty/servlet/DefaultServlet.java | 84 +++++++------------ 1 file changed, 29 insertions(+), 55 deletions(-) diff --git a/jetty-servlet/src/main/java/org/eclipse/jetty/servlet/DefaultServlet.java b/jetty-servlet/src/main/java/org/eclipse/jetty/servlet/DefaultServlet.java index b5855333a07..e09cbcf2e72 100644 --- a/jetty-servlet/src/main/java/org/eclipse/jetty/servlet/DefaultServlet.java +++ b/jetty-servlet/src/main/java/org/eclipse/jetty/servlet/DefaultServlet.java @@ -658,7 +658,7 @@ public class DefaultServlet extends HttpServlet implements ResourceFactory if (ifm!=null) { boolean match=false; - if (content!=null && content.getETag()!=null) + if (content.getETag()!=null) { QuotedStringTokenizer quoted = new QuotedStringTokenizer(ifm,", ",false,true); while (!match && quoted.hasMoreTokens()) @@ -671,48 +671,39 @@ public class DefaultServlet extends HttpServlet implements ResourceFactory if (!match) { - Response r = Response.getResponse(response); - r.reset(true); - r.setStatus(HttpServletResponse.SC_PRECONDITION_FAILED); + response.setStatus(HttpServletResponse.SC_PRECONDITION_FAILED); return false; } } - String ifnm=request.getHeader(HttpHeader.IF_NONE_MATCH.asString()); - if (ifnm!=null && content!=null && content.getETag()!=null) + String if_non_match_etag=request.getHeader(HttpHeader.IF_NONE_MATCH.asString()); + if (if_non_match_etag!=null && content.getETag()!=null) { // Look for GzipFiltered version of etag if (content.getETag().toString().equals(request.getAttribute("o.e.j.s.GzipFilter.ETag"))) { - Response r = Response.getResponse(response); - r.reset(true); - r.setStatus(HttpServletResponse.SC_NOT_MODIFIED); - r.getHttpFields().put(HttpHeader.ETAG,ifnm); + response.setStatus(HttpServletResponse.SC_NOT_MODIFIED); + response.setHeader(HttpHeader.ETAG.asString(),if_non_match_etag); return false; } - // Handle special case of exact match. - if (content.getETag().toString().equals(ifnm)) + if (content.getETag().toString().equals(if_non_match_etag)) { - Response r = Response.getResponse(response); - r.reset(true); - r.setStatus(HttpServletResponse.SC_NOT_MODIFIED); - r.getHttpFields().put(HttpHeader.ETAG,content.getETag()); + response.setStatus(HttpServletResponse.SC_NOT_MODIFIED); + response.setHeader(HttpHeader.ETAG.asString(),content.getETag()); return false; } // Handle list of tags - QuotedStringTokenizer quoted = new QuotedStringTokenizer(ifnm,", ",false,true); + QuotedStringTokenizer quoted = new QuotedStringTokenizer(if_non_match_etag,", ",false,true); while (quoted.hasMoreTokens()) { String tag = quoted.nextToken(); if (content.getETag().toString().equals(tag)) { - Response r = Response.getResponse(response); - r.reset(true); - r.setStatus(HttpServletResponse.SC_NOT_MODIFIED); - r.getHttpFields().put(HttpHeader.ETAG,content.getETag()); + response.setStatus(HttpServletResponse.SC_NOT_MODIFIED); + response.setHeader(HttpHeader.ETAG.asString(),content.getETag()); return false; } } @@ -727,50 +718,33 @@ public class DefaultServlet extends HttpServlet implements ResourceFactory if (ifms!=null) { //Get jetty's Response impl - Response r = Response.getResponse(response); - - if (content!=null) + String mdlm=content.getLastModified(); + if (mdlm!=null && ifms.equals(mdlm)) { - String mdlm=content.getLastModified(); - if (mdlm!=null) - { - if (ifms.equals(mdlm)) - { - r.reset(true); - r.setStatus(HttpServletResponse.SC_NOT_MODIFIED); - if (_etags) - r.getHttpFields().add(HttpHeader.ETAG,content.getETag()); - r.flushBuffer(); - return false; - } - } + response.setStatus(HttpServletResponse.SC_NOT_MODIFIED); + if (_etags) + response.setHeader(HttpHeader.ETAG.asString(),content.getETag()); + response.flushBuffer(); + return false; } long ifmsl=request.getDateHeader(HttpHeader.IF_MODIFIED_SINCE.asString()); - if (ifmsl!=-1) - { - if (resource.lastModified()/1000 <= ifmsl/1000) - { - r.reset(true); - r.setStatus(HttpServletResponse.SC_NOT_MODIFIED); - if (_etags) - r.getHttpFields().add(HttpHeader.ETAG,content.getETag()); - r.flushBuffer(); - return false; - } + if (ifmsl!=-1 && resource.lastModified()/1000 <= ifmsl/1000) + { + response.setStatus(HttpServletResponse.SC_NOT_MODIFIED); + if (_etags) + response.setHeader(HttpHeader.ETAG.asString(),content.getETag()); + response.flushBuffer(); + return false; } } // Parse the if[un]modified dates and compare to resource long date=request.getDateHeader(HttpHeader.IF_UNMODIFIED_SINCE.asString()); - - if (date!=-1) + if (date!=-1 && resource.lastModified()/1000 > date/1000) { - if (resource.lastModified()/1000 > date/1000) - { - response.sendError(HttpServletResponse.SC_PRECONDITION_FAILED); - return false; - } + response.sendError(HttpServletResponse.SC_PRECONDITION_FAILED); + return false; } } From 7bb3a7be282de6cc2996f8065f98aff1b26ad236 Mon Sep 17 00:00:00 2001 From: Greg Wilkins Date: Mon, 3 Jun 2013 18:21:11 +1000 Subject: [PATCH 4/5] 409556 Resource files not closed Made Resource a closeable so that it is easier to close in a try(){} block. Deprecated release() and made it close() instead. FileResource no longer extends URLResource as it can more efficiently implement all the methods with pure File operations and have no connections or streams that need to be release/closed --- .../eclipse/jetty/ant/AntWebAppContext.java | 13 +- .../org/eclipse/jetty/http/HttpContent.java | 2 +- .../jetty/maven/plugin/JettyRunMojo.java | 14 +- .../osgi/boot/AbstractContextProvider.java | 25 +- .../java/org/eclipse/jetty/runner/Runner.java | 109 +++++---- .../eclipse/jetty/server/ResourceCache.java | 2 +- .../eclipse/jetty/servlet/DefaultServlet.java | 2 +- .../org/eclipse/jetty/util/BufferUtil.java | 13 +- .../jetty/util/component/FileDestroyable.java | 11 +- .../jetty/util/resource/FileResource.java | 222 ++++++++++-------- .../jetty/util/resource/JarFileResource.java | 16 +- .../jetty/util/resource/JarResource.java | 4 +- .../eclipse/jetty/util/resource/Resource.java | 51 ++-- .../util/resource/ResourceCollection.java | 11 +- .../jetty/util/resource/URLResource.java | 2 +- .../util/resource/ResourceAliasTest.java | 118 ++++++++++ .../jetty/util/resource/ResourceTest.java | 42 ++-- .../org/eclipse/jetty/webapp/Descriptor.java | 2 +- .../eclipse/jetty/webapp/OrderingTest.java | 4 +- .../jetty/webapp/WebAppClassLoaderTest.java | 2 +- 20 files changed, 388 insertions(+), 277 deletions(-) create mode 100644 jetty-util/src/test/java/org/eclipse/jetty/util/resource/ResourceAliasTest.java diff --git a/jetty-ant/src/main/java/org/eclipse/jetty/ant/AntWebAppContext.java b/jetty-ant/src/main/java/org/eclipse/jetty/ant/AntWebAppContext.java index dca34758fcf..47ecd5bc0ea 100644 --- a/jetty-ant/src/main/java/org/eclipse/jetty/ant/AntWebAppContext.java +++ b/jetty-ant/src/main/java/org/eclipse/jetty/ant/AntWebAppContext.java @@ -540,9 +540,8 @@ public class AntWebAppContext extends WebAppContext if (getDescriptor() != null) { - try + try (Resource r = Resource.newResource(getDescriptor());) { - Resource r = Resource.newResource(getDescriptor()); scanList.add(r.getFile()); } catch (IOException e) @@ -553,9 +552,8 @@ public class AntWebAppContext extends WebAppContext if (getJettyEnvXml() != null) { - try + try (Resource r = Resource.newResource(getJettyEnvXml());) { - Resource r = Resource.newResource(getJettyEnvXml()); scanList.add(r.getFile()); } catch (IOException e) @@ -566,11 +564,10 @@ public class AntWebAppContext extends WebAppContext if (getDefaultsDescriptor() != null) { - try + try (Resource r = Resource.newResource(getDefaultsDescriptor());) { - if (!AntWebAppContext.WEB_DEFAULTS_XML.equals(getDefaultsDescriptor())) - { - Resource r = Resource.newResource(getDefaultsDescriptor()); + if (!WebAppContext.WEB_DEFAULTS_XML.equals(getDefaultsDescriptor())) + { scanList.add(r.getFile()); } } diff --git a/jetty-http/src/main/java/org/eclipse/jetty/http/HttpContent.java b/jetty-http/src/main/java/org/eclipse/jetty/http/HttpContent.java index 4bc07053501..61775e579e7 100644 --- a/jetty-http/src/main/java/org/eclipse/jetty/http/HttpContent.java +++ b/jetty-http/src/main/java/org/eclipse/jetty/http/HttpContent.java @@ -171,7 +171,7 @@ public interface HttpContent @Override public void release() { - _resource.release(); + _resource.close(); } } } diff --git a/jetty-maven-plugin/src/main/java/org/eclipse/jetty/maven/plugin/JettyRunMojo.java b/jetty-maven-plugin/src/main/java/org/eclipse/jetty/maven/plugin/JettyRunMojo.java index 1c244b30ed1..cf9a9b50e0d 100644 --- a/jetty-maven-plugin/src/main/java/org/eclipse/jetty/maven/plugin/JettyRunMojo.java +++ b/jetty-maven-plugin/src/main/java/org/eclipse/jetty/maven/plugin/JettyRunMojo.java @@ -405,9 +405,8 @@ public class JettyRunMojo extends AbstractJettyMojo scanList = new ArrayList(); if (webApp.getDescriptor() != null) { - try + try (Resource r = Resource.newResource(webApp.getDescriptor());) { - Resource r = Resource.newResource(webApp.getDescriptor()); scanList.add(r.getFile()); } catch (IOException e) @@ -418,9 +417,8 @@ public class JettyRunMojo extends AbstractJettyMojo if (webApp.getJettyEnvXml() != null) { - try + try (Resource r = Resource.newResource(webApp.getJettyEnvXml());) { - Resource r = Resource.newResource(webApp.getJettyEnvXml()); scanList.add(r.getFile()); } catch (IOException e) @@ -431,13 +429,10 @@ public class JettyRunMojo extends AbstractJettyMojo if (webApp.getDefaultsDescriptor() != null) { - try + try (Resource r = Resource.newResource(webApp.getDefaultsDescriptor());) { if (!WebAppContext.WEB_DEFAULTS_XML.equals(webApp.getDefaultsDescriptor())) - { - Resource r = Resource.newResource(webApp.getDefaultsDescriptor()); scanList.add(r.getFile()); - } } catch (IOException e) { @@ -447,9 +442,8 @@ public class JettyRunMojo extends AbstractJettyMojo if (webApp.getOverrideDescriptor() != null) { - try + try (Resource r = Resource.newResource(webApp.getOverrideDescriptor());) { - Resource r = Resource.newResource(webApp.getOverrideDescriptor()); scanList.add(r.getFile()); } catch (IOException e) diff --git a/jetty-osgi/jetty-osgi-boot/src/main/java/org/eclipse/jetty/osgi/boot/AbstractContextProvider.java b/jetty-osgi/jetty-osgi-boot/src/main/java/org/eclipse/jetty/osgi/boot/AbstractContextProvider.java index b15d6179dbc..f056bfb47db 100644 --- a/jetty-osgi/jetty-osgi-boot/src/main/java/org/eclipse/jetty/osgi/boot/AbstractContextProvider.java +++ b/jetty-osgi/jetty-osgi-boot/src/main/java/org/eclipse/jetty/osgi/boot/AbstractContextProvider.java @@ -169,7 +169,7 @@ public abstract class AbstractContextProvider extends AbstractLifeCycle implemen jettyHome = jettyHome.substring(0,jettyHome.length()-1); res = getFileAsResource(jettyHome, _contextFile); - if (LOG.isDebugEnabled()) LOG.debug("jetty home context file:"+res); + LOG.debug("jetty home context file: {}",res); } } } @@ -179,8 +179,11 @@ public abstract class AbstractContextProvider extends AbstractLifeCycle implemen { if (bundleOverrideLocation != null) { - res = getFileAsResource(Resource.newResource(bundleOverrideLocation).getFile(), _contextFile); - if (LOG.isDebugEnabled()) LOG.debug("Bundle override location context file:"+res); + try(Resource location=Resource.newResource(bundleOverrideLocation)) + { + res=location.addPath(_contextFile); + } + LOG.debug("Bundle override location context file: {}",res); } } @@ -295,22 +298,6 @@ public abstract class AbstractContextProvider extends AbstractLifeCycle implemen } return r; } - - private Resource getFileAsResource (File dir, String file) - { - Resource r = null; - try - { - File asFile = new File (dir, file); - if (asFile.exists()) - r = Resource.newResource(asFile); - } - catch (Exception e) - { - r = null; - } - return r; - } } /* ------------------------------------------------------------ */ diff --git a/jetty-runner/src/main/java/org/eclipse/jetty/runner/Runner.java b/jetty-runner/src/main/java/org/eclipse/jetty/runner/Runner.java index 19a9a2d4a54..3cf257d99f4 100644 --- a/jetty-runner/src/main/java/org/eclipse/jetty/runner/Runner.java +++ b/jetty-runner/src/main/java/org/eclipse/jetty/runner/Runner.java @@ -118,17 +118,18 @@ public class Runner if (".".equals(path) || "..".equals(path)) continue; - Resource item = lib.addPath(path); - - if (item.isDirectory()) - addJars(item); - else + try(Resource item = lib.addPath(path);) { - if (path.toLowerCase().endsWith(".jar") || - path.toLowerCase().endsWith(".zip")) + if (item.isDirectory()) + addJars(item); + else { - URL url = item.getURL(); - _classpath.add(url); + if (path.toLowerCase().endsWith(".jar") || + path.toLowerCase().endsWith(".zip")) + { + URL url = item.getURL(); + _classpath.add(url); + } } } } @@ -214,24 +215,30 @@ public class Runner { if ("--lib".equals(args[i])) { - Resource lib = Resource.newResource(args[++i]); - if (!lib.exists() || !lib.isDirectory()) - usage("No such lib directory "+lib); - _classpath.addJars(lib); + try(Resource lib = Resource.newResource(args[++i]);) + { + if (!lib.exists() || !lib.isDirectory()) + usage("No such lib directory "+lib); + _classpath.addJars(lib); + } } else if ("--jar".equals(args[i])) { - Resource jar = Resource.newResource(args[++i]); - if (!jar.exists() || jar.isDirectory()) - usage("No such jar "+jar); - _classpath.addPath(jar); + try(Resource jar = Resource.newResource(args[++i]);) + { + if (!jar.exists() || jar.isDirectory()) + usage("No such jar "+jar); + _classpath.addPath(jar); + } } else if ("--classes".equals(args[i])) { - Resource classes = Resource.newResource(args[++i]); - if (!classes.exists() || !classes.isDirectory()) - usage("No such classes directory "+classes); - _classpath.addPath(classes); + try(Resource classes = Resource.newResource(args[++i]);) + { + if (!classes.exists() || !classes.isDirectory()) + usage("No such classes directory "+classes); + _classpath.addPath(classes); + } } else if (args[i].startsWith("--")) i++; @@ -315,8 +322,11 @@ public class Runner { for (String cfg:_configFiles) { - XmlConfiguration xmlConfiguration = new XmlConfiguration(Resource.newResource(cfg).getURL()); - xmlConfiguration.configure(_server); + try (Resource resource=Resource.newResource(cfg)) + { + XmlConfiguration xmlConfiguration = new XmlConfiguration(resource.getURL()); + xmlConfiguration.configure(_server); + } } } @@ -416,34 +426,35 @@ public class Runner } // Create a context - Resource ctx = Resource.newResource(args[i]); - if (!ctx.exists()) - usage("Context '"+ctx+"' does not exist"); - - if (contextPathSet && !(contextPath.startsWith("/"))) - contextPath = "/"+contextPath; + try(Resource ctx = Resource.newResource(args[i]);) + { + if (!ctx.exists()) + usage("Context '"+ctx+"' does not exist"); - // Configure the context - if (!ctx.isDirectory() && ctx.toString().toLowerCase().endsWith(".xml")) - { - // It is a context config file - XmlConfiguration xmlConfiguration=new XmlConfiguration(ctx.getURL()); - xmlConfiguration.getIdMap().put("Server",_server); - ContextHandler handler=(ContextHandler)xmlConfiguration.configure(); - if (contextPathSet) - handler.setContextPath(contextPath); - _contexts.addHandler(handler); - handler.setAttribute("org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern", __containerIncludeJarPattern); + if (contextPathSet && !(contextPath.startsWith("/"))) + contextPath = "/"+contextPath; + + // Configure the context + if (!ctx.isDirectory() && ctx.toString().toLowerCase().endsWith(".xml")) + { + // It is a context config file + XmlConfiguration xmlConfiguration=new XmlConfiguration(ctx.getURL()); + xmlConfiguration.getIdMap().put("Server",_server); + ContextHandler handler=(ContextHandler)xmlConfiguration.configure(); + if (contextPathSet) + handler.setContextPath(contextPath); + _contexts.addHandler(handler); + handler.setAttribute("org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern", __containerIncludeJarPattern); + } + else + { + // assume it is a WAR file + WebAppContext webapp = new WebAppContext(_contexts,ctx.toString(),contextPath); + webapp.setConfigurationClasses(__plusConfigurationClasses); + webapp.setAttribute("org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern", + __containerIncludeJarPattern); + } } - else - { - // assume it is a WAR file - WebAppContext webapp = new WebAppContext(_contexts,ctx.toString(),contextPath); - webapp.setConfigurationClasses(__plusConfigurationClasses); - webapp.setAttribute("org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern", - __containerIncludeJarPattern); - } - //reset contextPathSet = false; contextPath = __defaultContextPath; diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/ResourceCache.java b/jetty-server/src/main/java/org/eclipse/jetty/server/ResourceCache.java index 6dc7640ce07..9cd68a401c0 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/ResourceCache.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/ResourceCache.java @@ -444,7 +444,7 @@ public class ResourceCache // Invalidate it _cachedSize.addAndGet(-_length); _cachedFiles.decrementAndGet(); - _resource.release(); + _resource.close(); } /* ------------------------------------------------------------ */ diff --git a/jetty-servlet/src/main/java/org/eclipse/jetty/servlet/DefaultServlet.java b/jetty-servlet/src/main/java/org/eclipse/jetty/servlet/DefaultServlet.java index e09cbcf2e72..4a32465a731 100644 --- a/jetty-servlet/src/main/java/org/eclipse/jetty/servlet/DefaultServlet.java +++ b/jetty-servlet/src/main/java/org/eclipse/jetty/servlet/DefaultServlet.java @@ -567,7 +567,7 @@ public class DefaultServlet extends HttpServlet implements ResourceFactory if (content!=null) content.release(); else if (resource!=null) - resource.release(); + resource.close(); } } diff --git a/jetty-util/src/main/java/org/eclipse/jetty/util/BufferUtil.java b/jetty-util/src/main/java/org/eclipse/jetty/util/BufferUtil.java index 0dbfff9eb05..07185d038cc 100644 --- a/jetty-util/src/main/java/org/eclipse/jetty/util/BufferUtil.java +++ b/jetty-util/src/main/java/org/eclipse/jetty/util/BufferUtil.java @@ -26,7 +26,6 @@ import java.io.RandomAccessFile; import java.nio.Buffer; import java.nio.BufferOverflowException; import java.nio.ByteBuffer; -import java.nio.MappedByteBuffer; import java.nio.channels.FileChannel; import java.nio.channels.FileChannel.MapMode; import java.nio.charset.Charset; @@ -372,12 +371,14 @@ public class BufferUtil /* ------------------------------------------------------------ */ public static void readFrom(File file, ByteBuffer buffer) throws IOException { - RandomAccessFile raf = new RandomAccessFile(file,"r"); - FileChannel channel = raf.getChannel(); - long needed=raf.length(); + try(RandomAccessFile raf = new RandomAccessFile(file,"r");) + { + FileChannel channel = raf.getChannel(); + long needed=raf.length(); - while (needed>0 && buffer.hasRemaining()) - needed=needed-channel.read(buffer); + while (needed>0 && buffer.hasRemaining()) + needed=needed-channel.read(buffer); + } } /* ------------------------------------------------------------ */ diff --git a/jetty-util/src/main/java/org/eclipse/jetty/util/component/FileDestroyable.java b/jetty-util/src/main/java/org/eclipse/jetty/util/component/FileDestroyable.java index e38eb8b35d5..fdc9f9834f3 100644 --- a/jetty-util/src/main/java/org/eclipse/jetty/util/component/FileDestroyable.java +++ b/jetty-util/src/main/java/org/eclipse/jetty/util/component/FileDestroyable.java @@ -50,7 +50,10 @@ public class FileDestroyable implements Destroyable public void addFile(String file) throws IOException { - _files.add(Resource.newResource(file).getFile()); + try(Resource r = Resource.newResource(file);) + { + _files.add(r.getFile()); + } } public void addFile(File file) @@ -65,7 +68,10 @@ public class FileDestroyable implements Destroyable public void removeFile(String file) throws IOException { - _files.remove(Resource.newResource(file).getFile()); + try(Resource r = Resource.newResource(file);) + { + _files.remove(r.getFile()); + } } public void removeFile(File file) @@ -73,6 +79,7 @@ public class FileDestroyable implements Destroyable _files.remove(file); } + @Override public void destroy() { for (File file : _files) diff --git a/jetty-util/src/main/java/org/eclipse/jetty/util/resource/FileResource.java b/jetty-util/src/main/java/org/eclipse/jetty/util/resource/FileResource.java index 030f02a314c..3fc1ef7d8d7 100644 --- a/jetty-util/src/main/java/org/eclipse/jetty/util/resource/FileResource.java +++ b/jetty-util/src/main/java/org/eclipse/jetty/util/resource/FileResource.java @@ -20,10 +20,8 @@ package org.eclipse.jetty.util.resource; import java.io.File; import java.io.FileInputStream; -import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; -import java.io.OutputStream; import java.net.MalformedURLException; import java.net.URI; import java.net.URISyntaxException; @@ -50,25 +48,24 @@ import org.eclipse.jetty.util.log.Logger; * * */ -public class FileResource extends URLResource +public class FileResource extends Resource { private static final Logger LOG = Log.getLogger(FileResource.class); /* ------------------------------------------------------------ */ - private File _file; - private transient URL _alias=null; - private transient boolean _aliasChecked=false; + private final File _file; + private final String _uri; + private final URL _alias; /* -------------------------------------------------------- */ public FileResource(URL url) throws IOException, URISyntaxException { - super(url,null); - + File file; try { // Try standard API to convert URL to file. - _file =new File(new URI(url.toString())); + file =new File(url.toURI()); } catch (URISyntaxException e) { @@ -76,48 +73,86 @@ public class FileResource extends URLResource } catch (Exception e) { + if (!url.toString().startsWith("file:")) + throw new IllegalArgumentException("!file:"); + LOG.ignore(e); try { - // Assume that File.toURL produced unencoded chars. So try - // encoding them. + // Assume that File.toURL produced unencoded chars. So try encoding them. String file_url="file:"+URIUtil.encodePath(url.toString().substring(5)); URI uri = new URI(file_url); if (uri.getAuthority()==null) - _file = new File(uri); + file = new File(uri); else - _file = new File("//"+uri.getAuthority()+URIUtil.decodePath(url.getFile())); + file = new File("//"+uri.getAuthority()+URIUtil.decodePath(url.getFile())); } catch (Exception e2) { LOG.ignore(e2); - // Still can't get the file. Doh! try good old hack! - checkConnection(); - Permission perm = _connection.getPermission(); - _file = new File(perm==null?url.getFile():perm.getName()); + URLConnection connection=url.openConnection(); + Permission perm = connection.getPermission(); + file = new File(perm==null?url.getFile():perm.getName()); } } - if (_file.isDirectory()) - { - if (!_urlString.endsWith("/")) - _urlString=_urlString+"/"; - } - else - { - if (_urlString.endsWith("/")) - _urlString=_urlString.substring(0,_urlString.length()-1); - } - + + _file=file; + _uri=normalizeURI(_file,url.toURI()); + _alias=checkAlias(_file); } /* -------------------------------------------------------- */ - FileResource(URL url, URLConnection connection, File file) + public FileResource(URI uri) { - super(url,connection); + File file=new File(uri); _file=file; - if (_file.isDirectory() && !_urlString.endsWith("/")) - _urlString=_urlString+"/"; + _uri=normalizeURI(_file,uri); + _alias=checkAlias(_file); + } + + /* -------------------------------------------------------- */ + FileResource(File file) + { + _file=file; + _uri=normalizeURI(_file,_file.toURI()); + _alias=checkAlias(_file); + } + + /* -------------------------------------------------------- */ + private static String normalizeURI(File file, URI uri) + { + String u =uri.toASCIIString(); + if (file.isDirectory()) + { + if(!u.endsWith("/")) + u+="/"; + } + else if (file.exists() && u.endsWith("/")) + u=u.substring(0,u.length()-1); + return u; + } + + /* -------------------------------------------------------- */ + private static URL checkAlias(File file) + { + try + { + String abs=file.getAbsolutePath(); + String can=file.getCanonicalPath(); + + if (!abs.equals(can)) + { + LOG.debug("ALIAS abs={} can={}",abs,can); + return new File(can).toURI().toURL(); + } + } + catch(IOException e) + { + LOG.warn(e); + } + + return null; } /* -------------------------------------------------------- */ @@ -125,45 +160,35 @@ public class FileResource extends URLResource public Resource addPath(String path) throws IOException,MalformedURLException { - URLResource r=null; - String url=null; - path = org.eclipse.jetty.util.URIUtil.canonicalPath(path); - + + if (path==null) + throw new MalformedURLException(); + if ("/".equals(path)) return this; - else if (!isDirectory()) - { - r=(FileResource)super.addPath(path); - url=r._urlString; - } - else - { - if (path==null) - throw new MalformedURLException(); - - // treat all paths being added as relative - String rel=path; - if (path.startsWith("/")) - rel = path.substring(1); - - url=URIUtil.addPaths(_urlString,URIUtil.encodePath(rel)); - r=(URLResource)Resource.newResource(url); - } - String encoded=URIUtil.encodePath(path); - int expected=r.toString().length()-encoded.length(); - int index = r._urlString.lastIndexOf(encoded, expected); + path=URIUtil.encodePath(path); - if (expected!=index && ((expected-1)!=index || path.endsWith("/") || !r.isDirectory())) + URI uri; + try { - if (!(r instanceof BadResource)) + if (_file.isDirectory()) { - ((FileResource)r)._alias=new URL(url); - ((FileResource)r)._aliasChecked=true; + // treat all paths being added as relative + uri=new URI(URIUtil.addPaths(_uri,path)); } - } - return r; + else + { + uri=new URI(_uri+path); + } + } + catch(final URISyntaxException e) + { + throw new MalformedURLException(){{initCause(e);}}; + } + + return new FileResource(uri); } @@ -171,30 +196,6 @@ public class FileResource extends URLResource @Override public URL getAlias() { - if (!_aliasChecked) - { - try - { - String abs=_file.getAbsolutePath(); - String can=_file.getCanonicalPath(); - - if (abs.length()!=can.length() || !abs.equals(can)) - _alias=Resource.toURL(new File(can)); - - _aliasChecked=true; - - if (_alias!=null && LOG.isDebugEnabled()) - { - LOG.debug("ALIAS abs="+abs); - LOG.debug("ALIAS can="+can); - } - } - catch(Exception e) - { - LOG.warn(Log.EXCEPTION,e); - return getURL(); - } - } return _alias; } @@ -220,12 +221,12 @@ public class FileResource extends URLResource /* -------------------------------------------------------- */ /** - * Returns true if the respresenetd resource is a container/directory. + * Returns true if the resource is a container/directory. */ @Override public boolean isDirectory() { - return _file.isDirectory(); + return _file.exists() && _file.isDirectory() || _uri.endsWith("/"); } /* --------------------------------------------------------- */ @@ -320,18 +321,6 @@ public class FileResource extends URLResource } return list; } - - /* ------------------------------------------------------------ */ - /** Encode according to this resource type. - * File URIs are encoded. - * @param uri URI to encode. - * @return The uri unchanged. - */ - @Override - public String encode(String uri) - { - return uri; - } /* ------------------------------------------------------------ */ /** @@ -377,4 +366,35 @@ public class FileResource extends URLResource IO.copy(getFile(),destination); } } + + @Override + public boolean isContainedIn(Resource r) throws MalformedURLException + { + return false; + } + + @Override + public void close() + { + } + + @Override + public URL getURL() + { + try + { + return _file.toURI().toURL(); + } + catch (MalformedURLException e) + { + throw new IllegalStateException(e); + } + } + + @Override + public String toString() + { + return _uri; + } + } diff --git a/jetty-util/src/main/java/org/eclipse/jetty/util/resource/JarFileResource.java b/jetty-util/src/main/java/org/eclipse/jetty/util/resource/JarFileResource.java index 79a5d8fe5ee..a4004b8b4f0 100644 --- a/jetty-util/src/main/java/org/eclipse/jetty/util/resource/JarFileResource.java +++ b/jetty-util/src/main/java/org/eclipse/jetty/util/resource/JarFileResource.java @@ -60,7 +60,7 @@ class JarFileResource extends JarResource /* ------------------------------------------------------------ */ @Override - public synchronized void release() + public synchronized void close() { _list=null; _entry=null; @@ -83,7 +83,7 @@ class JarFileResource extends JarResource } } _jarFile=null; - super.release(); + super.close(); } /* ------------------------------------------------------------ */ @@ -374,18 +374,6 @@ class JarFileResource extends JarResource return -1; } - - /* ------------------------------------------------------------ */ - /** Encode according to this resource type. - * File URIs are not encoded. - * @param uri URI to encode. - * @return The uri unchanged. - */ - @Override - public String encode(String uri) - { - return uri; - } /** diff --git a/jetty-util/src/main/java/org/eclipse/jetty/util/resource/JarResource.java b/jetty-util/src/main/java/org/eclipse/jetty/util/resource/JarResource.java index a221d539cab..fe9dbb44e27 100644 --- a/jetty-util/src/main/java/org/eclipse/jetty/util/resource/JarResource.java +++ b/jetty-util/src/main/java/org/eclipse/jetty/util/resource/JarResource.java @@ -55,10 +55,10 @@ public class JarResource extends URLResource /* ------------------------------------------------------------ */ @Override - public synchronized void release() + public synchronized void close() { _jarConnection=null; - super.release(); + super.close(); } /* ------------------------------------------------------------ */ diff --git a/jetty-util/src/main/java/org/eclipse/jetty/util/resource/Resource.java b/jetty-util/src/main/java/org/eclipse/jetty/util/resource/Resource.java index f80e5ab5f54..683156672f1 100644 --- a/jetty-util/src/main/java/org/eclipse/jetty/util/resource/Resource.java +++ b/jetty-util/src/main/java/org/eclipse/jetty/util/resource/Resource.java @@ -18,6 +18,7 @@ package org.eclipse.jetty.util.resource; +import java.io.Closeable; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; @@ -26,7 +27,6 @@ import java.io.OutputStream; import java.net.MalformedURLException; import java.net.URI; import java.net.URL; -import java.net.URLConnection; import java.nio.channels.ReadableByteChannel; import java.text.DateFormat; import java.util.Arrays; @@ -45,7 +45,7 @@ import org.eclipse.jetty.util.log.Logger; /** * Abstract resource class. */ -public abstract class Resource implements ResourceFactory +public abstract class Resource implements ResourceFactory, Closeable { private static final Logger LOG = Log.getLogger(Resource.class); public static boolean __defaultUseCaches = true; @@ -149,7 +149,7 @@ public abstract class Resource implements ResourceFactory * @param useCaches controls URLConnection caching * @return A Resource object. */ - public static Resource newResource (String resource, boolean useCaches) + public static Resource newResource(String resource, boolean useCaches) throws MalformedURLException, IOException { URL url=null; @@ -171,11 +171,7 @@ public abstract class Resource implements ResourceFactory resource=resource.substring(2); File file=new File(resource).getCanonicalFile(); - url=Resource.toURL(file); - - URLConnection connection=url.openConnection(); - connection.setUseCaches(useCaches); - return new FileResource(url,connection,file); + return new FileResource(file); } catch(Exception e2) { @@ -194,15 +190,9 @@ public abstract class Resource implements ResourceFactory } /* ------------------------------------------------------------ */ - public static Resource newResource (File file) - throws MalformedURLException, IOException + public static Resource newResource(File file) { - file = file.getCanonicalFile(); - URL url = Resource.toURL(file); - - URLConnection connection = url.openConnection(); - FileResource fileResource = new FileResource(url, connection, file); - return fileResource; + return new FileResource(file); } /* ------------------------------------------------------------ */ @@ -300,7 +290,7 @@ public abstract class Resource implements ResourceFactory @Override protected void finalize() { - release(); + close(); } /* ------------------------------------------------------------ */ @@ -309,9 +299,18 @@ public abstract class Resource implements ResourceFactory /* ------------------------------------------------------------ */ /** Release any temporary resources held by the resource. + * @deprecated use {@link #close()} */ - public abstract void release(); - + public final void release() + { + close(); + } + + /* ------------------------------------------------------------ */ + /** Release any temporary resources held by the resource. + */ + @Override + public abstract void close(); /* ------------------------------------------------------------ */ /** @@ -420,19 +419,19 @@ public abstract class Resource implements ResourceFactory /** * Returns the resource contained inside the current resource with the * given name. - * @param path The path segment to add, which should be encoded by the - * encode method. + * @param path The path segment to add, which is not encoded */ public abstract Resource addPath(String path) throws IOException,MalformedURLException; /* ------------------------------------------------------------ */ - /** Get a resource from withing this resource. + /** Get a resource from within this resource. *

* This method is essentially an alias for {@link #addPath(String)}, but without checked exceptions. * This method satisfied the {@link ResourceFactory} interface. * @see org.eclipse.jetty.util.resource.ResourceFactory#getResource(java.lang.String) */ + @Override public Resource getResource(String path) { try @@ -447,14 +446,12 @@ public abstract class Resource implements ResourceFactory } /* ------------------------------------------------------------ */ - /** Encode according to this resource type. - * The default implementation calls URI.encodePath(uri) - * @param uri - * @return String encoded for this resource type. + /** + * @deprecated */ public String encode(String uri) { - return URIUtil.encodePath(uri); + return null; } /* ------------------------------------------------------------ */ diff --git a/jetty-util/src/main/java/org/eclipse/jetty/util/resource/ResourceCollection.java b/jetty-util/src/main/java/org/eclipse/jetty/util/resource/ResourceCollection.java index 36fd49b4546..451a1514b27 100644 --- a/jetty-util/src/main/java/org/eclipse/jetty/util/resource/ResourceCollection.java +++ b/jetty-util/src/main/java/org/eclipse/jetty/util/resource/ResourceCollection.java @@ -21,7 +21,6 @@ package org.eclipse.jetty.util.resource; import java.io.File; import java.io.IOException; import java.io.InputStream; -import java.io.OutputStream; import java.net.MalformedURLException; import java.net.URL; import java.nio.channels.ReadableByteChannel; @@ -32,7 +31,6 @@ import java.util.List; import java.util.StringTokenizer; import org.eclipse.jetty.util.URIUtil; -import org.eclipse.jetty.util.component.AbstractLifeCycle; import org.eclipse.jetty.util.log.Log; import org.eclipse.jetty.util.log.Logger; @@ -228,12 +226,15 @@ public class ResourceCollection extends Resource Resource r = _resources[i].addPath(path); if (r.exists() && r.isDirectory()) { + if (resources==null) + resources = new ArrayList(); + if (resource!=null) { - resources = new ArrayList(); resources.add(resource); resource=null; } + resources.add(r); } } @@ -443,13 +444,13 @@ public class ResourceCollection extends Resource /* ------------------------------------------------------------ */ @Override - public void release() + public void close() { if(_resources==null) throw new IllegalStateException("*resources* not set."); for(Resource r : _resources) - r.release(); + r.close(); } /* ------------------------------------------------------------ */ diff --git a/jetty-util/src/main/java/org/eclipse/jetty/util/resource/URLResource.java b/jetty-util/src/main/java/org/eclipse/jetty/util/resource/URLResource.java index 2431b880088..4bb59a4e830 100644 --- a/jetty-util/src/main/java/org/eclipse/jetty/util/resource/URLResource.java +++ b/jetty-util/src/main/java/org/eclipse/jetty/util/resource/URLResource.java @@ -81,7 +81,7 @@ public class URLResource extends Resource /** Release any resources held by the resource. */ @Override - public synchronized void release() + public synchronized void close() { if (_in!=null) { diff --git a/jetty-util/src/test/java/org/eclipse/jetty/util/resource/ResourceAliasTest.java b/jetty-util/src/test/java/org/eclipse/jetty/util/resource/ResourceAliasTest.java new file mode 100644 index 00000000000..546d80a5c3a --- /dev/null +++ b/jetty-util/src/test/java/org/eclipse/jetty/util/resource/ResourceAliasTest.java @@ -0,0 +1,118 @@ +// +// ======================================================================== +// Copyright (c) 1995-2013 Mort Bay Consulting Pty. Ltd. +// ------------------------------------------------------------------------ +// All rights reserved. This program and the accompanying materials +// are made available under the terms of the Eclipse Public License v1.0 +// and Apache License v2.0 which accompanies this distribution. +// +// The Eclipse Public License is available at +// http://www.eclipse.org/legal/epl-v10.html +// +// The Apache License v2.0 is available at +// http://www.opensource.org/licenses/apache2.0.php +// +// You may elect to redistribute this code under either of these licenses. +// ======================================================================== +// + +package org.eclipse.jetty.util.resource; + +import static junit.framework.Assert.assertFalse; +import static junit.framework.Assert.assertNotNull; +import static junit.framework.Assert.assertNull; +import static junit.framework.Assert.assertTrue; + +import java.io.File; +import java.net.MalformedURLException; +import java.net.URISyntaxException; + +import junit.framework.Assert; + +import org.eclipse.jetty.toolchain.test.FS; +import org.eclipse.jetty.toolchain.test.MavenTestingUtils; +import org.eclipse.jetty.toolchain.test.TestingDir; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + + +public class ResourceAliasTest +{ + static File __dir; + + @BeforeClass + public static void beforeClass() + { + __dir=MavenTestingUtils.getTargetTestingDir("RAT"); + } + + @Before + public void before() + { + FS.ensureDirExists(__dir); + FS.ensureEmpty(__dir); + } + + + /* ------------------------------------------------------------ */ + @Test + public void testNullCharEndingFilename() throws Exception + { + File file=new File(__dir,"test.txt"); + assertFalse(file.exists()); + file.createNewFile(); + assertTrue(file.exists()); + + File file0=new File(__dir,"test.txt\0"); + if (!file0.exists()) + return; // this file system does not suffer this problem + + assertTrue(file0.exists()); // This is an alias! + + Resource dir = Resource.newResource(__dir); + + // Test not alias paths + Resource resource = Resource.newResource(file); + assertTrue(resource.exists()); + assertNull(resource.getAlias()); + resource = Resource.newResource(file.getAbsoluteFile()); + assertTrue(resource.exists()); + assertNull(resource.getAlias()); + resource = Resource.newResource(file.toURI()); + assertTrue(resource.exists()); + assertNull(resource.getAlias()); + resource = Resource.newResource(file.toURI().toString()); + assertTrue(resource.exists()); + assertNull(resource.getAlias()); + resource = dir.addPath("test.txt"); + assertTrue(resource.exists()); + assertNull(resource.getAlias()); + + + // Test alias paths + resource = Resource.newResource(file0); + assertTrue(resource.exists()); + assertNotNull(resource.getAlias()); + resource = Resource.newResource(file0.getAbsoluteFile()); + assertTrue(resource.exists()); + assertNotNull(resource.getAlias()); + resource = Resource.newResource(file0.toURI()); + assertTrue(resource.exists()); + assertNotNull(resource.getAlias()); + resource = Resource.newResource(file0.toURI().toString()); + assertTrue(resource.exists()); + assertNotNull(resource.getAlias()); + + try + { + resource = dir.addPath("test.txt\0"); + assertTrue(resource.exists()); + assertNotNull(resource.getAlias()); + } + catch(MalformedURLException e) + { + assertTrue(true); + } + } +} diff --git a/jetty-util/src/test/java/org/eclipse/jetty/util/resource/ResourceTest.java b/jetty-util/src/test/java/org/eclipse/jetty/util/resource/ResourceTest.java index 1c5889fd15b..c811daa7643 100644 --- a/jetty-util/src/test/java/org/eclipse/jetty/util/resource/ResourceTest.java +++ b/jetty-util/src/test/java/org/eclipse/jetty/util/resource/ResourceTest.java @@ -33,12 +33,8 @@ import java.util.Arrays; import java.util.HashSet; import java.util.Set; -import java.util.TimeZone; -import java.util.jar.JarFile; import java.util.zip.ZipFile; -import junit.framework.Assert; - import org.eclipse.jetty.toolchain.test.MavenTestingUtils; import org.eclipse.jetty.toolchain.test.OS; import org.eclipse.jetty.util.IO; @@ -329,13 +325,13 @@ public class ResourceTest { String s = "jar:"+__userURL+"TestData/test.zip!/subdir/numbers"; - - ZipFile zf = new ZipFile(MavenTestingUtils.getTestResourceFile("TestData/test.zip")); - - long last = zf.getEntry("subdir/numbers").getTime(); + try(ZipFile zf = new ZipFile(MavenTestingUtils.getTestResourceFile("TestData/test.zip"))) + { + long last = zf.getEntry("subdir/numbers").getTime(); - Resource r = Resource.newResource(s); - assertEquals(last,r.lastModified()); + Resource r = Resource.newResource(s); + assertEquals(last,r.lastModified()); + } } /* ------------------------------------------------------------ */ @@ -367,6 +363,7 @@ public class ResourceTest assertEquals(1, dest.getParentFile().listFiles().length); FilenameFilter dotdotFilenameFilter = new FilenameFilter() { + @Override public boolean accept(File directory, String name) { return name.equals("dotdot.txt"); @@ -376,6 +373,7 @@ public class ResourceTest assertEquals(0, dest.getParentFile().listFiles(dotdotFilenameFilter).length); FilenameFilter extractfileFilenameFilter = new FilenameFilter() { + @Override public boolean accept(File directory, String name) { return name.equals("extract-filenotdir"); @@ -385,6 +383,7 @@ public class ResourceTest assertEquals(0, dest.getParentFile().listFiles(extractfileFilenameFilter).length); FilenameFilter currentDirectoryFilenameFilter = new FilenameFilter() { + @Override public boolean accept(File directory, String name) { return name.equals("current.txt"); @@ -405,15 +404,14 @@ public class ResourceTest { final String classPathName="Resource.class"; - Resource resource=Resource.newClassPathResource(classPathName); + try(Resource resource=Resource.newClassPathResource(classPathName);) + { + // A class path cannot be a directory + assertFalse("Class path cannot be a directory.",resource.isDirectory()); - assertTrue(resource!=null); - - // A class path cannot be a directory - assertFalse("Class path cannot be a directory.",resource.isDirectory()); - - // A class path must exist - assertTrue("Class path resource does not exist.",resource.exists()); + // A class path must exist + assertTrue("Class path resource does not exist.",resource.exists()); + } } /** @@ -426,8 +424,6 @@ public class ResourceTest Resource resource=Resource.newClassPathResource(classPathName); - assertTrue(resource!=null); - // A class path cannot be a directory assertFalse("Class path cannot be a directory.",resource.isDirectory()); @@ -445,9 +441,6 @@ public class ResourceTest Resource resource=Resource.newClassPathResource(classPathName); - - assertTrue(resource!=null); - // A class path must be a directory assertTrue("Class path must be a directory.",resource.isDirectory()); @@ -469,8 +462,6 @@ public class ResourceTest // Will locate a resource in the class path Resource resource=Resource.newClassPathResource(classPathName); - assertTrue(resource!=null); - // A class path cannot be a directory assertFalse("Class path must be a directory.",resource.isDirectory()); @@ -478,7 +469,6 @@ public class ResourceTest File file=resource.getFile(); - assertTrue("File returned from class path should not be null.",file!=null); assertEquals("File name from class path is not equal.",fileName,file.getName()); assertTrue("File returned from class path should be a file.",file.isFile()); diff --git a/jetty-webapp/src/main/java/org/eclipse/jetty/webapp/Descriptor.java b/jetty-webapp/src/main/java/org/eclipse/jetty/webapp/Descriptor.java index 038230b4e11..dfaa5f70531 100644 --- a/jetty-webapp/src/main/java/org/eclipse/jetty/webapp/Descriptor.java +++ b/jetty-webapp/src/main/java/org/eclipse/jetty/webapp/Descriptor.java @@ -66,7 +66,7 @@ public abstract class Descriptor } finally { - _xml.release(); + _xml.close(); } } } diff --git a/jetty-webapp/src/test/java/org/eclipse/jetty/webapp/OrderingTest.java b/jetty-webapp/src/test/java/org/eclipse/jetty/webapp/OrderingTest.java index 7c19ca0d23a..5e5eec3eca6 100644 --- a/jetty-webapp/src/test/java/org/eclipse/jetty/webapp/OrderingTest.java +++ b/jetty-webapp/src/test/java/org/eclipse/jetty/webapp/OrderingTest.java @@ -167,10 +167,10 @@ public class OrderingTest } /** - * @see org.eclipse.jetty.util.resource.Resource#release() + * @see org.eclipse.jetty.util.resource.Resource#close() */ @Override - public void release() + public void close() { } diff --git a/jetty-webapp/src/test/java/org/eclipse/jetty/webapp/WebAppClassLoaderTest.java b/jetty-webapp/src/test/java/org/eclipse/jetty/webapp/WebAppClassLoaderTest.java index f815f7c63e1..41c6413df09 100644 --- a/jetty-webapp/src/test/java/org/eclipse/jetty/webapp/WebAppClassLoaderTest.java +++ b/jetty-webapp/src/test/java/org/eclipse/jetty/webapp/WebAppClassLoaderTest.java @@ -60,7 +60,7 @@ public class WebAppClassLoaderTest assertTrue(cantLoadClass("org.eclipse.jetty.webapp.Configuration")); - Class clazzA = _loader.loadClass("org.acme.webapp.ClassInJarA"); + Class clazzA = _loader.loadClass("org.acme.webapp.ClassInJarA"); assertTrue(clazzA.getField("FROM_PARENT")!=null); } From 1bbab6d6f25b2085d9a767e7d08a0b4488e66257 Mon Sep 17 00:00:00 2001 From: Greg Wilkins Date: Mon, 3 Jun 2013 18:54:48 +1000 Subject: [PATCH 5/5] 408945 XML Args ignored without DTD --- .../eclipse/jetty/xml/XmlConfiguration.java | 4 +-- .../jetty/xml/XmlConfigurationTest.java | 29 +++++++++++++++++++ 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/jetty-xml/src/main/java/org/eclipse/jetty/xml/XmlConfiguration.java b/jetty-xml/src/main/java/org/eclipse/jetty/xml/XmlConfiguration.java index 467f5d77bd8..fc798536913 100644 --- a/jetty-xml/src/main/java/org/eclipse/jetty/xml/XmlConfiguration.java +++ b/jetty-xml/src/main/java/org/eclipse/jetty/xml/XmlConfiguration.java @@ -741,7 +741,6 @@ public class XmlConfiguration private Object newObj(Object obj, XmlParser.Node node) throws Exception { Class oClass = nodeClass(node); - int size = 0; int argIndex = node.size(); for (int i = 0; i < node.size(); i++) { @@ -753,13 +752,12 @@ public class XmlConfiguration argIndex = i; break; } - size++; } Map namedArgMap = new HashMap<>(); List arguments = new LinkedList<>(); - for (int i = 0; i < size; i++) + for (int i = 0; i < node.size(); i++) { Object o = node.get(i); diff --git a/jetty-xml/src/test/java/org/eclipse/jetty/xml/XmlConfigurationTest.java b/jetty-xml/src/test/java/org/eclipse/jetty/xml/XmlConfigurationTest.java index 49d68c74b87..22267cfdd4d 100644 --- a/jetty-xml/src/test/java/org/eclipse/jetty/xml/XmlConfigurationTest.java +++ b/jetty-xml/src/test/java/org/eclipse/jetty/xml/XmlConfigurationTest.java @@ -25,6 +25,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; +import java.io.ByteArrayInputStream; import java.net.URL; import java.util.HashMap; import java.util.Map; @@ -541,6 +542,34 @@ public class XmlConfigurationTest Assert.assertEquals("nested second parameter not wired correctly","arg2", atc.getNested().getSecond()); Assert.assertEquals("nested third parameter not wired correctly","arg3", atc.getNested().getThird()); } + + @Test + public void testArgumentsGetIgnoredMissingDTD() throws Exception + { + XmlConfiguration xmlConfiguration = new XmlConfiguration(new ByteArrayInputStream(("" + + "" + + " arg1 " + + " arg2 " + + " arg3 " + + " " + + " \n" + + " arg1\n" + + " arg2\n" + + " arg3\n" + + " " + + " " + + "").getBytes("ISO-8859-1"))); +// XmlConfiguration xmlConfiguration = new XmlConfiguration(url); + + AnnotatedTestConfiguration atc = (AnnotatedTestConfiguration)xmlConfiguration.configure(); + + Assert.assertEquals("first parameter not wired correctly","arg1", atc.getFirst()); + Assert.assertEquals("second parameter not wired correctly","arg2", atc.getSecond()); + Assert.assertEquals("third parameter not wired correctly","arg3", atc.getThird()); + Assert.assertEquals("nested first parameter not wired correctly","arg1", atc.getNested().getFirst()); + Assert.assertEquals("nested second parameter not wired correctly","arg2", atc.getNested().getSecond()); + Assert.assertEquals("nested third parameter not wired correctly","arg3", atc.getNested().getThird()); + } @Test public void testNestedConstructorNamedInjectionUnorderedMixed() throws Exception