1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
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
49
50 public void service(ServiceManager arg0) throws ServiceException {
51 dns = (DNSService) arg0.lookup(DNSService.ROLE);
52 }
53
54
55
56
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
94
95
96
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
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
135
136 public synchronized boolean addDomain(String domain) {
137 getLogger().info("Add domain " + domain + " to DomainList");
138
139
140 return addDomainInternal(domain);
141 }
142
143
144
145
146 public synchronized boolean removeDomain(String domain) {
147 getLogger().info("Remove domain " + domain + " from DomainList");
148
149
150
151 return removeDomainInternal(domain);
152 }
153
154
155
156
157 public synchronized void setAutoDetect(boolean autoDetect) {
158 getLogger().info("Set autodetect to: " + autoDetect);
159 this.autoDetect = autoDetect;
160 }
161
162
163
164
165 public synchronized void setAutoDetectIP(boolean autoDetectIP) {
166 getLogger().info("Set autodetectIP to: " + autoDetectIP);
167 this.autoDetectIP = autoDetectIP;
168 }
169
170
171
172
173
174
175 protected DNSService getDNSServer() {
176 return dns;
177 }
178
179
180
181
182
183
184 protected abstract List getDomainListInternal();
185
186
187
188
189
190
191
192 protected abstract boolean addDomainInternal(String domain);
193
194
195
196
197
198
199
200 protected abstract boolean removeDomainInternal(String domain);
201 }