View Javadoc
1   /*
2    * This file is part of ***  M y C o R e  ***
3    * See http://www.mycore.de/ for details.
4    *
5    * MyCoRe is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU General Public License as published by
7    * the Free Software Foundation, either version 3 of the License, or
8    * (at your option) any later version.
9    *
10   * MyCoRe is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with MyCoRe.  If not, see <http://www.gnu.org/licenses/>.
17   */
18  
19  package org.mycore.common;
20  
21  import java.io.File;
22  import java.net.MalformedURLException;
23  import java.net.URL;
24  import java.net.URLClassLoader;
25  import java.util.Optional;
26  
27  public class MCRClassTools {
28      private static volatile ClassLoader extendedClassLoader;
29  
30      static {
31          updateClassLoader(); //first init
32      }
33  
34      public static Object loadClassFromURL(String classPath, String className)
35          throws MalformedURLException, ReflectiveOperationException {
36          return loadClassFromURL(new File(classPath), className);
37      }
38  
39      public static Object loadClassFromURL(File file, String className)
40          throws MalformedURLException, ReflectiveOperationException {
41          if (file.exists()) {
42              URL url = file.toURI().toURL();
43              URLClassLoader urlClassLoader = new URLClassLoader(new URL[] { url },
44                  Thread.currentThread().getContextClassLoader());
45              Class<?> clazz = urlClassLoader.loadClass(className);
46              return clazz.getDeclaredConstructor().newInstance();
47          }
48  
49          return null;
50      }
51  
52      /**
53       * Loads a class via default ClassLoader or <code>Thread.currentThread().getContextClassLoader()</code>.
54       * @param classname Name of class
55       * @param <T> Type of Class
56       * @return the initialized class
57       * @throws ClassNotFoundException if both ClassLoader cannot load the Class
58       */
59      public static <T> Class<? extends T> forName(String classname) throws ClassNotFoundException {
60          @SuppressWarnings("unchecked")
61          Class<? extends T> forName;
62          try {
63              forName = (Class<? extends T>) Class.forName(classname);
64          } catch (ClassNotFoundException cnfe) {
65              forName = (Class<? extends T>) Class.forName(classname, true, extendedClassLoader);
66          }
67          return forName;
68      }
69  
70      /**
71       * @return a ClassLoader that should be used to load resources
72       */
73      public static ClassLoader getClassLoader() {
74          return extendedClassLoader;
75      }
76  
77      public static void updateClassLoader() {
78          extendedClassLoader = Optional.ofNullable(Thread.currentThread().getContextClassLoader())
79              .orElseGet(MCRClassTools.class::getClassLoader);
80      }
81  
82  }