Author: nbelaevski
Date: 2009-10-25 15:30:04 -0400 (Sun, 25 Oct 2009)
New Revision: 15754
Added:
root/framework/trunk/api/src/main/java/org/ajax4jsf/javascript/PropertyUtils.java
Modified:
root/framework/trunk/api/pom.xml
root/framework/trunk/api/src/main/java/org/ajax4jsf/javascript/ScriptUtils.java
root/framework/trunk/api/src/test/java/org/ajax4jsf/javascript/ScriptUtilsTest.java
root/framework/trunk/impl/pom.xml
Log:
https://jira.jboss.org/jira/browse/RF-7556
Modified: root/framework/trunk/api/pom.xml
===================================================================
--- root/framework/trunk/api/pom.xml 2009-10-23 22:46:56 UTC (rev 15753)
+++ root/framework/trunk/api/pom.xml 2009-10-25 19:30:04 UTC (rev 15754)
@@ -17,11 +17,6 @@
<dependencies>
<dependency>
- <groupId>commons-beanutils</groupId>
- <artifactId>commons-beanutils</artifactId>
- </dependency>
-
- <dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
Added: root/framework/trunk/api/src/main/java/org/ajax4jsf/javascript/PropertyUtils.java
===================================================================
--- root/framework/trunk/api/src/main/java/org/ajax4jsf/javascript/PropertyUtils.java
(rev 0)
+++
root/framework/trunk/api/src/main/java/org/ajax4jsf/javascript/PropertyUtils.java 2009-10-25
19:30:04 UTC (rev 15754)
@@ -0,0 +1,92 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2009, Red Hat, Inc. and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+package org.ajax4jsf.javascript;
+
+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 java.text.MessageFormat;
+
+import org.richfaces.log.RichfacesLogger;
+import org.slf4j.Logger;
+
+/**
+ * @author Nick Belaevski
+ * @since 4.0
+ */
+final class PropertyUtils {
+
+ private static final PropertyDescriptor[] EMPTY_DESCRIPTORS_ARRAY = new
PropertyDescriptor[0];
+
+ private static final Logger LOGGER = RichfacesLogger.APPLICATION.getLogger();
+
+ private PropertyUtils() {
+ //private constructor of pure utility methods class
+ }
+
+ public static PropertyDescriptor[] getPropertyDescriptors(Object bean) {
+ if (bean == null) {
+ throw new IllegalArgumentException("argument is null");
+ }
+
+ PropertyDescriptor[] descriptors = null;
+ try {
+ //TODO - cache introspection info?
+ BeanInfo beanInfo = Introspector.getBeanInfo(bean.getClass());
+ descriptors = beanInfo.getPropertyDescriptors();
+ } catch (IntrospectionException e) {
+ LOGGER.error(e.getMessage(), e);
+ }
+
+ if (descriptors == null) {
+ descriptors = EMPTY_DESCRIPTORS_ARRAY;
+ }
+
+ return descriptors;
+ }
+
+ public static Object readPropertyValue(Object bean, PropertyDescriptor descriptor)
throws Exception {
+ Method readMethod = descriptor.getReadMethod();
+ if (readMethod == null) {
+ throw new NoSuchMethodException(MessageFormat.format("Read method for property
''{0}'' not found",
+ descriptor.getName()));
+ }
+
+ try {
+ return readMethod.invoke(bean);
+ } catch (InvocationTargetException e) {
+ Throwable cause = e.getCause();
+ if (cause instanceof Exception) {
+ throw (Exception) cause;
+ } else if (cause instanceof Error) {
+ throw (Error) cause;
+ } else {
+ throw e;
+ }
+ }
+ }
+
+
+}
Modified: root/framework/trunk/api/src/main/java/org/ajax4jsf/javascript/ScriptUtils.java
===================================================================
---
root/framework/trunk/api/src/main/java/org/ajax4jsf/javascript/ScriptUtils.java 2009-10-23
22:46:56 UTC (rev 15753)
+++
root/framework/trunk/api/src/main/java/org/ajax4jsf/javascript/ScriptUtils.java 2009-10-25
19:30:04 UTC (rev 15754)
@@ -33,8 +33,6 @@
import javax.faces.FacesException;
import javax.faces.context.ResponseWriter;
-import org.apache.commons.beanutils.PropertyUtils;
-
/**
* @author shura (latest modification by $Author: alexsmirnov $)
* @version $Revision: 1.1.2.3 $ $Date: 2007/01/24 13:22:31 $
@@ -46,7 +44,6 @@
* This is utility class, don't instantiate.
*/
private ScriptUtils() {
-
}
private static void writeScriptToStream(Writer writer, Object obj) throws IOException {
@@ -67,7 +64,7 @@
}
writer.write("] ");
- } else if (obj instanceof Collection) {
+ } else if (obj instanceof Collection<?>) {
// Collections put as JavaScript array.
@SuppressWarnings("unchecked")
@@ -84,7 +81,7 @@
first = false;
}
writer.write("] ");
- } else if (obj instanceof Map) {
+ } else if (obj instanceof Map<?, ?>) {
// Maps put as JavaScript hash.
@SuppressWarnings("unchecked")
@@ -109,36 +106,9 @@
} else if (obj instanceof String) {
// all other put as encoded strings.
writeEncodedString(writer, obj);
- } else if (obj instanceof Enum) {
+ } else if (obj instanceof Enum<?>) {
// all other put as encoded strings.
writeEncodedString(writer, obj);
- } else if (obj.getClass().getName().startsWith("java.sql.")) {
- writer.write("{");
- boolean first = true;
- for (PropertyDescriptor propertyDescriptor :
- PropertyUtils.getPropertyDescriptors(obj)) {
- String key = propertyDescriptor.getName();
- if ("class".equals(key)) {
- continue;
- }
- Object value = null;
- try {
- value = PropertyUtils.getProperty(obj, key);
- } catch (Exception e) {
- continue;
- }
-
- if (!first) {
- writer.write(',');
- }
-
- writeEncodedString(writer, key);
- writer.write(":");
- writeScriptToStream(writer, value);
-
- first = false;
- }
- writer.write("} ");
} else {
// All other objects threaded as Java Beans.
writer.write("{");
@@ -151,26 +121,32 @@
"Error in conversion Java Object to JavaScript", e);
}
+ boolean ignorePropertyReadException =
obj.getClass().getName().startsWith("java.sql.");
boolean first = true;
+
for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
String key = propertyDescriptor.getName();
if ("class".equals(key)) {
continue;
}
+
+ Object propertyValue;
+ try{
+ propertyValue = PropertyUtils.readPropertyValue(obj,
propertyDescriptor);
+ } catch (Exception e) {
+ if (!ignorePropertyReadException) {
+ throw new FacesException(
+ "Error in conversion Java Object to
JavaScript", e);
+ } else {
+ continue;
+ }
+ }
+
if (!first) {
writer.write(',');
}
writeEncodedString(writer, key);
writer.write(":");
-
- Object propertyValue;
- try{
- propertyValue = PropertyUtils.getProperty(obj, key);
- } catch (Exception e) {
- throw new FacesException(
- "Error in conversion Java Object to JavaScript", e);
- }
-
writeScriptToStream(writer, propertyValue);
first = false;
}
Modified:
root/framework/trunk/api/src/test/java/org/ajax4jsf/javascript/ScriptUtilsTest.java
===================================================================
---
root/framework/trunk/api/src/test/java/org/ajax4jsf/javascript/ScriptUtilsTest.java 2009-10-23
22:46:56 UTC (rev 15753)
+++
root/framework/trunk/api/src/test/java/org/ajax4jsf/javascript/ScriptUtilsTest.java 2009-10-25
19:30:04 UTC (rev 15754)
@@ -134,10 +134,18 @@
public void testSqlDate() {
java.sql.Time obj = new java.sql.Time(1);
- assertNotNull(ScriptUtils.toScript(obj));
-
+ String timeString = ScriptUtils.toScript(obj);
+ timeString = ScriptUtils.toScript(obj);
+ timeString = ScriptUtils.toScript(obj);
+ timeString = ScriptUtils.toScript(obj);
+ timeString = ScriptUtils.toScript(obj);
+ assertNotNull(timeString);
+ assertFalse(timeString.contains("year"));
+
java.sql.Date obj1 = new java.sql.Date(1);
- assertNotNull(ScriptUtils.toScript(obj1));
+ String dateString = ScriptUtils.toScript(obj1);
+ assertNotNull(dateString);
+ assertFalse(dateString.contains("seconds"));
}
/**
Modified: root/framework/trunk/impl/pom.xml
===================================================================
--- root/framework/trunk/impl/pom.xml 2009-10-23 22:46:56 UTC (rev 15753)
+++ root/framework/trunk/impl/pom.xml 2009-10-25 19:30:04 UTC (rev 15754)
@@ -124,6 +124,10 @@
<dependencies>
<dependency>
+ <groupId>commons-beanutils</groupId>
+ <artifactId>commons-beanutils</artifactId>
+ </dependency>
+ <dependency>
<groupId>${project.groupId}</groupId>
<artifactId>richfaces-api</artifactId>
</dependency>