KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > vut > XMLVirtualUserTableTest


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.vut;
22
23 import java.util.ArrayList JavaDoc;
24 import java.util.Collection JavaDoc;
25
26
27 import org.apache.avalon.cornerstone.services.datasources.DataSourceSelector;
28 import org.apache.avalon.framework.configuration.Configuration;
29 import org.apache.avalon.framework.configuration.ConfigurationException;
30 import org.apache.avalon.framework.configuration.DefaultConfiguration;
31 import org.apache.avalon.framework.container.ContainerUtil;
32 import org.apache.avalon.framework.service.DefaultServiceManager;
33
34 import org.apache.james.services.DNSServer;
35 import org.apache.james.services.FileSystem;
36 import org.apache.james.services.VirtualUserTable;
37
38 import org.apache.james.test.mock.avalon.MockLogger;
39
40 import org.apache.james.test.mock.james.MockFileSystem;
41 import org.apache.james.test.mock.util.AttrValConfiguration;
42
43 import org.apache.james.test.util.Util;
44 import org.apache.james.util.VirtualUserTableUtil;
45
46 public class XMLVirtualUserTableTest extends AbstractVirtualUserTableTest {
47     DefaultConfiguration defaultConfiguration = new DefaultConfiguration("conf");
48     
49     protected AbstractVirtualUserTable getVirtalUserTable() throws Exception JavaDoc {
50         DefaultServiceManager serviceManager = new DefaultServiceManager();
51         serviceManager.put(FileSystem.ROLE, new MockFileSystem());
52         serviceManager.put(DataSourceSelector.ROLE, Util.getDataSourceSelector());
53         serviceManager.put(DNSServer.ROLE, setUpDNSServer());
54         XMLVirtualUserTable mr = new XMLVirtualUserTable();
55         ContainerUtil.enableLogging(mr, new MockLogger());
56
57         ContainerUtil.service(mr, serviceManager);
58         return mr;
59     }
60     
61     
62
63     /**
64      * @see org.apache.james.vut.AbstractVirtualUserTableTest#addMapping(java.lang.String, java.lang.String, java.lang.String, int)
65      */

66     protected boolean addMapping(String JavaDoc user, String JavaDoc domain, String JavaDoc mapping, int type) throws InvalidMappingException {
67         if (user == null) user = "*";
68         if (domain == null) domain = "*";
69         
70         Collection JavaDoc mappings = virtualUserTable.getUserDomainMappings(user, domain);
71
72         if (mappings == null) {
73             mappings = new ArrayList JavaDoc();
74         } else {
75            removeMappings(user,domain,mappings);
76         }
77     
78         if (type == ERROR_TYPE) {
79             mappings.add(VirtualUserTable.ERROR_PREFIX + mapping);
80         } else if (type == REGEX_TYPE) {
81             mappings.add(VirtualUserTable.REGEX_PREFIX + mapping);
82         } else if (type == ADDRESS_TYPE) {
83             mappings.add(mapping);
84         } else if (type == ALIASDOMAIN_TYPE) {
85             mappings.add(VirtualUserTable.ALIASDOMAIN_PREFIX + mapping);
86         }
87         
88         if (mappings.size() > 0) {
89             defaultConfiguration.addChild(new AttrValConfiguration("mapping",user + "@" + domain +"=" + VirtualUserTableUtil.CollectionToMapping(mappings)));
90         }
91     
92         try {
93             ContainerUtil.configure(((XMLVirtualUserTable) virtualUserTable),defaultConfiguration);
94         } catch (ConfigurationException e) {
95             if (mappings.size() > 0) {
96                 return false;
97             } else {
98                 return true;
99             }
100         }
101         return true;
102     }
103
104     /**
105      * @see org.apache.james.vut.AbstractVirtualUserTableTest#removeMapping(java.lang.String, java.lang.String, java.lang.String, int)
106      */

107     protected boolean removeMapping(String JavaDoc user, String JavaDoc domain, String JavaDoc mapping, int type) throws InvalidMappingException {
108         if (user == null) user = "*";
109         if (domain == null) domain = "*";
110         
111         Collection JavaDoc mappings = virtualUserTable.getUserDomainMappings(user, domain);
112         
113         if (mappings == null) {
114             return false;
115         }
116     
117         removeMappings(user,domain, mappings);
118     
119         if (type == ERROR_TYPE) {
120             mappings.remove(VirtualUserTable.ERROR_PREFIX + mapping);
121         } else if (type == REGEX_TYPE) {
122             mappings.remove(VirtualUserTable.REGEX_PREFIX + mapping);
123         } else if (type == ADDRESS_TYPE) {
124             mappings.remove(mapping);
125         } else if (type == ALIASDOMAIN_TYPE) {
126             mappings.remove(VirtualUserTable.ALIASDOMAIN_PREFIX + mapping);
127         }
128
129         if (mappings.size() > 0) {
130             defaultConfiguration.addChild(new AttrValConfiguration("mapping",user + "@" + domain +"=" + VirtualUserTableUtil.CollectionToMapping(mappings)));
131         }
132     
133         try {
134             ContainerUtil.configure(((XMLVirtualUserTable) virtualUserTable),defaultConfiguration);
135         } catch (ConfigurationException e) {
136            if (mappings.size() > 0) {
137                return false;
138            } else {
139                return true;
140            }
141         }
142         return true;
143     }
144     
145     
146     private void removeMappings(String JavaDoc user, String JavaDoc domain, Collection JavaDoc mappings) {
147         Configuration [] conf = defaultConfiguration.getChildren("mapping");
148         
149         for (int i = 0; i < conf.length; i++ ) {
150             DefaultConfiguration c = (DefaultConfiguration) conf[i];
151             try {
152                 String JavaDoc mapping = user + "@" + domain + "=" + VirtualUserTableUtil.CollectionToMapping(mappings);
153             
154             
155                 if (c.getValue().equalsIgnoreCase(mapping)){
156                     defaultConfiguration.removeChild(c);
157                 }
158             } catch (ConfigurationException e) {
159                 e.printStackTrace();
160             }
161         }
162     }
163
164 }
165
Popular Tags