KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > security > deploy > MapOfSets


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.geronimo.security.deploy;
18
19 import java.beans.PropertyEditorManager JavaDoc;
20 import java.io.ByteArrayInputStream JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.util.Arrays JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.HashSet JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.Map JavaDoc;
27 import java.util.Properties JavaDoc;
28 import java.util.Set JavaDoc;
29
30 import org.apache.geronimo.common.propertyeditor.PropertyEditorException;
31 import org.apache.geronimo.common.propertyeditor.TextPropertyEditorSupport;
32
33 /**
34  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
35  */

36 public class MapOfSets extends HashMap JavaDoc {
37
38     public MapOfSets() {
39         super();
40     }
41
42     public MapOfSets(int size) {
43         super(size);
44     }
45
46     public MapOfSets(Map JavaDoc map) {
47         super(map);
48     }
49
50     static {
51         PropertyEditorManager.registerEditor(MapOfSets.class, MapOfSetsEditor.class);
52     }
53
54     public static class MapOfSetsEditor extends TextPropertyEditorSupport {
55
56         public void setAsText(String JavaDoc text) {
57             if (text != null) {
58                 try {
59                     ByteArrayInputStream JavaDoc is = new ByteArrayInputStream JavaDoc(text.getBytes());
60                     Properties JavaDoc p = new Properties JavaDoc();
61                     p.load(is);
62
63                     Map JavaDoc result = new MapOfSets(p.size());
64                     for (Iterator JavaDoc iterator = p.entrySet().iterator(); iterator.hasNext();) {
65                         Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iterator.next();
66                         Set JavaDoc values = new HashSet JavaDoc(Arrays.asList(((String JavaDoc) entry.getValue()).split(",")));
67                         result.put(entry.getKey(), values);
68                     }
69                     setValue(result);
70                 } catch (IOException JavaDoc e) {
71                     throw new PropertyEditorException(e);
72                 }
73             } else {
74                 setValue(null);
75             }
76         }
77
78         public String JavaDoc getAsText() {
79             Map JavaDoc map = (Map JavaDoc) getValue();
80             if (map == null) {
81                 return null;
82             }
83             StringBuffer JavaDoc text = new StringBuffer JavaDoc();
84             for (Iterator JavaDoc iterator = map.entrySet().iterator(); iterator.hasNext();) {
85                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iterator.next();
86                 text.append(entry.getKey()).append("=");
87                 Set JavaDoc values = (Set JavaDoc) entry.getValue();
88                 for (Iterator JavaDoc iterator1 = values.iterator(); iterator1.hasNext();) {
89                     String JavaDoc value = (String JavaDoc) iterator1.next();
90                     text.append(value);
91                     if (iterator1.hasNext()) {
92                         text.append(",");
93                     }
94                 }
95             }
96             return text.toString();
97         }
98
99     }
100 }
101
Popular Tags