/** {@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); } }
private void restoreDefinition(@Nonnull Class<?> redefinedClass) { if (redefinedClassesWithNativeMethods.contains(redefinedClass.getName())) { reregisterNativeMethodsForRestoredClass(redefinedClass); } removeMockedClass(redefinedClass); }
public boolean isMockedClass(@Nonnull String targetClassName) { int n = mockedClasses.size(); for (int i = 0; i < n; i++) { Class<?> mockedClass = mockedClasses.get(i); if (targetClassName.equals(mockedClass.getName())) { return true; } } return false; }
/** * 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; }
/** * @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; }