1 17 18 package org.apache.james.mailrepository; 19 20 import org.apache.james.core.MailImpl; 21 import org.apache.james.services.SpoolRepository; 22 import org.apache.mailet.Mail; 23 24 import java.util.ConcurrentModificationException ; 25 import java.util.Iterator ; 26 27 38 public class AvalonSpoolRepository 39 extends AvalonMailRepository 40 implements SpoolRepository { 41 42 51 public synchronized Mail accept() throws InterruptedException { 52 if ((DEEP_DEBUG) && (getLogger().isDebugEnabled())) { 53 getLogger().debug("Method accept() called"); 54 } 55 return accept(new SpoolRepository.AcceptFilter () { 56 public boolean accept (String _, String __, long ___, String ____) { 57 return true; 58 } 59 60 public long getWaitTime () { 61 return 0; 62 } 63 }); 64 } 65 66 77 public synchronized Mail accept(final long delay) throws InterruptedException 78 { 79 if ((DEEP_DEBUG) && (getLogger().isDebugEnabled())) { 80 getLogger().debug("Method accept(delay) called"); 81 } 82 return accept(new SpoolRepository.AcceptFilter () { 83 long youngest = 0; 84 85 public boolean accept (String key, String state, long lastUpdated, String errorMessage) { 86 if (state.equals(Mail.ERROR)) { 87 long timeToProcess = delay + lastUpdated; 89 90 if (System.currentTimeMillis() > timeToProcess) { 91 return true; 93 } else { 94 if (youngest == 0 || youngest > timeToProcess) { 96 youngest = timeToProcess; 98 } 99 return false; 100 } 101 } else { 102 return true; 104 } 105 } 106 107 public long getWaitTime () { 108 if (youngest == 0) { 109 return 0; 110 } else { 111 long duration = youngest - System.currentTimeMillis(); 112 youngest = 0; return duration <= 0 ? 1 : duration; 114 } 115 } 116 }); 117 } 118 119 120 132 public synchronized Mail accept(SpoolRepository.AcceptFilter filter) throws InterruptedException { 133 if ((DEEP_DEBUG) && (getLogger().isDebugEnabled())) { 134 getLogger().debug("Method accept(Filter) called"); 135 } 136 while (!Thread.currentThread().isInterrupted()) try { 137 for (Iterator it = list(); it.hasNext(); ) { 138 String s = it.next().toString(); 139 if ((DEEP_DEBUG) && (getLogger().isDebugEnabled())) { 140 StringBuffer logBuffer = 141 new StringBuffer (64) 142 .append("Found item ") 143 .append(s) 144 .append(" in spool."); 145 getLogger().debug(logBuffer.toString()); 146 } 147 if (lock(s)) { 148 if ((DEEP_DEBUG) && (getLogger().isDebugEnabled())) { 149 getLogger().debug("accept(Filter) has locked: " + s); 150 } 151 try { 152 MailImpl mail = retrieve(s); 153 if (mail == null || !filter.accept (mail.getName(), 157 mail.getState(), 158 mail.getLastUpdated().getTime(), 159 mail.getErrorMessage())) { 160 unlock(s); 161 continue; 162 } 163 return mail; 164 } catch (javax.mail.MessagingException e) { 165 unlock(s); 166 getLogger().error("Exception during retrieve -- skipping item " + s, e); 167 } 168 } 169 } 170 171 wait (filter.getWaitTime()); 173 } catch (InterruptedException ex) { 174 throw ex; 175 } catch (ConcurrentModificationException cme) { 176 getLogger().error("CME in spooler - please report to http://james.apache.org", cme); 178 } 179 throw new InterruptedException (); 180 } 181 182 } 183 | Popular Tags |