private void undeploy(WebApp webApp) {

    String contextName = webApp.getContextName();
    LinkedList<WebApp> queue = contexts.get(contextName);
    if (queue != null) {

      // Are we the published web app??
      if (queue.get(0) == webApp) {

        webApp.setDeploymentState(WebApp.UNDEPLOYED_STATE);
        eventDispatcher.webEvent(
            new WebEvent(
                WebEvent.UNDEPLOYING,
                "/" + contextName,
                webApp.getBundle(),
                bundleContext.getBundle()));
        m_publisher.unpublish(webApp);
        eventDispatcher.webEvent(
            new WebEvent(
                WebEvent.UNDEPLOYED,
                "/" + contextName,
                webApp.getBundle(),
                bundleContext.getBundle()));
        queue.removeFirst();

        // Below checks if another webapp is waiting for the context, if so the webapp is published.
        LOG.debug("Check for a waiting webapp.");
        if (!queue.isEmpty()) {
          LOG.debug("Found another bundle waiting for the context");
          WebApp next = queue.getFirst();

          eventDispatcher.webEvent(
              new WebEvent(
                  WebEvent.DEPLOYING,
                  "/" + contextName,
                  next.getBundle(),
                  bundleContext.getBundle()));

          // let the publisher set the deployment state and send web event
          m_publisher.publish(next, eventDispatcher, bundleContext);
        } else {
          contexts.remove(contextName);
        }

      } else if (queue.remove(webApp)) {
        webApp.setDeploymentState(WebApp.UNDEPLOYED_STATE);
        eventDispatcher.webEvent(
            new WebEvent(
                WebEvent.UNDEPLOYED,
                "/" + contextName,
                webApp.getBundle(),
                bundleContext.getBundle()));
      } else {
        LOG.debug("Web application was not in the deployment queue");
      }

    } else {
      LOG.debug(String.format("No web application published under context: %s", contextName));
    }
  }
  private void deploy(WebApp webApp) {

    Bundle bundle = webApp.getBundle();
    String contextName = webApp.getContextName();

    eventDispatcher.webEvent(
        new WebEvent(WebEvent.DEPLOYING, "/" + contextName, bundle, bundleContext.getBundle()));
    if (!contexts.containsKey(contextName)) {
      LinkedList<WebApp> queue = new LinkedList<WebApp>();
      contexts.put(contextName, queue);
      queue.add(webApp);

      // let the publisher set the deployment state and send web event
      m_publisher.publish(webApp, eventDispatcher, bundleContext);
    } else {
      LinkedList<WebApp> queue = contexts.get(contextName);
      queue.add(webApp);

      Collection<Long> duplicateIds = new LinkedList<Long>();
      for (WebApp duplicateWebApp : queue) {
        duplicateIds.add(duplicateWebApp.getBundle().getBundleId());
      }

      webApp.setDeploymentState(WebApp.WAITING_STATE);
      eventDispatcher.webEvent(
          new WebEvent(
              WebEvent.WAITING,
              "/" + contextName,
              bundle,
              bundleContext.getBundle(),
              duplicateIds));
    }
  }