You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by rd...@apache.org on 2002/02/09 13:04:37 UTC

cvs commit: jakarta-commons-sandbox/betwixt/src/java/org/apache/commons/betwixt XMLIntrospector.java

rdonkin     02/02/09 04:04:37

  Modified:    betwixt/src/java/org/apache/commons/betwixt
                        XMLIntrospector.java
  Log:
  Added XMLBeanInfo caching (plus more trace logging)
  
  Revision  Changes    Path
  1.15      +74 -15    jakarta-commons-sandbox/betwixt/src/java/org/apache/commons/betwixt/XMLIntrospector.java
  
  Index: XMLIntrospector.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/betwixt/src/java/org/apache/commons/betwixt/XMLIntrospector.java,v
  retrieving revision 1.14
  retrieving revision 1.15
  diff -u -r1.14 -r1.15
  --- XMLIntrospector.java	31 Jan 2002 19:56:02 -0000	1.14
  +++ XMLIntrospector.java	9 Feb 2002 12:04:37 -0000	1.15
  @@ -1,7 +1,7 @@
   /*
  - * $Header: /home/cvs/jakarta-commons-sandbox/betwixt/src/java/org/apache/commons/betwixt/XMLIntrospector.java,v 1.14 2002/01/31 19:56:02 jstrachan Exp $
  - * $Revision: 1.14 $
  - * $Date: 2002/01/31 19:56:02 $
  + * $Header: /home/cvs/jakarta-commons-sandbox/betwixt/src/java/org/apache/commons/betwixt/XMLIntrospector.java,v 1.15 2002/02/09 12:04:37 rdonkin Exp $
  + * $Revision: 1.15 $
  + * $Date: 2002/02/09 12:04:37 $
    *
    * ====================================================================
    *
  @@ -57,7 +57,7 @@
    * information on the Apache Software Foundation, please see
    * <http://www.apache.org/>.
    * 
  - * $Id: XMLIntrospector.java,v 1.14 2002/01/31 19:56:02 jstrachan Exp $
  + * $Id: XMLIntrospector.java,v 1.15 2002/02/09 12:04:37 rdonkin Exp $
    */
   package org.apache.commons.betwixt;
   
  @@ -77,6 +77,7 @@
   import java.util.Iterator;
   import java.util.List;
   import java.util.Map;
  +import java.util.HashMap;
   
   import org.apache.commons.logging.LogSource;
   import org.apache.commons.logging.Log;
  @@ -90,8 +91,13 @@
   
   /** <p><code>XMLIntrospector</code> an introspector of beans to create a XMLBeanInfo instance.</p>
     *
  +  * <p>By default, <code>XMLBeanInfo</code> caching is switched on.
  +  * This means that the first time that a request is made for a <code>XMLBeanInfo</code>
  +  * for a particular class, the <code>XMLBeanInfo</code> is cached.
  +  * Later requests for the same class will return the cached value.</p>
  +  *
     * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
  -  * @version $Revision: 1.14 $
  +  * @version $Revision: 1.15 $
     */
   public class XMLIntrospector {
   
  @@ -99,37 +105,56 @@
       private boolean attributesForPrimitives = true;
       /** Log used for logging (Doh!) */
       protected Log log = LogSource.makeNewLogInstance("org.apache.commons.betwixt.XMLIntrospector");
  -
  +    /** Maps classes to <code>XMLBeanInfo</code>'s */
  +    protected HashMap cacheXMLBeanInfos = new HashMap();
  +    /** Is <code>XMLBeanInfo</code> caching enabled? */
  +    boolean cachingEnabled = true;
       
       /** Base constructor */
       public XMLIntrospector() {
       }
       
       /**
  -     * <p> Get the current level for logging. </p>
  -     *
  -     * @return a <code>org.apache.commons.logging.Log</code> level constant
  +     * <p> Get the current logging implementation. </p>
        */ 
       public Log getLog() {
           return log;
       }
   
       /**
  -     * <p> Set the current logging level. </p>
  -     *
  -     * @param level a <code>org.apache.commons.logging.Log</code> level constant
  +     * <p> Set the current logging implementation. </p>
        */ 
       public void setLog(Log log) {
           this.log = log;
       }
       
  +    /** 
  +     * Is <code>XMLBeanInfo</code> caching enabled? 
  +     */
  +    public boolean isCachingEnabled() {
  +        return cachingEnabled;
  +    }
  +
  +    /**
  +     * Set whether <code>XMLBeanInfo</code> caching should be enabled.
  +     */    
  +    public void setCachingEnabled(boolean cachingEnabled) {
  +        this.cachingEnabled = cachingEnabled;
  +    }
  +    
  +    /**
  +     * Flush existing cached <code>XMLBeanInfo</code>'s.
  +     */
  +    public void flushCache() {
  +        cacheXMLBeanInfos.clear();
  +    }
       
       /** Create a standard <code>XMLBeanInfo</code> by introspection
           The actual introspection depends only on the <code>BeanInfo</code>
           associated with the bean.
           */
       public XMLBeanInfo introspect(Object bean) throws IntrospectionException {
  -        log.debug("Introspecting...");
  +        log.debug( "Introspecting..." );
           log.debug(bean);
           
           return introspect( bean.getClass() );
  @@ -140,8 +165,23 @@
           associated with the bean.        
         */
       public XMLBeanInfo introspect(Class aClass) throws IntrospectionException {
  -        BeanInfo info = Introspector.getBeanInfo( aClass );
  -        return introspect( info );
  +        // if caching is disabled, then create fresh each time
  +        if (!cachingEnabled) {
  +            BeanInfo info = Introspector.getBeanInfo( aClass );
  +            return introspect( info );
  +        }
  +    
  +        // if caching is enabled, try in caching first
  +        XMLBeanInfo xmlInfo = (XMLBeanInfo) cacheXMLBeanInfos.get( aClass );
  +        if (xmlInfo == null) {
  +            BeanInfo info = Introspector.getBeanInfo( aClass );
  +            xmlInfo =  introspect( info );
  +            if (xmlInfo != null) {
  +                cacheXMLBeanInfos.put( aClass, xmlInfo);
  +            }
  +        }
  +        
  +        return xmlInfo;
       }
       
       /** Create a standard <code>XMLBeanInfo</code> by introspection. 
  @@ -158,6 +198,8 @@
           elementDescriptor.setLocalName( beanDescriptor.getName() );
           elementDescriptor.setPropertyType( beanInfo.getBeanDescriptor().getBeanClass() );
           
  +        log.trace(elementDescriptor);
  +        
           // add default string value for primitive types
           if ( isPrimitiveType( beanType ) ) {
               elementDescriptor.setTextExpression( StringExpression.getInstance() );
  @@ -238,6 +280,8 @@
                   addProperty(beanInfo, descriptors[i], elements, attributes);
               }
           }
  +        log.trace(elements);
  +        log.trace(attributes);
       }
       
       /** 
  @@ -256,19 +300,32 @@
           NodeDescriptor nodeDescriptor = null;
           Method readMethod = propertyDescriptor.getReadMethod();
           Method writeMethod = propertyDescriptor.getWriteMethod();
  +        
           if ( readMethod == null ) {
  +            log.trace( "No read method" );
               return;
           }
  +        
  +        if ( log.isTraceEnabled() ) {
  +            log.trace( "Read method=" + readMethod.getName() );
  +        }
  +        
  +        // choose response from property type
  +        
           // XXX: ignore class property ??
           if ( Class.class.equals( type ) && "class".equals( propertyDescriptor.getName() ) ) {
  +            log.trace( "Ignoring class property" );
               return;
           }
           if ( isPrimitiveType( type ) ) {
  +            log.trace( "Primative type" );
               if ( isAttributesForPrimitives() ) {
  +                log.trace( "Added attribute" );
                   nodeDescriptor = new AttributeDescriptor();
                   attributes.add( nodeDescriptor );
               }
               else {
  +                log.trace( "Added element" );
                   nodeDescriptor = new ElementDescriptor(true);
                   elements.add( nodeDescriptor );
               }
  @@ -276,6 +333,7 @@
               nodeDescriptor.setUpdater( new MethodUpdater( writeMethod ) );
           }
           else if ( isLoopType( type ) ) {
  +            log.trace("Loop type");
               ElementDescriptor loopDescriptor = new ElementDescriptor();
               loopDescriptor.setContextExpression(
                   new IteratorExpression( new MethodExpression( readMethod ) )
  @@ -293,6 +351,7 @@
               elements.add( nodeDescriptor );
           }
           else {
  +            log.trace( "Standard property" );
               ElementDescriptor elementDescriptor = new ElementDescriptor();
               elementDescriptor.setContextExpression( new MethodExpression( readMethod ) );
               elementDescriptor.setUpdater( new MethodUpdater( writeMethod ) );
  
  
  

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>