Clover Coverage Report - Atlassian Mail
Coverage timestamp: Mon Sep 29 2008 21:26:36 CDT
47   168   21   2.94
8   131   0.45   16
16     1.31  
1    
 
 
  MailQueueImpl       Line # 26 47 21 0% 0.0
 
No Tests
 
1    /**
2    * Created by IntelliJ IDEA.
3    * User: Mike Cannon-Brookes
4    * Date: Jan 22, 2003
5    * Time: 12:39:32 AM
6    * To change this template use Options | File Templates.
7    */
8    package com.atlassian.mail.queue;
9   
10    import com.atlassian.mail.MailException;
11    import org.apache.commons.collections.Buffer;
12    import org.apache.commons.collections.BufferUtils;
13    import org.apache.commons.collections.UnboundedFifoBuffer;
14    import org.apache.log4j.Category;
15   
16    import java.sql.Timestamp;
17    import java.util.ArrayList;
18    import java.util.List;
19    import java.util.Iterator;
20   
21    /**
22    * This is a volatile queue of the outgoing emails from JIRA.
23    * <p/>
24    * Note - this class may lose emails if the server shuts down.
25    */
 
26    public class MailQueueImpl implements MailQueue
27    {
28    private static final Category log = Category.getInstance(MailQueueImpl.class);
29    private Buffer items;
30    private Buffer errorItems;
31    private boolean sending;
32    private MailQueueItem itemBeingSent;
33    private Timestamp sendingStarted;
34   
 
35  0 toggle public MailQueueImpl()
36    {
37  0 items = BufferUtils.synchronizedBuffer(new UnboundedFifoBuffer());
38  0 errorItems = BufferUtils.synchronizedBuffer(new UnboundedFifoBuffer());
39    }
40   
 
41  0 toggle public void sendBuffer()
42    {
43  0 if (sending)
44    {
45  0 log.warn("Already sending "+items.size()+" mails:");
46  0 Iterator iter = items.iterator();
47  0 while (iter.hasNext()) {
48  0 Object o = iter.next();
49  0 log.warn("Sending: "+o+", "+o.getClass());
50    }
51  0 return;
52    }
53   
54  0 sendingStarted();
55  0 List failed = new ArrayList();
56   
57  0 try
58    {
59  0 while (!items.isEmpty())
60    {
61  0 String origThreadName = Thread.currentThread().getName();
62  0 MailQueueItem item = (MailQueueItem) items.remove();
63  0 this.itemBeingSent = item;
64  0 log.debug("Sending: " + item);
65  0 try
66    {
67  0 Thread.currentThread().setName("Sending mailitem "+item);
68  0 item.send();
69    }
70    catch (MailException e)
71    {
72  0 if (item.getSendCount() > 10)
73  0 errorItems.add(item);
74    else
75  0 failed.add(item);
76   
77  0 log.error("Error occurred in sending e-mail: " + item, e);
78    }
79    finally
80    {
81  0 Thread.currentThread().setName(origThreadName);
82    }
83    }
84   
85  0 items.addAll(failed);
86    }
87    finally
88    {
89    // Set sending to false no matter what happens
90  0 sendingStopped();
91    }
92    }
93   
 
94  0 toggle public int size()
95    {
96  0 return items.size();
97    }
98   
 
99  0 toggle public int errorSize()
100    {
101  0 return errorItems.size();
102    }
103   
 
104  0 toggle public void addItem(MailQueueItem item)
105    {
106  0 log.debug("Queued: " + item);
107  0 items.add(item);
108    }
109   
 
110  0 toggle public void addErrorItem(MailQueueItem item)
111    {
112  0 log.debug("Queued error: " + item);
113  0 errorItems.add(item);
114    }
115   
 
116  0 toggle public Buffer getQueue()
117    {
118  0 return items;
119    }
120   
 
121  0 toggle public Buffer getErrorQueue()
122    {
123  0 return errorItems;
124    }
125   
 
126  0 toggle public boolean isSending()
127    {
128  0 return sending;
129    }
130   
 
131  0 toggle public Timestamp getSendingStarted()
132    {
133  0 return sendingStarted;
134    }
135   
 
136  0 toggle public MailQueueItem getItemBeingSent()
137    {
138  0 return itemBeingSent;
139    }
140   
 
141  0 toggle public void unstickQueue() {
142  0 log.error("Mail on queue was considered stuck: " + itemBeingSent);
143  0 sendingStopped();
144    }
145   
 
146  0 toggle public void emptyErrorQueue()
147    {
148  0 errorItems.clear();
149    }
150   
 
151  0 toggle public void resendErrorQueue()
152    {
153  0 items.addAll(errorItems);
154  0 emptyErrorQueue();
155    }
156   
 
157  0 toggle public void sendingStarted()
158    {
159  0 sending = true;
160  0 sendingStarted = new Timestamp(System.currentTimeMillis());
161    }
162   
 
163  0 toggle public void sendingStopped()
164    {
165  0 sending = false;
166  0 sendingStarted = null;
167    }
168    }