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.geronimo.security.deploy;
018    
019    import java.beans.PropertyEditorManager;
020    import java.io.ByteArrayInputStream;
021    import java.io.IOException;
022    import java.util.Arrays;
023    import java.util.HashMap;
024    import java.util.HashSet;
025    import java.util.Iterator;
026    import java.util.Map;
027    import java.util.Properties;
028    import java.util.Set;
029    
030    import org.apache.geronimo.common.propertyeditor.PropertyEditorException;
031    import org.apache.geronimo.common.propertyeditor.TextPropertyEditorSupport;
032    
033    /**
034     * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
035     */
036    public class MapOfSets extends HashMap {
037    
038        public MapOfSets() {
039            super();
040        }
041    
042        public MapOfSets(int size) {
043            super(size);
044        }
045    
046        public MapOfSets(Map map) {
047            super(map);
048        }
049    
050        static {
051            PropertyEditorManager.registerEditor(MapOfSets.class, MapOfSetsEditor.class);
052        }
053    
054        public static class MapOfSetsEditor extends TextPropertyEditorSupport {
055    
056            public void setAsText(String text) {
057                if (text != null) {
058                    try {
059                        ByteArrayInputStream is = new ByteArrayInputStream(text.getBytes());
060                        Properties p = new Properties();
061                        p.load(is);
062    
063                        Map result = new MapOfSets(p.size());
064                        for (Iterator iterator = p.entrySet().iterator(); iterator.hasNext();) {
065                            Map.Entry entry = (Map.Entry) iterator.next();
066                            Set values = new HashSet(Arrays.asList(((String) entry.getValue()).split(",")));
067                            result.put(entry.getKey(), values);
068                        }
069                        setValue(result);
070                    } catch (IOException e) {
071                        throw new PropertyEditorException(e);
072                    }
073                } else {
074                    setValue(null);
075                }
076            }
077    
078            public String getAsText() {
079                Map map = (Map) getValue();
080                if (map == null) {
081                    return null;
082                }
083                StringBuffer text = new StringBuffer();
084                for (Iterator iterator = map.entrySet().iterator(); iterator.hasNext();) {
085                    Map.Entry entry = (Map.Entry) iterator.next();
086                    text.append(entry.getKey()).append("=");
087                    Set values = (Set) entry.getValue();
088                    for (Iterator iterator1 = values.iterator(); iterator1.hasNext();) {
089                        String value = (String) iterator1.next();
090                        text.append(value);
091                        if (iterator1.hasNext()) {
092                            text.append(",");
093                        }
094                    }
095                }
096                return text.toString();
097            }
098    
099        }
100    }