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.domain;
23  
24  import java.net.InetAddress;
25  import java.net.UnknownHostException;
26  import java.util.ArrayList;
27  import java.util.Iterator;
28  import java.util.List;
29  import java.util.Locale;
30  
31  import org.apache.avalon.framework.logger.AbstractLogEnabled;
32  import org.apache.avalon.framework.logger.Logger;
33  import org.apache.avalon.framework.service.ServiceException;
34  import org.apache.avalon.framework.service.ServiceManager;
35  import org.apache.avalon.framework.service.Serviceable;
36  import org.apache.james.api.dnsservice.DNSService;
37  import org.apache.james.api.domainlist.ManageableDomainList;
38  
39  /**
40   * All implementations of the DomainList interface should extends this abstract class
41   */
42  public abstract class AbstractDomainList extends AbstractLogEnabled implements Serviceable, ManageableDomainList {
43      private DNSService dns;
44      private boolean autoDetect = true;
45      private boolean autoDetectIP = true;
46  
47      /**
48       * @see org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager)
49       */
50      public void service(ServiceManager arg0) throws ServiceException {
51          dns = (DNSService) arg0.lookup(DNSService.ROLE);
52      }
53      
54  
55      /**
56       * @see org.apache.james.api.domainlist.DomainList#getDomains()
57       */
58      public List getDomains() {  
59          List domains = getDomainListInternal();
60          if (domains != null) {
61              
62              String hostName = null;
63              try {
64                  hostName = getDNSServer().getHostName(getDNSServer().getLocalHost());
65              } catch  (UnknownHostException ue) {
66                  hostName = "localhost";
67              }
68              
69              getLogger().info("Local host is: " + hostName);
70              
71              if (autoDetect == true && (!hostName.equals("localhost"))) {
72                  domains.add(hostName.toLowerCase(Locale.US));
73              }
74  
75              
76              if (autoDetectIP == true) {
77                  domains.addAll(getDomainsIP(domains,dns,getLogger()));
78              }
79         
80              if (getLogger().isInfoEnabled()) {
81                  for (Iterator i = domains.iterator(); i.hasNext(); ) {
82                      getLogger().debug("Handling mail for: " + i.next());
83                  }
84              }  
85              return domains;
86          } else {
87              return null;
88          }
89      }
90      
91      
92      /**
93       * Return a List which holds all ipAddress of the domains in the given List
94       * 
95       * @param domains List of domains
96       * @return domainIP List of ipaddress for domains
97       */
98      private static List getDomainsIP(List domains,DNSService dns,Logger log) {
99          List domainIP = new ArrayList();
100         if (domains.size() > 0 ) {
101             for (int i = 0; i < domains.size(); i++) {
102                 List domList = getDomainIP(domains.get(i).toString(),dns,log);
103                 
104                 for(int i2 = 0; i2 < domList.size();i2++) {
105                     if(domainIP.contains(domList.get(i2)) == false) {
106                         domainIP.add(domList.get(i2));
107                     }
108                 }
109             }
110         }
111         return domainIP;    
112     }
113     
114     /**
115      * @see #getDomainsIP(List, DNSService, Logger)
116      */
117     private static List getDomainIP(String domain, DNSService dns, Logger log) {
118         List domainIP = new ArrayList();
119         try {
120             InetAddress[]  addrs = dns.getAllByName(domain);
121             for (int j = 0; j < addrs.length ; j++) {
122                 String ip = addrs[j].getHostAddress();
123                 if (domainIP.contains(ip) == false) {
124                     domainIP.add(ip);
125                 }
126             }
127         } catch (UnknownHostException e) {
128             log.error("Cannot get IP address(es) for " + domain);
129         }
130         return domainIP;
131     }
132     
133     /**
134      * @see org.apache.james.api.domainlist.ManageableDomainList#addDomain(java.lang.String)
135      */
136     public synchronized boolean addDomain(String domain) {
137         getLogger().info("Add domain " + domain + " to DomainList");
138     
139         //TODO: Should we care about autoDetectIP ?
140         return addDomainInternal(domain);
141     }
142     
143     /**
144      * @see org.apache.james.api.domainlist.ManageableDomainList#removeDomain(java.lang.String)
145      */
146     public synchronized boolean removeDomain(String domain) {
147         getLogger().info("Remove domain " + domain + " from DomainList");
148     
149     
150         //TODO: Should we care about autoDetectIP ?
151         return removeDomainInternal(domain);
152     }
153     
154     /**
155      * @see org.apache.james.api.domainlist.DomainList#setAutoDetect(boolean)
156      */
157     public synchronized void setAutoDetect(boolean autoDetect) {
158         getLogger().info("Set autodetect to: " + autoDetect);
159         this.autoDetect = autoDetect;
160     }
161     
162     /**
163      * @see org.apache.james.api.domainlist.DomainList#setAutoDetectIP(boolean)
164      */
165     public synchronized void setAutoDetectIP(boolean autoDetectIP) {
166         getLogger().info("Set autodetectIP to: " + autoDetectIP);
167         this.autoDetectIP = autoDetectIP;
168     }
169     
170     /**
171      * Return dnsServer
172      * 
173      * @return dns
174      */
175     protected DNSService getDNSServer() {
176         return dns;
177     }
178     
179     /**
180      * Return domainList
181      * 
182      * @return List
183      */
184     protected abstract List getDomainListInternal();
185     
186     /**
187      * Add domain
188      * 
189      * @param domain domain to add
190      * @return true if successfully
191      */
192     protected abstract boolean addDomainInternal(String domain);
193     
194     /**
195      * Remove domain
196      * 
197      * @param domain domain to remove
198      * @return true if successfully
199      */
200     protected abstract boolean removeDomainInternal(String domain);
201 }