WIP. Partially moved ASM logic to separate class.

Project: http://git-wip-us.apache.org/repos/asf/ignite/repo
Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/e63d3054
Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/e63d3054
Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/e63d3054

Branch: refs/heads/ignite-3912
Commit: e63d305453f270def19c8f140ab68fa879ceaf31
Parents: 7019934
Author: vozerov-gridgain <voze...@gridgain.com>
Authored: Fri Sep 16 11:45:58 2016 +0300
Committer: vozerov-gridgain <voze...@gridgain.com>
Committed: Fri Sep 16 11:45:58 2016 +0300

----------------------------------------------------------------------
 .../processors/hadoop/HadoopClassLoader.java    | 57 +++++-----------
 .../hadoop/HadoopClassLoaderUtils.java          | 70 ++++++++++++++++++++
 2 files changed, 85 insertions(+), 42 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ignite/blob/e63d3054/modules/hadoop/src/main/java/org/apache/ignite/internal/processors/hadoop/HadoopClassLoader.java
----------------------------------------------------------------------
diff --git 
a/modules/hadoop/src/main/java/org/apache/ignite/internal/processors/hadoop/HadoopClassLoader.java
 
b/modules/hadoop/src/main/java/org/apache/ignite/internal/processors/hadoop/HadoopClassLoader.java
index 95a47f0..8561021 100644
--- 
a/modules/hadoop/src/main/java/org/apache/ignite/internal/processors/hadoop/HadoopClassLoader.java
+++ 
b/modules/hadoop/src/main/java/org/apache/ignite/internal/processors/hadoop/HadoopClassLoader.java
@@ -42,15 +42,12 @@ import org.objectweb.asm.AnnotationVisitor;
 import org.objectweb.asm.Attribute;
 import org.objectweb.asm.ClassReader;
 import org.objectweb.asm.ClassVisitor;
-import org.objectweb.asm.ClassWriter;
 import org.objectweb.asm.FieldVisitor;
 import org.objectweb.asm.Handle;
 import org.objectweb.asm.Label;
 import org.objectweb.asm.MethodVisitor;
 import org.objectweb.asm.Opcodes;
 import org.objectweb.asm.Type;
-import org.objectweb.asm.commons.Remapper;
-import org.objectweb.asm.commons.RemappingClassAdapter;
 
 /**
  * Class loader allowing explicitly load classes without delegation to parent 
class loader.
@@ -234,11 +231,11 @@ public class HadoopClassLoader extends URLClassLoader 
implements ClassCache {
         try {
             if (isHadoop(name)) { // Always load Hadoop classes explicitly, 
since Hadoop can be available in App classpath.
                 if (name.equals(CLS_SHUTDOWN_HOOK_MANAGER))  // Dirty hack to 
get rid of Hadoop shutdown hooks.
-                    return loadFromBytes(name, 
CLS_SHUTDOWN_HOOK_MANAGER_REPLACE);
+                    return loadReplace(name, 
CLS_SHUTDOWN_HOOK_MANAGER_REPLACE);
                 else if (name.equals(CLS_DAEMON))
                     // We replace this in order to be able to forcibly stop 
some daemon threads
                     // that otherwise never stop (e.g. PeerCache runnables):
-                    return loadFromBytes(name, CLS_DAEMON_REPLACE);
+                    return loadReplace(name, CLS_DAEMON_REPLACE);
 
                 return loadClassExplicitly(name, resolve);
             }
@@ -264,55 +261,31 @@ public class HadoopClassLoader extends URLClassLoader 
implements ClassCache {
     }
 
     /**
-     * @param name Name.
-     * @param replace Replacement.
+     * Load a class replacing it with our own implementation.
+     *
+     * @param originalName Name.
+     * @param replaceName Replacement.
      * @return Class.
      */
-    private Class<?> loadFromBytes(final String name, final String replace) {
-        synchronized (getClassLoadingLock(name)) {
+    private Class<?> loadReplace(final String originalName, final String 
replaceName) {
+        synchronized (getClassLoadingLock(originalName)) {
             // First, check if the class has already been loaded
-            Class c = findLoadedClass(name);
+            Class c = findLoadedClass(originalName);
 
             if (c != null)
                 return c;
 
-            byte[] bytes = bytesCache.get(name);
+            byte[] bytes = bytesCache.get(originalName);
 
             if (bytes == null) {
-                InputStream in = loadClassBytes(getParent(), replace);
-
-                ClassReader rdr;
-
-                try {
-                    rdr = new ClassReader(in);
-                }
-                catch (IOException e) {
-                    throw new RuntimeException(e);
-                }
-
-                ClassWriter w = new ClassWriter(Opcodes.ASM4);
-
-                rdr.accept(new RemappingClassAdapter(w, new Remapper() {
-                    /** */
-                    String replaceType = replace.replace('.', '/');
-
-                    /** */
-                    String nameType = name.replace('.', '/');
-
-                    @Override public String map(String type) {
-                        if (type.equals(replaceType))
-                            return nameType;
-
-                        return type;
-                    }
-                }), ClassReader.EXPAND_FRAMES);
+                InputStream in = loadClassBytes(getParent(), replaceName);
 
-                bytes = w.toByteArray();
+                bytes = HadoopClassLoaderUtils.loadReplace(in, originalName, 
replaceName);
 
-                bytesCache.put(name, bytes);
+                bytesCache.put(originalName, bytes);
             }
 
-            return defineClass(name, bytes, 0, bytes.length);
+            return defineClass(originalName, bytes, 0, bytes.length);
         }
     }
 
@@ -363,7 +336,7 @@ public class HadoopClassLoader extends URLClassLoader 
implements ClassCache {
      * @param clsName Class.
      * @return Input stream.
      */
-    @Nullable private InputStream loadClassBytes(ClassLoader ldr, String 
clsName) {
+    @Nullable private static InputStream loadClassBytes(ClassLoader ldr, 
String clsName) {
         return ldr.getResourceAsStream(clsName.replace('.', '/') + ".class");
     }
 

http://git-wip-us.apache.org/repos/asf/ignite/blob/e63d3054/modules/hadoop/src/main/java/org/apache/ignite/internal/processors/hadoop/HadoopClassLoaderUtils.java
----------------------------------------------------------------------
diff --git 
a/modules/hadoop/src/main/java/org/apache/ignite/internal/processors/hadoop/HadoopClassLoaderUtils.java
 
b/modules/hadoop/src/main/java/org/apache/ignite/internal/processors/hadoop/HadoopClassLoaderUtils.java
new file mode 100644
index 0000000..0762a91
--- /dev/null
+++ 
b/modules/hadoop/src/main/java/org/apache/ignite/internal/processors/hadoop/HadoopClassLoaderUtils.java
@@ -0,0 +1,70 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.internal.processors.hadoop;
+
+import org.objectweb.asm.ClassReader;
+import org.objectweb.asm.ClassWriter;
+import org.objectweb.asm.Opcodes;
+import org.objectweb.asm.commons.Remapper;
+import org.objectweb.asm.commons.RemappingClassAdapter;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+/**
+ * Utility methods for Hadoop classloader required to avoid direct 3rd-party 
dependencies in class loader.
+ */
+public class HadoopClassLoaderUtils {
+    /**
+     * Load special replacement and impersonate
+     *
+     * @param in Input stream.
+     * @param originalName Original class name.
+     * @param replaceName Replacer class name.
+     * @return Result.
+     */
+    public static byte[] loadReplace(InputStream in, final String 
originalName, final String replaceName) {
+        ClassReader rdr;
+
+        try {
+            rdr = new ClassReader(in);
+        }
+        catch (IOException e) {
+            throw new RuntimeException(e);
+        }
+
+        ClassWriter w = new ClassWriter(Opcodes.ASM4);
+
+        rdr.accept(new RemappingClassAdapter(w, new Remapper() {
+            /** */
+            String replaceType = replaceName.replace('.', '/');
+
+            /** */
+            String nameType = originalName.replace('.', '/');
+
+            @Override public String map(String type) {
+                if (type.equals(replaceType))
+                    return nameType;
+
+                return type;
+            }
+        }), ClassReader.EXPAND_FRAMES);
+
+        return w.toByteArray();
+    }
+}

Reply via email to