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  import org.eclipse.jetty.util.log.Logger;
27  
28  public class StandardUndeployer implements AppLifeCycle.Binding
29  {
30      private static final Logger LOG = Log.getLogger(StandardUndeployer.class);
31  
32      public String[] getBindingTargets()
33      {
34          return new String[]
35          { "undeploying" };
36      }
37  
38      public void processBinding(Node node, App app) throws Exception
39      {
40          ContextHandler handler = app.getContextHandler();
41          ContextHandlerCollection chcoll = app.getDeploymentManager().getContexts();
42  
43          recursiveRemoveContext(chcoll,handler);
44      }
45  
46      private void recursiveRemoveContext(HandlerCollection coll, ContextHandler context)
47      {
48          Handler children[] = coll.getHandlers();
49          int originalCount = children.length;
50  
51          for (int i = 0, n = children.length; i < n; i++)
52          {
53              Handler child = children[i];
54              LOG.debug("Child handler {}",child);
55              if (child.equals(context))
56              {
57                  LOG.debug("Removing handler {}",child);
58                  coll.removeHandler(child);
59                  child.destroy();
60                  if (LOG.isDebugEnabled())
61                      LOG.debug("After removal: {} (originally {})",coll.getHandlers().length,originalCount);
62              }
63              else if (child instanceof HandlerCollection)
64              {
65                  recursiveRemoveContext((HandlerCollection)child,context);
66              }
67          }
68      }
69  }