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