001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.activemq.util;
018
019import java.util.ArrayList;
020import java.util.List;
021import java.util.StringTokenizer;
022
023/**
024 * Class for converting to/from String[] to be used instead of a
025 * {@link java.beans.PropertyEditor} which otherwise causes
026 * memory leaks as the JDK {@link java.beans.PropertyEditorManager}
027 * is a static class and has strong references to classes, causing
028 * problems in hot-deployment environments.
029 */
030public class StringArrayConverter {
031
032    public static String[] convertToStringArray(Object value) {
033        if (value == null) {
034            return null;
035        }
036
037        String text = value.toString();
038        if (text == null || text.length() == 0) {
039            return null;
040        }
041
042        StringTokenizer stok = new StringTokenizer(text, ",");
043        final List<String> list = new ArrayList<String>();
044
045        while (stok.hasMoreTokens()) {
046            list.add(stok.nextToken());
047        }
048
049        String[] array = list.toArray(new String[list.size()]);
050        return array;
051    }
052
053    public static String convertToString(String[] value) {
054        if (value == null || value.length == 0) {
055            return null;
056        }
057
058        StringBuffer result = new StringBuffer(String.valueOf(value[0]));
059        for (int i = 1; i < value.length; i++) {
060            result.append(",").append(value[i]);
061        }
062
063        return result.toString();
064    }
065
066}
067