You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by js...@apache.org on 2003/02/25 23:54:23 UTC

cvs commit: jakarta-commons/jelly/jelly-tags/bean/src/test/org/apache/commons/jelly/tags/bean Manager.java suite.jelly

jstrachan    2003/02/25 14:54:23

  Modified:    jelly/jelly-tags/bean/src/java/org/apache/commons/jelly/tags/bean
                        BeanTagLibrary.java BeandefTag.java BeanTag.java
                        BeanPropertyTag.java
               jelly/jelly-tags/bean/src/test/org/apache/commons/jelly/tags/bean
                        suite.jelly
  Added:       jelly/jelly-tags/bean/src/test/org/apache/commons/jelly/tags/bean
                        Manager.java
  Log:
  Patched the bean tag library so that an invoke method can be specified to be called on the beans once they are created.
  This was suggested by Thomas Nichols
  
  Revision  Changes    Path
  1.4       +35 -6     jakarta-commons/jelly/jelly-tags/bean/src/java/org/apache/commons/jelly/tags/bean/BeanTagLibrary.java
  
  Index: BeanTagLibrary.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/jelly/jelly-tags/bean/src/java/org/apache/commons/jelly/tags/bean/BeanTagLibrary.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- BeanTagLibrary.java	24 Jan 2003 06:41:22 -0000	1.3
  +++ BeanTagLibrary.java	25 Feb 2003 22:54:22 -0000	1.4
  @@ -61,9 +61,11 @@
    */
   package org.apache.commons.jelly.tags.bean;
   
  +import java.lang.reflect.Method;
   import java.util.Hashtable;
   import java.util.Map;
   
  +import org.apache.commons.beanutils.MethodUtils;
   import org.apache.commons.jelly.JellyException;
   import org.apache.commons.jelly.Tag;
   import org.apache.commons.jelly.TagLibrary;
  @@ -82,6 +84,9 @@
       /** Synchronized map of tag names to bean classes */
       private Map beanTypes = new Hashtable();
       
  +    /** Synchronized map of tag names to invoke methods */
  +    private Map invokeMethods = new Hashtable();
  +    
       public BeanTagLibrary() {
           registerTagFactory(
               "beandef",
  @@ -100,6 +105,29 @@
           beanTypes.put(name, type);
       }
       
  +    /**
  +     * Allows tags to register new bean types with an associated method
  +     */
  +    public void registerBean(String name, Class type, Method method) {
  +        registerBean(name, type);
  +        if (method != null) {
  +            invokeMethods.put(name, method);
  +        }
  +        else {
  +            invokeMethods.remove(name);
  +        }
  +    }
  +    
  +    /**
  +     * Allows tags to register new bean types with an associated method
  +     */
  +    public void registerBean(String name, Class type, String methodName) {
  +        Method method = MethodUtils.getAccessibleMethod(
  +        	type, methodName, BeandefTag.EMPTY_ARGUMENT_TYPES
  +        );
  +        registerBean(name, type, method);
  +    }
  +    
       // TagLibrary interface
       //-------------------------------------------------------------------------                    
       public TagScript createTagScript(
  @@ -135,7 +163,8 @@
           // is the name bound to a specific class
           Class beanType = getBeanType(name, attributes);
           if (beanType != null) {
  -            return new BeanTag(beanType, name);
  +            Method invokeMethod = (Method) invokeMethods.get(name);
  +            return new BeanTag(beanType, name, invokeMethod);
           }
           
           // its a property tag
  
  
  
  1.4       +40 -6     jakarta-commons/jelly/jelly-tags/bean/src/java/org/apache/commons/jelly/tags/bean/BeandefTag.java
  
  Index: BeandefTag.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/jelly/jelly-tags/bean/src/java/org/apache/commons/jelly/tags/bean/BeandefTag.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- BeandefTag.java	25 Jan 2003 23:09:35 -0000	1.3
  +++ BeandefTag.java	25 Feb 2003 22:54:22 -0000	1.4
  @@ -62,9 +62,11 @@
   
   package org.apache.commons.jelly.tags.bean;
   
  +import java.lang.reflect.Method;
   import java.util.HashMap;
   import java.util.Map;
   
  +import org.apache.commons.beanutils.MethodUtils;
   import org.apache.commons.jelly.JellyTagException;
   import org.apache.commons.jelly.MissingAttributeException;
   import org.apache.commons.jelly.TagSupport;
  @@ -87,6 +89,8 @@
   
       /** An empty Map as I think Collections.EMPTY_MAP is only JDK 1.3 onwards */
       private static final Map EMPTY_MAP = new HashMap();
  +    
  +    protected static final Class[] EMPTY_ARGUMENT_TYPES = {};
   
       /** the name of the tag to create */
       private String name;
  @@ -94,6 +98,9 @@
       /** the Java class name to use for the tag */
       private String className;
   
  +	/** the name of the invoke method */
  +	private String methodName;
  +	 
       /** the ClassLoader used to load beans */
       private ClassLoader classLoader;
       
  @@ -139,9 +146,11 @@
   				}
   			}
   		}
  +		
  +		Method invokeMethod = getInvokeMethod(theClass);
           
           // @todo should we allow the variable name to be specified?
  -        library.registerBean(name, theClass);
  +        library.registerBean(name, theClass, invokeMethod);
   	}
   
       
  @@ -184,5 +193,30 @@
               return answer;
           }
           return classLoader;
  +    }
  +
  +    /**
  +     * @return String
  +     */
  +    public String getMethodName() {
  +        return methodName;
  +    }
  +
  +    /**
  +     * Sets the methodName.
  +     * @param methodName The methodName to set
  +     */
  +    public void setMethodName(String methodName) {
  +        this.methodName = methodName;
  +    }
  +    
  +    // Implementation methods
  +    //-------------------------------------------------------------------------                    
  +    protected Method getInvokeMethod(Class theClass) {
  +        if (methodName != null) {
  +            // lets lookup the method name
  +            return MethodUtils.getAccessibleMethod(theClass, methodName, EMPTY_ARGUMENT_TYPES);
  +        }
  +        return null;
       }
   }
  
  
  
  1.6       +25 -6     jakarta-commons/jelly/jelly-tags/bean/src/java/org/apache/commons/jelly/tags/bean/BeanTag.java
  
  Index: BeanTag.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/jelly/jelly-tags/bean/src/java/org/apache/commons/jelly/tags/bean/BeanTag.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- BeanTag.java	25 Jan 2003 23:34:42 -0000	1.5
  +++ BeanTag.java	25 Feb 2003 22:54:22 -0000	1.6
  @@ -89,6 +89,7 @@
       /** The Log to which logging calls will be made. */
       private static final Log log = LogFactory.getLog(BeanTag.class);
   
  +    protected static final Object[] EMPTY_ARGUMENTS = {};
   
       /** the name of the property to create */
       private String tagName;
  @@ -96,10 +97,18 @@
       /** the name of the adder method */
       protected String addMethodName;
   
  -    
  +    /** if present this is used to call a doit method when the bean is constructed */
  +    private Method invokeMethod;
  +
  +
       public BeanTag(Class defaultClass, String tagName) {
  +        this(defaultClass, tagName, null);
  +    }
  +
  +    public BeanTag(Class defaultClass, String tagName, Method invokeMethod) {
           super(defaultClass);
           this.tagName = tagName;
  +        this.invokeMethod = invokeMethod;
           
           if (tagName.length() > 0) {
               addMethodName = "add" 
  @@ -164,6 +173,16 @@
                   }
                   else if(var == null) { //warn if the bean gets lost in space
                       log.warn( "Could not add bean to parent for bean: " + bean );
  +                }
  +            }
  +            
  +            if (invokeMethod != null) {
  +                Object[] args = { bean };
  +                try {
  +                    invokeMethod.invoke(bean, EMPTY_ARGUMENTS);
  +                }
  +                catch (Exception e) {
  +                    throw new JellyTagException( "failed to invoke method: " + invokeMethod + " on bean: " + bean + " reason: " + e, e );
                   }
               }
           }
  
  
  
  1.6       +6 -7      jakarta-commons/jelly/jelly-tags/bean/src/java/org/apache/commons/jelly/tags/bean/BeanPropertyTag.java
  
  Index: BeanPropertyTag.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/jelly/jelly-tags/bean/src/java/org/apache/commons/jelly/tags/bean/BeanPropertyTag.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- BeanPropertyTag.java	25 Jan 2003 23:34:42 -0000	1.5
  +++ BeanPropertyTag.java	25 Feb 2003 22:54:22 -0000	1.6
  @@ -139,8 +139,7 @@
        * @param parentClass
        * @return the class of the first and only parameter
        */
  -    protected Class findAddMethodClass(Class parentClass)
  -    {
  +    protected Class findAddMethodClass(Class parentClass) {
           Method[] methods = parentClass.getMethods();
           for (int i = 0; i < methods.length; i++) {
               Method method = methods[i];
  
  
  
  1.3       +24 -0     jakarta-commons/jelly/jelly-tags/bean/src/test/org/apache/commons/jelly/tags/bean/suite.jelly
  
  Index: suite.jelly
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/jelly/jelly-tags/bean/src/test/org/apache/commons/jelly/tags/bean/suite.jelly,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- suite.jelly	21 Jan 2003 15:16:32 -0000	1.2
  +++ suite.jelly	25 Feb 2003 22:54:23 -0000	1.3
  @@ -14,6 +14,7 @@
   			 which it appears does not play any role - it becomes globally visible after it is executed -->
   			 
   	<beandef name="customer" className="org.apache.commons.jelly.tags.bean.Customer"/>
  +	<beandef name="manager" className="org.apache.commons.jelly.tags.bean.Manager" methodName="run"/>
   	
   	<test:case name="testNestedBeanTag">
   
  @@ -180,4 +181,27 @@
   	
   	</test:case>
   
  +	<test:case name="testInvokeMethod">
  +
  +		<manager var="foo">
  +			<customer name="James" location="London">
  +				<order amount="100" price="2.99">
  +					<product id="p1" name="Beer"/>
  +				</order>
  +				<order amount="200" price="4.99">
  +					<product id="p2" name="Pizza"/>
  +				</order>
  +			</customer>
  +			<customer name="Bob" location="Atlanta">
  +				<order amount="200" price="2.99">
  +					<product id="p1" name="Beer"/>
  +				</order>
  +			</customer>
  +		</manager>	
  +
  +		<test:assertTrue test="${foo.isInvoked()}"/>
  +		<test:assertTrue test="${size(foo.customers) == 2}"/>
  +		
  +	</test:case>
  +	
   </test:suite>
  
  
  
  1.1                  jakarta-commons/jelly/jelly-tags/bean/src/test/org/apache/commons/jelly/tags/bean/Manager.java
  
  Index: Manager.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons/jelly/jelly-tags/bean/src/test/org/apache/commons/jelly/tags/bean/Manager.java,v 1.1 2003/02/25 22:54:23 jstrachan Exp $
   * $Revision: 1.1 $
   * $Date: 2003/02/25 22:54:23 $
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2002 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Commons", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   * 
   * $Id: Manager.java,v 1.1 2003/02/25 22:54:23 jstrachan Exp $
   */
  package org.apache.commons.jelly.tags.bean;
  
  import java.util.ArrayList;
  import java.util.List;
  
  import org.apache.commons.logging.Log;
  import org.apache.commons.logging.LogFactory;
  
  /** 
   * A sample bean that we can construct via Jelly tags
   *
   * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
   * @version $Revision: 1.1 $
   */
  public class Manager {
  
      /** The Log to which logging calls will be made. */
      private static final Log log = LogFactory.getLog(Manager.class);
  
      private List customers = new ArrayList();
      
      boolean invoked = false;
          
      public Manager() {
      }
      
      public String toString() {
          return super.toString() + "[customers=" + customers + "]";
      }
  
  	/**
  	 * The invoke method which is called when the bean is constructed 
  	 */
  	public void run() {
  		invoked = true;	
  		
  		log.info("Invoked the run() method with customers: " + customers);    		
  	}
  
  	
      public List getCustomers() {
          return customers;
      }
      
      public void addCustomer(Customer customer) {
          customers.add(customer);
      }
      
      public void removeCustomer(Customer customer) {
          customers.remove(customer);
      }    
  
      /**
       * @return boolean
       */
      public boolean isInvoked() {
          return invoked;
      }
  
      /**
       * Sets the invoked.
       * @param invoked The invoked to set
       */
      public void setInvoked(boolean invoked) {
          this.invoked = invoked;
      }
  
  }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org