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  package org.apache.james.jspf.impl;
21  
22  import org.apache.james.jspf.core.DNSRequest;
23  import org.apache.james.jspf.core.exceptions.TimeoutException;
24  import org.apache.james.jspf.executor.DNSAsynchLookupService;
25  import org.apache.james.jspf.executor.IResponse;
26  import org.apache.james.jspf.executor.IResponseQueue;
27  import org.xbill.DNS.DClass;
28  import org.xbill.DNS.ExtendedNonblockingResolver;
29  import org.xbill.DNS.LookupAsynch;
30  import org.xbill.DNS.Message;
31  import org.xbill.DNS.Name;
32  import org.xbill.DNS.Record;
33  import org.xbill.DNS.Resolver;
34  import org.xbill.DNS.TextParseException;
35  import org.xbill.DNS.Type;
36  
37  public class DNSJnioAsynchService implements DNSAsynchLookupService {
38  
39      private ExtendedNonblockingResolver resolver;
40  
41      public DNSJnioAsynchService(ExtendedNonblockingResolver resolver) {
42          this.resolver = resolver;
43          LookupAsynch.setDefaultResolver(resolver);
44      }
45      
46      /**
47       * Set the timeout for the resolvers
48       * @param timeout
49       */
50      public synchronized void setTimeout(int timeout) {
51          Resolver[] res = resolver.getResolvers();
52          for (int i = 0; i < res.length; i++) {
53              res[i].setTimeout(timeout);
54          }
55      }
56      
57      /**
58       * @see org.apache.james.jspf.executor.DNSAsynchLookupService#getRecordsAsynch(org.apache.james.jspf.core.DNSRequest, int, org.apache.james.jspf.executor.IResponseQueue)
59       */
60      public void getRecordsAsynch(DNSRequest request, int id,
61              IResponseQueue responsePool) {
62          
63          Message message;
64          try {
65              message = makeQuery(request, id);
66              LookupAsynch la = new LookupAsynch(message.getQuestion().getName(), message.getQuestion().getType());
67              la.runAsynch(new Runnable() {
68  
69                  private IResponseQueue responsePool;
70                  private Integer id;
71                  private LookupAsynch lookup;
72  
73                  public void run() {
74                      responsePool.insertResponse(new IResponse() {
75  
76                          public Exception getException() {
77                              if (lookup.getResult() == LookupAsynch.TRY_AGAIN) {
78                                  return new TimeoutException(lookup.getErrorString());
79                              } else {
80                                  return null;
81                              }
82                          }
83  
84                          public Object getId() {
85                              return id;
86                          }
87  
88                          public Object getValue() {
89                              return (DNSServiceXBillImpl.convertRecordsToList(lookup.getAnswers()));
90                          }
91                          
92                      });
93                  }
94  
95                  public Runnable setResponsePool(LookupAsynch la, IResponseQueue responsePool,
96                          Integer integer) {
97                      this.lookup = la;
98                      this.responsePool = responsePool;
99                      this.id = integer;
100                     return this;
101                 }
102                 
103             }.setResponsePool(la, responsePool, new Integer(id)));
104             // this.resolver.sendAsync(message, new Integer(id), new ResponseQueueAdaptor(responsePool));
105         } catch (TextParseException e) {
106             // TODO Auto-generated catch block
107             e.printStackTrace();
108         }
109     }
110 
111     private Message makeQuery(DNSRequest request, int id) throws TextParseException {
112         Name name = Name.fromString(request.getHostname(), Name.root);
113         
114         int type;
115         switch (request.getRecordType()) {
116             case DNSRequest.A: type = Type.A; break;
117             case DNSRequest.AAAA: type = Type.AAAA; break;
118             case DNSRequest.MX: type = Type.MX; break;
119             case DNSRequest.PTR: type = Type.PTR; break;
120             case DNSRequest.SPF: type = Type.SPF; break;
121             case DNSRequest.TXT: type = Type.TXT; break;
122             default: 
123                 throw new UnsupportedOperationException("Unknown query type: "+request.getRecordType());
124         }
125         
126         Record question = Record.newRecord(name, type, DClass.ANY);
127         Message query = Message.newQuery(question);
128         query.getHeader().setID(id);
129         return query;
130     }
131 }