| 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 |
|
|
| 20 |
|
|
| 21 |
|
|
| 22 |
|
|
| 23 |
|
|
| 24 |
|
|
|
|
|
| 91.9% |
Uncovered Elements: 3 (37) |
Complexity: 9 |
Complexity Density: 0.32 |
|
| 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 |
|
|
|
|
|
| 88.9% |
Uncovered Elements: 3 (27) |
Complexity: 5 |
Complexity Density: 0.22 |
|
| 31 |
3
|
public ConfigLoader(String file)... |
| 32 |
|
{ |
| 33 |
|
|
| 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 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 76 |
2
|
public static MailServerManager getServerManager()... |
| 77 |
|
{ |
| 78 |
2
|
return getServerManager(DEFAULT_CONFIG_FILE); |
| 79 |
|
} |
| 80 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 81 |
3
|
public static MailServerManager getServerManager(String file)... |
| 82 |
|
{ |
| 83 |
3
|
ConfigLoader configLoader = new ConfigLoader(file); |
| 84 |
3
|
return configLoader.getLoadedManager(); |
| 85 |
|
} |
| 86 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 87 |
3
|
public MailServerManager getLoadedManager()... |
| 88 |
|
{ |
| 89 |
3
|
return loadedManager; |
| 90 |
|
} |
| 91 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 92 |
3
|
public void setLoadedManager(MailServerManager loadedManager)... |
| 93 |
|
{ |
| 94 |
3
|
this.loadedManager = loadedManager; |
| 95 |
|
} |
| 96 |
|
} |