View Javadoc

1   /*
2    * Copyright 2007-2013 smartics, Kronseder & Reiner GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package de.smartics.exceptions.runtime;
17  
18  import de.smartics.util.lang.Arg;
19  
20  /**
21   * Allows to provide a fallback, secondary class loader to be consulted, if the
22   * primary class loader cannot provide a resource.
23   */
24  public final class FallbackClassLoader extends ClassLoader
25  {
26    // ********************************* Fields *********************************
27  
28    // --- constants ------------------------------------------------------------
29  
30    // --- members --------------------------------------------------------------
31  
32    /**
33     * The class loader consulted first.
34     */
35    private final ClassLoader primaryClassLoader;
36  
37    // ****************************** Initializer *******************************
38  
39    // ****************************** Constructors ******************************
40  
41    /**
42     * Default constructor.
43     *
44     * @param primaryClassLoader the class loader consulted first.
45     * @param secondaryClassLoader the class loader consulted, if resource cannot
46     *          be found by primary class loader.
47     */
48    public FallbackClassLoader(final ClassLoader primaryClassLoader,
49        final ClassLoader secondaryClassLoader)
50    {
51      super(secondaryClassLoader);
52      this.primaryClassLoader =
53          Arg.checkNotNull("primaryClassLoader", primaryClassLoader);
54    }
55  
56    // ****************************** Inner Classes *****************************
57  
58    // ********************************* Methods ********************************
59  
60    // --- init -----------------------------------------------------------------
61  
62    // --- get&set --------------------------------------------------------------
63  
64    // --- business -------------------------------------------------------------
65  
66    @Override
67    protected Class<?> findClass(final String name) throws ClassNotFoundException
68    {
69      if (primaryClassLoader != null)
70      {
71        return primaryClassLoader.loadClass(name);
72      }
73      else
74      {
75        return super.findClass(name);
76      }
77    }
78  
79    // --- object basics --------------------------------------------------------
80  
81  }