From 866255f47418c6883502f0c1e5bd725ce7dd6a79 Mon Sep 17 00:00:00 2001 From: Greg Wilkins Date: Tue, 10 May 2011 04:57:06 +0000 Subject: [PATCH] 332907 (work in progress) Added AbstractHandlerContainer.findContainerOf git-svn-id: svn+ssh://dev.eclipse.org/svnroot/rt/org.eclipse.jetty/jetty/trunk@3105 7e9141cc-0065-0410-87d8-b60c137991c4 --- VERSION.txt | 1 + .../handler/AbstractHandlerContainer.java | 23 +++++++++- .../handler/ContextHandlerCollectionTest.java | 44 +++++++++++++++++++ 3 files changed, 67 insertions(+), 1 deletion(-) diff --git a/VERSION.txt b/VERSION.txt index 6b92548f6bf..5fedc2fa2ca 100644 --- a/VERSION.txt +++ b/VERSION.txt @@ -1,4 +1,5 @@ jetty-7.4.1-SNAPSHOT + + 332907 (work in progress) Added AbstractHandlerContainer.findContainerOf + 340040 Support for a total timeout + 343083 Set nested dispatch type and connection + 343172 Check package implementor for version diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/handler/AbstractHandlerContainer.java b/jetty-server/src/main/java/org/eclipse/jetty/server/handler/AbstractHandlerContainer.java index b07bf8243bf..6f20cea663c 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/handler/AbstractHandlerContainer.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/handler/AbstractHandlerContainer.java @@ -84,7 +84,28 @@ public abstract class AbstractHandlerContainer extends AbstractHandler implement return list; } - + + /* ------------------------------------------------------------ */ + public static T findContainerOf(HandlerContainer root,Classtype, Handler handler) + { + Handler[] branches=root.getChildHandlersByClass(type); + if (branches!=null) + { + for (Handler h:branches) + { + T container = (T)h; + Handler[] candidates = container.getChildHandlersByClass(handler.getClass()); + if (candidates!=null) + { + for (Handler c:candidates) + if (c==handler) + return container; + } + } + } + return null; + } + /* ------------------------------------------------------------ */ public void dump(Appendable out,String indent) throws IOException { diff --git a/jetty-server/src/test/java/org/eclipse/jetty/server/handler/ContextHandlerCollectionTest.java b/jetty-server/src/test/java/org/eclipse/jetty/server/handler/ContextHandlerCollectionTest.java index 688e4151751..e61ab3eca39 100644 --- a/jetty-server/src/test/java/org/eclipse/jetty/server/handler/ContextHandlerCollectionTest.java +++ b/jetty-server/src/test/java/org/eclipse/jetty/server/handler/ContextHandlerCollectionTest.java @@ -15,6 +15,7 @@ package org.eclipse.jetty.server.handler; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; +import static org.junit.Assert.assertEquals; import java.io.IOException; @@ -153,6 +154,46 @@ public class ContextHandlerCollectionTest } + + @Test + public void testFindContainer() throws Exception + { + Server server = new Server(); + + ContextHandler contextA = new ContextHandler("/a"); + IsHandledHandler handlerA = new IsHandledHandler(); + contextA.setHandler(handlerA); + + ContextHandler contextB = new ContextHandler("/b"); + IsHandledHandler handlerB = new IsHandledHandler(); + HandlerWrapper wrapperB = new HandlerWrapper(); + wrapperB.setHandler(handlerB); + contextB.setHandler(wrapperB); + + ContextHandler contextC = new ContextHandler("/c"); + IsHandledHandler handlerC = new IsHandledHandler(); + contextC.setHandler(handlerC); + + ContextHandlerCollection collection = new ContextHandlerCollection(); + + collection.addHandler(contextA); + collection.addHandler(contextB); + collection.addHandler(contextC); + + HandlerWrapper wrapper = new HandlerWrapper(); + wrapper.setHandler(collection); + server.setHandler(wrapper); + + assertEquals(wrapper,AbstractHandlerContainer.findContainerOf(server,HandlerWrapper.class,handlerA)); + assertEquals(contextA,AbstractHandlerContainer.findContainerOf(server,ContextHandler.class,handlerA)); + assertEquals(contextB,AbstractHandlerContainer.findContainerOf(server,ContextHandler.class,handlerB)); + assertEquals(wrapper,AbstractHandlerContainer.findContainerOf(server,HandlerWrapper.class,handlerB)); + assertEquals(contextB,AbstractHandlerContainer.findContainerOf(collection,HandlerWrapper.class,handlerB)); + assertEquals(wrapperB,AbstractHandlerContainer.findContainerOf(contextB,HandlerWrapper.class,handlerB)); + + } + + private static final class IsHandledHandler extends AbstractHandler { private boolean handled; @@ -173,4 +214,7 @@ public class ContextHandlerCollectionTest handled = false; } } + + + }