1   package com.atlassian.mail.config;
2   
3   import com.atlassian.core.util.ClassLoaderUtils;
4   import com.atlassian.core.util.XMLUtils;
5   import com.atlassian.mail.server.MailServerManager;
6   import org.apache.log4j.Category;
7   import org.w3c.dom.Document;
8   import org.w3c.dom.Element;
9   import org.w3c.dom.NodeList;
10  
11  import javax.xml.parsers.DocumentBuilder;
12  import javax.xml.parsers.DocumentBuilderFactory;
13  import java.io.IOException;
14  import java.io.InputStream;
15  import java.util.HashMap;
16  import java.util.Map;
17  
18  /**
19   * Created by IntelliJ IDEA.
20   * User: Administrator
21   * Date: Dec 9, 2002
22   * Time: 2:44:02 PM
23   * To change this template use Options | File Templates.
24   */
25  public class ConfigLoader
26  {
27      private static final Category log = Category.getInstance(ConfigLoader.class);
28      private static final String DEFAULT_CONFIG_FILE = "mail-config.xml";
29      private MailServerManager loadedManager;
30  
31      public ConfigLoader(String file)
32      {
33          //This is a bit of a hack for JBOSS
34          InputStream is = ClassLoaderUtils.getResourceAsStream(file, ConfigLoader.class);
35          try
36          {
37              DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
38              Document xmlDoc = db.parse(is);
39              Element root = xmlDoc.getDocumentElement();
40              Element manager = (Element) root.getElementsByTagName("manager").item(0);
41              Class aClass = ClassLoaderUtils.loadClass(manager.getAttribute("class"), this.getClass());
42              MailServerManager msm = (MailServerManager) aClass.newInstance();
43              Map params = new HashMap();
44              NodeList properties = manager.getElementsByTagName("property");
45              if (properties.getLength() > 0)
46              {
47                  for (int i = 0; i < properties.getLength(); i++)
48                  {
49                      Element property = (Element) properties.item(i);
50                      String name = XMLUtils.getContainedText(property, "name");
51                      String value = XMLUtils.getContainedText(property, "value");
52                      params.put(name, value);
53                  }
54              }
55              msm.init(params);
56              setLoadedManager(msm);
57          }
58          catch (Exception e)
59          {
60              log.fatal(e, e);
61              throw new RuntimeException("Error in mail config: " + e.getMessage(), e);
62          }
63          finally
64          {
65              try
66              {
67                  is.close();
68              }
69              catch (IOException e2)
70              {
71                  log.error(e2);
72              }
73          }
74      }
75  
76      public static MailServerManager getServerManager()
77      {
78          return getServerManager(DEFAULT_CONFIG_FILE);
79      }
80  
81      public static MailServerManager getServerManager(String file)
82      {
83          ConfigLoader configLoader = new ConfigLoader(file);
84          return configLoader.getLoadedManager();
85      }
86  
87      public MailServerManager getLoadedManager()
88      {
89          return loadedManager;
90      }
91  
92      public void setLoadedManager(MailServerManager loadedManager)
93      {
94          this.loadedManager = loadedManager;
95      }
96  }