1 package net.sf.provisioner.config;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.util.Iterator;
6 import java.util.List;
7
8 import net.sf.provisioner.utils.PathHelper;
9
10 import org.apache.log4j.Logger;
11 import org.jdom.Document;
12 import org.jdom.Element;
13 import org.jdom.input.SAXBuilder;
14
15 public class NetworkElementFactory {
16
17 private static Logger logger = Logger.getLogger(NetworkElementFactory.class);
18
19
20 public static NetworkElement createElement(String networkElementName, String configPath) {
21 NetworkElement ne;
22 if (networkElementName.equalsIgnoreCase("ldap_ne_name")) {
23 ne = new LDAPNetworkElement();
24 } else {
25 ne = defaultElement();
26 }
27 ne.setConfigPath(configPath);
28
29 InputStream customFileRef = null;
30 try {
31 customFileRef = PathHelper.pathToStream(networkElementName);
32 } catch (IOException ioe) {
33
34
35
36 }
37
38 if (customFileRef == null) {
39 try {
40 customFileRef = PathHelper.pathToStream(configPath + networkElementName + ".xml");
41 } catch (IOException ioe) {
42 logger.fatal("Network element config. could not be read for '" + networkElementName + "'");
43
44 return null;
45 }
46 }
47
48 if (customFileRef == null) {
49 logger.fatal("Network element config. could not be found for '" + networkElementName + "'");
50
51 return null;
52 }
53
54 return populateNetworkElement(ne, customFileRef);
55
56
57
58
59
60
61
62
63
64
65 }
66
67 public static NetworkElement defaultElement() {
68 return new NetworkElement();
69 }
70
71 private static NetworkElement populateNetworkElement(NetworkElement ne, InputStream xmlConfig) {
72 try {
73 Document d = new SAXBuilder().build(xmlConfig);
74 List children = d.getContent();
75 Iterator iterator = children.iterator();
76 while (iterator.hasNext()) {
77 Element child = (Element) iterator.next();
78 ne.getParameters(child);
79 }
80 return ne;
81 } catch (Exception e) {
82 logger.fatal("Error reading network element configuration file " + xmlConfig);
83
84 return null;
85 }
86 }
87 }