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    
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     */
031    public 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);
043                String[] array = text.split(",");
044    
045                List<ActiveMQDestination> list = new ArrayList<ActiveMQDestination>();
046                for (String item : array) {
047                    list.add(ActiveMQDestination.createDestination(item.trim(), ActiveMQDestination.QUEUE_TYPE));
048                }
049                return list;
050            } else {
051                return null;
052            }
053        }
054    
055        public static String convertFromActiveMQDestination(Object value) {
056            if (value == null) {
057                return null;
058            }
059    
060            StringBuilder sb = new StringBuilder("[");
061            if (value instanceof List) {
062                List list = (List) value;
063                for (int i = 0; i < list.size(); i++) {
064                    Object e = list.get(i);
065                    if (e instanceof ActiveMQDestination) {
066                        ActiveMQDestination destination = (ActiveMQDestination) e;
067                        sb.append(destination);
068                        if (i < list.size() - 1) {
069                            sb.append(", ");
070                        }
071                    }
072                }
073            }
074            sb.append("]");
075    
076            if (sb.length() > 2) {
077                return sb.toString();
078            } else {
079                return null;
080            }
081        }
082    
083    }