root / src / com / rackspacecloud / android / ViewLoadBalancerActivity.java @ 4f9d1a69
History | View | Annotate | Download (13 kB)
1 |
/**
|
---|---|
2 |
*
|
3 |
*/
|
4 |
package com.rackspacecloud.android; |
5 |
|
6 |
import java.io.IOException; |
7 |
import java.io.StringReader; |
8 |
|
9 |
import javax.xml.parsers.FactoryConfigurationError; |
10 |
import javax.xml.parsers.ParserConfigurationException; |
11 |
import javax.xml.parsers.SAXParser; |
12 |
import javax.xml.parsers.SAXParserFactory; |
13 |
|
14 |
import org.apache.http.HttpResponse; |
15 |
import org.apache.http.client.ClientProtocolException; |
16 |
import org.apache.http.impl.client.BasicResponseHandler; |
17 |
import org.xml.sax.InputSource; |
18 |
import org.xml.sax.SAXException; |
19 |
import org.xml.sax.XMLReader; |
20 |
|
21 |
import android.app.Activity; |
22 |
import android.app.AlertDialog; |
23 |
import android.app.Dialog; |
24 |
import android.app.ProgressDialog; |
25 |
import android.content.Context; |
26 |
import android.content.DialogInterface; |
27 |
import android.content.Intent; |
28 |
import android.os.AsyncTask; |
29 |
import android.os.Bundle; |
30 |
import android.util.Log; |
31 |
import android.view.Menu; |
32 |
import android.view.MenuInflater; |
33 |
import android.view.MenuItem; |
34 |
import android.view.View; |
35 |
import android.view.View.OnClickListener; |
36 |
import android.view.WindowManager; |
37 |
import android.view.ViewGroup.LayoutParams; |
38 |
import android.widget.Button; |
39 |
import android.widget.ProgressBar; |
40 |
import android.widget.TextView; |
41 |
|
42 |
import com.rackspace.cloud.loadbalancer.api.client.LoadBalancer; |
43 |
import com.rackspace.cloud.loadbalancer.api.client.LoadBalancerManager; |
44 |
import com.rackspace.cloud.loadbalancers.api.client.http.LoadBalancersException; |
45 |
import com.rackspace.cloud.servers.api.client.CloudServersException; |
46 |
import com.rackspace.cloud.servers.api.client.http.HttpBundle; |
47 |
import com.rackspace.cloud.servers.api.client.parsers.CloudServersFaultXMLParser; |
48 |
|
49 |
public class ViewLoadBalancerActivity extends Activity { |
50 |
|
51 |
private static final int EDIT_LOAD_BALANCER_CODE = 184; |
52 |
private static final int EDIT_NODES_CODE = 185; |
53 |
|
54 |
private LoadBalancer loadBalancer;
|
55 |
private LoadBalancer returnLoadBalancer;
|
56 |
private Context context; |
57 |
ProgressDialog pDialog; |
58 |
|
59 |
@Override
|
60 |
public void onCreate(Bundle savedInstanceState) { |
61 |
super.onCreate(savedInstanceState);
|
62 |
loadBalancer = (LoadBalancer) this.getIntent().getExtras().get("loadBalancer"); |
63 |
Log.i("VIEWLOADBALANCERS: ", loadBalancer.getAlgorithm() +","+loadBalancer.getProtocol()+","+loadBalancer.getStatus()); |
64 |
|
65 |
setContentView(R.layout.view_loadbalancer); |
66 |
restoreState(savedInstanceState); |
67 |
} |
68 |
|
69 |
@Override
|
70 |
protected void onSaveInstanceState(Bundle outState) { |
71 |
super.onSaveInstanceState(outState);
|
72 |
outState.putSerializable("loadBalancer", loadBalancer);
|
73 |
} |
74 |
|
75 |
private void restoreState(Bundle state) { |
76 |
context = getApplicationContext(); |
77 |
if (state != null && state.containsKey("loadBalancer")) { |
78 |
loadBalancer = (LoadBalancer) state.getSerializable("loadBalancer");
|
79 |
} |
80 |
new LoadLoadBalancerTask().execute((Void[]) null); |
81 |
setUpButtons(); |
82 |
} |
83 |
|
84 |
private void setUpButtons(){ |
85 |
Button editLoadBalancer = (Button)findViewById(R.id.edit_loadbalancer_button); |
86 |
editLoadBalancer.setOnClickListener(new OnClickListener() {
|
87 |
|
88 |
@Override
|
89 |
public void onClick(View v) { |
90 |
Intent editLoadBalancerIntent = new Intent(context, EditLoadBalancerActivity.class);
|
91 |
editLoadBalancerIntent.putExtra("loadBalancer", loadBalancer);
|
92 |
startActivityForResult(editLoadBalancerIntent, EDIT_LOAD_BALANCER_CODE); |
93 |
} |
94 |
|
95 |
}); |
96 |
|
97 |
Button deleteLoadBalancer = (Button)findViewById(R.id.delete_loadbalancer_button); |
98 |
deleteLoadBalancer.setOnClickListener(new OnClickListener() {
|
99 |
|
100 |
@Override
|
101 |
public void onClick(View v) { |
102 |
showDialog(R.id.view_server_delete_button); |
103 |
} |
104 |
|
105 |
}); |
106 |
|
107 |
Button editNodes = (Button)findViewById(R.id.edit_nodes_button); |
108 |
editNodes.setOnClickListener(new OnClickListener() {
|
109 |
|
110 |
@Override
|
111 |
public void onClick(View v) { |
112 |
Intent editLoadBalancerIntent = new Intent(context, EditNodesActivity.class);
|
113 |
Log.d("info", "the nodes are null before?" + Boolean.toString(returnLoadBalancer.getNodes() == null)); |
114 |
editLoadBalancerIntent.putExtra("nodes", returnLoadBalancer.getNodes());
|
115 |
editLoadBalancerIntent.putExtra("loadBalancer", returnLoadBalancer);
|
116 |
startActivityForResult(editLoadBalancerIntent, EDIT_NODES_CODE); |
117 |
} |
118 |
}); |
119 |
|
120 |
|
121 |
} |
122 |
|
123 |
@Override
|
124 |
protected Dialog onCreateDialog(int id) { |
125 |
switch (id) {
|
126 |
case R.id.view_server_delete_button:
|
127 |
return new AlertDialog.Builder(ViewLoadBalancerActivity.this) |
128 |
.setIcon(R.drawable.alert_dialog_icon) |
129 |
.setTitle("Delete Load Balancer")
|
130 |
.setMessage("Are you sure you want to delete the load balancer?")
|
131 |
.setPositiveButton("Delete", new DialogInterface.OnClickListener() { |
132 |
public void onClick(DialogInterface dialog, int whichButton) { |
133 |
// User clicked OK so do some stuff
|
134 |
new DeleteLoadBalancerTask().execute((Void[]) null); |
135 |
} |
136 |
}) |
137 |
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { |
138 |
public void onClick(DialogInterface dialog, int whichButton) { |
139 |
// User clicked Cancel so do some stuff
|
140 |
} |
141 |
}) |
142 |
.create(); |
143 |
} |
144 |
return null; |
145 |
} |
146 |
|
147 |
private void loadLoadBalancerData() { |
148 |
TextView name = (TextView) findViewById(R.id.view_name); |
149 |
name.setText(returnLoadBalancer.getName()); |
150 |
|
151 |
TextView id = (TextView) findViewById(R.id.view_lb_id); |
152 |
id.setText(returnLoadBalancer.getId()); |
153 |
|
154 |
TextView protocol = (TextView) findViewById(R.id.view_protocol); |
155 |
protocol.setText(returnLoadBalancer.getProtocol()); |
156 |
|
157 |
TextView port = (TextView) findViewById(R.id.view_port); |
158 |
port.setText(returnLoadBalancer.getPort()); |
159 |
|
160 |
TextView algorithm = (TextView) findViewById(R.id.view_algorithm); |
161 |
algorithm.setText(returnLoadBalancer.getAlgorithm()); |
162 |
|
163 |
TextView status = (TextView) findViewById(R.id.view_status); |
164 |
status.setText(returnLoadBalancer.getStatus()); |
165 |
|
166 |
TextView connectionLogging = (TextView) findViewById(R.id.view_islogging); |
167 |
connectionLogging.setText(returnLoadBalancer.getIsConnectionLoggingEnabled()); |
168 |
|
169 |
loadVirutalIpData(); |
170 |
} |
171 |
|
172 |
private void loadVirutalIpData() { |
173 |
TextView vipId = (TextView) findViewById(R.id.view_vip_id); |
174 |
vipId.setText(returnLoadBalancer.getVirtualIps().get(0).getId());
|
175 |
|
176 |
TextView address = (TextView) findViewById(R.id.view_vip_address); |
177 |
address.setText(returnLoadBalancer.getVirtualIps().get(0).getAddress());
|
178 |
|
179 |
TextView ipVersion = (TextView) findViewById(R.id.view_ipversion); |
180 |
ipVersion.setText(returnLoadBalancer.getVirtualIps().get(0).getIpVersion());
|
181 |
|
182 |
TextView type = (TextView) findViewById(R.id.view_vip_type); |
183 |
type.setText(returnLoadBalancer.getVirtualIps().get(0).getType());
|
184 |
|
185 |
loadNodeData(); |
186 |
} |
187 |
|
188 |
private void loadNodeData() { |
189 |
TextView nodeID = (TextView) findViewById(R.id.view_node_id); |
190 |
nodeID.setText(returnLoadBalancer.getNodes().get(0).getId());
|
191 |
|
192 |
TextView address = (TextView) findViewById(R.id.view_node_address); |
193 |
address.setText(returnLoadBalancer.getNodes().get(0).getAddress());
|
194 |
|
195 |
TextView nodePort = (TextView) findViewById(R.id.view_node_port); |
196 |
nodePort.setText(returnLoadBalancer.getNodes().get(0).getPort());
|
197 |
|
198 |
TextView condition = (TextView) findViewById(R.id.view_node_condition); |
199 |
condition.setText(returnLoadBalancer.getNodes().get(0).getCondition());
|
200 |
|
201 |
TextView nodeStatus = (TextView) findViewById(R.id.view_node_status); |
202 |
nodeStatus.setText(returnLoadBalancer.getNodes().get(0).getStatus());
|
203 |
|
204 |
} |
205 |
|
206 |
//setup menu for when menu button is pressed
|
207 |
public boolean onCreateOptionsMenu(Menu menu) { |
208 |
super.onCreateOptionsMenu(menu);
|
209 |
MenuInflater inflater = getMenuInflater(); |
210 |
inflater.inflate(R.menu.view_loadbalancer_menu, menu); |
211 |
return true; |
212 |
} |
213 |
|
214 |
@Override
|
215 |
public boolean onOptionsItemSelected(MenuItem item) { |
216 |
switch (item.getItemId()) {
|
217 |
case R.id.refresh_loadbalancer:
|
218 |
new LoadLoadBalancerTask().execute((Void[]) null); |
219 |
return true; |
220 |
} |
221 |
return false; |
222 |
} |
223 |
|
224 |
@Override
|
225 |
public void onActivityResult(int requestCode, int resultCode, Intent data) { |
226 |
super.onActivityResult(requestCode, resultCode, data);
|
227 |
|
228 |
if (resultCode == RESULT_OK) {
|
229 |
new LoadLoadBalancerTask().execute((Void[]) null); |
230 |
} |
231 |
} |
232 |
|
233 |
private void startLoadBalancerError(String message, HttpBundle bundle){ |
234 |
Intent viewIntent = new Intent(getApplicationContext(), ServerErrorActivity.class);
|
235 |
viewIntent.putExtra("errorMessage", message);
|
236 |
viewIntent.putExtra("response", bundle.getResponseText());
|
237 |
viewIntent.putExtra("request", bundle.getCurlRequest());
|
238 |
startActivity(viewIntent); |
239 |
} |
240 |
|
241 |
//using cloudServersException, it works for us too
|
242 |
private CloudServersException parseCloudServersException(HttpResponse response) {
|
243 |
CloudServersException cse = new CloudServersException();
|
244 |
try {
|
245 |
BasicResponseHandler responseHandler = new BasicResponseHandler();
|
246 |
String body = responseHandler.handleResponse(response);
|
247 |
CloudServersFaultXMLParser parser = new CloudServersFaultXMLParser();
|
248 |
SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser(); |
249 |
XMLReader xmlReader = saxParser.getXMLReader(); |
250 |
xmlReader.setContentHandler(parser); |
251 |
xmlReader.parse(new InputSource(new StringReader(body))); |
252 |
cse = parser.getException(); |
253 |
} catch (ClientProtocolException e) {
|
254 |
cse = new CloudServersException();
|
255 |
cse.setMessage(e.getLocalizedMessage()); |
256 |
} catch (IOException e) { |
257 |
cse = new CloudServersException();
|
258 |
cse.setMessage(e.getLocalizedMessage()); |
259 |
} catch (ParserConfigurationException e) { |
260 |
cse = new CloudServersException();
|
261 |
cse.setMessage(e.getLocalizedMessage()); |
262 |
} catch (SAXException e) {
|
263 |
cse = new CloudServersException();
|
264 |
cse.setMessage(e.getLocalizedMessage()); |
265 |
} catch (FactoryConfigurationError e) { |
266 |
cse = new CloudServersException();
|
267 |
cse.setMessage(e.getLocalizedMessage()); |
268 |
} |
269 |
return cse;
|
270 |
} |
271 |
|
272 |
// HTTP request tasks
|
273 |
private class PollServerTask extends AsyncTask<Void, Void, LoadBalancer> { |
274 |
|
275 |
@Override
|
276 |
protected LoadBalancer doInBackground(Void... arg0) { |
277 |
try {
|
278 |
returnLoadBalancer = (new LoadBalancerManager(context)).getLoadBalancerById(Integer.parseInt(loadBalancer.getId())); |
279 |
} catch (NumberFormatException e) { |
280 |
// we're polling, so need to show exceptions
|
281 |
} catch (LoadBalancersException e) {
|
282 |
// we're polling, so need to show exceptions
|
283 |
} |
284 |
return returnLoadBalancer;
|
285 |
} |
286 |
|
287 |
@Override
|
288 |
protected void onPostExecute(LoadBalancer result) { |
289 |
returnLoadBalancer = result; |
290 |
loadLoadBalancerData(); |
291 |
} |
292 |
} |
293 |
|
294 |
private class LoadLoadBalancerTask extends AsyncTask<Void, Void, LoadBalancer> { |
295 |
private LoadBalancersException exception;
|
296 |
|
297 |
protected void onPreExecute() { |
298 |
showDialog(); |
299 |
} |
300 |
|
301 |
@Override
|
302 |
protected LoadBalancer doInBackground(Void... arg0) { |
303 |
LoadBalancer result = null;
|
304 |
try {
|
305 |
result = (new LoadBalancerManager(context)).getLoadBalancerById(Integer.parseInt(loadBalancer.getId())); |
306 |
} catch (LoadBalancersException e) {
|
307 |
exception = e; |
308 |
} |
309 |
pDialog.dismiss(); |
310 |
return result;
|
311 |
} |
312 |
|
313 |
@Override
|
314 |
protected void onPostExecute(LoadBalancer result) { |
315 |
if (exception != null) { |
316 |
pDialog.dismiss(); |
317 |
showAlert("Error", exception.getMessage());
|
318 |
} |
319 |
pDialog.dismiss(); |
320 |
returnLoadBalancer = result; |
321 |
loadLoadBalancerData(); |
322 |
} |
323 |
} |
324 |
|
325 |
public class DeleteLoadBalancerTask extends AsyncTask<Void, Void, HttpBundle> { |
326 |
|
327 |
private CloudServersException exception;
|
328 |
|
329 |
@Override
|
330 |
protected void onPreExecute(){ |
331 |
showDialog(); |
332 |
} |
333 |
|
334 |
@Override
|
335 |
protected HttpBundle doInBackground(Void... arg0) { |
336 |
HttpBundle bundle = null;
|
337 |
try {
|
338 |
bundle = (new LoadBalancerManager(context)).delete(loadBalancer);
|
339 |
} catch (CloudServersException e) {
|
340 |
exception = e; |
341 |
} |
342 |
return bundle;
|
343 |
} |
344 |
|
345 |
@Override
|
346 |
protected void onPostExecute(HttpBundle bundle) { |
347 |
pDialog.dismiss(); |
348 |
HttpResponse response = bundle.getResponse(); |
349 |
if (response != null) { |
350 |
int statusCode = response.getStatusLine().getStatusCode();
|
351 |
if (statusCode == 202) { |
352 |
//showToast("Delete successful");
|
353 |
setResult(Activity.RESULT_OK); |
354 |
finish(); |
355 |
} else {
|
356 |
CloudServersException cse = parseCloudServersException(response); |
357 |
if ("".equals(cse.getMessage())) { |
358 |
startLoadBalancerError("There was a problem deleting your load balancer.", bundle);
|
359 |
} else {
|
360 |
startLoadBalancerError("There was a problem deleting your load balancer: " + cse.getMessage(), bundle);
|
361 |
} |
362 |
} |
363 |
} else if (exception != null) { |
364 |
startLoadBalancerError("There was a problem deleting your load balancer: " + exception.getMessage(), bundle);
|
365 |
} |
366 |
} |
367 |
} |
368 |
|
369 |
private void showAlert(String title, String message) { |
370 |
// Can't create handler inside thread that has not called
|
371 |
// Looper.prepare()
|
372 |
// Looper.prepare();
|
373 |
try {
|
374 |
AlertDialog alert = new AlertDialog.Builder(this).create(); |
375 |
alert.setTitle(title); |
376 |
alert.setMessage(message); |
377 |
alert.setButton("OK", new DialogInterface.OnClickListener() { |
378 |
public void onClick(DialogInterface dialog, int which) { |
379 |
return;
|
380 |
} |
381 |
}); |
382 |
alert.show(); |
383 |
} catch (Exception e) { |
384 |
e.printStackTrace(); |
385 |
} |
386 |
} |
387 |
|
388 |
protected void showDialog() { |
389 |
pDialog = new ProgressDialog(this, R.style.NewDialog); |
390 |
// // Set blur to background
|
391 |
WindowManager.LayoutParams lp = pDialog.getWindow().getAttributes(); |
392 |
lp.dimAmount = 0.0f;
|
393 |
pDialog.getWindow().setAttributes(lp); |
394 |
pDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND); |
395 |
pDialog.show(); |
396 |
pDialog.setContentView(new ProgressBar(this), new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); |
397 |
} |
398 |
} |