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  
17  package org.apache.commons.jelly.tags.define;
18  
19  import java.net.MalformedURLException;
20  import java.net.URL;
21  import java.net.URLClassLoader;
22  
23  import org.apache.commons.jelly.JellyTagException;
24  import org.apache.commons.jelly.MissingAttributeException;
25  import org.apache.commons.jelly.XMLOutput;
26  import org.apache.commons.jelly.util.ClassLoaderUtils;
27  import org.apache.commons.logging.Log;
28  import org.apache.commons.logging.LogFactory;
29  
30  
31  /***
32   * Creates a new <code>URLClassLoader</code> to dynamically
33   * load tags froms.
34   *
35   * @author <a href="mailto:stephenh@chase3000.com">Stephen Haberman</a>
36   * @version $Revision: 155420 $
37   */
38  public class ClassLoaderTag extends BeanTag {
39  
40      /*** The Log to which logging calls will be made. */
41      private static final Log log = LogFactory.getLog(ClassLoaderTag.class);
42  
43      /*** The name to export the classloader to. */
44      private String var;
45  
46      /*** The URL to load the classes from. */
47      private String url;
48  
49      // Properties
50      //-------------------------------------------------------------------------
51  
52      /***
53       * @return the variable to store the class loader in
54       */
55      public String getVar() {
56          return this.var;
57      }
58  
59      /***
60       * @param var the variable to store the class loader in
61       */
62      public void setVar(String var) {
63          this.var = var;
64      }
65  
66      /***
67       * @return the url to load the classes from
68       */
69      public String getUrl() {
70          return this.url;
71      }
72  
73      /***
74       * @param url the url to load the classes from
75       */
76      public void setUrl(String url) {
77          this.url = url;
78      }
79  
80      // Implementation methods
81      //-------------------------------------------------------------------------
82  
83      public void doTag(XMLOutput output) throws MissingAttributeException, JellyTagException {
84          if ( getVar() == null ) {
85              throw new MissingAttributeException( "var" );
86          }
87          if ( getUrl() == null ) {
88              throw new MissingAttributeException( "url" );
89          }
90  
91          ClassLoader parent = Thread.currentThread().getContextClassLoader();
92          if (parent == null) {
93              parent = ClassLoaderUtils.getClassLoader(getClass());
94          }
95  
96          URLClassLoader newClassLoader = null;
97  
98          try {
99              newClassLoader =
100               new URLClassLoader( new URL[] { new URL(getUrl()) }, parent );
101         } catch (MalformedURLException e) {
102             throw new JellyTagException(e);
103         }
104 
105         log.debug("Storing the new classloader in " + getVar());
106 
107         context.setVariable(getVar(), newClassLoader);
108     }
109 
110 }