From d460786b94f06bca5c363b52ed9cf080db30a4aa Mon Sep 17 00:00:00 2001 From: Joakim Erdfelt Date: Mon, 27 Apr 2020 08:01:23 -0500 Subject: [PATCH] Issue #4814 - Exposing AttributeMap.getAttributeNameSet() on Attributes. The underlying AttributesMap already has a .getAttributeNameSet() method, expose it on the Attributes interface. Signed-off-by: Joakim Erdfelt --- .../eclipse/jetty/server/SecureRequestCustomizer.java | 6 +----- .../src/main/java/org/eclipse/jetty/server/Server.java | 7 +++++++ .../org/eclipse/jetty/server/handler/ContextHandler.java | 6 ++++++ .../jetty/server/handler/ManagedAttributeListener.java | 9 ++------- .../jetty/server/handler/jmx/ContextHandlerMBean.java | 5 +---- .../src/main/java/org/eclipse/jetty/util/Attributes.java | 9 +++++++++ .../main/java/org/eclipse/jetty/util/AttributesMap.java | 1 + .../jetty/util/component/AttributeContainerMap.java | 7 +++++++ 8 files changed, 34 insertions(+), 16 deletions(-) diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/SecureRequestCustomizer.java b/jetty-server/src/main/java/org/eclipse/jetty/server/SecureRequestCustomizer.java index a3e703f3f9a5..8ab0c107016f 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/SecureRequestCustomizer.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/SecureRequestCustomizer.java @@ -365,11 +365,7 @@ public Object getAttribute(String name) @Override public Enumeration getAttributeNames() { - Set names = new HashSet<>(); - for (Enumeration e = _attributes.getAttributeNames(); e.hasMoreElements(); ) - { - names.add(e.nextElement()); - } + Set names = new HashSet<>(_attributes.getAttributeNameSet()); names.add(JAVAX_SERVLET_REQUEST_X_509_CERTIFICATE); names.add(JAVAX_SERVLET_REQUEST_CIPHER_SUITE); names.add(JAVAX_SERVLET_REQUEST_KEY_SIZE); diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/Server.java b/jetty-server/src/main/java/org/eclipse/jetty/server/Server.java index d23416044c57..af8bcf1db36e 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/Server.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/Server.java @@ -26,6 +26,7 @@ import java.util.Arrays; import java.util.Enumeration; import java.util.List; +import java.util.Set; import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.Future; import javax.servlet.ServletContext; @@ -594,6 +595,12 @@ public Enumeration getAttributeNames() return _attributes.getAttributeNames(); } + @Override + public Set getAttributeNameSet() + { + return _attributes.getAttributeNameSet(); + } + /* * @see org.eclipse.util.AttributesMap#removeAttribute(java.lang.String) */ diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ContextHandler.java b/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ContextHandler.java index 725aeb5f40d3..d89a3b861f2f 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ContextHandler.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ContextHandler.java @@ -516,6 +516,12 @@ public Enumeration getAttributeNames() return AttributesMap.getAttributeNamesCopy(_attributes); } + @Override + public Set getAttributeNameSet() + { + return _attributes.getAttributeNameSet(); + } + /** * @return Returns the attributes. */ diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ManagedAttributeListener.java b/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ManagedAttributeListener.java index fdca2fdab8e9..225e0f1eaf5c 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ManagedAttributeListener.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ManagedAttributeListener.java @@ -18,7 +18,6 @@ package org.eclipse.jetty.server.handler; -import java.util.Enumeration; import java.util.HashSet; import java.util.Set; import javax.servlet.ServletContextAttributeEvent; @@ -77,10 +76,8 @@ public void attributeAdded(ServletContextAttributeEvent event) public void contextInitialized(ServletContextEvent event) { // Update existing attributes - Enumeration e = event.getServletContext().getAttributeNames(); - while (e.hasMoreElements()) + for (String name : _context.getServletContext().getAttributeNameSet()) { - String name = e.nextElement(); if (_managedAttributes.contains(name)) updateBean(name, null, event.getServletContext().getAttribute(name)); } @@ -89,10 +86,8 @@ public void contextInitialized(ServletContextEvent event) @Override public void contextDestroyed(ServletContextEvent event) { - Enumeration e = _context.getServletContext().getAttributeNames(); - while (e.hasMoreElements()) + for (String name : _context.getServletContext().getAttributeNameSet()) { - String name = e.nextElement(); if (_managedAttributes.contains(name)) updateBean(name, event.getServletContext().getAttribute(name), null); } diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/handler/jmx/ContextHandlerMBean.java b/jetty-server/src/main/java/org/eclipse/jetty/server/handler/jmx/ContextHandlerMBean.java index 1c73c8f11f97..b102b8769d0e 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/handler/jmx/ContextHandlerMBean.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/handler/jmx/ContextHandlerMBean.java @@ -18,7 +18,6 @@ package org.eclipse.jetty.server.handler.jmx; -import java.util.Enumeration; import java.util.HashMap; import java.util.Map; @@ -42,10 +41,8 @@ public Map getContextAttributes() { Map map = new HashMap(); Attributes attrs = ((ContextHandler)_managed).getAttributes(); - Enumeration en = attrs.getAttributeNames(); - while (en.hasMoreElements()) + for (String name : attrs.getAttributeNameSet()) { - String name = en.nextElement(); Object value = attrs.getAttribute(name); map.put(name, value); } diff --git a/jetty-util/src/main/java/org/eclipse/jetty/util/Attributes.java b/jetty-util/src/main/java/org/eclipse/jetty/util/Attributes.java index 3fa1f7c8fca0..c4e3294da753 100644 --- a/jetty-util/src/main/java/org/eclipse/jetty/util/Attributes.java +++ b/jetty-util/src/main/java/org/eclipse/jetty/util/Attributes.java @@ -19,6 +19,7 @@ package org.eclipse.jetty.util; import java.util.Enumeration; +import java.util.Set; /** * Attributes. @@ -32,6 +33,8 @@ public interface Attributes Object getAttribute(String name); + Set getAttributeNameSet(); + Enumeration getAttributeNames(); void clearAttributes(); @@ -83,6 +86,12 @@ public Enumeration getAttributeNames() return _attributes.getAttributeNames(); } + @Override + public Set getAttributeNameSet() + { + return _attributes.getAttributeNameSet(); + } + @Override public void clearAttributes() { diff --git a/jetty-util/src/main/java/org/eclipse/jetty/util/AttributesMap.java b/jetty-util/src/main/java/org/eclipse/jetty/util/AttributesMap.java index 5ea9ac45823a..8c41e073ce16 100644 --- a/jetty-util/src/main/java/org/eclipse/jetty/util/AttributesMap.java +++ b/jetty-util/src/main/java/org/eclipse/jetty/util/AttributesMap.java @@ -94,6 +94,7 @@ public Enumeration getAttributeNames() return Collections.enumeration(getAttributeNameSet()); } + @Override public Set getAttributeNameSet() { return keySet(); diff --git a/jetty-util/src/main/java/org/eclipse/jetty/util/component/AttributeContainerMap.java b/jetty-util/src/main/java/org/eclipse/jetty/util/component/AttributeContainerMap.java index abbd1543b08a..bfde2301bfd5 100644 --- a/jetty-util/src/main/java/org/eclipse/jetty/util/component/AttributeContainerMap.java +++ b/jetty-util/src/main/java/org/eclipse/jetty/util/component/AttributeContainerMap.java @@ -23,6 +23,7 @@ import java.util.Enumeration; import java.util.HashMap; import java.util.Map; +import java.util.Set; import org.eclipse.jetty.util.Attributes; @@ -60,6 +61,12 @@ public synchronized Enumeration getAttributeNames() return Collections.enumeration(_map.keySet()); } + @Override + public Set getAttributeNameSet() + { + return _map.keySet(); + } + @Override public synchronized void clearAttributes() {