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;
021
022import org.apache.activemq.command.ActiveMQDestination;
023
024/**
025 * Special converter for String -> List<ActiveMQDestination> to be used instead of a
026 * {@link java.beans.PropertyEditor} which otherwise causes
027 * memory leaks as the JDK {@link java.beans.PropertyEditorManager}
028 * is a static class and has strong references to classes, causing
029 * problems in hot-deployment environments.
030 */
031public class StringToListOfActiveMQDestinationConverter {
032
033    public static List<ActiveMQDestination> convertToActiveMQDestination(Object value) {
034        if (value == null) {
035            return null;
036        }
037
038        // text must be enclosed with []
039
040        String text = value.toString();
041        if (text.startsWith("[") && text.endsWith("]")) {
042            text = text.substring(1, text.length() - 1).trim();
043
044            if (text.isEmpty()) {
045                return null;
046            }
047
048            String[] array = text.split(",");
049
050            List<ActiveMQDestination> list = new ArrayList<ActiveMQDestination>();
051            for (String item : array) {
052                list.add(ActiveMQDestination.createDestination(item.trim(), ActiveMQDestination.QUEUE_TYPE));
053            }
054
055            return list;
056        } else {
057            return null;
058        }
059    }
060
061    public static String convertFromActiveMQDestination(Object value) {
062        if (value == null) {
063            return null;
064        }
065
066        StringBuilder sb = new StringBuilder("[");
067        if (value instanceof List) {
068            List list = (List) value;
069            for (int i = 0; i < list.size(); i++) {
070                Object e = list.get(i);
071                if (e instanceof ActiveMQDestination) {
072                    ActiveMQDestination destination = (ActiveMQDestination) e;
073                    sb.append(destination);
074                    if (i < list.size() - 1) {
075                        sb.append(", ");
076                    }
077                }
078            }
079        }
080        sb.append("]");
081
082        if (sb.length() > 2) {
083            return sb.toString();
084        } else {
085            return null;
086        }
087    }
088
089}