View Javadoc

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  			// An exception is acceptable here.  We've no way of knowing
34  			// if this points to a file, or if the user expects the
35  			// system to guess the file name.
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  				// TODO: decide if returning null is what we want to do here.
44  				return null;
45  			}
46  		}
47  		
48  		if (customFileRef == null) {
49  			logger.fatal("Network element config. could not be found for '" + networkElementName + "'");
50  			// TODO: decide if returning null is what we want to do here.
51  			return null;
52  		}
53  
54  		return populateNetworkElement(ne, customFileRef);
55  		/*if (customFileRef.exists()) {
56  			// The user has specified a path to the config. file.
57  			return populateNetworkElement(ne, customFileRef);
58  		} else {
59  			// The user wants the system to guess where the config. file is.
60  			return populateNetworkElement(
61  				ne, 
62  				new File("config/" + networkElementName + ".xml")
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  			// TODO: throw something meaningful here.
84  			return null;
85  		}		
86  	}
87  }