Clover Coverage Report - Atlassian Mail
Coverage timestamp: Mon Sep 29 2008 21:26:36 CDT
71   205   31   4.44
28   155   0.44   16
16     1.94  
1    
 
 
  XMLMailServerManager       Line # 18 71 31 73.9% 0.73913044
 
  (7)
 
1    package com.atlassian.mail.server.managers;
2   
3    import com.atlassian.core.util.ClassLoaderUtils;
4    import com.atlassian.mail.MailException;
5    import com.atlassian.mail.config.ConfigLoader;
6    import com.atlassian.mail.server.MailServer;
7    import com.atlassian.mail.server.PopMailServer;
8    import com.atlassian.mail.server.SMTPMailServer;
9    import com.atlassian.mail.server.impl.PopMailServerImpl;
10    import com.atlassian.mail.server.impl.SMTPMailServerImpl;
11    import org.apache.commons.digester.Digester;
12    import org.apache.commons.digester.ExtendedBaseRules;
13    import org.apache.log4j.Category;
14   
15    import java.io.InputStream;
16    import java.util.*;
17   
 
18    public class XMLMailServerManager extends AbstractMailServerManager
19    {
20    private static final Category log = Category.getInstance(XMLMailServerManager.class);
21   
22    // Map of server id -> server
23    Map serverIds;
24   
25    private static String DEFAULT_CONFIG_FILE = "mail-servers.xml";
26   
27    String configFile;
28   
 
29  7 toggle public void init(Map params)
30    {
31  7 configFile = DEFAULT_CONFIG_FILE;
32  7 serverIds = new HashMap();
33   
34  7 if (params.containsKey("config-file"))
35    {
36  6 configFile = (String) params.get("config-file");
37    }
38   
39  7 configure();
40    }
41   
 
42  7 toggle private void configure()
43    {
44    // configure from config file
45  7 try
46    {
47  7 Digester digester = new Digester();
48  7 digester.push(this);
49  7 digester.setRules(new ExtendedBaseRules());
50   
51    // load pop servers
52  7 digester.addObjectCreate("mail-servers/pop-server", getPopMailServerClass());
53  7 digester.addSetProperties("mail-servers/pop-server");
54  7 digester.addBeanPropertySetter("mail-servers/pop-server/?");
55  7 digester.addSetRoot("mail-servers/pop-server", "create");
56   
57    // load smtp servers
58  7 digester.addObjectCreate("mail-servers/smtp-server", getSMTPMailServerClass());
59  7 digester.addSetProperties("mail-servers/smtp-server");
60  7 digester.addBeanPropertySetter("mail-servers/smtp-server/?");
61  7 digester.addBeanPropertySetter("mail-servers/smtp-server/jndi-location", "jndiLocation");
62  7 digester.addSetRoot("mail-servers/smtp-server", "create");
63   
64  7 InputStream is = getConfigurationInputStream(configFile);
65  7 digester.parse(is);
66   
67   
68    }
69    catch (Exception e)
70    {
71  0 log.fatal(e, e);
72  0 throw new RuntimeException("Error in mail config: " + e.getMessage(), e);
73    }
74    }
75   
 
76  7 toggle protected InputStream getConfigurationInputStream(String resource)
77    {
78  7 return ClassLoaderUtils.getResourceAsStream(resource, ConfigLoader.class);
79    }
80   
 
81  3 toggle public String getConfigFile()
82    {
83  3 return configFile;
84    }
85   
 
86  1 toggle public MailServer getMailServer(Long id)
87    {
88  1 return (MailServer) serverIds.get(id);
89    }
90   
 
91  0 toggle public MailServer getMailServer(String name) throws MailException
92    {
93  0 if (name == null)
94  0 throw new MailException("name is null");
95   
96  0 for (Iterator iterator = serverIds.values().iterator(); iterator.hasNext();)
97    {
98  0 MailServer server = (MailServer) iterator.next();
99  0 if (name.equals(server.getName()))
100  0 return server;
101    }
102   
103  0 return null;
104    }
105   
 
106  15 toggle public synchronized Long create(MailServer mailServer) throws MailException
107    {
108  15 Long id = new Long((serverIds.size() + 1));
109   
110    // check if this id already exists. If it does, increment until a vacant one is found
111    // this will not scale! (we are assuming there won't be that many mail servers)
112  16 while (serverIds.containsKey(id))
113  1 id = new Long(id.longValue() + 1);
114   
115  15 mailServer.setId(id);
116  15 serverIds.put(id, mailServer);
117  15 return id;
118    }
119   
 
120  1 toggle public void update(MailServer mailServer) throws MailException
121    {
122  1 serverIds.put(mailServer.getId(), mailServer);
123    }
124   
 
125  3 toggle public void delete(Long mailServerId) throws MailException
126    {
127  3 if (mailServerId == null)
128  0 throw new MailException("mailServerId is null");
129  3 if (!serverIds.containsKey(mailServerId))
130  1 throw new MailException("A mail server with the specified mailServerId does not exist");
131   
132  2 serverIds.remove(mailServerId);
133    }
134   
 
135  0 toggle public List getServerNames() throws MailException
136    {
137  0 List result = new ArrayList();
138   
139  0 for (Iterator iterator = serverIds.values().iterator(); iterator.hasNext();)
140    {
141  0 MailServer server = (MailServer) iterator.next();
142  0 result.add(server.getName());
143    }
144   
145  0 return result;
146    }
147   
 
148  7 toggle public List getSmtpMailServers() throws MailException
149    {
150  7 List result = new ArrayList();
151   
152  18 for (Iterator iterator = serverIds.values().iterator(); iterator.hasNext();)
153    {
154  11 MailServer server = (MailServer) iterator.next();
155  11 if (server instanceof SMTPMailServer)
156  6 result.add(server);
157    }
158   
159  7 return result;
160    }
161   
 
162  3 toggle public List getPopMailServers() throws MailException
163    {
164  3 List result = new ArrayList();
165   
166  8 for (Iterator iterator = serverIds.values().iterator(); iterator.hasNext();)
167    {
168  5 MailServer server = (MailServer) iterator.next();
169  5 if (server instanceof PopMailServer)
170  2 result.add(server);
171    }
172   
173  3 return result;
174    }
175   
 
176  2 toggle public SMTPMailServer getDefaultSMTPMailServer() throws MailException
177    {
178  2 List smtpServers = getSmtpMailServers();
179   
180  2 if (smtpServers.size() > 0)
181  2 return (SMTPMailServer) smtpServers.get(0);
182   
183  0 return null;
184    }
185   
 
186  1 toggle public PopMailServer getDefaultPopMailServer() throws MailException
187    {
188  1 List popServers = getPopMailServers();
189   
190  1 if (popServers.size() > 0)
191  1 return (PopMailServer) popServers.get(0);
192   
193  0 return null;
194    }
195   
 
196  7 toggle protected Class getSMTPMailServerClass()
197    {
198  7 return SMTPMailServerImpl.class;
199    }
200   
 
201  7 toggle protected Class getPopMailServerClass()
202    {
203  7 return PopMailServerImpl.class;
204    }
205    }