View Javadoc

1   // ========================================================================
2   // Copyright (c) Webtide LLC
3   // ------------------------------------------------------------------------
4   // All rights reserved. This program and the accompanying materials
5   // are made available under the terms of the Eclipse Public License v1.0
6   // and Apache License v2.0 which accompanies this distribution.
7   //
8   // The Eclipse Public License is available at 
9   // http://www.eclipse.org/legal/epl-v10.html
10  //
11  // The Apache License v2.0 is available at
12  // http://www.apache.org/licenses/LICENSE-2.0.txt
13  //
14  // You may elect to redistribute this code under either of these licenses. 
15  // ========================================================================
16  package org.eclipse.jetty.deploy.bindings;
17  
18  import org.eclipse.jetty.deploy.App;
19  import org.eclipse.jetty.deploy.AppLifeCycle;
20  import org.eclipse.jetty.deploy.graph.Node;
21  import org.eclipse.jetty.server.Handler;
22  import org.eclipse.jetty.server.handler.ContextHandler;
23  import org.eclipse.jetty.server.handler.ContextHandlerCollection;
24  import org.eclipse.jetty.server.handler.HandlerCollection;
25  import org.eclipse.jetty.util.log.Log;
26  
27  public class StandardUndeployer implements AppLifeCycle.Binding
28  {
29      public String[] getBindingTargets()
30      {
31          return new String[]
32          { "undeploying" };
33      }
34  
35      public void processBinding(Node node, App app) throws Exception
36      {
37          ContextHandler handler = app.getContextHandler();
38          ContextHandlerCollection chcoll = app.getDeploymentManager().getContexts();
39  
40          recursiveRemoveContext(chcoll,handler);
41      }
42  
43      private void recursiveRemoveContext(HandlerCollection coll, ContextHandler context)
44      {
45          Handler children[] = coll.getHandlers();
46          int originalCount = children.length;
47  
48          for (int i = 0, n = children.length; i < n; i++)
49          {
50              Handler child = children[i];
51              Log.debug("Child handler: " + child);
52              if (child.equals(context))
53              {
54                  Log.debug("Removing handler: " + child);
55                  coll.removeHandler(child);
56                  ((ContextHandler)child).destroy();
57                  Log.debug(String.format("After removal: %d (originally %d)",coll.getHandlers().length,originalCount));
58              }
59              else if (child instanceof HandlerCollection)
60              {
61                  recursiveRemoveContext((HandlerCollection)child,context);
62              }
63          }
64      }
65  }