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.executor;
21
22 import org.apache.james.jspf.core.DNSLookupContinuation;
23 import org.apache.james.jspf.core.DNSResponse;
24 import org.apache.james.jspf.core.DNSService;
25 import org.apache.james.jspf.core.Logger;
26 import org.apache.james.jspf.core.SPFChecker;
27 import org.apache.james.jspf.core.SPFCheckerExceptionCatcher;
28 import org.apache.james.jspf.core.SPFSession;
29 import org.apache.james.jspf.core.exceptions.SPFResultException;
30 import org.apache.james.jspf.core.exceptions.TimeoutException;
31
32 /**
33 * Synchronous implementation of SPFExecuter. All queries will get executed synchronously
34 */
35 public class SynchronousSPFExecutor implements SPFExecutor {
36
37 private Logger log;
38 private DNSService dnsProbe;
39
40 public SynchronousSPFExecutor(Logger log, DNSService service) {
41 this.log = log;
42 this.dnsProbe = service;
43 }
44
45 /**
46 * @see org.apache.james.jspf.executor.SPFExecutor#execute(org.apache.james.jspf.core.SPFSession, org.apache.james.jspf.executor.FutureSPFResult)
47 */
48 public void execute(SPFSession session, FutureSPFResult result) {
49 SPFChecker checker;
50 while ((checker = session.popChecker()) != null) {
51 // only execute checkers we added (better recursivity)
52 log.debug("Executing checker: " + checker);
53 try {
54 DNSLookupContinuation cont = checker.checkSPF(session);
55 // if the checker returns a continuation we return it
56 while (cont != null) {
57 DNSResponse response;
58 try {
59 response = new DNSResponse(dnsProbe.getRecords(cont
60 .getRequest()));
61 } catch (TimeoutException e) {
62 response = new DNSResponse(e);
63 }
64 cont = cont.getListener().onDNSResponse(response, session);
65 }
66 } catch (Exception e) {
67 while (e != null) {
68 while (checker == null || !(checker instanceof SPFCheckerExceptionCatcher)) {
69 checker = session.popChecker();
70 }
71 try {
72 ((SPFCheckerExceptionCatcher) checker).onException(e, session);
73 e = null;
74 } catch (SPFResultException ex) {
75 e = ex;
76 } finally {
77 checker = null;
78 }
79 }
80 }
81 }
82 result.setSPFResult(session);
83 }
84
85 }