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  package org.apache.james.jspf;
21  
22  import org.apache.james.jspf.core.DNSRequest;
23  import org.apache.james.jspf.core.DNSService;
24  import org.apache.james.jspf.core.Logger;
25  import org.apache.james.jspf.core.exceptions.TimeoutException;
26  
27  import java.util.List;
28  
29  public class LoggingDNSService implements DNSService {
30  
31      private DNSService dnsService;
32      private Logger logger;
33  
34      public LoggingDNSService(DNSService service, Logger logger) {
35          this.dnsService = service;
36          this.logger = logger;
37      }
38      
39      public int getRecordLimit() {
40          return dnsService.getRecordLimit();
41      }
42  
43      public void setRecordLimit(int recordLimit) {
44          dnsService.setRecordLimit(recordLimit);
45      }
46  
47      public List getLocalDomainNames() {
48          List res = dnsService.getLocalDomainNames();
49          StringBuffer logBuff = new StringBuffer();
50          logBuff.append("getLocalDomainNames() = ");
51          if (res != null) {
52              for (int i = 0; i < res.size(); i++) {
53                  logBuff.append(res.get(i));
54                  if (i == res.size() - 1) {
55                      logBuff.append("");
56                  } else {
57                      logBuff.append(",");
58                  }
59              }
60          } else {
61              logBuff.append("getLocalDomainNames-ret: null");
62          }
63          logger.debug(logBuff.toString());
64          return res;
65  
66      }
67  
68      public void setTimeOut(int timeOut) {
69          dnsService.setTimeOut(timeOut);
70      }
71  
72      public List getRecords(DNSRequest request) throws TimeoutException {
73          try {
74              List result = dnsService.getRecords(request);
75              StringBuffer logBuff = new StringBuffer();
76              logBuff.append("getRecords(" + request.getHostname() + "," + request.getRecordType() + ") = ");
77              if (result != null) {
78                  for (int i = 0; i < result.size(); i++) {
79                      logBuff.append(result.get(i));
80                      if (i == result.size() - 1) {
81                          logBuff.append("");
82                      } else {
83                          logBuff.append(",");
84                      }
85                  }
86              } else {
87                  logBuff.append("getRecords-ret: null");
88              }
89              logger.debug(logBuff.toString());
90              return result;
91          } catch (TimeoutException e) {
92              logger.debug("getRecords(" + request.getHostname()
93                      + ") = TempErrorException[" + e.getMessage() + "]");
94              throw e;
95          }
96      }
97  
98  }