View Javadoc
1 /* 2 * $Header: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/core/ChooseTag.java,v 1.3 2002/05/15 06:25:46 jstrachan Exp $ 3 * $Revision: 1.3 $ 4 * $Date: 2002/05/15 06:25:46 $ 5 * 6 * ==================================================================== 7 * 8 * The Apache Software License, Version 1.1 9 * 10 * Copyright (c) 1999-2002 The Apache Software Foundation. All rights 11 * reserved. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 17 * 1. Redistributions of source code must retain the above copyright 18 * notice, this list of conditions and the following disclaimer. 19 * 20 * 2. Redistributions in binary form must reproduce the above copyright 21 * notice, this list of conditions and the following disclaimer in 22 * the documentation and/or other materials provided with the 23 * distribution. 24 * 25 * 3. The end-user documentation included with the redistribution, if 26 * any, must include the following acknowlegement: 27 * "This product includes software developed by the 28 * Apache Software Foundation (http://www.apache.org/)." 29 * Alternately, this acknowlegement may appear in the software itself, 30 * if and wherever such third-party acknowlegements normally appear. 31 * 32 * 4. The names "The Jakarta Project", "Commons", and "Apache Software 33 * Foundation" must not be used to endorse or promote products derived 34 * from this software without prior written permission. For written 35 * permission, please contact apache@apache.org. 36 * 37 * 5. Products derived from this software may not be called "Apache" 38 * nor may "Apache" appear in their names without prior written 39 * permission of the Apache Group. 40 * 41 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 42 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 43 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 44 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 45 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 46 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 47 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 48 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 49 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 50 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 51 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 52 * SUCH DAMAGE. 53 * ==================================================================== 54 * 55 * This software consists of voluntary contributions made by many 56 * individuals on behalf of the Apache Software Foundation. For more 57 * information on the Apache Software Foundation, please see 58 * <http://www.apache.org/>;. 59 * 60 * $Id: ChooseTag.java,v 1.3 2002/05/15 06:25:46 jstrachan Exp $ 61 */ 62 package org.apache.commons.jelly.tags.core; 63 64 import org.apache.commons.jelly.JellyContext; 65 import org.apache.commons.jelly.MissingAttributeException; 66 import org.apache.commons.jelly.TagSupport; 67 import org.apache.commons.jelly.XMLOutput; 68 69 /*** A tag which creates a new object of the given type 70 * 71 * @author <a href="mailto:jstrachan@apache.org">James Strachan</a> 72 * @version $Revision: 1.3 $ 73 */ 74 public class NewTag extends TagSupport { 75 76 /*** the variable exported */ 77 private String var; 78 79 /*** the class name of the object to instantiate */ 80 private String className; 81 82 /*** 83 * The class loader to use for instantiating application objects. 84 * If not specified, the context class loader, or the class loader 85 * used to load XMLParser itself, is used, based on the value of the 86 * <code>useContextClassLoader</code> variable. 87 */ 88 protected ClassLoader classLoader = null; 89 90 /*** 91 * Do we want to use the Context ClassLoader when loading classes 92 * for instantiating new objects? Default is <code>false</code>. 93 */ 94 protected boolean useContextClassLoader = false; 95 96 97 public NewTag() { 98 } 99 100 /*** Sets the name of the variable exported by this tag */ 101 public void setVar(String var) { 102 this.var = var; 103 } 104 105 /*** Sets the class name of the object to instantiate */ 106 public void setClassName(String className) { 107 this.className = className; 108 } 109 110 /*** 111 * Return the class loader to be used for instantiating application objects 112 * when required. This is determined based upon the following rules: 113 * <ul> 114 * <li>The class loader set by <code>setClassLoader()</code>, if any</li> 115 * <li>The thread context class loader, if it exists and the 116 * <code>useContextClassLoader</code> property is set to true</li> 117 * <li>The class loader used to load the XMLParser class itself. 118 * </ul> 119 */ 120 public ClassLoader getClassLoader() { 121 if (this.classLoader != null) { 122 return (this.classLoader); 123 } 124 if (this.useContextClassLoader) { 125 ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); 126 if (classLoader != null) { 127 return (classLoader); 128 } 129 } 130 return (this.getClass().getClassLoader()); 131 } 132 133 /*** 134 * Set the class loader to be used for instantiating application objects 135 * when required. 136 * 137 * @param classLoader The new class loader to use, or <code>null</code> 138 * to revert to the standard rules 139 */ 140 public void setClassLoader(ClassLoader classLoader) { 141 this.classLoader = classLoader; 142 } 143 144 /*** 145 * Return the boolean as to whether the context classloader should be used. 146 */ 147 public boolean getUseContextClassLoader() { 148 return useContextClassLoader; 149 } 150 151 /*** 152 * Determine whether to use the Context ClassLoader (the one found by 153 * calling <code>Thread.currentThread().getContextClassLoader()</code>) 154 * to resolve/load classes. If not 155 * using Context ClassLoader, then the class-loading defaults to 156 * using the calling-class' ClassLoader. 157 * 158 * @param boolean determines whether to use JellyContext ClassLoader. 159 */ 160 public void setUseContextClassLoader(boolean use) { 161 useContextClassLoader = use; 162 } 163 164 165 // Tag interface 166 //------------------------------------------------------------------------- 167 public void doTag(XMLOutput output) throws Exception { 168 if ( var == null ) { 169 throw new MissingAttributeException( "var" ); 170 } 171 if ( className == null ) { 172 throw new MissingAttributeException( "className" ); 173 } 174 Class theClass = getClassLoader().loadClass( className ); 175 Object object = theClass.newInstance(); 176 context.setVariable(var, object); 177 } 178 }

This page was automatically generated by Maven