Repository: qpid-jms Updated Branches: refs/heads/master 06141c5d4 -> 3d31992d5
Add support for setting options that take a string array from the URI and remove use of PropertyEditor as it is not thread safe and can leak in some contianer usage scenarios. Project: http://git-wip-us.apache.org/repos/asf/qpid-jms/repo Commit: http://git-wip-us.apache.org/repos/asf/qpid-jms/commit/3d31992d Tree: http://git-wip-us.apache.org/repos/asf/qpid-jms/tree/3d31992d Diff: http://git-wip-us.apache.org/repos/asf/qpid-jms/diff/3d31992d Branch: refs/heads/master Commit: 3d31992d5a4336f2d85f7bc21b17e27ff7b40a26 Parents: 06141c5 Author: Timothy Bish <[email protected]> Authored: Thu Jan 22 10:48:48 2015 -0500 Committer: Timothy Bish <[email protected]> Committed: Thu Jan 22 10:48:48 2015 -0500 ---------------------------------------------------------------------- .../org/apache/qpid/jms/util/PropertyUtil.java | 24 +++++--- .../qpid/jms/util/StringArrayConverter.java | 64 ++++++++++++++++++++ .../qpid/jms/util/TypeConversionSupport.java | 48 ++++++++++++--- .../apache/qpid/jms/util/PropertyUtilTest.java | 35 +++++++++++ .../qpid/jms/util/StringArrayConverterTest.java | 51 ++++++++++++++++ 5 files changed, 207 insertions(+), 15 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/qpid-jms/blob/3d31992d/qpid-jms-client/src/main/java/org/apache/qpid/jms/util/PropertyUtil.java ---------------------------------------------------------------------- diff --git a/qpid-jms-client/src/main/java/org/apache/qpid/jms/util/PropertyUtil.java b/qpid-jms-client/src/main/java/org/apache/qpid/jms/util/PropertyUtil.java index 4423ad6..8cd436c 100644 --- a/qpid-jms-client/src/main/java/org/apache/qpid/jms/util/PropertyUtil.java +++ b/qpid-jms-client/src/main/java/org/apache/qpid/jms/util/PropertyUtil.java @@ -19,8 +19,6 @@ package org.apache.qpid.jms.util; import java.beans.BeanInfo; import java.beans.Introspector; import java.beans.PropertyDescriptor; -import java.beans.PropertyEditor; -import java.beans.PropertyEditorManager; import java.io.UnsupportedEncodingException; import java.lang.reflect.Method; import java.net.URI; @@ -534,14 +532,26 @@ public class PropertyUtil { } private static Object convert(Object value, Class<?> type) throws Exception { - PropertyEditor editor = PropertyEditorManager.findEditor(type); - if (editor != null) { - editor.setAsText(value.toString()); - return editor.getValue(); + if (value == null) { + if (boolean.class.isAssignableFrom(type)) { + return Boolean.FALSE; + } + return null; + } + + if (type.isAssignableFrom(value.getClass())) { + return type.cast(value); + } + + // special for String[] as we do not want to use a PropertyEditor for that + if (type.isAssignableFrom(String[].class)) { + return StringArrayConverter.convertToStringArray(value); } + if (type == URI.class) { return new URI(value.toString()); } - return null; + + return TypeConversionSupport.convert(value, type); } } http://git-wip-us.apache.org/repos/asf/qpid-jms/blob/3d31992d/qpid-jms-client/src/main/java/org/apache/qpid/jms/util/StringArrayConverter.java ---------------------------------------------------------------------- diff --git a/qpid-jms-client/src/main/java/org/apache/qpid/jms/util/StringArrayConverter.java b/qpid-jms-client/src/main/java/org/apache/qpid/jms/util/StringArrayConverter.java new file mode 100644 index 0000000..ea13256 --- /dev/null +++ b/qpid-jms-client/src/main/java/org/apache/qpid/jms/util/StringArrayConverter.java @@ -0,0 +1,64 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.qpid.jms.util; + +import java.util.ArrayList; +import java.util.List; +import java.util.StringTokenizer; + +/** + * Class for converting to/from String[] to be used instead of a + * {@link java.beans.PropertyEditor} which otherwise causes memory leaks as the + * JDK {@link java.beans.PropertyEditorManager} is a static class and has strong + * references to classes, causing problems in hot-deployment environments. + */ +public class StringArrayConverter { + + public static String[] convertToStringArray(Object value) { + if (value == null) { + return null; + } + + String text = value.toString(); + if (text == null || text.length() == 0) { + return null; + } + + StringTokenizer stok = new StringTokenizer(text, ","); + final List<String> list = new ArrayList<String>(); + + while (stok.hasMoreTokens()) { + list.add(stok.nextToken()); + } + + String[] array = list.toArray(new String[list.size()]); + return array; + } + + public static String convertToString(String[] value) { + if (value == null || value.length == 0) { + return null; + } + + StringBuffer result = new StringBuffer(String.valueOf(value[0])); + for (int i = 1; i < value.length; i++) { + result.append(",").append(value[i]); + } + + return result.toString(); + } +} http://git-wip-us.apache.org/repos/asf/qpid-jms/blob/3d31992d/qpid-jms-client/src/main/java/org/apache/qpid/jms/util/TypeConversionSupport.java ---------------------------------------------------------------------- diff --git a/qpid-jms-client/src/main/java/org/apache/qpid/jms/util/TypeConversionSupport.java b/qpid-jms-client/src/main/java/org/apache/qpid/jms/util/TypeConversionSupport.java index fd1ef41..9bf71ee 100644 --- a/qpid-jms-client/src/main/java/org/apache/qpid/jms/util/TypeConversionSupport.java +++ b/qpid-jms-client/src/main/java/org/apache/qpid/jms/util/TypeConversionSupport.java @@ -154,24 +154,56 @@ public final class TypeConversionSupport { return new Double(((Number) value).doubleValue()); } }); - - } - - private TypeConversionSupport() { } - public static Object convert(Object value, Class<?> clazz) { + public static Object convert(Object value, Class<?> toClass) { - assert value != null && clazz != null; + assert value != null && toClass != null; - if (value.getClass() == clazz) { + if (value.getClass() == toClass) { return value; } - Converter c = CONVERSION_MAP.get(new ConversionKey(value.getClass(), clazz)); + Class<?> fromClass = value.getClass(); + + if (fromClass.isPrimitive()) { + fromClass = convertPrimitiveTypeToWrapperType(fromClass); + } + + if (toClass.isPrimitive()) { + toClass = convertPrimitiveTypeToWrapperType(toClass); + } + + Converter c = CONVERSION_MAP.get(new ConversionKey(fromClass, toClass)); if (c == null) { return null; } + return c.convert(value); } + + private static Class<?> convertPrimitiveTypeToWrapperType(Class<?> type) { + Class<?> rc = type; + if (type.isPrimitive()) { + if (type == int.class) { + rc = Integer.class; + } else if (type == long.class) { + rc = Long.class; + } else if (type == double.class) { + rc = Double.class; + } else if (type == float.class) { + rc = Float.class; + } else if (type == short.class) { + rc = Short.class; + } else if (type == byte.class) { + rc = Byte.class; + } else if (type == boolean.class) { + rc = Boolean.class; + } + } + + return rc; + } + + private TypeConversionSupport() {} } http://git-wip-us.apache.org/repos/asf/qpid-jms/blob/3d31992d/qpid-jms-client/src/test/java/org/apache/qpid/jms/util/PropertyUtilTest.java ---------------------------------------------------------------------- diff --git a/qpid-jms-client/src/test/java/org/apache/qpid/jms/util/PropertyUtilTest.java b/qpid-jms-client/src/test/java/org/apache/qpid/jms/util/PropertyUtilTest.java index 13ec06f..348d34a 100644 --- a/qpid-jms-client/src/test/java/org/apache/qpid/jms/util/PropertyUtilTest.java +++ b/qpid-jms-client/src/test/java/org/apache/qpid/jms/util/PropertyUtilTest.java @@ -64,6 +64,7 @@ public class PropertyUtilTest { private boolean booleanValue; private URL urlValue; private URI uriValue; + private String[] stringArray; private SSLContext context; private Embedded embedded = new Embedded(); @@ -151,6 +152,14 @@ public class PropertyUtilTest { public void setNotReadable(String value) { this.notReadable = value; } + + public String[] getStringArray() { + return stringArray; + } + + public void setStringArray(String[] stringArray) { + this.stringArray = stringArray; + } } @Test @@ -584,6 +593,32 @@ public class PropertyUtilTest { assertFalse(PropertyUtil.setProperty(configObject, "throwsWhenSet", "foo")); } + @Test + public void testSetPropertyOfStringArray() throws Exception { + Options configObject = new Options(); + assertTrue(PropertyUtil.setProperty(configObject, "stringArray", "foo,bar")); + assertNotNull(configObject.getStringArray()); + assertEquals(2, configObject.getStringArray().length); + assertEquals("foo", configObject.getStringArray()[0]); + assertEquals("bar", configObject.getStringArray()[1]); + } + + @Test + public void testSetPropertyOfStringArrayWithNull() throws Exception { + Options configObject = new Options(); + assertTrue(PropertyUtil.setProperty(configObject, "stringArray", null)); + assertNull(configObject.getStringArray()); + } + + @Test + public void testSetPropertyOfStringArraySingleValue() throws Exception { + Options configObject = new Options(); + assertTrue(PropertyUtil.setProperty(configObject, "stringArray", "foo")); + assertNotNull(configObject.getStringArray() != null); + assertEquals(1, configObject.getStringArray().length); + assertEquals("foo", configObject.getStringArray()[0]); + } + @Test(expected=IllegalArgumentException.class) public void testSetPropertiesWithNullObject() { PropertyUtil.setProperties(null, new HashMap<String, String>()); http://git-wip-us.apache.org/repos/asf/qpid-jms/blob/3d31992d/qpid-jms-client/src/test/java/org/apache/qpid/jms/util/StringArrayConverterTest.java ---------------------------------------------------------------------- diff --git a/qpid-jms-client/src/test/java/org/apache/qpid/jms/util/StringArrayConverterTest.java b/qpid-jms-client/src/test/java/org/apache/qpid/jms/util/StringArrayConverterTest.java new file mode 100644 index 0000000..6c370c7 --- /dev/null +++ b/qpid-jms-client/src/test/java/org/apache/qpid/jms/util/StringArrayConverterTest.java @@ -0,0 +1,51 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.qpid.jms.util; + +import junit.framework.TestCase; + +public class StringArrayConverterTest extends TestCase { + + public void testConvertToStringArray() throws Exception { + assertEquals(null, StringArrayConverter.convertToStringArray(null)); + assertEquals(null, StringArrayConverter.convertToStringArray("")); + + String[] array = StringArrayConverter.convertToStringArray("foo"); + assertEquals(1, array.length); + assertEquals("foo", array[0]); + + array = StringArrayConverter.convertToStringArray("foo,bar"); + assertEquals(2, array.length); + assertEquals("foo", array[0]); + assertEquals("bar", array[1]); + + array = StringArrayConverter.convertToStringArray("foo,bar,baz"); + assertEquals(3, array.length); + assertEquals("foo", array[0]); + assertEquals("bar", array[1]); + assertEquals("baz", array[2]); + } + + public void testConvertToString() throws Exception { + assertEquals(null, StringArrayConverter.convertToString(null)); + assertEquals(null, StringArrayConverter.convertToString(new String[]{})); + assertEquals("", StringArrayConverter.convertToString(new String[]{""})); + assertEquals("foo", StringArrayConverter.convertToString(new String[]{"foo"})); + assertEquals("foo,bar", StringArrayConverter.convertToString(new String[]{"foo", "bar"})); + assertEquals("foo,bar,baz", StringArrayConverter.convertToString(new String[]{"foo", "bar", "baz"})); + } +} --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
