View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2016 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      @Override
36      public String[] getBindingTargets()
37      {
38          return new String[]
39          { "undeploying" };
40      }
41  
42      @Override
43      public void processBinding(Node node, App app) throws Exception
44      {
45          ContextHandler handler = app.getContextHandler();
46          ContextHandlerCollection chcoll = app.getDeploymentManager().getContexts();
47  
48          recursiveRemoveContext(chcoll,handler);
49      }
50  
51      private void recursiveRemoveContext(HandlerCollection coll, ContextHandler context)
52      {
53          Handler children[] = coll.getHandlers();
54          int originalCount = children.length;
55  
56          for (int i = 0, n = children.length; i < n; i++)
57          {
58              Handler child = children[i];
59              LOG.debug("Child handler {}",child);
60              if (child.equals(context))
61              {
62                  LOG.debug("Removing handler {}",child);
63                  coll.removeHandler(child);
64                  child.destroy();
65                  if (LOG.isDebugEnabled())
66                      LOG.debug("After removal: {} (originally {})",coll.getHandlers().length,originalCount);
67              }
68              else if (child instanceof HandlerCollection)
69              {
70                  recursiveRemoveContext((HandlerCollection)child,context);
71              }
72          }
73      }
74  }