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
This commit is contained in:
Greg Wilkins 2011-05-10 04:57:06 +00:00
parent dedc2b648d
commit 866255f474
3 changed files with 67 additions and 1 deletions

View File

@ -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

View File

@ -84,7 +84,28 @@ public abstract class AbstractHandlerContainer extends AbstractHandler implement
return list;
}
/* ------------------------------------------------------------ */
public static <T extends HandlerContainer> T findContainerOf(HandlerContainer root,Class<T>type, 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
{

View File

@ -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;
}
}
}