Clover Coverage Report - Atlassian Mail
Coverage timestamp: Mon Sep 29 2008 21:26:36 CDT
28   96   9   5.6
4   80   0.32   5
5     1.8  
1    
 
 
  ConfigLoader       Line # 25 28 9 91.9% 0.9189189
 
  (3)
 
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  3 toggle public ConfigLoader(String file)
32    {
33    //This is a bit of a hack for JBOSS
34  3 InputStream is = ClassLoaderUtils.getResourceAsStream(file, ConfigLoader.class);
35  3 try
36    {
37  3 DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
38  3 Document xmlDoc = db.parse(is);
39  3 Element root = xmlDoc.getDocumentElement();
40  3 Element manager = (Element) root.getElementsByTagName("manager").item(0);
41  3 Class aClass = ClassLoaderUtils.loadClass(manager.getAttribute("class"), this.getClass());
42  3 MailServerManager msm = (MailServerManager) aClass.newInstance();
43  3 Map params = new HashMap();
44  3 NodeList properties = manager.getElementsByTagName("property");
45  3 if (properties.getLength() > 0)
46    {
47  4 for (int i = 0; i < properties.getLength(); i++)
48    {
49  2 Element property = (Element) properties.item(i);
50  2 String name = XMLUtils.getContainedText(property, "name");
51  2 String value = XMLUtils.getContainedText(property, "value");
52  2 params.put(name, value);
53    }
54    }
55  3 msm.init(params);
56  3 setLoadedManager(msm);
57    }
58    catch (Exception e)
59    {
60  0 log.fatal(e, e);
61  0 throw new RuntimeException("Error in mail config: " + e.getMessage(), e);
62    }
63    finally
64    {
65  3 try
66    {
67  3 is.close();
68    }
69    catch (IOException e2)
70    {
71  0 log.error(e2);
72    }
73    }
74    }
75   
 
76  2 toggle public static MailServerManager getServerManager()
77    {
78  2 return getServerManager(DEFAULT_CONFIG_FILE);
79    }
80   
 
81  3 toggle public static MailServerManager getServerManager(String file)
82    {
83  3 ConfigLoader configLoader = new ConfigLoader(file);
84  3 return configLoader.getLoadedManager();
85    }
86   
 
87  3 toggle public MailServerManager getLoadedManager()
88    {
89  3 return loadedManager;
90    }
91   
 
92  3 toggle public void setLoadedManager(MailServerManager loadedManager)
93    {
94  3 this.loadedManager = loadedManager;
95    }
96    }