View Javadoc

1   package net.sf.provisioner.requests;
2   
3   import java.util.Enumeration;
4   import java.util.Iterator;
5   import java.util.List;
6   import java.util.regex.Matcher;
7   import java.util.regex.Pattern;
8   
9   import net.sf.provisioner.adapters.TelnetAdapter;
10  import net.sf.provisioner.commands.MerakCommand;
11  import net.sf.provisioner.responses.Response;
12  
13  import org.jdom.Element;
14  import org.jdom.Document;
15  
16  
17  /**
18   * Esta clase es .
19   * <p>
20   * 
21   * .
22   * <p>
23   * 
24   *             
25   * @version $Revision: 1.1.2.1 $, $Date: 2007/11/12 01:57:25 $
26   * @author Gonzalo Espert
27   */
28  public class MerakRequest extends Request{
29  	
30  	/* Comando del requerimiento */ 
31  	MerakCommand command = new MerakCommand();
32  		
33  	/**
34       * .
35       * <p>
36       * 
37       * .
38       * <p>
39       * 
40       * 
41       * 
42       * @param 
43       *            
44       * @throws 
45       *            
46       *           
47       */
48  	public MerakRequest(net.sf.provisioner.config.ConfigRequest request, Document parameters) {
49  		
50  		_initRequest(parameters);
51  		this.ne = request.service.ne;
52  		this.ne.loginPrompt = "ogin:";
53  		this.ne.port = "23";
54  		this.ne.sendInitialCRLF = false;
55  		
56  		this.command.bin = this.ne.bin;
57  		this.command.operation = request.operationType;
58  
59  	}
60  
61  	/**
62       * .
63       * <p>
64       * 
65       * .
66       * <p>
67       * 
68       * 
69       * 
70       * @param 
71       *            
72       * @throws 
73       *            
74       *           
75       */
76  	void _initRequest(Document parameters) {
77  		
78  		String paramName;
79  		
80  		List children = parameters.getContent();  
81  	    Iterator iterator = children.iterator();
82  	    while (iterator.hasNext()) {
83  	      Element child = (Element) iterator.next();
84  	      if (child.getName().equalsIgnoreCase("operation")) {
85  	    	  List children1 = child.getChildren();
86  	    	  Iterator iterator1 = children1.iterator();
87  	    	  while (iterator1.hasNext()) {
88  	    		  Element child1 = (Element) iterator1.next();
89  	    		  if (child1.getName().equalsIgnoreCase("parameter")) {
90  	    			  paramName = child1.getAttributeValue("name");
91  	    		      	if (paramName.equalsIgnoreCase("account")) this.command.account = child1.getAttributeValue("value");
92  	    		      	if (paramName.equalsIgnoreCase("u_name")) this.command.user = child1.getAttributeValue("value");
93  	    		      	if (paramName.equalsIgnoreCase("u_password")) this.command.password = child1.getAttributeValue("value");
94  	    		      	if (paramName.equalsIgnoreCase("u_accounttype")) this.command.accountType = child1.getAttributeValue("value");
95  	    		  }
96  	    	  }
97  	      }
98  	    }
99  	}
100 	
101 	/**
102      * .
103      * <p>
104      * 
105      * .
106      * <p>
107      * 
108      * 
109      * 
110      * @param 
111      *            
112      * @throws 
113      *            
114      *           
115      */
116 	public Response sendRequest() throws Exception{
117 		
118 		Response response = new Response();
119 		
120 		String operation = new String();
121 		
122 		TelnetAdapter merak = new TelnetAdapter(this.ne);
123 		
124 		if (this.command.operation.equalsIgnoreCase("create")) operation = this.ne.create;
125 	    if (this.command.operation.equalsIgnoreCase("delete")) operation = this.ne.delete;
126 		
127 		/* Enviamos a Merak */
128 		try { 
129 			
130 			/* Ejecutamos el comando */
131 			String respuesta = merak.ExecuteCommand(this.command.bin + " " + 
132 					                                operation + " account " + 
133                                                     this.command.account + " u_name \"" + 
134                                                     this.command.user + "\" u_password \"" + 
135                                                     this.command.password + "\" u_accounttype " + 
136                                                     this.command.accountType);
137 			
138 			logger.debug(respuesta);
139 			
140 			/* Interpretamos la respuesta */
141 			response = interpretaRespuesta(respuesta);
142 			
143 		} catch (Exception e) { throw e; }
144 		
145 		return response;
146 	}
147 	
148 	/**
149      * .
150      * <p>
151      * 
152      * .
153      * <p>
154      * 
155      * 
156      * 
157      * @param 
158      *            
159      * @throws 
160      *            
161      *           
162      */
163 	Response interpretaRespuesta (String respuesta) {
164 		
165 		Response posibleRespuesta = new Response();
166 				
167 		/* Matcheamos la respuesta con las configuradas en el archivo xml */
168 		Enumeration respuestas = this.ne.responses.elements();
169 		while (respuestas.hasMoreElements()) {
170 			posibleRespuesta = (Response)respuestas.nextElement();
171 			Pattern pattern = Pattern.compile(posibleRespuesta.result, Pattern.CASE_INSENSITIVE);
172 			Matcher matcher = pattern.matcher(respuesta);
173 			if (matcher.find() && this.command.operation.equalsIgnoreCase(posibleRespuesta.tipoOperacion))
174 				return posibleRespuesta;
175 		}	
176 		posibleRespuesta.errorStr = "No se encontro ningun match en el archivo de respuestas, respuesta desconocida";
177 		posibleRespuesta.successfull = false;
178 		posibleRespuesta.retry = false;
179 		return posibleRespuesta;
180 	}
181 	
182 }