Revision b722cab3 src/com/rackspacecloud/android/AddMoreNodesActivity.java
b/src/com/rackspacecloud/android/AddMoreNodesActivity.java | ||
---|---|---|
1 | 1 |
package com.rackspacecloud.android; |
2 | 2 |
|
3 |
import java.io.IOException; |
|
4 |
import java.io.StringReader; |
|
5 | 3 |
import java.util.ArrayList; |
6 |
import java.util.Arrays; |
|
7 |
|
|
8 |
import javax.xml.parsers.FactoryConfigurationError; |
|
9 |
import javax.xml.parsers.ParserConfigurationException; |
|
10 |
import javax.xml.parsers.SAXParser; |
|
11 |
import javax.xml.parsers.SAXParserFactory; |
|
12 | 4 |
|
13 | 5 |
import org.apache.http.HttpResponse; |
14 |
import org.apache.http.client.ClientProtocolException; |
|
15 |
import org.apache.http.impl.client.BasicResponseHandler; |
|
16 |
import org.xml.sax.InputSource; |
|
17 |
import org.xml.sax.SAXException; |
|
18 |
import org.xml.sax.XMLReader; |
|
19 | 6 |
|
20 | 7 |
import com.rackspace.cloud.loadbalancer.api.client.LoadBalancer; |
21 |
import com.rackspace.cloud.loadbalancer.api.client.LoadBalancerManager; |
|
22 | 8 |
import com.rackspace.cloud.loadbalancer.api.client.Node; |
23 |
import com.rackspace.cloud.servers.api.client.Account;
|
|
9 |
import com.rackspace.cloud.loadbalancer.api.client.NodeManager;
|
|
24 | 10 |
import com.rackspace.cloud.servers.api.client.CloudServersException; |
25 | 11 |
import com.rackspace.cloud.servers.api.client.Server; |
26 | 12 |
import com.rackspace.cloud.servers.api.client.ServerManager; |
27 | 13 |
import com.rackspace.cloud.servers.api.client.http.HttpBundle; |
28 |
import com.rackspace.cloud.servers.api.client.parsers.CloudServersFaultXMLParser; |
|
29 | 14 |
|
30 | 15 |
import android.app.Activity; |
31 |
import android.app.AlertDialog; |
|
32 |
import android.app.ListActivity; |
|
33 |
import android.app.ProgressDialog; |
|
34 |
import android.content.Context; |
|
35 |
import android.content.DialogInterface; |
|
36 | 16 |
import android.content.Intent; |
37 | 17 |
import android.os.AsyncTask; |
38 | 18 |
import android.os.Bundle; |
... | ... | |
41 | 21 |
import android.view.View; |
42 | 22 |
import android.view.View.OnClickListener; |
43 | 23 |
import android.view.ViewGroup; |
44 |
import android.view.WindowManager; |
|
45 |
import android.view.ViewGroup.LayoutParams; |
|
46 | 24 |
import android.widget.ArrayAdapter; |
47 | 25 |
import android.widget.Button; |
48 | 26 |
import android.widget.CheckBox; |
... | ... | |
51 | 29 |
import android.widget.ListView; |
52 | 30 |
import android.widget.CompoundButton.OnCheckedChangeListener; |
53 | 31 |
import android.widget.ImageView; |
54 |
import android.widget.ProgressBar; |
|
55 | 32 |
import android.widget.TextView; |
56 | 33 |
|
57 |
public class AddMoreNodesActivity extends ListActivity { |
|
34 |
public class AddMoreNodesActivity extends CloudListActivity {
|
|
58 | 35 |
|
59 | 36 |
private static final int ADD_NODE_CODE = 178; |
60 |
private Server[] servers; |
|
61 |
private Context context; |
|
37 |
private Server[] servers; |
|
62 | 38 |
private int lastCheckedPos; |
63 | 39 |
private ArrayList<Node> nodes; |
64 | 40 |
private ArrayList<Node> nodesToAdd; |
65 | 41 |
private LoadBalancer loadBalancer; |
66 |
ProgressDialog pDialog; |
|
67 | 42 |
|
43 |
@SuppressWarnings("unchecked") |
|
68 | 44 |
@Override |
69 | 45 |
public void onCreate(Bundle savedInstanceState) { |
70 | 46 |
super.onCreate(savedInstanceState); |
... | ... | |
77 | 53 |
@Override |
78 | 54 |
protected void onSaveInstanceState(Bundle outState) { |
79 | 55 |
super.onSaveInstanceState(outState); |
56 |
|
|
80 | 57 |
outState.putSerializable("nodes", nodes); |
81 | 58 |
outState.putSerializable("loadBalancer", loadBalancer); |
82 | 59 |
outState.putSerializable("nodesToAdd", nodesToAdd); |
60 |
outState.putSerializable("servers", servers); |
|
83 | 61 |
} |
84 | 62 |
|
85 |
private void restoreState(Bundle state) {
|
|
86 |
|
|
87 |
context = getApplicationContext();
|
|
63 |
@SuppressWarnings("unchecked")
|
|
64 |
protected void restoreState(Bundle state) { |
|
65 |
super.restoreState(state);
|
|
88 | 66 |
|
89 | 67 |
if (state != null && state.containsKey("nodes")){ |
90 | 68 |
nodes = (ArrayList<Node>) state.getSerializable("nodes"); |
... | ... | |
93 | 71 |
} |
94 | 72 |
} |
95 | 73 |
|
74 |
if (state != null && state.containsKey("loadBalancer")){ |
|
75 |
loadBalancer = (LoadBalancer) state.getSerializable("loadBalancer"); |
|
76 |
if(loadBalancer == null){ |
|
77 |
loadBalancer = new LoadBalancer(); |
|
78 |
} |
|
79 |
} |
|
80 |
|
|
96 | 81 |
if (state != null && state.containsKey("nodesToAdd")){ |
97 | 82 |
nodesToAdd = (ArrayList<Node>) state.getSerializable("nodesToAdd"); |
98 | 83 |
} |
... | ... | |
100 | 85 |
nodesToAdd = new ArrayList<Node>(); |
101 | 86 |
} |
102 | 87 |
|
103 |
if (state != null && state.containsKey("server")) {
|
|
88 |
if (state != null && state.containsKey("servers") && state.getSerializable("servers") != null) {
|
|
104 | 89 |
servers = (Server[]) state.getSerializable("servers"); |
105 | 90 |
if (servers.length == 0) { |
106 | 91 |
displayNoServersCell(); |
... | ... | |
113 | 98 |
} |
114 | 99 |
|
115 | 100 |
Button submitNodes = (Button) findViewById(R.id.submit_nodes_button); |
101 |
submitNodes.setText("Submit"); |
|
116 | 102 |
submitNodes.setOnClickListener(new OnClickListener() { |
117 | 103 |
|
118 | 104 |
@Override |
... | ... | |
204 | 190 |
new LoadServersTask().execute((Void[]) null); |
205 | 191 |
} |
206 | 192 |
|
207 |
protected void showDialog() { |
|
208 |
pDialog = new ProgressDialog(this, R.style.NewDialog); |
|
209 |
// // Set blur to background |
|
210 |
WindowManager.LayoutParams lp = pDialog.getWindow().getAttributes(); |
|
211 |
lp.dimAmount = 0.0f; |
|
212 |
pDialog.getWindow().setAttributes(lp); |
|
213 |
pDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND); |
|
214 |
pDialog.show(); |
|
215 |
pDialog.setContentView(new ProgressBar(this), new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); |
|
216 |
} |
|
217 |
|
|
218 |
private void showAlert(String title, String message) { |
|
219 |
AlertDialog alert = new AlertDialog.Builder(this).create(); |
|
220 |
alert.setTitle(title); |
|
221 |
alert.setMessage(message); |
|
222 |
alert.setButton("OK", new DialogInterface.OnClickListener() { |
|
223 |
public void onClick(DialogInterface dialog, int which) { |
|
224 |
return; |
|
225 |
} }); |
|
226 |
alert.show(); |
|
227 |
} |
|
228 |
|
|
229 | 193 |
// * Adapter/ |
230 | 194 |
class ServerAdapter extends ArrayAdapter<Server> { |
231 | 195 |
ServerAdapter() { |
... | ... | |
266 | 230 |
final int pos = position; |
267 | 231 |
CheckBox add = (CheckBox) row.findViewById(R.id.add_node_checkbox); |
268 | 232 |
|
233 |
if(inToAddList(server)){ |
|
234 |
add.setChecked(true); |
|
235 |
} |
|
236 |
|
|
269 | 237 |
add.setOnCheckedChangeListener(new OnCheckedChangeListener() { |
270 | 238 |
|
271 | 239 |
@Override |
272 | 240 |
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { |
273 | 241 |
if(isChecked){ |
274 | 242 |
lastCheckedPos = pos; |
275 |
Intent viewIntent = new Intent(context, AddNodeActivity.class);
|
|
243 |
Intent viewIntent = new Intent(getContext(), AddNodeActivity.class);
|
|
276 | 244 |
viewIntent.putExtra("ipAddresses", ipAddresses); |
245 |
viewIntent.putExtra("name", server.getName()); |
|
246 |
viewIntent.putExtra("weighted", loadBalancer.getAlgorithm().contains("WEIGHTED")); |
|
277 | 247 |
startActivityForResult(viewIntent, ADD_NODE_CODE); |
278 | 248 |
} |
279 | 249 |
else{ |
250 |
Log.d("info", "before removing node, size is " + nodesToAdd.size()); |
|
280 | 251 |
removeNodeFromList(server); |
252 |
Log.d("info", "after removing node, size is " + nodesToAdd.size()); |
|
281 | 253 |
} |
282 | 254 |
} |
283 | 255 |
}); |
284 | 256 |
|
285 | 257 |
return(row); |
286 | 258 |
} |
287 |
} |
|
288 |
|
|
289 |
/*
|
|
290 |
* need to remove by id because that is
|
|
291 |
* what is unique
|
|
292 |
*/
|
|
293 |
private void removeNodeFromList(Server server){
|
|
294 |
for(int i = 0; i < nodes.size(); i++){
|
|
295 |
Node node = nodes.get(i);
|
|
296 |
if(serverHasIp(server, node.getAddress())){
|
|
297 |
nodes.remove(i);
|
|
298 |
break;
|
|
259 |
|
|
260 |
/* |
|
261 |
* need to remove by id because that is
|
|
262 |
* what is unique
|
|
263 |
*/
|
|
264 |
private void removeNodeFromList(Server server){
|
|
265 |
for(int i = 0; i < nodesToAdd.size(); i++){
|
|
266 |
Node node = nodesToAdd.get(i);
|
|
267 |
if(serverHasIp(server, node.getAddress())){
|
|
268 |
nodesToAdd.remove(i);
|
|
269 |
break;
|
|
270 |
}
|
|
299 | 271 |
} |
300 | 272 |
} |
301 | 273 |
} |
302 |
|
|
303 | 274 |
private boolean serverHasIp(Server server, String address){ |
304 | 275 |
String[] addresses = server.getPrivateIpAddresses(); |
305 | 276 |
for(int i = 0; i < addresses.length; i++){ |
277 |
Log.d("info", "server address: " + addresses[i] + " node address: " + address); |
|
306 | 278 |
if(addresses[i].equals(address)){ |
307 | 279 |
return true; |
308 | 280 |
} |
... | ... | |
316 | 288 |
return false; |
317 | 289 |
} |
318 | 290 |
|
319 |
private void startLoadBalancerError(String message, HttpBundle bundle){ |
|
320 |
Intent viewIntent = new Intent(getApplicationContext(), ServerErrorActivity.class); |
|
321 |
viewIntent.putExtra("errorMessage", message); |
|
322 |
viewIntent.putExtra("response", bundle.getResponseText()); |
|
323 |
viewIntent.putExtra("request", bundle.getCurlRequest()); |
|
324 |
startActivity(viewIntent); |
|
325 |
} |
|
326 |
|
|
327 |
//using cloudServersException, it works for us too |
|
328 |
private CloudServersException parseCloudServersException(HttpResponse response) { |
|
329 |
CloudServersException cse = new CloudServersException(); |
|
330 |
try { |
|
331 |
BasicResponseHandler responseHandler = new BasicResponseHandler(); |
|
332 |
String body = responseHandler.handleResponse(response); |
|
333 |
CloudServersFaultXMLParser parser = new CloudServersFaultXMLParser(); |
|
334 |
SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser(); |
|
335 |
XMLReader xmlReader = saxParser.getXMLReader(); |
|
336 |
xmlReader.setContentHandler(parser); |
|
337 |
xmlReader.parse(new InputSource(new StringReader(body))); |
|
338 |
cse = parser.getException(); |
|
339 |
} catch (ClientProtocolException e) { |
|
340 |
cse = new CloudServersException(); |
|
341 |
cse.setMessage(e.getLocalizedMessage()); |
|
342 |
} catch (IOException e) { |
|
343 |
cse = new CloudServersException(); |
|
344 |
cse.setMessage(e.getLocalizedMessage()); |
|
345 |
} catch (ParserConfigurationException e) { |
|
346 |
cse = new CloudServersException(); |
|
347 |
cse.setMessage(e.getLocalizedMessage()); |
|
348 |
} catch (SAXException e) { |
|
349 |
cse = new CloudServersException(); |
|
350 |
cse.setMessage(e.getLocalizedMessage()); |
|
351 |
} catch (FactoryConfigurationError e) { |
|
352 |
cse = new CloudServersException(); |
|
353 |
cse.setMessage(e.getLocalizedMessage()); |
|
291 |
private boolean inToAddList(Server server){ |
|
292 |
for(Node node : nodesToAdd){ |
|
293 |
if(serverHasIp(server, node.getAddress())){ |
|
294 |
return true; |
|
295 |
} |
|
354 | 296 |
} |
355 |
return cse;
|
|
297 |
return false;
|
|
356 | 298 |
} |
299 |
|
|
357 | 300 |
|
358 | 301 |
private class AddNodesTask extends AsyncTask<Void, Void, HttpBundle> { |
359 | 302 |
private CloudServersException exception; |
... | ... | |
366 | 309 |
protected HttpBundle doInBackground(Void... arg0) { |
367 | 310 |
HttpBundle bundle = null; |
368 | 311 |
try { |
369 |
bundle = (new LoadBalancerManager(context)).addNodes(loadBalancer, nodesToAdd);
|
|
312 |
bundle = (new NodeManager(getContext())).add(loadBalancer, nodesToAdd);
|
|
370 | 313 |
} catch (CloudServersException e) { |
371 | 314 |
exception = e; |
372 | 315 |
} |
... | ... | |
375 | 318 |
|
376 | 319 |
@Override |
377 | 320 |
protected void onPostExecute(HttpBundle bundle) { |
378 |
pDialog.dismiss();
|
|
321 |
hideDialog();
|
|
379 | 322 |
HttpResponse response = bundle.getResponse(); |
380 | 323 |
if (response != null) { |
381 | 324 |
int statusCode = response.getStatusLine().getStatusCode(); |
... | ... | |
385 | 328 |
} else { |
386 | 329 |
CloudServersException cse = parseCloudServersException(response); |
387 | 330 |
if ("".equals(cse.getMessage())) { |
388 |
startLoadBalancerError("There was a problem creating your load balancer.", bundle);
|
|
331 |
showError("There was a problem creating your load balancer.", bundle);
|
|
389 | 332 |
} else { |
390 | 333 |
//if container with same name already exists |
391 |
startLoadBalancerError("There was a problem creating your load balancer: " + cse.getMessage() + "\n See details for more information", bundle);
|
|
334 |
showError("There was a problem creating your load balancer: " + cse.getMessage() + "\n See details for more information", bundle);
|
|
392 | 335 |
} |
393 | 336 |
} |
394 | 337 |
} else if (exception != null) { |
395 |
startLoadBalancerError("There was a problem creating your container: " + exception.getMessage()+"\n See details for more information.", bundle);
|
|
338 |
showError("There was a problem creating your container: " + exception.getMessage()+"\n See details for more information.", bundle);
|
|
396 | 339 |
} |
397 | 340 |
finish(); |
398 | 341 |
} |
... | ... | |
410 | 353 |
protected ArrayList<Server> doInBackground(Void... arg0) { |
411 | 354 |
ArrayList<Server> servers = null; |
412 | 355 |
try { |
413 |
servers = (new ServerManager()).createList(true, context);
|
|
356 |
servers = (new ServerManager()).createList(true, getContext());
|
|
414 | 357 |
} catch (CloudServersException e) { |
415 | 358 |
exception = e; |
416 |
} |
|
417 |
pDialog.dismiss(); |
|
359 |
} |
|
418 | 360 |
return servers; |
419 | 361 |
} |
420 | 362 |
|
421 | 363 |
@Override |
422 | 364 |
protected void onPostExecute(ArrayList<Server> result) { |
365 |
hideDialog(); |
|
423 | 366 |
if (exception != null) { |
424 | 367 |
showAlert("Error", exception.getMessage()); |
425 | 368 |
} |
... | ... | |
435 | 378 |
node.setCondition(data.getStringExtra("nodeCondition")); |
436 | 379 |
node.setName(servers[pos].getName()); |
437 | 380 |
node.setPort(data.getStringExtra("nodePort")); |
438 |
Log.d("info", "nodesToAdd is null? " + Boolean.toString(nodesToAdd == null)); |
|
381 |
Log.d("info", "setting the weight in addmore to " + data.getStringExtra("nodeWeight")); |
|
382 |
node.setWeight(data.getStringExtra("nodeWeight")); |
|
439 | 383 |
nodesToAdd.add(node); |
440 | 384 |
} |
441 | 385 |
else if(requestCode == ADD_NODE_CODE && resultCode == RESULT_CANCELED){ |
Also available in: Unified diff