Use Inverted ClassLoader For External Functions

Invert normal classloader order for external functions, first attempting to load
classes from external library before parent (system) classpath.  Classes from the
bootclassloader will load normally.

Change-Id: I7c152efd62af41bdedcbffdd29cd1041a8237ca4
Reviewed-on: https://asterix-gerrit.ics.uci.edu/973
Tested-by: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Integration-Tests: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Reviewed-by: Till Westmann <tillw@apache.org>
diff --git a/asterixdb/asterix-app/src/main/java/org/apache/asterix/app/external/ExternalLibraryClassLoader.java b/asterixdb/asterix-app/src/main/java/org/apache/asterix/app/external/ExternalLibraryClassLoader.java
new file mode 100644
index 0000000..711ac6d
--- /dev/null
+++ b/asterixdb/asterix-app/src/main/java/org/apache/asterix/app/external/ExternalLibraryClassLoader.java
@@ -0,0 +1,56 @@
+/*
+ * 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.asterix.app.external;
+
+import java.net.URL;
+import java.net.URLClassLoader;
+
+public class ExternalLibraryClassLoader extends URLClassLoader {
+
+    private static final ClassLoader bootClassLoader = new ClassLoader(null){};
+
+    public ExternalLibraryClassLoader(URL[] urls, ClassLoader parentClassLoader) {
+        super(urls, parentClassLoader);
+    }
+
+    @Override
+    protected Class<?> loadClass(String name, boolean resolveClass) throws ClassNotFoundException {
+        synchronized (getClassLoadingLock(name)) {
+            Class<?> clazz = findLoadedClass(name);
+            if (clazz == null) {
+                try {
+                    clazz = bootClassLoader.loadClass(name);
+                } catch (ClassNotFoundException ex) {
+                    // this is expected path for non-bootclassloader classes
+                    try {
+                        clazz = findClass(name);
+                    } catch (ClassNotFoundException ex2) {
+                        // this is expected path for classes not defined in the external library classpath,
+                        // finally we try our parent classloader
+                        clazz = getParent().loadClass(name);
+                    }
+                }
+            }
+            if (resolveClass) {
+                resolveClass(clazz);
+            }
+            return clazz;
+        }
+    }
+}
diff --git a/asterixdb/asterix-app/src/main/java/org/apache/asterix/app/external/ExternalLibraryUtils.java b/asterixdb/asterix-app/src/main/java/org/apache/asterix/app/external/ExternalLibraryUtils.java
index a5654bd..04aebf5 100755
--- a/asterixdb/asterix-app/src/main/java/org/apache/asterix/app/external/ExternalLibraryUtils.java
+++ b/asterixdb/asterix-app/src/main/java/org/apache/asterix/app/external/ExternalLibraryUtils.java
@@ -21,7 +21,6 @@
 import java.io.File;
 import java.io.FilenameFilter;
 import java.net.URL;
-import java.net.URLClassLoader;
 import java.rmi.RemoteException;
 import java.util.ArrayList;
 import java.util.HashMap;
@@ -379,8 +378,7 @@
         }
 
         // create and return the class loader
-        ClassLoader classLoader = new URLClassLoader(urls, parentClassLoader);
-        return classLoader;
+        return new ExternalLibraryClassLoader(urls, parentClassLoader);
     }
 
     /**