Search in sources :

Example 6 with GridPeerDeployAware

use of org.apache.ignite.internal.util.lang.GridPeerDeployAware in project ignite by apache.

the class IgniteUtils method hasCommonClassLoader.

/**
 * Check if all elements from the collection could be loaded with the same classloader as the given object.
 *
 * @param obj base object.
 * @param c collection to check elements from.
 * @return {@code true} if all elements could be loaded with {@code obj}'s classloader, {@code false} otherwise
 */
private static boolean hasCommonClassLoader(Object obj, Iterable<?> c) {
    assert obj != null;
    assert c != null;
    ClassLoader ldr = obj instanceof GridPeerDeployAware ? ((GridPeerDeployAware) obj).classLoader() : detectClassLoader(obj.getClass());
    boolean found = true;
    for (Object obj2 : c) {
        if (obj2 == null || obj2 == obj)
            continue;
        // Obj2 class name.
        String clsName = obj2 instanceof GridPeerDeployAware ? ((GridPeerDeployAware) obj2).deployClass().getName() : obj2.getClass().getName();
        if (!isLoadableBy(clsName, ldr)) {
            found = false;
            break;
        }
    }
    return found;
}
Also used : GridPeerDeployAware(org.apache.ignite.internal.util.lang.GridPeerDeployAware) URLClassLoader(java.net.URLClassLoader)

Example 7 with GridPeerDeployAware

use of org.apache.ignite.internal.util.lang.GridPeerDeployAware in project ignite by apache.

the class IgniteUtils method nestedPeerDeployAware.

/**
 * Gets peer deploy class if there is any {@link GridPeerDeployAware} within reach.
 *
 * @param obj Object to check.
 * @param top Indicates whether object is top level or a nested field.
 * @param processed Set of processed objects to avoid infinite recursion.
 * @return Peer deploy class, or {@code null} if one could not be found.
 */
@Nullable
private static GridPeerDeployAware nestedPeerDeployAware(Object obj, boolean top, Set<Object> processed) {
    // Avoid infinite recursion.
    if (!processed.add(obj))
        return null;
    if (obj instanceof GridPeerDeployAware) {
        GridPeerDeployAware p = (GridPeerDeployAware) obj;
        if (!top && p.deployClass() != null)
            return p;
        for (Class<?> cls = obj.getClass(); !cls.equals(Object.class); cls = cls.getSuperclass()) {
            // Cache by class name instead of class to avoid infinite growth of the
            // caching map in case of multiple redeployment of the same class.
            IgniteBiTuple<Class<?>, Collection<Field>> tup = p2pFields.get(cls.getName());
            boolean cached = tup != null && tup.get1().equals(cls);
            Iterable<Field> fields = cached ? tup.get2() : Arrays.asList(cls.getDeclaredFields());
            if (!cached) {
                tup = new IgniteBiTuple<>();
                tup.set1(cls);
            }
            for (Field f : fields) // Special handling for anonymous classes.
            if (cached || f.getName().startsWith("this$") || f.getName().startsWith("val$")) {
                if (!cached) {
                    f.setAccessible(true);
                    if (tup.get2() == null)
                        tup.set2(new LinkedList<Field>());
                    tup.get2().add(f);
                }
                try {
                    Object o = f.get(obj);
                    if (o != null) {
                        // Recursion.
                        p = nestedPeerDeployAware(o, false, processed);
                        if (p != null) {
                            if (!cached)
                                // Potentially replace identical value
                                // stored by another thread.
                                p2pFields.put(cls.getName(), tup);
                            return p;
                        }
                    }
                } catch (IllegalAccessException ignored) {
                    return null;
                }
            }
        }
    } else // Don't go into internal Ignite structures.
    if (isIgnite(obj.getClass()))
        return null;
    else if (obj instanceof Iterable)
        for (Object o : (Iterable<?>) obj) {
            // Recursion.
            GridPeerDeployAware p = nestedPeerDeployAware(o, false, processed);
            if (p != null)
                return p;
        }
    else if (obj.getClass().isArray()) {
        Class<?> type = obj.getClass().getComponentType();
        // We don't care about primitives or internal JDK types.
        if (!type.isPrimitive() && !isJdk(type)) {
            Object[] arr = (Object[]) obj;
            for (Object o : arr) {
                // Recursion.
                GridPeerDeployAware p = nestedPeerDeployAware(o, false, processed);
                if (p != null)
                    return p;
            }
        }
    }
    return null;
}
Also used : GridPeerDeployAware(org.apache.ignite.internal.util.lang.GridPeerDeployAware) Field(java.lang.reflect.Field) Collection(java.util.Collection) Nullable(org.jetbrains.annotations.Nullable)

Example 8 with GridPeerDeployAware

use of org.apache.ignite.internal.util.lang.GridPeerDeployAware in project ignite by apache.

the class IgniteUtilsSelfTest method testDetectPeerDeployAwareInfiniteRecursion.

/**
 * @throws Exception If test fails.
 */
@Test
public void testDetectPeerDeployAwareInfiniteRecursion() throws Exception {
    Ignite g = startGrid(1);
    try {
        final SelfReferencedJob job = new SelfReferencedJob(g);
        GridPeerDeployAware d = U.detectPeerDeployAware(U.peerDeployAware(job));
        assert d != null;
        assert SelfReferencedJob.class == d.deployClass();
        assert d.classLoader() == SelfReferencedJob.class.getClassLoader();
    } finally {
        stopGrid(1);
    }
}
Also used : GridPeerDeployAware(org.apache.ignite.internal.util.lang.GridPeerDeployAware) Ignite(org.apache.ignite.Ignite) GridCommonTest(org.apache.ignite.testframework.junits.common.GridCommonTest) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Aggregations

GridPeerDeployAware (org.apache.ignite.internal.util.lang.GridPeerDeployAware)8 URLClassLoader (java.net.URLClassLoader)2 Collection (java.util.Collection)2 GridDeployment (org.apache.ignite.internal.managers.deployment.GridDeployment)2 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)2 GridCommonTest (org.apache.ignite.testframework.junits.common.GridCommonTest)2 Test (org.junit.Test)2 Field (java.lang.reflect.Field)1 ArrayList (java.util.ArrayList)1 UUID (java.util.UUID)1 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)1 Ignite (org.apache.ignite.Ignite)1 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)1 ClusterNode (org.apache.ignite.cluster.ClusterNode)1 ComputeExecutionRejectedException (org.apache.ignite.compute.ComputeExecutionRejectedException)1 ComputeTask (org.apache.ignite.compute.ComputeTask)1 ComputeTaskMapAsync (org.apache.ignite.compute.ComputeTaskMapAsync)1 DiscoveryEvent (org.apache.ignite.events.DiscoveryEvent)1 Event (org.apache.ignite.events.Event)1 TaskEvent (org.apache.ignite.events.TaskEvent)1