You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@struts.apache.org by cr...@apache.org on 2006/01/17 08:12:48 UTC
svn commit: r369704 - in
/struts/shale/trunk/core-library/src/java/org/apache/shale/util:
ConverterHelper.java PropertyHelper.java
Author: craigmcc
Date: Mon Jan 16 23:12:46 2006
New Revision: 369704
URL: http://svn.apache.org/viewcvs?rev=369704&view=rev
Log:
Add JSF-friendly helper beans to access property values and converters, so that
Shale code does not need to depend on commons-beanutils directly for these
purposes (although it currently inherits a transitive dependency by virtue
of the fact that it uses Commons Digester to parse configuration files).
Added:
struts/shale/trunk/core-library/src/java/org/apache/shale/util/ConverterHelper.java (with props)
struts/shale/trunk/core-library/src/java/org/apache/shale/util/PropertyHelper.java (with props)
Added: struts/shale/trunk/core-library/src/java/org/apache/shale/util/ConverterHelper.java
URL: http://svn.apache.org/viewcvs/struts/shale/trunk/core-library/src/java/org/apache/shale/util/ConverterHelper.java?rev=369704&view=auto
==============================================================================
--- struts/shale/trunk/core-library/src/java/org/apache/shale/util/ConverterHelper.java (added)
+++ struts/shale/trunk/core-library/src/java/org/apache/shale/util/ConverterHelper.java Mon Jan 16 23:12:46 2006
@@ -0,0 +1,100 @@
+/*
+ * Copyright 2006 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.shale.util;
+
+import javax.faces.FacesException;
+import javax.faces.context.FacesContext;
+import javax.faces.convert.Converter;
+import javax.faces.convert.ConverterException;
+
+/**
+ * <p>Helper class to provide access to by-type JavaServer Faces
+ * <code>Converter</code> capabilities. This implementation is stateless
+ * and maintains no cached information, so instances may be freely created
+ * and destroyed with no side effects.</p>
+ *
+ * $Id$
+ *
+ * @since 1.0.1
+ */
+public class ConverterHelper {
+
+
+ // ---------------------------------------------------------- Public Methods
+
+
+ /**
+ * <p>Use the registered by-type <code>Converter</code> to convert the
+ * specified String value to a corresponding Object value.</p>
+ *
+ * @param context <code>FacesContext</code> for the current request
+ * @param type Type to which this value should be converted
+ * (used to select the appropriate by-type converter)
+ * @param value Value to be converted
+ *
+ * @exception ConverterException if a conversion error occurs
+ */
+ public Object asObject(FacesContext context, Class type, String value) {
+
+ return converter(context, type).getAsObject(context, context.getViewRoot(), value);
+
+ }
+
+
+ /**
+ * <p>Use the registered by-type <code>Converter</code> to convert the
+ * specified Object value to a corresponding String value.</p>
+ *
+ * @param context <code>FacesContext</code> for the current request
+ * @param type Type from which this value should be converted
+ * (used to select the appropriate by-type converter)
+ * @param value Value to be converted
+ *
+ * @exception ConverterException if a conversion error occurs
+ */
+ public String asString(FacesContext context, Class type, Object value) {
+
+ return converter(context, type).getAsString(context, context.getViewRoot(), value);
+
+ }
+
+
+ // --------------------------------------------------------- Private Methods
+
+
+ /**
+ * <p>Return an appropriate <code>Converter</code> instance for the
+ * specified type.</p>
+ *
+ * @param context <code>FacesContext</code> for the current request
+ * @param type Type for which to retrieve a <code>Converter</code>
+ *
+ * @exception ConverterException if no <code>Converter</code> has been
+ * registered for the specified type
+ */
+ private Converter converter(FacesContext context, Class type) {
+
+ try {
+ return context.getApplication().createConverter(type);
+ } catch (FacesException e) {
+ throw new ConverterException(e);
+ }
+
+ }
+
+
+}
Propchange: struts/shale/trunk/core-library/src/java/org/apache/shale/util/ConverterHelper.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: struts/shale/trunk/core-library/src/java/org/apache/shale/util/ConverterHelper.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Added: struts/shale/trunk/core-library/src/java/org/apache/shale/util/PropertyHelper.java
URL: http://svn.apache.org/viewcvs/struts/shale/trunk/core-library/src/java/org/apache/shale/util/PropertyHelper.java?rev=369704&view=auto
==============================================================================
--- struts/shale/trunk/core-library/src/java/org/apache/shale/util/PropertyHelper.java (added)
+++ struts/shale/trunk/core-library/src/java/org/apache/shale/util/PropertyHelper.java Mon Jan 16 23:12:46 2006
@@ -0,0 +1,189 @@
+/*
+ * Copyright 2006 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.shale.util;
+
+import java.beans.BeanInfo;
+import java.beans.IntrospectionException;
+import java.beans.Introspector;
+import java.beans.PropertyDescriptor;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import javax.faces.el.EvaluationException;
+import javax.faces.el.PropertyNotFoundException;
+
+/**
+ * <p>Helper class to provide access to the properties of JavaBeans. This
+ * implementation is stateless and maintains no cached information, so
+ * instances may be freely created and destroyed, with no side effects
+ * (unless there are side effects from caching inside the JDK itself).</p>
+ *
+ * $Id$
+ *
+ * @since 1.0.1
+ */
+public class PropertyHelper {
+
+
+ // ---------------------------------------------------------- Public Methods
+
+
+ /**
+ * <p>Return the value of the specified property from the specified bean.</p>
+ *
+ * @param bean Bean whose property is to be retrieved
+ * @param name Name of the property to get
+ *
+ * @exception PropertyNotFoundException if the bean class does not have
+ * a property with the specified name, or if the property is write only
+ * @exception EvaluationException if an exception occurs while
+ * retrieving the property value
+ */
+ public Object getValue(Object bean, String name) {
+
+ PropertyDescriptor descriptor = descriptor(bean, name);
+ Method method = descriptor.getReadMethod();
+ if (method == null) {
+ throw new PropertyNotFoundException(name);
+ }
+
+ try {
+ return method.invoke(bean, new Object[0]);
+ } catch (InvocationTargetException e) {
+ throw new EvaluationException(e.getCause());
+ } catch (Exception e) {
+ throw new EvaluationException(e);
+ }
+
+ }
+
+
+ /**
+ * <p>Return <code>true</code> if the specified property is read only on
+ * the underlying bean class.</p>
+ *
+ * @param bean Bean whose property is to be checked
+ * @param name Name of the property to check
+ *
+ * @exception PropertyNotFoundException if the bean class does not have
+ * a property with the specified name, or if the property is write only
+ * @exception EvaluationException if an exception occurs while
+ * checking the read only status
+ */
+ public boolean isReadOnly(Object bean, String name) {
+
+ PropertyDescriptor descriptor = descriptor(bean, name);
+ if (descriptor.getReadMethod() != null) {
+ return descriptor.getWriteMethod() == null;
+ }
+ throw new PropertyNotFoundException(name);
+
+ }
+
+
+ /**
+ * <p>Return the type of the specified property on the underlying bean
+ * class.</p>
+ *
+ * @param bean Bean whose property is to be analyzed
+ * @param name Name of the property to return the type of
+ *
+ * @exception PropertyNotFoundException if the bean class does not have
+ * a property with the specified name
+ * @exception EvaluationException if an exception occurs while
+ * retrieving the property type
+ */
+ public Class getType(Object bean, String name) {
+
+ PropertyDescriptor descriptor = descriptor(bean, name);
+ return descriptor.getPropertyType();
+
+ }
+
+
+ /**
+ * <p>Set the specified value on the specified property of the specified
+ * bean.</p>
+ *
+ * @param bean Bean whose property is to be set
+ * @param name Name of the property to be set
+ * @param value New value for this property
+ *
+ * @exception PropertyNotFoundException if the bean class does not have
+ * a property with the specified name, or if the property is read only
+ * @exception EvaluationException if an exception occurs while
+ * setting the property value
+ */
+ public void setValue(Object bean, String name, Object value) {
+
+ PropertyDescriptor descriptor = descriptor(bean, name);
+ Method method = descriptor.getWriteMethod();
+ if (method == null) {
+ throw new PropertyNotFoundException(name);
+ }
+
+ try {
+ method.invoke(bean, new Object[] { value });
+ } catch (InvocationTargetException e) {
+ throw new EvaluationException(e.getCause());
+ } catch (Exception e) {
+ throw new EvaluationException(e);
+ }
+
+ }
+
+
+ // ------------------------------------------------------- Private Methods
+
+
+ /**
+ * <p>Return the <code>PropertyDescriptor</code> for the specified
+ * property of the specified class.</p>
+ *
+ * @param bean Bean whose property descriptor is to be returned
+ * @param name Name of the property to retrieve a descrpitor for
+ *
+ * @exception PropertyNotFoundException if the bean class does not have
+ * a property with the specified name
+ * @exception EvaluationException if an exception occurs introspecting
+ * the underlying bean class
+ */
+ private PropertyDescriptor descriptor(Object bean, String name) {
+
+ BeanInfo beanInfo = null;
+ try {
+ Introspector.getBeanInfo(bean.getClass());
+ } catch (IntrospectionException e) {
+ throw new EvaluationException(e);
+ }
+
+ PropertyDescriptor descriptors[] = beanInfo.getPropertyDescriptors();
+ if (descriptors == null) {
+ descriptors = new PropertyDescriptor[0];
+ }
+ for (int i = 0; i < descriptors.length; i++) {
+ if (name.equals(descriptors[i].getName())) {
+ return descriptors[i];
+ }
+ return descriptors[i];
+ }
+
+ throw new PropertyNotFoundException(name);
+
+ }
+
+
+}
Propchange: struts/shale/trunk/core-library/src/java/org/apache/shale/util/PropertyHelper.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: struts/shale/trunk/core-library/src/java/org/apache/shale/util/PropertyHelper.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org
For additional commands, e-mail: dev-help@struts.apache.org