mirror of https://github.com/apache/activemq.git
AMQ-3797: Avoid using StringArrayEditor as property editor as it causes memory leak when undeploying app in a container such as Tomcat.
git-svn-id: https://svn.apache.org/repos/asf/activemq/trunk@1380168 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
bed42a27c6
commit
e85a8bd11c
|
@ -56,7 +56,6 @@ public final class IntrospectionSupport {
|
||||||
String[] newSearchPath = list.toArray(new String[list.size()]);
|
String[] newSearchPath = list.toArray(new String[list.size()]);
|
||||||
try {
|
try {
|
||||||
PropertyEditorManager.setEditorSearchPath(newSearchPath);
|
PropertyEditorManager.setEditorSearchPath(newSearchPath);
|
||||||
PropertyEditorManager.registerEditor(String[].class, StringArrayEditor.class);
|
|
||||||
} catch(java.security.AccessControlException ignore) {
|
} catch(java.security.AccessControlException ignore) {
|
||||||
// we might be in an applet...
|
// we might be in an applet...
|
||||||
}
|
}
|
||||||
|
@ -91,7 +90,7 @@ public final class IntrospectionSupport {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
||||||
Object value = method.invoke(target, new Object[] {});
|
Object value = method.invoke(target);
|
||||||
if (value == null) {
|
if (value == null) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
@ -198,10 +197,10 @@ public final class IntrospectionSupport {
|
||||||
// If the type is null or it matches the needed type, just use the
|
// If the type is null or it matches the needed type, just use the
|
||||||
// value directly
|
// value directly
|
||||||
if (value == null || value.getClass() == setter.getParameterTypes()[0]) {
|
if (value == null || value.getClass() == setter.getParameterTypes()[0]) {
|
||||||
setter.invoke(target, new Object[] {value});
|
setter.invoke(target, value);
|
||||||
} else {
|
} else {
|
||||||
// We need to convert it
|
// We need to convert it
|
||||||
setter.invoke(target, new Object[] {convert(value, setter.getParameterTypes()[0])});
|
setter.invoke(target, convert(value, setter.getParameterTypes()[0]));
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
} catch (Throwable ignore) {
|
} catch (Throwable ignore) {
|
||||||
|
@ -210,6 +209,11 @@ public final class IntrospectionSupport {
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Object convert(Object value, Class type) {
|
private static Object convert(Object value, Class type) {
|
||||||
|
// special for String[] as we do not want to use a PropertyEditor for that
|
||||||
|
if (type.isAssignableFrom(String[].class)) {
|
||||||
|
return StringArrayConverter.convertToStringArray(value);
|
||||||
|
}
|
||||||
|
|
||||||
PropertyEditor editor = PropertyEditorManager.findEditor(type);
|
PropertyEditor editor = PropertyEditorManager.findEditor(type);
|
||||||
if (editor != null) {
|
if (editor != null) {
|
||||||
editor.setAsText(value.toString());
|
editor.setAsText(value.toString());
|
||||||
|
@ -219,6 +223,12 @@ public final class IntrospectionSupport {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String convertToString(Object value, Class type) {
|
public static String convertToString(Object value, Class type) {
|
||||||
|
// special for String[] as we do not want to use a PropertyEditor for that
|
||||||
|
if (value != null && value.getClass().isAssignableFrom(String[].class)) {
|
||||||
|
String[] array = (String[]) value;
|
||||||
|
return StringArrayConverter.convertToString(array);
|
||||||
|
}
|
||||||
|
|
||||||
PropertyEditor editor = PropertyEditorManager.findEditor(type);
|
PropertyEditor editor = PropertyEditorManager.findEditor(type);
|
||||||
if (editor != null) {
|
if (editor != null) {
|
||||||
editor.setValue(value);
|
editor.setValue(value);
|
||||||
|
@ -242,6 +252,11 @@ public final class IntrospectionSupport {
|
||||||
}
|
}
|
||||||
|
|
||||||
private static boolean isSettableType(Class clazz) {
|
private static boolean isSettableType(Class clazz) {
|
||||||
|
// special for String[]
|
||||||
|
if (clazz.isAssignableFrom(String[].class)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
if (PropertyEditorManager.findEditor(clazz) != null) {
|
if (PropertyEditorManager.findEditor(clazz) != null) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,69 @@
|
||||||
|
/**
|
||||||
|
* 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.activemq.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 {
|
||||||
|
|
||||||
|
// TODO: Remove System.out
|
||||||
|
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
|
@ -1,60 +0,0 @@
|
||||||
/**
|
|
||||||
* 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.activemq.util;
|
|
||||||
|
|
||||||
import java.beans.PropertyEditorSupport;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.StringTokenizer;
|
|
||||||
|
|
||||||
|
|
||||||
public class StringArrayEditor extends PropertyEditorSupport {
|
|
||||||
|
|
||||||
public void setAsText(String text) {
|
|
||||||
if (text == null || text.length() == 0) {
|
|
||||||
setValue(null);
|
|
||||||
} else {
|
|
||||||
StringTokenizer stok = new StringTokenizer(text, ",");
|
|
||||||
final List<String> list = new ArrayList<String>();
|
|
||||||
|
|
||||||
while (stok.hasMoreTokens()) {
|
|
||||||
list.add(stok.nextToken());
|
|
||||||
}
|
|
||||||
|
|
||||||
Object array = list.toArray(new String[list.size()]);
|
|
||||||
|
|
||||||
setValue(array);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getAsText() {
|
|
||||||
Object[] objects = (Object[]) getValue();
|
|
||||||
if (objects == null || objects.length == 0) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
StringBuffer result = new StringBuffer(String.valueOf(objects[0]));
|
|
||||||
for (int i = 1; i < objects.length; i++) {
|
|
||||||
result.append(",").append(objects[i]);
|
|
||||||
}
|
|
||||||
|
|
||||||
return result.toString();
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
|
@ -21,52 +21,35 @@ import junit.framework.TestCase;
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class StringArrayEditorTest extends TestCase {
|
public class StringArrayConverterTest extends TestCase {
|
||||||
|
|
||||||
private StringArrayEditor editor = new StringArrayEditor();
|
|
||||||
|
|
||||||
public void testConvertToStringArray() throws Exception {
|
public void testConvertToStringArray() throws Exception {
|
||||||
editor.setAsText(null);
|
assertEquals(null, StringArrayConverter.convertToStringArray(null));
|
||||||
assertEquals(null, editor.getValue());
|
assertEquals(null, StringArrayConverter.convertToStringArray(""));
|
||||||
editor.setAsText("");
|
|
||||||
assertEquals(null, editor.getValue());
|
|
||||||
|
|
||||||
editor.setAsText("foo");
|
String[] array = StringArrayConverter.convertToStringArray("foo");
|
||||||
String[] array = (String[]) editor.getValue();
|
|
||||||
assertEquals(1, array.length);
|
assertEquals(1, array.length);
|
||||||
assertEquals("foo", array[0]);
|
assertEquals("foo", array[0]);
|
||||||
|
|
||||||
editor.setAsText("foo,bar");
|
array = StringArrayConverter.convertToStringArray("foo,bar");
|
||||||
array = (String[]) editor.getValue();
|
|
||||||
assertEquals(2, array.length);
|
assertEquals(2, array.length);
|
||||||
assertEquals("foo", array[0]);
|
assertEquals("foo", array[0]);
|
||||||
assertEquals("bar", array[1]);
|
assertEquals("bar", array[1]);
|
||||||
|
|
||||||
editor.setAsText("foo,bar,baz");
|
array = StringArrayConverter.convertToStringArray("foo,bar,baz");
|
||||||
array = (String[]) editor.getValue();
|
assertEquals(3, array.length);
|
||||||
assertEquals("foo", array[0]);
|
assertEquals("foo", array[0]);
|
||||||
assertEquals("bar", array[1]);
|
assertEquals("bar", array[1]);
|
||||||
assertEquals("baz", array[2]);
|
assertEquals("baz", array[2]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testConvertToString() throws Exception {
|
public void testConvertToString() throws Exception {
|
||||||
editor.setValue(null);
|
assertEquals(null, StringArrayConverter.convertToString(null));
|
||||||
assertEquals(null, editor.getAsText());
|
assertEquals(null, StringArrayConverter.convertToString(new String[]{}));
|
||||||
|
assertEquals("", StringArrayConverter.convertToString(new String[]{""}));
|
||||||
editor.setValue(new String[]{});
|
assertEquals("foo", StringArrayConverter.convertToString(new String[]{"foo"}));
|
||||||
assertEquals(null, editor.getAsText());
|
assertEquals("foo,bar", StringArrayConverter.convertToString(new String[]{"foo", "bar"}));
|
||||||
|
assertEquals("foo,bar,baz", StringArrayConverter.convertToString(new String[]{"foo", "bar", "baz"}));
|
||||||
editor.setValue(new String[]{""});
|
|
||||||
assertEquals("", editor.getAsText());
|
|
||||||
|
|
||||||
editor.setValue(new String[]{"foo"});
|
|
||||||
assertEquals("foo", editor.getAsText());
|
|
||||||
|
|
||||||
editor.setValue(new String[]{"foo", "bar"});
|
|
||||||
assertEquals("foo,bar", editor.getAsText());
|
|
||||||
|
|
||||||
editor.setValue(new String[]{"foo", "bar", "baz"});
|
|
||||||
assertEquals("foo,bar,baz", editor.getAsText());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue