On 1/17/06, Gary VanMatre <[EMAIL PROTECTED]> wrote:
>
> This seems like a handly utility.  Clay has something similar, PropUtils,
> that uses the ConvertUtils.  I think we could refactor to use this
> utility.  Is there a specific reason that you wanted to factor out Commons
> BeanUtils?


Yes ... it seems to be the source of some memory leaks from not always
getting cleaned up at runtime.  Plus, when I develop on Windows,
commons-beanutils.jar gets left locked a lot, meaning you have to restart
your container to be able to do a clean rebuild.

This doesn't help a lot right at the moment, because we still inherit the
dependency from Digester and the JSF implementation if its bundled.  But
it's a step down the path of simplifying the dependency hierarchy for Shale,
especially in a JavaEE 5 world where the JSF implementation will already be
provided by the container.

Gary


Craig


-------------- Original message --------------
> From: [EMAIL PROTECTED]
>
> > Author: craigmcc
> > Date: Tue Jan 17 16:58:12 2006
> > New Revision: 369992
> >
> > URL: http://svn.apache.org/viewcvs?rev=369992&view=rev
> > Log:
> > In VariableResolverImpl, remove direct dependency on Commons BeanUtils
> by
> > using the new helper methods for property access and conversion. As a
> side
> > effect of this change, also change the unit tests that specified
> properties
> > of type java.sql.Date, since JSF does not supply a standard converter
> for this,
> > while BeanUtils did.
> >
> > Modified:
> > struts/shale/trunk/tiger/nbproject/private/private.xml
> >
> >
> struts/shale/trunk/tiger/src/java/org/apache/shale/tiger/faces/VariableResolverI
> > mpl.java
> >
> >
> struts/shale/trunk/tiger/src/test/org/apache/shale/tiger/config/FacesConfigParse
> > rTestCase.java
> >
> > struts/shale/trunk/tiger/src/test/org/apache/shale/tiger/config/test-
> config-4.xm
> > l
> >
> > struts/shale/trunk/tiger/src/test/org/apache/shale/tiger/config/test-
> config-5.xm
> > l
> >
> >
> struts/shale/trunk/tiger/src/test/org/apache/shale/tiger/faces/VariableResolverI
> > mpl4TestCase.java
> >
> >
> struts/shale/trunk/tiger/src/test/org/apache/shale/tiger/faces/VariableResolverI
> > mpl5TestCase.java
> >
> > Modified: struts/shale/trunk/tiger/nbproject/private/private.xml
> > URL:
> >
> http://svn.apache.org/viewcvs/struts/shale/trunk/tiger/nbproject/private/private
> > .xml?rev=369992&r1=369991&r2=369992&view=diff
> >
> ==============================================================================
> > --- struts/shale/trunk/tiger/nbproject/private/private.xml (original)
> > +++ struts/shale/trunk/tiger/nbproject/private/private.xml Tue Jan 17
> 16:58:12
> > 2006
> > @@ -1,4 +1,4 @@
> > -
> > -
> > -
> > -
> > +
> > +
> > +
> > +
> >
> > Modified:
> >
> struts/shale/trunk/tiger/src/java/org/apache/shale/tiger/faces/VariableResolverI
> > mpl.java
> > URL:
> >
> http://svn.apache.org/viewcvs/struts/shale/trunk/tiger/src/java/org/apache/shale
> >
> /tiger/faces/VariableResolverImpl.java?rev=369992&r1=369991&r2=369992&view=diff
> >
> ==============================================================================
> > ---
> >
> struts/shale/trunk/tiger/src/java/org/apache/shale/tiger/faces/VariableResolverI
> > mpl.java (original)
> > +++
> >
> struts/shale/trunk/tiger/src/java/org/apache/shale/tiger/faces/VariableResolverI
> > mpl.java Tue Jan 17 16:58:12 2006
> > @@ -23,12 +23,9 @@
> > import javax.faces.context.ExternalContext;
> > import javax.faces.context.FacesContext;
> > import javax.faces.el.EvaluationException;
> > +import javax.faces.el.PropertyNotFoundException;
> > import javax.faces.el.ValueBinding;
> > import javax.faces.el.VariableResolver;
> > -import org.apache.commons.beanutils.BeanUtils;
> > -import org.apache.commons.beanutils.ConversionException;
> > -import org.apache.commons.beanutils.ConvertUtils;
> > -import org.apache.commons.beanutils.PropertyUtils;
> > import org.apache.commons.logging.Log;
> > import org.apache.commons.logging.LogFactory;
> > import org.apache.shale.tiger.config.FacesConfigConfig;
> > @@ -40,7 +37,9 @@
> > import org.apache.shale.tiger.managed.config.ManagedPropertyConfig;
> > import org.apache.shale.tiger.managed.config.MapEntriesConfig;
> > import org.apache.shale.tiger.managed.config.MapEntryConfig;
> > +import org.apache.shale.util.ConverterHelper;
> > import org.apache.shale.util.Messages;
> > +import org.apache.shale.util.PropertyHelper;
> >
> > /**
> > *
> Implementation of VariableResolver that delegates
> > @@ -101,6 +100,12 @@
> >
> >
> > /**
> > + *
> Helper bean for performing conversions.
>
> > + */
> > + private ConverterHelper convHelper = new ConverterHelper();
> > +
> > +
> > + /**
> > *
> Log instance for this class.
>
> > */
> > private transient Log log = null;
> > @@ -119,6 +124,13 @@
> > private VariableResolver original = null;
> >
> >
> > + /**
> > + *
> Helper bean for accessing properties.
>
> > + */
> > + private PropertyHelper propHelper = new PropertyHelper();
> > +
> > +
> > +
> > // ----------------------------------------------- VariableResolver
> Methods
> >
> >
> > @@ -193,60 +205,6 @@
> >
> >
> > /**
> > - *
> Convert the specified value to the specified type.
>
> > - *
> > - * @param context FacesContext for the current request
> > - * @param type Type to which the value should be converted, or
> > - * null to leave it as a string
> > - * @param value Value to be converted
> > - *
> > - * @exception EvaluationException if an evaluation error occurs
> > - */
> > - private Object convert(FacesContext context, Class type, String value)
> {
> > -
> > - // If the type is not specified, return the value unchanged
> > - if (type == null) {
> > - return value;
> > - }
> > -
> > - // Handle primitive type conversions explicitly
> > - try {
> > - if ((type == Boolean.TYPE) || (type == Boolean.class)) {
> > - return Boolean.valueOf(value);
> > - } else if ((type == Byte.TYPE) || (type == Byte.class)) {
> > - return Byte.valueOf(value);
> > - } else if ((type == Double.TYPE) || (type == Double.class)) {
> > - return Double.valueOf(value);
> > - } else if ((type == Float.TYPE) || (type == Float.class)) {
> > - return Float.valueOf(value);
> > - } else if ((type == Integer.TYPE) || (type == Integer.class)) {
> > - return Integer.valueOf(value);
> > - } else if ((type == Long.TYPE) || (type == Long.class)) {
> > - return Long.valueOf(value);
> > - } else if ((type == Short.TYPE) || (type == Short.class)) {
> > - return Short.valueOf(value);
> > - }
> > - } catch (NumberFormatException e) {
> > - throw new EvaluationException(messages().
> > - getMessage("convert.format",
> > - context.getViewRoot().getLocale(),
> > - new Object[] { value, type.getName() }), e);
> > - }
> > -
> > - // For all other cases, use the conversion utility
> > - try {
> > - return ConvertUtils.convert(value, type);
> > - } catch (ConversionException e) {
> > - throw new EvaluationException(messages().
> > - getMessage("convert.exception",
> > - context.getViewRoot().getLocale(),
> > - new Object[] { value, type.getName() }), e);
> > - }
> > -
> > - }
> > -
> > -
> > - /**
> > *
> Create, configure, and return a new instance based on the
> > * specified managed bean, after storing it in the configured
> > * scope (if any).
>
> > @@ -390,7 +348,11 @@
> >
> > context.getApplication().createValueBinding(entry.getValue());
> > list.add(vb.getValue(context));
> > } else {
> > - list.add(convert(context, type, entry.getValue()));
> > + if (type != null) {
> > + list.add(convHelper.asObject(context, type,
> > entry.getValue()));
> > + } else {
> > + list.add(entry.getValue());
> > + }
> > }
> > }
> >
> > @@ -455,7 +417,12 @@
> >
> > // Add a map key/value pair for each configuration element that is
> > present
> > for (MapEntryConfig entry : config.getEntries()) {
> > - Object key = convert(context, keyClass, entry.getKey());
> > + Object key = null;
> > + if (keyClass != null) {
> > + key = convHelper.asObject(context, keyClass, entry.getKey());
> > + } else {
> > + key = entry.getKey();
> > + }
> > if (entry.isNullValue()) {
> > map.put(key, null);
> > } else if (entry.isExpression()) {
> > @@ -464,7 +431,11 @@
> >
> > context.getApplication().createValueBinding(entry.getValue());
> > map.put(key, vb.getValue(context));
> > } else {
> > - map.put(key, convert(context, valueClass, entry.getValue()));
> > + if (valueClass != null) {
> > + map.put(key, convHelper.asObject(context, valueClass,
> > entry.getValue()));
> > + } else {
> > + map.put(key, entry.getValue());
> > + }
> > }
> > }
> >
> > @@ -509,8 +480,9 @@
> > // specified instance, if it exists
> > Object property = null;
> > try {
> > - property = PropertyUtils.getProperty(instance, mp.getName());
> > - } catch (NoSuchMethodException e) {
> > + property = propHelper.getValue(instance, mp.getName());
> > +// property = PropertyUtils.getProperty(instance, mp.getName());
> > + } catch (PropertyNotFoundException e) {
> > ; // Fall through to creating our own list
> > } catch (Exception e) {
> > throw new EvaluationException(messages().
> > @@ -535,7 +507,8 @@
> >
> > // Store the value of the property
> > try {
> > - BeanUtils.setProperty(instance, mp.getName(), property);
> > + propHelper.setValue(instance, mp.getName(), property);
> > +// BeanUtils.setProperty(instance, mp.getName(), property);
> > } catch (Exception e) {
> > throw new EvaluationException(messages().
> > getMessage("list.set",
> > @@ -556,8 +529,9 @@
> > // specified instance, if it exists
> > Object property = null;
> > try {
> > - property = PropertyUtils.getProperty(instance, mp.getName());
> > - } catch (NoSuchMethodException e) {
> > + property = propHelper.getValue(instance, mp.getName());
> > +// property = PropertyUtils.getProperty(instance, mp.getName());
> > + } catch (PropertyNotFoundException e) {
> > ; // Fall through to creating our own map
> > } catch (Exception e) {
> > throw new EvaluationException(messages().
> > @@ -581,7 +555,8 @@
> >
> > // Store the value of the property
> > try {
> > - BeanUtils.setProperty(instance, mp.getName(), property);
> > + propHelper.setValue(instance, mp.getName(), property);
> > +// BeanUtils.setProperty(instance, mp.getName(), property);
> > } catch (Exception e) {
> > throw new EvaluationException(messages().
> > getMessage("map.set",
> > @@ -615,7 +590,11 @@
> >
> > // Assign the acquired value to the specified bean property
> > try {
> > - BeanUtils.setProperty(instance, mp.getName(), value);
> > + Class type = propHelper.getType(instance, mp.getName());
> > + if ((value != null) && (value instanceof String)) {
> > + value = convHelper.asObject(context, type, (String) value);
> > + }
> > + propHelper.setValue(instance, mp.getName(), value);
> > } catch (Exception e) {
> > throw new EvaluationException(messages().
> > getMessage("variable.evaluate",
> >
> > Modified:
> >
> struts/shale/trunk/tiger/src/test/org/apache/shale/tiger/config/FacesConfigParse
> > rTestCase.java
> > URL:
> >
> http://svn.apache.org/viewcvs/struts/shale/trunk/tiger/src/test/org/apache/shale
> >
> /tiger/config/FacesConfigParserTestCase.java?rev=369992&r1=369991&r2=369992&view
> > =diff
> >
> ==============================================================================
> > ---
> >
> struts/shale/trunk/tiger/src/test/org/apache/shale/tiger/config/FacesConfigParse
> > rTestCase.java (original)
> > +++
> >
> struts/shale/trunk/tiger/src/test/org/apache/shale/tiger/config/FacesConfigParse
> > rTestCase.java Tue Jan 17 16:58:12 2006
> > @@ -106,9 +106,9 @@
> > assertEquals(4, facesConfig.getManagedBeans().size());
> >
> > // Validate bean "explicitSqlDateList"
> > - mb = facesConfig.getManagedBean("explicitSqlDateList");
> > + mb = facesConfig.getManagedBean("explicitIntegerList");
> > assertNotNull(mb);
> > - assertEquals("explicitSqlDateList", mb.getName());
> > + assertEquals("explicitIntegerList", mb.getName());
> > assertEquals("none", mb.getScope());
> > assertEquals("java.util.Vector", mb.getType());
> > assertNotNull(mb.getListEntries());
> > @@ -116,18 +116,18 @@
> > assertEquals(0, mb.getProperties().size());
> >
> > entries = mb.getListEntries();
> > - assertEquals("java.sql.Date", entries.getValueType());
> > + assertEquals("java.lang.Integer", entries.getValueType());
> > assertEquals(4, entries.getEntries().size());
> > entry = entries.getEntries().get(0);
> > assertNotNull(entry);
> > assertTrue(!entry.isExpression());
> > assertTrue(!entry.isNullValue());
> > - assertEquals("2006-01-02", entry.getValue());
> > + assertEquals("123", entry.getValue());
> > entry = entries.getEntries().get(1);
> > assertNotNull(entry);
> > assertTrue(!entry.isExpression());
> > assertTrue(!entry.isNullValue());
> > - assertEquals("2006-03-04", entry.getValue());
> > + assertEquals("234", entry.getValue());
> > entry = entries.getEntries().get(2);
> > assertNotNull(entry);
> > assertTrue(!entry.isExpression());
> > @@ -137,7 +137,7 @@
> > assertNotNull(entry);
> > assertTrue(!entry.isExpression());
> > assertTrue(!entry.isNullValue());
> > - assertEquals("2006-05-06", entry.getValue());
> > + assertEquals("345", entry.getValue());
> >
> > // Validate bean "explicitStringList"
> > mb = facesConfig.getManagedBean("explicitStringList");
> > @@ -272,9 +272,9 @@
> > assertEquals(2, facesConfig.getManagedBeans().size());
> >
> > // Validate bean "stringDateMap"
> > - mb = facesConfig.getManagedBean("stringDateMap");
> > + mb = facesConfig.getManagedBean("stringIntegerMap");
> > assertNotNull(mb);
> > - assertEquals("stringDateMap", mb.getName());
> > + assertEquals("stringIntegerMap", mb.getName());
> > assertEquals("none", mb.getScope());
> > assertEquals("java.util.TreeMap", mb.getType());
> > assertNull(mb.getListEntries());
> > @@ -283,18 +283,18 @@
> >
> > entries = mb.getMapEntries();
> > assertEquals("java.lang.String", entries.getKeyType());
> > - assertEquals("java.sql.Date", entries.getValueType());
> > + assertEquals("java.lang.Integer", entries.getValueType());
> > assertEquals(4, entries.getEntries().size());
> >
> > entry = entries.getEntries().get(0);
> > assertEquals("First", entry.getKey());
> > - assertEquals("2006-01-02", entry.getValue());
> > + assertEquals("123", entry.getValue());
> > assertTrue(!entry.isExpression());
> > assertTrue(!entry.isNullValue());
> >
> > entry = entries.getEntries().get(1);
> > assertEquals("Second", entry.getKey());
> > - assertEquals("2006-03-04", entry.getValue());
> > + assertEquals("234", entry.getValue());
> > assertTrue(!entry.isExpression());
> > assertTrue(!entry.isNullValue());
> >
> > @@ -306,7 +306,7 @@
> >
> > entry = entries.getEntries().get(3);
> > assertEquals("Fourth", entry.getKey());
> > - assertEquals("2006-05-06", entry.getValue());
> > + assertEquals("345", entry.getValue());
> > assertTrue(!entry.isExpression());
> > assertTrue(!entry.isNullValue());
> >
> >
> > Modified:
> > struts/shale/trunk/tiger/src/test/org/apache/shale/tiger/config/test-
> config-4.xm
> > l
> > URL:
> >
> http://svn.apache.org/viewcvs/struts/shale/trunk/tiger/src/test/org/apache/shale
> > /tiger/config/test-config-4.xml?rev=369992&r1=369991&r2=369992&view=diff
> >
> ==============================================================================
> > ---
> > struts/shale/trunk/tiger/src/test/org/apache/shale/tiger/config/test-
> config-4.xm
> > l (original)
> > +++
> > struts/shale/trunk/tiger/src/test/org/apache/shale/tiger/config/test-
> config-4.xm
> > l Tue Jan 17 16:58:12 2006
> > @@ -29,15 +29,15 @@
> >
> >
> >
> > - explicitSqlDateList
> > + explicitIntegerList
> > java.util.Vector
> > none
> >
> > - java.sql.Date
> > - 2006-01-02
> > - 2006-03-04
> > + java.lang.Integer
> > + 123
> > + 234
> >
> > - 2006-05-06
> > + 345
> >
> >
> >
> >
> > Modified:
> > struts/shale/trunk/tiger/src/test/org/apache/shale/tiger/config/test-
> config-5.xm
> > l
> > URL:
> >
> http://svn.apache.org/viewcvs/struts/shale/trunk/tiger/src/test/org/apache/shale
> > /tiger/config/test-config-5.xml?rev=369992&r1=369991&r2=369992&view=diff
> >
> ==============================================================================
> > ---
> > struts/shale/trunk/tiger/src/test/org/apache/shale/tiger/config/test-
> config-5.xm
> > l (original)
> > +++
> > struts/shale/trunk/tiger/src/test/org/apache/shale/tiger/config/test-
> config-5.xm
> > l Tue Jan 17 16:58:12 2006
> > @@ -29,19 +29,19 @@
> >
> >
> >
> > - stringDateMap
> > + stringIntegerMap
> > java.util.TreeMap
> > none
> >
> > java.lang.String
> > - java.sql.Date
> > + java.lang.Integer
> >
> > First
> > - 2006-01-02
> > + 123
> >
> >
> > Second
> > - 2006-03-04
> > + 234
> >
> >
> > Third
> > @@ -49,7 +49,7 @@
> >
> >
> > Fourth
> > - 2006-05-06
> > + 345
> >
> >
> >
> >
> > Modified:
> >
> struts/shale/trunk/tiger/src/test/org/apache/shale/tiger/faces/VariableResolverI
> > mpl4TestCase.java
> > URL:
> >
> http://svn.apache.org/viewcvs/struts/shale/trunk/tiger/src/test/org/apache/shale
> >
> /tiger/faces/VariableResolverImpl4TestCase.java?rev=369992&r1=369991&r2=369992&v
> > iew=diff
> >
> ==============================================================================
> > ---
> >
> struts/shale/trunk/tiger/src/test/org/apache/shale/tiger/faces/VariableResolverI
> > mpl4TestCase.java (original)
> > +++
> >
> struts/shale/trunk/tiger/src/test/org/apache/shale/tiger/faces/VariableResolverI
> > mpl4TestCase.java Tue Jan 17 16:58:12 2006
> > @@ -110,19 +110,19 @@
> > // ------------------------------------------------------------ Test
> > Methods
> >
> >
> > - // Test creating bean "explicitSqlDateList"
> > - public void testExplicitSqlDateList() {
> > + // Test creating bean "explicitIntegerList"
> > + public void testExplicitIntegerList() {
> >
> > - Object instance = resolver.resolveVariable(facesContext,
> > "explicitSqlDateList");
> > + Object instance = resolver.resolveVariable(facesContext,
> > "explicitIntegerList");
> > assertNotNull(instance);
> > assertTrue(instance instanceof Vector);
> > List list = (List) instance;
> > assertEquals(4, list.size());
> >
> > - assertEquals(new Date(106, 0, 2), list.get(0));
> > - assertEquals(new Date(106, 2, 4), list.get(1));
> > + assertEquals(new Integer(123), list.get(0));
> > + assertEquals(new Integer(234), list.get(1));
> > assertNull(list.get(2));
> > - assertEquals(new Date(106, 4, 6), list.get(3));
> > + assertEquals(new Integer(345), list.get(3));
> >
> > }
> >
> >
> > Modified:
> >
> struts/shale/trunk/tiger/src/test/org/apache/shale/tiger/faces/VariableResolverI
> > mpl5TestCase.java
> > URL:
> >
> http://svn.apache.org/viewcvs/struts/shale/trunk/tiger/src/test/org/apache/shale
> >
> /tiger/faces/VariableResolverImpl5TestCase.java?rev=369992&r1=369991&r2=369992&v
> > iew=diff
> >
> ==============================================================================
> > ---
> >
> struts/shale/trunk/tiger/src/test/org/apache/shale/tiger/faces/VariableResolverI
> > mpl5TestCase.java (original)
> > +++
> >
> struts/shale/trunk/tiger/src/test/org/apache/shale/tiger/faces/VariableResolverI
> > mpl5TestCase.java Tue Jan 17 16:58:12 2006
> > @@ -111,19 +111,19 @@
> > // ------------------------------------------------------------ Test
> > Methods
> >
> >
> > - // Test creating bean "stringDateMap"
> > - public void testStringDateMap() {
> > + // Test creating bean "stringIntegerMap"
> > + public void testStringIntegerMap() {
> >
> > - Object instance = resolver.resolveVariable(facesContext,
> > "stringDateMap");
> > + Object instance = resolver.resolveVariable(facesContext,
> > "stringIntegerMap");
> > assertNotNull(instance);
> > assertTrue(instance instanceof TreeMap);
> > Map map = (Map) instance;
> > assertEquals(4, map.size());
> >
> > - assertEquals(new Date(106, 0, 2), map.get("First"));
> > - assertEquals(new Date(106, 2, 4), map.get("Second"));
> > + assertEquals(new Integer(123), map.get("First"));
> > + assertEquals(new Integer(234), map.get("Second"));
> > assertNull(map.get("Third"));
> > - assertEquals(new Date(106, 4, 6), map.get("Fourth"));
> > + assertEquals(new Integer(345), map.get("Fourth"));
> >
> > }
> >
> >
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: [EMAIL PROTECTED]
> > For additional commands, e-mail: [EMAIL PROTECTED]
> >
>

Reply via email to