Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2002,2004 The Apache Software Foundation.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 package org.apache.commons.jelly.impl;
 
 import java.util.Map;
 import java.util.Set;
 
 import  org.apache.commons.logging.Log;
 import  org.apache.commons.logging.LogFactory;

This tag is bound onto a Java Bean class. When the tag is invoked a bean will be created using the tags attributes. The bean may also have an invoke method called invoke(), run(), execute() or some such method which will be invoked after the bean has been configured.

Author(s):
James Strachan
Jason van Zyl
Version:
$Revision: 155420 $
 
 public class DynamicBeanTag extends DynaBeanTagSupport implements BeanSource {

    
The Log to which logging calls will be made.
 
     private static final Log log = LogFactory.getLog(DynamicBeanTag.class);

    
Empty arguments for Method.invoke()
 
     private static final Object[] emptyArgs = {};

    
the bean class
 
     private Class beanClass;

    
the current bean instance
 
     private Object bean;

    
the method to invoke on the bean
 
     private Method method;

    
the tag attribute name that is used to declare the name of the variable to export after running this tag
 
     private String variableNameAttribute;

    
the current variable name that the bean should be exported as
 
     private String var;

    
the set of attribute names we've already set
 
     private Set setAttributesSet = new HashSet();

    
the attribute definitions
 
     private Map attributes;

    

Parameters:
beanClass Class of the bean that will receive the setter events
attributes
variableNameAttribute
method method of the Bean to invoke after the attributes have been set. Can be null.
 
     public DynamicBeanTag(Class beanClassMap attributesString variableNameAttributeMethod method) {
         this. = beanClass;
         this. = method;
         this. = attributes;
         this. = variableNameAttribute;
     }
 
     public void beforeSetAttributes() throws JellyTagException {
         // create a new dynabean before the attributes are set
         try {
              = .newInstance();
             setDynaBeannew ConvertingWrapDynaBean ) );
         } catch (InstantiationException e) {
             throw new JellyTagException("Could not instantiate dynabean",e);
         } catch (IllegalAccessException e) {
            throw new JellyTagException("Could not instantiate dynabean",e);
        }
        .clear();
    }
    public void setAttribute(String nameObject valuethrows JellyTagException {
        boolean isVariableName = false;
        if ( != null ) {
            if ( .equalsname ) ) {
                if (value == null) {
                     = null;
                }
                else {
                     = value.toString();
                }
                isVariableName = true;
            }
        }
        if (! isVariableName) {
            // #### strictly speaking we could
            // know what attributes are specified at compile time
            // so this dynamic set is unnecessary
            .add(name);
            // we could maybe implement attribute specific validation here
            super.setAttribute(namevalue);
        }
    }
    // Tag interface
    //-------------------------------------------------------------------------
    public void doTag(XMLOutput outputthrows JellyTagException {
        // lets find any attributes that are not set and
        for ( Iterator iter = .values().iterator(); iter.hasNext(); ) {
            Attribute attribute = (Attributeiter.next();
            String name = attribute.getName();
            if ( ! .containsname ) ) {
                if ( attribute.isRequired() ) {
                    throw new MissingAttributeException(name);
                }
                // lets get the default value
                Object value = null;
                Expression expression = attribute.getDefaultValue();
                if ( expression != null ) {
                    value = expression.evaluate();
                }
                // only set non-null values?
                if ( value != null ) {
                    super.setAttribute(namevalue);
                }
            }
        }
        // If the dynamic bean is itself a tag, let it execute itself
        if ( instanceof Tag)
        {
            Tag tag = (Tag;
            tag.setBody(getBody());
            tag.setContext(getContext());
            tag.setParent(getParent());
            ((Tag).doTag(output);
            return;
        }
        invokeBody(output);
        // export the bean if required
        if (  != null ) {
            .setVariable();
        }
        // now, I may invoke the 'execute' method if I have one
        if (  != null ) {
            try {
                .invoke );
            }
            catch (IllegalAccessException e) {
                methodInvocationException(e);
            }
            catch (IllegalArgumentException e) {
                methodInvocationException(e);
            }
            catch (InvocationTargetException e) {
                // methodInvocationError(bean, method, e);
                Throwable inner = e.getTargetException();
                throw new JellyTagException(inner);
            }
        }
    }

    
Report the state of the bean when method invocation fails so that the user can determine any problems that might be occuring while using dynamic jelly beans.

Parameters:
bean Bean on which method
method Method that was invoked
e Exception throw when method was invoked
    private void methodInvocationException(Object beanMethod methodException ethrows JellyTagException {
        .error("Could not invoke " + methode);
        BeanMap beanMap = new BeanMap(bean);
        .error("Bean properties:");
        for (Iterator i = beanMap.keySet().iterator(); i.hasNext();) {
            String property = (Stringi.next();
            Object value = beanMap.get(property);
            .error(property + " -> " + value);
        }
        .error(beanMap);
        throw new JellyTagException(e);
    }
    // Properties
    //-------------------------------------------------------------------------
    

Returns:
the bean that has just been created
    public Object getBean() {
        return ;
    }
New to GrepCode? Check out our FAQ X