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