View Javadoc

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