/**
   * Records undeploy event.
   *
   * @param dep Undeployed class loader.
   * @param recordEvt Flag indicating whether to record events.
   */
  private void recordUndeploy(GridDeployment dep, boolean recordEvt) {
    assert dep.isUndeployed();

    if (ctx.event().isRecordable(EVT_TASK_UNDEPLOYED)
        || ctx.event().isRecordable(EVT_CLASS_UNDEPLOYED)) {
      for (Class<?> cls : dep.deployedClasses()) {
        boolean isTask = isTask(cls);

        String msg =
            isTask ? "Task locally undeployed: " + cls : "Class locally undeployed: " + cls;

        if (ctx.event().isRecordable(isTask ? EVT_TASK_UNDEPLOYED : EVT_CLASS_UNDEPLOYED)) {
          if (recordEvt) {
            GridDeploymentEvent evt = new GridDeploymentEvent();

            evt.message(msg);
            evt.nodeId(ctx.localNodeId());
            evt.type(isTask ? EVT_TASK_UNDEPLOYED : EVT_CLASS_UNDEPLOYED);
            evt.alias(getAlias(dep, cls));

            ctx.event().record(evt);
          }
        }

        if (log.isInfoEnabled()) {
          log.info(msg);
        }
      }
    }
  }
  /**
   * @param alias Class alias.
   * @return Deployed class.
   */
  @Nullable
  private GridDeployment getDeployment(String alias) {
    assert Thread.holdsLock(mux);

    LinkedList<GridDeployment> deps = cache.get(alias);

    if (deps != null) {
      assert !deps.isEmpty();

      GridDeployment dep = deps.getFirst();

      if (!dep.isUndeployed()) {
        return dep;
      }
    }

    return null;
  }
  /**
   * @param depMode Deployment mode.
   * @param ldr Class loader to deploy.
   * @param cls Class.
   * @param alias Class alias.
   * @return Deployment.
   */
  @SuppressWarnings({"ConstantConditions"})
  private GridDeployment deploy(
      GridDeploymentMode depMode, ClassLoader ldr, Class<?> cls, String alias) {
    assert Thread.holdsLock(mux);

    LinkedList<GridDeployment> cachedDeps = null;

    GridDeployment dep = null;

    // Find existing class loader info.
    for (LinkedList<GridDeployment> deps : cache.values()) {
      for (GridDeployment d : deps) {
        if (d.classLoader() == ldr) {
          // Cache class and alias.
          d.addDeployedClass(cls, alias);

          cachedDeps = deps;

          dep = d;

          break;
        }
      }

      if (cachedDeps != null) {
        break;
      }
    }

    if (cachedDeps != null) {
      assert dep != null;

      cache.put(alias, cachedDeps);

      if (!cls.getName().equals(alias)) {
        // Cache by class name as well.
        cache.put(cls.getName(), cachedDeps);
      }

      return dep;
    }

    GridUuid ldrId = GridUuid.randomUuid();

    long seqNum = seq.incrementAndGet();

    String userVer = getUserVersion(ldr);

    dep = new GridDeployment(depMode, ldr, ldrId, seqNum, userVer, cls.getName(), true);

    dep.addDeployedClass(cls, alias);

    LinkedList<GridDeployment> deps =
        F.addIfAbsent(cache, alias, F.<GridDeployment>newLinkedList());

    if (!deps.isEmpty()) {
      for (GridDeployment d : deps) {
        if (!d.isUndeployed()) {
          U.error(
              log,
              "Found more than one active deployment for the same resource "
                  + "[cls="
                  + cls
                  + ", depMode="
                  + depMode
                  + ", dep="
                  + d
                  + ']');

          return null;
        }
      }
    }

    // Add at the beginning of the list for future fast access.
    deps.addFirst(dep);

    if (!cls.getName().equals(alias)) {
      // Cache by class name as well.
      cache.put(cls.getName(), deps);
    }

    if (log.isDebugEnabled()) {
      log.debug("Created new deployment: " + dep);
    }

    return dep;
  }