View Javadoc
1 /* 2 * $Header: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/define/DynamicTag.java,v 1.7 2002/05/17 15:18:12 jstrachan Exp $ 3 * $Revision: 1.7 $ 4 * $Date: 2002/05/17 15:18:12 $ 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: DynamicTag.java,v 1.7 2002/05/17 15:18:12 jstrachan Exp $ 61 */ 62 package org.apache.commons.jelly.impl; 63 64 import java.lang.reflect.InvocationTargetException; 65 import java.lang.reflect.Method; 66 import java.util.HashSet; 67 import java.util.Iterator; 68 import java.util.Map; 69 import java.util.Set; 70 71 import org.apache.commons.beanutils.ConvertingWrapDynaBean; 72 73 import org.apache.commons.collections.BeanMap; 74 75 import org.apache.commons.jelly.DynaBeanTagSupport; 76 import org.apache.commons.jelly.JellyException; 77 import org.apache.commons.jelly.MissingAttributeException; 78 import org.apache.commons.jelly.Tag; 79 import org.apache.commons.jelly.XMLOutput; 80 import org.apache.commons.jelly.expression.Expression; 81 import org.apache.commons.jelly.impl.BeanSource; 82 83 import org.apache.commons.logging.Log; 84 import org.apache.commons.logging.LogFactory; 85 86 /*** 87 * This tag is bound onto a Java Bean class. When the tag is invoked a bean will be created 88 * using the tags attributes. 89 * The bean may also have an invoke method called invoke(), run(), execute() or some such method 90 * which will be invoked after the bean has been configured.</p> 91 * 92 * @author <a href="mailto:jstrachan@apache.org">James Strachan</a> 93 * @author <a href="mailto:jason@zenplex.com">Jason van Zyl</a> 94 * @version $Revision: 1.7 $ 95 */ 96 public class DynamicBeanTag extends DynaBeanTagSupport implements BeanSource { 97 98 /*** The Log to which logging calls will be made. */ 99 private static final Log log = LogFactory.getLog(DynamicBeanTag.class); 100 101 /*** Empty arguments for Method.invoke() */ 102 private static final Object[] emptyArgs = {}; 103 104 /*** the bean class */ 105 private Class beanClass; 106 107 /*** the current bean instance */ 108 private Object bean; 109 110 /*** the method to invoke on the bean */ 111 private Method method; 112 113 /*** 114 * the tag attribute name that is used to declare the name 115 * of the variable to export after running this tag 116 */ 117 private String variableNameAttribute; 118 119 /*** the current variable name that the bean should be exported as */ 120 private String var; 121 122 /*** the set of attribute names we've already set */ 123 private Set setAttributesSet = new HashSet(); 124 125 /*** the attribute definitions */ 126 private Map attributes; 127 128 public DynamicBeanTag(Class beanClass, Map attributes, String variableNameAttribute, Method method) { 129 this.beanClass = beanClass; 130 this.method = method; 131 this.attributes = attributes; 132 this.variableNameAttribute = variableNameAttribute; 133 } 134 135 public void beforeSetAttributes() throws Exception { 136 // create a new dynabean before the attributes are set 137 bean = beanClass.newInstance(); 138 setDynaBean( new ConvertingWrapDynaBean( bean ) ); 139 140 setAttributesSet.clear(); 141 } 142 143 public void setAttribute(String name, Object value) throws Exception { 144 boolean isVariableName = false; 145 if (variableNameAttribute != null ) { 146 if ( variableNameAttribute.equals( name ) ) { 147 if (value == null) { 148 var = null; 149 } 150 else { 151 var = value.toString(); 152 } 153 isVariableName = true; 154 } 155 } 156 if (! isVariableName) { 157 158 // #### strictly speaking we could 159 // know what attributes are specified at compile time 160 // so this dynamic set is unnecessary 161 setAttributesSet.add(name); 162 163 // we could maybe implement attribute specific validation here 164 165 super.setAttribute(name, value); 166 } 167 } 168 169 // Tag interface 170 //------------------------------------------------------------------------- 171 public void doTag(XMLOutput output) throws Exception { 172 173 // lets find any attributes that are not set and 174 for ( Iterator iter = attributes.values().iterator(); iter.hasNext(); ) { 175 Attribute attribute = (Attribute) iter.next(); 176 String name = attribute.getName(); 177 if ( ! setAttributesSet.contains( name ) ) { 178 if ( attribute.isRequired() ) { 179 throw new MissingAttributeException(name); 180 } 181 // lets get the default value 182 Object value = null; 183 Expression expression = attribute.getDefaultValue(); 184 if ( expression != null ) { 185 value = expression.evaluate(context); 186 } 187 188 // only set non-null values? 189 if ( value != null ) { 190 super.setAttribute(name, value); 191 } 192 } 193 } 194 195 // If the dynamic bean is itself a tag, let it execute itself 196 if (bean instanceof Tag) 197 { 198 Tag tag = (Tag) bean; 199 tag.setBody(getBody()); 200 tag.setContext(getContext()); 201 tag.setParent(getParent()); 202 ((Tag) bean).doTag(output); 203 204 return; 205 } 206 207 invokeBody(output); 208 209 // export the bean if required 210 if ( var != null ) { 211 context.setVariable(var, bean); 212 } 213 214 // now, I may invoke the 'execute' method if I have one 215 if ( method != null ) { 216 try { 217 method.invoke( bean, emptyArgs ); 218 } 219 catch (IllegalAccessException e) { 220 methodInvocationError(bean, method, e); 221 } 222 catch (IllegalArgumentException e) { 223 methodInvocationError(bean, method, e); 224 } 225 catch (InvocationTargetException e) { 226 // methodInvocationError(bean, method, e); 227 228 Throwable inner = e.getTargetException(); 229 230 if ( inner instanceof Exception ) 231 { 232 throw (Exception) inner; 233 } 234 else 235 { 236 throw new JellyException( inner ); 237 } 238 } 239 } 240 } 241 242 /*** 243 * Report the state of the bean when method invocation fails 244 * so that the user can determine any problems that might 245 * be occuring while using dynamic jelly beans. 246 * 247 * @param bean Bean on which <code>method</code was invoked 248 * @param method Method that was invoked 249 * @param e Exception throw when <code>method</code> was invoked 250 */ 251 private void methodInvocationError(Object bean, Method method, Exception e) throws Exception { 252 log.error("Could not invoke " + method, e); 253 BeanMap beanMap = new BeanMap(bean); 254 255 log.error("Bean properties:"); 256 for (Iterator i = beanMap.keySet().iterator(); i.hasNext();) { 257 String property = (String) i.next(); 258 Object value = beanMap.get(property); 259 log.error(property + " -> " + value); 260 } 261 262 log.error(beanMap); 263 throw e; 264 } 265 266 // Properties 267 //------------------------------------------------------------------------- 268 /*** 269 * @return the bean that has just been created 270 */ 271 public Object getBean() { 272 return bean; 273 } 274 }

This page was automatically generated by Maven