1 package net.sf.provisioner.config;
2
3 import java.io.File;
4 import java.io.FileInputStream;
5 import java.io.FileNotFoundException;
6 import java.io.IOException;
7 import java.io.InputStream;
8 import java.util.ArrayList;
9 import java.util.Collection;
10 import java.util.Hashtable;
11 import java.util.Iterator;
12 import java.util.List;
13
14 import net.sf.provisioner.responses.Response;
15 import net.sf.provisioner.utils.PathHelper;
16
17 import org.apache.log4j.Logger;
18 import org.jdom.Document;
19 import org.jdom.Element;
20 import org.jdom.input.SAXBuilder;
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38 public class NetworkElement {
39
40
41
42 Logger logger = Logger.getLogger(getClass());
43
44 public String name = new String();
45 public String port = new String();
46 public String type = new String();
47 public String user = "user";
48 public String password = "password";
49 public String authKey = "authKey";
50 public String bin = "command";
51 public String create = "create";
52 public String delete = "delete";
53 public String enable = "enable";
54 public String disable = "disable";
55 public String loginPrompt = "ogin username:";
56 public String systemPrompt = ">";
57 public String sudoPassword = "";
58 public String sudoPasswordPrompt = "Password:";
59 public String loginErrorSecuence = "Failed";
60 public boolean sendInitialCRLF = false;
61 public String parameter1 = "";
62 public String parameter2 = "";
63 public String parameter3 = "";
64 public String parameter4 = "";
65 public String parameter5 = "";
66
67 public Hashtable responses = new Hashtable();
68
69 String configPath;
70
71
72
73
74
75 public NetworkElement() {}
76
77
78
79
80
81
82
83
84
85
86 public NetworkElement(String neName) throws FileNotFoundException {
87 this(new File(neName + ".xml"));
88 }
89
90
91
92
93
94
95
96
97 public NetworkElement(File xmlConfig) throws FileNotFoundException {
98 this(new FileInputStream(xmlConfig));
99 }
100
101 public NetworkElement(InputStream xmlConfig) {
102
103 try {
104 Document d = new SAXBuilder().build(xmlConfig);
105 List children = d.getContent();
106 Iterator iterator = children.iterator();
107 while (iterator.hasNext()) {
108 Element child = (Element) iterator.next();
109 getParameters(child);
110 }
111 } catch (Exception e) {
112 logger.fatal("Error reading network element configuration file " + name);
113 e.printStackTrace();
114 }
115 }
116
117
118
119
120
121
122
123
124
125
126 void getParameters(Element current) {
127
128 for (Element child : filterChildParameters(current)) {
129 String paramName = child.getAttributeValue("name").toLowerCase();
130 String value = child.getAttributeValue("value");
131
132 if (paramName.equals("host" )) name = value;
133 if (paramName.equals("type" )) type = value;
134 if (paramName.equals("username" )) user = value;
135 if (paramName.equals("password" )) password = value;
136 if (paramName.equals("password_sudo")) {
137 sudoPassword = value;
138 }
139 if (paramName.equals("password_authorisation")) {
140 authKey = value;
141 }
142
143 if (paramName.equals("bin" )) bin = value;
144 if (paramName.equals("create" )) create = value;
145 if (paramName.equals("delete" )) delete = value;
146 if (paramName.equals("enable" )) enable = value;
147 if (paramName.equals("disable" )) disable = value;
148 if (paramName.equals("errorlogin")) loginErrorSecuence = value;
149
150
151 if (paramName.equals("$1")) parameter1 = value;
152 if (paramName.equals("$2")) parameter2 = value;
153 if (paramName.equals("$3")) parameter3 = value;
154 if (paramName.equals("$4")) parameter4 = value;
155 if (paramName.equals("$5")) parameter5 = value;
156
157
158 if (paramName.equals("responses")) {
159 storePossibleResponses(child.getAttributeValue("value"));
160 }
161 }
162 }
163
164 protected Collection<Element> filterChildParameters(Element currentElement) {
165 List children = currentElement.getChildren();
166 List<Element> params = new ArrayList<Element>();
167 for (Object c : children) {
168 Element child = (Element) c;
169 if (child.getName().equalsIgnoreCase("parameter")) {
170 params.add(child);
171 }
172 }
173 return params;
174 }
175
176
177
178
179
180
181
182
183
184 void storePossibleResponses(String responses) {
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202 InputStream configFile = null;
203 try {
204 configFile = PathHelper.pathToStream(responses);
205 } catch (IOException ioe) {
206
207
208
209 }
210
211 if (configFile == null) {
212 try {
213 configFile = PathHelper.pathToStream(this.configPath + responses + ".xml");
214 } catch (IOException ioe) {
215 logger.fatal("Couldn't find responses for '" + responses + "'", ioe);
216
217 return;
218 }
219 }
220
221 if (configFile == null) {
222 logger.fatal("Couldn't find responses for '" + responses + "'");
223
224 return;
225 }
226
227
228
229
230 try {
231
232 Document d = new SAXBuilder().build(configFile);
233 List children = d.getContent();
234 Iterator iterator = children.iterator();
235 while (iterator.hasNext()) {
236 Element child = (Element) iterator.next();
237 getResponses(child, "");
238 }
239 } catch (Exception e) {
240 Exception wrapper = new Exception(
241 "Error reading possible responses from configuration file "+ responses,
242 e
243 );
244 logger.fatal(wrapper);
245 }
246 }
247
248
249
250
251
252
253
254
255
256 void getResponses(Element current, String operationType) {
257
258 List children = current.getChildren();
259 Iterator iterator = children.iterator();
260 while (iterator.hasNext()) {
261 Element child = (Element) iterator.next();
262 if (child.getName().equalsIgnoreCase("operation"))
263 operationType = child.getAttributeValue("type");;
264 if (child.getName().equalsIgnoreCase("response")) {
265
266 Response respuestaPosible = new Response();
267 respuestaPosible.tipoOperacion = operationType;
268 respuestaPosible.result = child.getAttributeValue("result");
269 if (child.getAttributeValue("success").equalsIgnoreCase("yes")) respuestaPosible.successfull = true;
270 respuestaPosible.errorStr = child.getAttributeValue("respond");
271 if (child.getAttributeValue("retry").equalsIgnoreCase("yes")) respuestaPosible.retry = true;
272
273 int i = this.responses.size();
274 this.responses.put(new Integer(i++), respuestaPosible);
275
276 }
277 getResponses(child, operationType);
278 }
279 }
280
281 public String toString() {
282 return "host=" + name + ":" + port + ", type=" + type + ", user=" + user;
283 }
284
285
286
287
288 public String getConfigPath() {
289 return configPath;
290 }
291
292
293
294
295 public void setConfigPath(String configPath) {
296 this.configPath = configPath;
297 }
298 }