1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.apache.james.vut;
23
24 import java.util.ArrayList;
25 import java.util.Collection;
26 import java.util.HashMap;
27 import java.util.Iterator;
28 import java.util.List;
29 import java.util.Map;
30
31 import org.apache.avalon.framework.configuration.Configurable;
32 import org.apache.avalon.framework.configuration.Configuration;
33 import org.apache.avalon.framework.configuration.ConfigurationException;
34 import org.apache.james.impl.vut.AbstractVirtualUserTable;
35 import org.apache.james.impl.vut.VirtualUserTableUtil;
36
37 public class XMLVirtualUserTable extends AbstractVirtualUserTable implements Configurable {
38
39
40
41 private Map mappings;
42
43 private List domains;
44
45 private final static String WILDCARD = "*";
46
47
48
49
50 public void configure(Configuration arg0) throws ConfigurationException {
51 super.configure(arg0);
52 Configuration[] mapConf = arg0.getChildren("mapping");
53
54 mappings = new HashMap();
55 domains = new ArrayList();
56
57 if (mapConf != null && mapConf.length > 0) {
58 for (int i = 0; i < mapConf.length; i ++) {
59 mappings.putAll(VirtualUserTableUtil.getXMLMappings(mapConf[i].getValue()));
60 }
61 } else {
62 throw new ConfigurationException("No mapping configured");
63 }
64
65
66 Iterator keys = mappings.keySet().iterator();
67
68 while (keys.hasNext()) {
69 String key = keys.next().toString();
70
71 String[] args1 = key.split("@");
72 if (args1 != null && args1.length > 1) {
73 String domain = args1[1].toLowerCase();
74 if (domains.contains(domain) == false && domain.equals(WILDCARD) == false) {
75 domains.add(domain);
76 }
77 }
78 }
79
80 Configuration autoConf = arg0.getChild("autodetect");
81 if (autoConf != null) {
82 setAutoDetect(autoConf.getValueAsBoolean(true));
83 }
84
85 Configuration autoIPConf = arg0.getChild("autodetectIP");
86 if (autoConf != null) {
87 setAutoDetectIP(autoIPConf.getValueAsBoolean(true));
88 }
89 }
90
91
92
93
94 public boolean addMappingInternal(String user, String domain, String mapping) {
95
96 return false;
97 }
98
99
100
101
102 protected String mapAddressInternal(String user, String domain) {
103 if (mappings == null) {
104 return null;
105 } else {
106 return VirtualUserTableUtil.getTargetString(user, domain, mappings);
107
108 }
109 }
110
111
112
113
114 public boolean removeMappingInternal(String user, String domain, String mapping) {
115
116 return false;
117 }
118
119
120
121
122 public Collection getUserDomainMappingsInternal(String user, String domain) {
123 if (mappings == null) {
124 return null;
125 } else {
126 String maps = (String) mappings.get(user + "@" + domain);
127 if (maps != null) {
128 return VirtualUserTableUtil.mappingToCollection(maps);
129 } else {
130 return null;
131 }
132 }
133 }
134
135
136
137
138 protected List getDomainsInternal() {
139 return domains;
140 }
141
142
143
144
145 public boolean containsDomain(String domain) {
146 if (domains == null) {
147 return false;
148 } else {
149 return domains.contains(domain);
150 }
151 }
152
153
154
155
156 public Map getAllMappingsInternal() {
157 if ( mappings != null && mappings.size() > 0) {
158 Map mappingsNew = new HashMap();
159 Iterator maps = mappings.keySet().iterator();
160
161 while (maps.hasNext()) {
162 String key = maps.next().toString();
163 mappingsNew.put(key, VirtualUserTableUtil.mappingToCollection(mappings.get(key).toString()));
164 }
165 return mappingsNew;
166 } else {
167 return null;
168 }
169 }
170 }