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