/** {@inheritDoc} */
  @Nullable
  @Override
  public GridDeployment explicitDeploy(Class<?> cls, ClassLoader clsLdr) throws GridException {
    try {
      // Make sure not to deploy peer loaded tasks with non-local class loader,
      // if local one exists.
      if (clsLdr.getClass().equals(GridDeploymentClassLoader.class)) clsLdr = clsLdr.getParent();

      GridDeployment dep;

      synchronized (mux) {
        boolean deployed = spi.register(clsLdr, cls);

        if (deployed) {
          dep = getDeployment(cls.getName());

          if (dep == null) {
            GridDeploymentResource rsrc = spi.findResource(cls.getName());

            if (rsrc != null && rsrc.getClassLoader() == clsLdr) {
              dep =
                  deploy(
                      ctx.config().getDeploymentMode(),
                      rsrc.getClassLoader(),
                      rsrc.getResourceClass(),
                      rsrc.getName());
            }
          }

          if (dep != null) {
            recordDeploy(cls, cls.getName(), true);
          }
        } else {
          dep = getDeployment(cls.getName());
        }
      }

      return dep;
    } catch (GridSpiException e) {
      recordDeployFailed(cls, clsLdr, true);

      // Avoid double wrapping.
      if (e.getCause() instanceof GridException) {
        throw (GridException) e.getCause();
      }

      throw new GridException("Failed to deploy class: " + cls.getName(), e);
    }
  }
  /**
   * Gets alias for a class.
   *
   * @param dep Deployment.
   * @param cls Class.
   * @return Alias for a class.
   */
  private String getAlias(GridDeployment dep, Class<?> cls) {
    String alias = cls.getName();

    if (isTask(cls)) {
      GridTaskName ann = dep.annotation(cls, GridTaskName.class);

      if (ann != null) {
        alias = ann.value();
      }
    }

    return alias;
  }
    /** {@inheritDoc} */
    @Override
    public void onDeployed(Class<?> cls) {
      assert !Thread.holdsLock(mux);

      boolean isTask = isTask(cls);

      String msg =
          (isTask ? "Task" : "Class") + " was deployed in SHARED or CONTINUOUS mode: " + cls;

      int type = isTask ? EVT_TASK_DEPLOYED : EVT_CLASS_DEPLOYED;

      if (ctx.event().isRecordable(type)) {
        GridDeploymentEvent evt = new GridDeploymentEvent();

        evt.nodeId(ctx.localNodeId());
        evt.message(msg);
        evt.type(type);
        evt.alias(cls.getName());

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

      if (log.isInfoEnabled()) log.info(msg);
    }
  /**
   * @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;
  }