1 19 20 21 package org.apache.james.vut; 22 23 import java.util.ArrayList ; 24 import java.util.Collection ; 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 { 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 66 protected boolean addMapping(String user, String domain, String mapping, int type) throws InvalidMappingException { 67 if (user == null) user = "*"; 68 if (domain == null) domain = "*"; 69 70 Collection mappings = virtualUserTable.getUserDomainMappings(user, domain); 71 72 if (mappings == null) { 73 mappings = new ArrayList (); 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 107 protected boolean removeMapping(String user, String domain, String mapping, int type) throws InvalidMappingException { 108 if (user == null) user = "*"; 109 if (domain == null) domain = "*"; 110 111 Collection 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 user, String domain, Collection 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 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 |