View Javadoc

1   /*
2    * Grapht, an open source dependency injector.
3    * Copyright 2014-2015 various contributors (see CONTRIBUTORS.txt)
4    * Copyright 2010-2014 Regents of the University of Minnesota
5    *
6    * This program is free software; you can redistribute it and/or modify
7    * it under the terms of the GNU Lesser General Public License as
8    * published by the Free Software Foundation; either version 2.1 of the
9    * License, or (at your option) any later version.
10   *
11   * This program is distributed in the hope that it will be useful, but WITHOUT
12   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13   * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14   * details.
15   *
16   * You should have received a copy of the GNU General Public License along with
17   * this program; if not, write to the Free Software Foundation, Inc., 51
18   * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19   */
20  package org.grouplens.grapht.util;
21  
22  /**
23   * Utility methods for class loaders.
24   * @author <a href="http://www.grouplens.org">GroupLens Research</a>
25   * @since 0.8.1
26   */
27  public final class ClassLoaders {
28      private ClassLoaders() {}
29  
30      /**
31       * Infer a default class loader.
32       * @return A reasonable default class loader.
33       */
34      public static ClassLoader inferDefault() {
35          return inferDefault(ClassLoaders.class);
36      }
37  
38      /**
39       * Infer a default class loader.
40       * @param refClass a reference class to use for looking up the class loader.  If there is not
41       *                 a context class loader on the current thread, the class loader used to load
42       *                 this class is returned.
43       * @return A reasonable default class loader.
44       */
45      public static ClassLoader inferDefault(Class<?> refClass) {
46          ClassLoader loader = Thread.currentThread().getContextClassLoader();
47          if (loader == null) {
48              loader = refClass.getClassLoader();
49          }
50          if (loader == null) {
51              loader = ClassLoader.getSystemClassLoader();
52          }
53          return loader;
54      }
55  
56      /**
57       * Use the specified class loader for the current thread's context class loader.  This method
58       * returns a context to make it easy to restore the original class loader.  Example usage:
59       *
60       * {@code
61       * ClassLoaderContext context = ClassLoaders.pushContext(classLoader);
62       * try {
63       *     // some code with classLoader as the current loader
64       * } finally {
65       *     context.pop();
66       * }
67       * }
68       *
69       * @param loader The class loader to use.
70       * @return A context for restoring the original class loader.
71       */
72      public static ClassLoaderContext pushContext(ClassLoader loader) {
73          return pushContext(Thread.currentThread(), loader);
74      }
75  
76      /**
77       * Use the specified class loader for the given thread's context class loader.
78       *
79       * @param thread The thread whose class loader should be modified.
80       * @param loader The class loader to use.
81       * @return A context for restoring the original class loader.
82       * @see #pushContext(ClassLoader)
83       */
84      public static ClassLoaderContext pushContext(Thread thread, ClassLoader loader) {
85          ClassLoader orig = thread.getContextClassLoader();
86          ClassLoaderContext context = new ClassLoaderContext(thread, orig);
87          thread.setContextClassLoader(loader);
88          return context;
89      }
90  }