KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > test > mock > james > MockVirtualUserTableManagementImpl


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

19
20
21 package org.apache.james.test.mock.james;
22
23 import java.util.Collection JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.Map JavaDoc;
26
27 import org.apache.james.services.VirtualUserTable;
28 import org.apache.james.services.VirtualUserTableManagement;
29 import org.apache.james.util.VirtualUserTableUtil;
30 import org.apache.james.vut.ErrorMappingException;
31 import org.apache.james.vut.InvalidMappingException;
32
33 public class MockVirtualUserTableManagementImpl implements VirtualUserTableManagement {
34
35     HashMap JavaDoc store = new HashMap JavaDoc();
36     
37     public boolean addAddressMapping(String JavaDoc user, String JavaDoc domain, String JavaDoc address) throws InvalidMappingException {
38         return addRawMapping(user,domain,address);
39     }
40
41     public boolean addErrorMapping(String JavaDoc user, String JavaDoc domain, String JavaDoc error) throws InvalidMappingException {
42         return addRawMapping(user,domain,VirtualUserTable.ERROR_PREFIX + error);
43     }
44
45     public boolean addMapping(String JavaDoc user, String JavaDoc domain, String JavaDoc mapping) throws InvalidMappingException {
46         if (mapping.startsWith(VirtualUserTable.ERROR_PREFIX)){
47             return addErrorMapping(user,domain,mapping.substring(VirtualUserTable.ERROR_PREFIX.length()));
48         } else if (mapping.startsWith(VirtualUserTable.REGEX_PREFIX)) {
49             return addErrorMapping(user,domain,mapping.substring(VirtualUserTable.REGEX_PREFIX.length()));
50         } else {
51             return addAddressMapping(user,domain,mapping);
52         }
53     }
54
55     public boolean addRegexMapping(String JavaDoc user, String JavaDoc domain, String JavaDoc regex) throws InvalidMappingException {
56         return addRawMapping(user,domain,VirtualUserTable.REGEX_PREFIX + regex);
57     }
58
59     public Map JavaDoc getAllMappings() {
60         if (store.size() > 0) {
61             return store;
62         } else {
63             return null;
64         }
65     }
66
67     public Collection JavaDoc getUserDomainMappings(String JavaDoc user, String JavaDoc domain) throws InvalidMappingException {
68         String JavaDoc mapping = (String JavaDoc) store.get(user + "@" + domain);
69         if (mapping != null) {
70             return VirtualUserTableUtil.mappingToCollection(mapping);
71         } else {
72             return null;
73         }
74     }
75
76     public boolean removeAddressMapping(String JavaDoc user, String JavaDoc domain, String JavaDoc address) throws InvalidMappingException {
77         return removeRawMapping(user,domain,address);
78     }
79
80     public boolean removeErrorMapping(String JavaDoc user, String JavaDoc domain, String JavaDoc error) throws InvalidMappingException {
81         return removeRawMapping(user,domain,VirtualUserTable.ERROR_PREFIX + error);
82     }
83
84     public boolean removeMapping(String JavaDoc user, String JavaDoc domain, String JavaDoc mapping) throws InvalidMappingException {
85         if (mapping.startsWith(VirtualUserTable.ERROR_PREFIX)){
86             return removeErrorMapping(user,domain,mapping.substring(VirtualUserTable.ERROR_PREFIX.length()));
87         } else if (mapping.startsWith(VirtualUserTable.REGEX_PREFIX)) {
88             return removeErrorMapping(user,domain,mapping.substring(VirtualUserTable.REGEX_PREFIX.length()));
89         } else {
90             return removeAddressMapping(user,domain,mapping);
91         }
92     }
93
94     public boolean removeRegexMapping(String JavaDoc user, String JavaDoc domain, String JavaDoc regex) throws InvalidMappingException {
95         return removeRawMapping(user,domain,VirtualUserTable.REGEX_PREFIX + regex);
96     }
97
98     public Collection JavaDoc getMappings(String JavaDoc user, String JavaDoc domain) throws ErrorMappingException {
99         throw new UnsupportedOperationException JavaDoc("Not implemented yet");
100     }
101     
102     private boolean addRawMapping(String JavaDoc user,String JavaDoc domain, String JavaDoc mapping) {
103         Collection JavaDoc map;
104         String JavaDoc key = user + "@" + domain;
105         String JavaDoc mappings = (String JavaDoc) store.get(key);
106     
107         if (mappings != null) {
108             map = VirtualUserTableUtil.mappingToCollection(mappings);
109             
110             if (map.contains(mapping)) {
111                 return false;
112             } else {
113                 map.add(mapping);
114                 store.put(key, VirtualUserTableUtil.CollectionToMapping(map));
115                 return true;
116             }
117         } else {
118             store.put(key, mapping);
119             return true;
120         }
121     }
122     
123     private boolean removeRawMapping(String JavaDoc user,String JavaDoc domain, String JavaDoc mapping) {
124         Collection JavaDoc map;
125         String JavaDoc key = user + "@" + domain;
126         String JavaDoc mappings = (String JavaDoc) store.get(key);
127         if (mappings != null) {
128             map = VirtualUserTableUtil.mappingToCollection(mappings);
129             if (map.remove(mapping)) {
130                 store.put(key, VirtualUserTableUtil.CollectionToMapping(map));
131                 return true;
132             }
133         }
134         return false;
135     }
136
137     public boolean addAliasDomainMapping(String JavaDoc aliasDomain, String JavaDoc realDomain) throws InvalidMappingException {
138     return addRawMapping(null,aliasDomain,VirtualUserTable.ALIASDOMAIN_PREFIX + realDomain);
139     }
140
141     public boolean removeAliasDomainMapping(String JavaDoc aliasDomain, String JavaDoc realDomain) throws InvalidMappingException {
142         return removeRawMapping(null,aliasDomain,VirtualUserTable.ALIASDOMAIN_PREFIX + realDomain);
143     }
144
145 }
146
Popular Tags