View Javadoc

1   /*
2    * Copyright 2002,2004 The Apache Software Foundation.
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 org.apache.commons.jelly.tags.core;
17  
18  import org.apache.commons.jelly.TagSupport;
19  import org.apache.commons.jelly.util.ClassLoaderUtils;
20  
21  /*** Abstract base tag providing {@link ClassLoader} support.
22    *
23    * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
24    * @author Rodney Waldhoff
25    * @version $Revision: 155420 $
26    */
27  public abstract class BaseClassLoaderTag extends TagSupport {
28      /***
29       * The class loader to use for instantiating application objects.
30       * If not specified, the context class loader, or the class loader
31       * used to load XMLParser itself, is used, based on the value of the
32       * <code>useContextClassLoader</code> variable.
33       */
34      protected ClassLoader classLoader = null;
35  
36      /***
37       * Do we want to use the Context ClassLoader when loading classes
38       * for instantiating new objects?  Default is <code>false</code>.
39       */
40      protected boolean useContextClassLoader = false;
41  
42      /***
43       * Return the class loader to be used for instantiating application objects
44       * when required.  This is determined based upon the following rules:
45       * <ul>
46       * <li>The class loader set by <code>setClassLoader()</code>, if any</li>
47       * <li>The thread context class loader, if it exists and the
48       *     <code>useContextClassLoader</code> property is set to true</li>
49       * <li>The class loader used to load the XMLParser class itself.
50       * </ul>
51       */
52      public ClassLoader getClassLoader() {
53          return ClassLoaderUtils.getClassLoader(classLoader, useContextClassLoader, getClass());
54      }
55  
56      /***
57       * Set the class loader to be used for instantiating application objects
58       * when required.
59       *
60       * @param classLoader The new class loader to use, or <code>null</code>
61       *  to revert to the standard rules
62       */
63      public void setClassLoader(ClassLoader classLoader) {
64          this.classLoader = classLoader;
65      }
66  
67      /***
68       * Return the boolean as to whether the context classloader should be used.
69       */
70      public boolean getUseContextClassLoader() {
71          return useContextClassLoader;
72      }
73  
74      /***
75       * Determine whether to use the Context ClassLoader (the one found by
76       * calling <code>Thread.currentThread().getContextClassLoader()</code>)
77       * to resolve/load classes.  If not
78       * using Context ClassLoader, then the class-loading defaults to
79       * using the calling-class' ClassLoader.
80       *
81       * @param boolean determines whether to use JellyContext ClassLoader.
82       */
83      public void setUseContextClassLoader(boolean use) {
84          useContextClassLoader = use;
85      }
86  }