01: /****************************************************************
02: * Licensed to the Apache Software Foundation (ASF) under one *
03: * or more contributor license agreements. See the NOTICE file *
04: * distributed with this work for additional information *
05: * regarding copyright ownership. The ASF licenses this file *
06: * to you under the Apache License, Version 2.0 (the *
07: * "License"); you may not use this file except in compliance *
08: * with the License. You may obtain a copy of the License at *
09: * *
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: ****************************************************************/package org.apache.james.jspf.executor;
19:
20: import org.apache.james.jspf.core.DNSLookupContinuation;
21: import org.apache.james.jspf.core.DNSResponse;
22: import org.apache.james.jspf.core.DNSService;
23: import org.apache.james.jspf.core.Logger;
24: import org.apache.james.jspf.core.SPFChecker;
25: import org.apache.james.jspf.core.SPFCheckerExceptionCatcher;
26: import org.apache.james.jspf.core.SPFSession;
27: import org.apache.james.jspf.core.exceptions.SPFResultException;
28: import org.apache.james.jspf.core.exceptions.TimeoutException;
29:
30: /**
31: * Synchronous implementation of SPFExecuter. All queries will get executed synchronously
32: */
33: public class SynchronousSPFExecutor implements SPFExecutor {
34:
35: private Logger log;
36: private DNSService dnsProbe;
37:
38: public SynchronousSPFExecutor(Logger log, DNSService service) {
39: this .log = log;
40: this .dnsProbe = service;
41: }
42:
43: /**
44: * @see org.apache.james.jspf.executor.SPFExecutor#execute(org.apache.james.jspf.core.SPFSession, org.apache.james.jspf.executor.FutureSPFResult)
45: */
46: public void execute(SPFSession session, FutureSPFResult result) {
47: SPFChecker checker;
48: while ((checker = session.popChecker()) != null) {
49: // only execute checkers we added (better recursivity)
50: log.debug("Executing checker: " + checker);
51: try {
52: DNSLookupContinuation cont = checker.checkSPF(session);
53: // if the checker returns a continuation we return it
54: while (cont != null) {
55: DNSResponse response;
56: try {
57: response = new DNSResponse(dnsProbe
58: .getRecords(cont.getRequest()));
59: } catch (TimeoutException e) {
60: response = new DNSResponse(e);
61: }
62: cont = cont.getListener().onDNSResponse(response,
63: session);
64: }
65: } catch (Exception e) {
66: while (e != null) {
67: while (checker == null
68: || !(checker instanceof SPFCheckerExceptionCatcher)) {
69: checker = session.popChecker();
70: }
71: try {
72: ((SPFCheckerExceptionCatcher) checker)
73: .onException(e, session);
74: e = null;
75: } catch (SPFResultException ex) {
76: e = ex;
77: } finally {
78: checker = null;
79: }
80: }
81: }
82: }
83: result.setSPFResult(session);
84: }
85:
86: }
|