View Javadoc

1   /**
2    *
3    * Copyright 2003-2004 The Apache Software Foundation
4    *
5    *  Licensed under the Apache License, Version 2.0 (the "License");
6    *  you may not use this file except in compliance with the License.
7    *  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;
20  import java.io.ByteArrayInputStream;
21  import java.io.IOException;
22  import java.util.Arrays;
23  import java.util.HashMap;
24  import java.util.HashSet;
25  import java.util.Iterator;
26  import java.util.Map;
27  import java.util.Properties;
28  import java.util.Set;
29  
30  import org.apache.geronimo.common.propertyeditor.PropertyEditorException;
31  import org.apache.geronimo.common.propertyeditor.TextPropertyEditorSupport;
32  
33  /**
34   * @version $Rev: 355877 $ $Date: 2005-12-10 18:48:27 -0800 (Sat, 10 Dec 2005) $
35   */
36  public class MapOfSets extends HashMap {
37  
38      public MapOfSets() {
39          super();
40      }
41  
42      public MapOfSets(int size) {
43          super(size);
44      }
45  
46      public MapOfSets(Map 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 text) {
57              if (text != null) {
58                  try {
59                      ByteArrayInputStream is = new ByteArrayInputStream(text.getBytes());
60                      Properties p = new Properties();
61                      p.load(is);
62  
63                      Map result = new MapOfSets(p.size());
64                      for (Iterator iterator = p.entrySet().iterator(); iterator.hasNext();) {
65                          Map.Entry entry = (Map.Entry) iterator.next();
66                          Set values = new HashSet(Arrays.asList(((String) entry.getValue()).split(",")));
67                          result.put(entry.getKey(), values);
68                      }
69                      setValue(result);
70                  } catch (IOException e) {
71                      throw new PropertyEditorException(e);
72                  }
73              } else {
74                  setValue(null);
75              }
76          }
77  
78          public String getAsText() {
79              Map map = (Map) getValue();
80              if (map == null) {
81                  return null;
82              }
83              StringBuffer text = new StringBuffer();
84              for (Iterator iterator = map.entrySet().iterator(); iterator.hasNext();) {
85                  Map.Entry entry = (Map.Entry) iterator.next();
86                  text.append(entry.getKey()).append("=");
87                  Set values = (Set) entry.getValue();
88                  for (Iterator iterator1 = values.iterator(); iterator1.hasNext();) {
89                      String value = (String) iterator1.next();
90                      text.append(value);
91                      if (iterator1.hasNext()) {
92                          text.append(",");
93                      }
94                  }
95              }
96              return text.toString();
97          }
98  
99      }
100 }