View Javadoc

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      public void init(Map params)
30      {
31          configFile = DEFAULT_CONFIG_FILE;
32          serverIds = new HashMap();
33  
34          if (params.containsKey("config-file"))
35          {
36              configFile = (String) params.get("config-file");
37          }
38  
39          configure();
40      }
41  
42      private void configure()
43      {
44          // configure from config file
45          try
46          {
47              Digester digester = new Digester();
48              digester.push(this);
49              digester.setRules(new ExtendedBaseRules());
50  
51              // load pop servers
52              digester.addObjectCreate("mail-servers/pop-server", getPopMailServerClass());
53              digester.addSetProperties("mail-servers/pop-server");
54              digester.addBeanPropertySetter("mail-servers/pop-server/?");
55              digester.addSetRoot("mail-servers/pop-server", "create");
56  
57              // load smtp servers
58              digester.addObjectCreate("mail-servers/smtp-server", getSMTPMailServerClass());
59              digester.addSetProperties("mail-servers/smtp-server");
60              digester.addBeanPropertySetter("mail-servers/smtp-server/?");
61              digester.addBeanPropertySetter("mail-servers/smtp-server/jndi-location", "jndiLocation");
62              digester.addSetRoot("mail-servers/smtp-server", "create");
63  
64              InputStream is = getConfigurationInputStream(configFile);
65              digester.parse(is);
66  
67  
68          }
69          catch (Exception e)
70          {
71              log.fatal(e, e);
72              throw new RuntimeException("Error in mail config: " + e.getMessage(), e);
73          }
74      }
75  
76      protected InputStream getConfigurationInputStream(String resource)
77      {
78          return ClassLoaderUtils.getResourceAsStream(resource, ConfigLoader.class);
79      }
80  
81      public String getConfigFile()
82      {
83          return configFile;
84      }
85  
86      public MailServer getMailServer(Long id)
87      {
88          return (MailServer) serverIds.get(id);
89      }
90  
91      public MailServer getMailServer(String name) throws MailException
92      {
93          if (name == null)
94              throw new MailException("name is null");
95  
96          for (Iterator iterator = serverIds.values().iterator(); iterator.hasNext();)
97          {
98              MailServer server = (MailServer) iterator.next();
99              if (name.equals(server.getName()))
100                 return server;
101         }
102 
103         return null;
104     }
105 
106     public synchronized Long create(MailServer mailServer) throws MailException
107     {
108         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         while (serverIds.containsKey(id))
113             id = new Long(id.longValue() + 1);
114 
115         mailServer.setId(id);
116         serverIds.put(id, mailServer);
117         return id;
118     }
119 
120     public void update(MailServer mailServer) throws MailException
121     {
122         serverIds.put(mailServer.getId(), mailServer);
123     }
124 
125     public void delete(Long mailServerId) throws MailException
126     {
127         if (mailServerId == null)
128             throw new MailException("mailServerId is null");
129         if (!serverIds.containsKey(mailServerId))
130             throw new MailException("A mail server with the specified mailServerId does not exist");
131 
132         serverIds.remove(mailServerId);
133     }
134 
135     public List getServerNames() throws MailException
136     {
137         List result = new ArrayList();
138 
139         for (Iterator iterator = serverIds.values().iterator(); iterator.hasNext();)
140         {
141             MailServer server = (MailServer) iterator.next();
142             result.add(server.getName());
143         }
144 
145         return result;
146     }
147 
148     public List getSmtpMailServers() throws MailException
149     {
150         List result = new ArrayList();
151 
152         for (Iterator iterator = serverIds.values().iterator(); iterator.hasNext();)
153         {
154             MailServer server = (MailServer) iterator.next();
155             if (server instanceof SMTPMailServer)
156                 result.add(server);
157         }
158 
159         return result;
160     }
161 
162     public List getPopMailServers() throws MailException
163     {
164         List result = new ArrayList();
165 
166         for (Iterator iterator = serverIds.values().iterator(); iterator.hasNext();)
167         {
168             MailServer server = (MailServer) iterator.next();
169             if (server instanceof PopMailServer)
170                 result.add(server);
171         }
172 
173         return result;
174     }
175 
176     public SMTPMailServer getDefaultSMTPMailServer() throws MailException
177     {
178         List smtpServers = getSmtpMailServers();
179 
180         if (smtpServers.size() > 0)
181             return (SMTPMailServer) smtpServers.get(0);
182 
183         return null;
184     }
185 
186     public PopMailServer getDefaultPopMailServer() throws MailException
187     {
188         List popServers = getPopMailServers();
189 
190         if (popServers.size() > 0)
191             return (PopMailServer) popServers.get(0);
192 
193         return null;
194     }
195 
196     protected Class getSMTPMailServerClass()
197     {
198         return SMTPMailServerImpl.class;
199     }
200 
201     protected Class getPopMailServerClass()
202     {
203         return PopMailServerImpl.class;
204     }
205 }