Return-Path: Delivered-To: apmail-jakarta-commons-dev-archive@apache.org Received: (qmail 81910 invoked from network); 9 Feb 2002 12:04:41 -0000 Received: from unknown (HELO nagoya.betaversion.org) (192.18.49.131) by daedalus.apache.org with SMTP; 9 Feb 2002 12:04:41 -0000 Received: (qmail 2000 invoked by uid 97); 9 Feb 2002 12:04:39 -0000 Delivered-To: qmlist-jakarta-archive-commons-dev@jakarta.apache.org Received: (qmail 1984 invoked by uid 97); 9 Feb 2002 12:04:39 -0000 Mailing-List: contact commons-dev-help@jakarta.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Subscribe: List-Help: List-Post: List-Id: "Jakarta Commons Developers List" Reply-To: "Jakarta Commons Developers List" Delivered-To: mailing list commons-dev@jakarta.apache.org Received: (qmail 1973 invoked by uid 97); 9 Feb 2002 12:04:38 -0000 Date: 9 Feb 2002 12:04:37 -0000 Message-ID: <20020209120437.61618.qmail@icarus.apache.org> From: rdonkin@apache.org To: jakarta-commons-sandbox-cvs@apache.org Subject: cvs commit: jakarta-commons-sandbox/betwixt/src/java/org/apache/commons/betwixt XMLIntrospector.java X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N 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 * . * - * $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 @@ /**

XMLIntrospector an introspector of beans to create a XMLBeanInfo instance.

* + *

By default, XMLBeanInfo caching is switched on. + * This means that the first time that a request is made for a XMLBeanInfo + * for a particular class, the XMLBeanInfo is cached. + * Later requests for the same class will return the cached value.

+ * * @author James Strachan - * @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 XMLBeanInfo's */ + protected HashMap cacheXMLBeanInfos = new HashMap(); + /** Is XMLBeanInfo caching enabled? */ + boolean cachingEnabled = true; /** Base constructor */ public XMLIntrospector() { } /** - *

Get the current level for logging.

- * - * @return a org.apache.commons.logging.Log level constant + *

Get the current logging implementation.

*/ public Log getLog() { return log; } /** - *

Set the current logging level.

- * - * @param level a org.apache.commons.logging.Log level constant + *

Set the current logging implementation.

*/ public void setLog(Log log) { this.log = log; } + /** + * Is XMLBeanInfo caching enabled? + */ + public boolean isCachingEnabled() { + return cachingEnabled; + } + + /** + * Set whether XMLBeanInfo caching should be enabled. + */ + public void setCachingEnabled(boolean cachingEnabled) { + this.cachingEnabled = cachingEnabled; + } + + /** + * Flush existing cached XMLBeanInfo's. + */ + public void flushCache() { + cacheXMLBeanInfos.clear(); + } /** Create a standard XMLBeanInfo by introspection The actual introspection depends only on the BeanInfo 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 XMLBeanInfo 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: For additional commands, e-mail: