View Javadoc

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  
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       * Holds the configured mappings
40       */
41      private Map mappings;
42      
43      private List domains;
44      
45      private final static String WILDCARD = "*";
46      
47      /**
48       * @see org.apache.avalon.framework.configuration.Configurable#configure(org.apache.avalon.framework.configuration.Configuration)
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          // Add domains of the mappings map to the domains List
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       * Not implemented
93       */
94      public boolean addMappingInternal(String user, String domain, String mapping) {
95          // Not supported
96          return false;
97      }
98  
99      /**
100      * @see org.apache.james.impl.vut.AbstractVirtualUserTable#mapAddressInternal(java.lang.String, java.lang.String)
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      * Not implemented
113      */
114     public boolean removeMappingInternal(String user, String domain, String mapping) {
115         // Not supported
116         return false;
117     }
118 
119     /**
120      * @see org.apache.james.impl.vut.AbstractVirtualUserTable#getUserDomainMappingsInternal(java.lang.String, java.lang.String)
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      * @see org.apache.james.impl.vut.AbstractVirtualUserTable#getDomainsInternal()
137      */
138     protected List getDomainsInternal() {
139         return domains;
140     }
141 
142     /**
143      * @see org.apache.james.api.domainlist.DomainList#containsDomain(java.lang.String)
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      * @see org.apache.james.impl.vut.AbstractVirtualUserTable#getAllMappingsInternal()
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 }