Statistics
| Branch: | Revision:

root / src / com / rackspacecloud / android / ViewLoadBalancerActivity.java @ 5018a7f8

History | View | Annotate | Download (25.2 kB)

1
/**
2
 * 
3
 */
4
package com.rackspacecloud.android;
5

    
6
import java.util.ArrayList;
7
import java.util.Collections;
8

    
9
import org.apache.http.HttpResponse;
10

    
11
import android.app.Activity;
12
import android.app.AlertDialog;
13
import android.app.Dialog;
14
import android.content.DialogInterface;
15
import android.content.Intent;
16
import android.graphics.Color;
17
import android.os.AsyncTask;
18
import android.os.Bundle;
19
import android.util.Log;
20
import android.util.TypedValue;
21
import android.view.Menu;
22
import android.view.MenuInflater;
23
import android.view.MenuItem;
24
import android.view.View;
25
import android.view.View.OnClickListener;
26
import android.widget.Button;
27
import android.widget.HorizontalScrollView;
28
import android.widget.LinearLayout;
29
import android.widget.TextView;
30

    
31
import com.rackspace.cloud.loadbalancer.api.client.LoadBalancer;
32
import com.rackspace.cloud.loadbalancer.api.client.LoadBalancerManager;
33
import com.rackspace.cloud.loadbalancer.api.client.Node;
34
import com.rackspace.cloud.loadbalancer.api.client.VirtualIp;
35
import com.rackspace.cloud.loadbalancer.api.client.http.LoadBalancersException;
36
import com.rackspace.cloud.servers.api.client.CloudServersException;
37
import com.rackspace.cloud.servers.api.client.http.HttpBundle;
38

    
39
public class ViewLoadBalancerActivity extends CloudActivity {
40

    
41
        private final int EDIT_LOAD_BALANCER_CODE = 184;
42
        private final int EDIT_NODES_CODE = 185;
43
        private final int EDIT_THROTTLE_CODE = 186;
44
        private final int EDIT_ACCESS_CONTROL_CODE = 187;
45

    
46
        private final String DELETED = "DELETED";
47

    
48
        private LoadBalancer loadBalancer;
49
        private PollLoadBalancerTask pollLoadBalancerTask;
50
        private AndroidCloudApplication app;
51
        private LoggingListenerTask loggingListenerTask;
52
        private SessionPersistenceListenerTask sessionPersistenceListenerTask;
53

    
54
        @Override
55
        public void onCreate(Bundle savedInstanceState) {
56
                super.onCreate(savedInstanceState);
57
                trackPageView(GoogleAnalytics.PAGE_LOADBALANCER);
58
                loadBalancer = (LoadBalancer) this.getIntent().getExtras().get("loadBalancer");
59
                setContentView(R.layout.view_loadbalancer);
60
                app = (AndroidCloudApplication)this.getApplication();
61
                restoreState(savedInstanceState);
62
        }
63

    
64
        @Override
65
        protected void onSaveInstanceState(Bundle outState) {
66
                super.onSaveInstanceState(outState);
67
                outState.putSerializable("loadBalancer", loadBalancer);
68
        }
69

    
70
        protected void restoreState(Bundle state) {
71
                super.restoreState(state);
72
                
73
                if (state != null && state.containsKey("loadBalancer") && state.getSerializable("loadBalancer") != null) {
74
                        loadBalancer = (LoadBalancer) state.getSerializable("loadBalancer");
75
                        loadLoadBalancerData();
76
                        setUpButtons();
77
                }
78
                else{
79
                        new LoadLoadBalancerTask().execute((Void[]) null);
80
                }
81
                
82
                /*
83
                 * if is setting logs we need another listener
84
                 */
85
                if(app.isSettingLogs()){
86
                        loggingListenerTask = new LoggingListenerTask();
87
                        loggingListenerTask.execute();
88
                }
89
                
90
                if(app.isSettingSessionPersistence()){
91
                        sessionPersistenceListenerTask = new SessionPersistenceListenerTask();
92
                        sessionPersistenceListenerTask.execute();
93
                }
94
        }
95

    
96
        @Override
97
        protected void onDestroy(){
98
                super.onDestroy();
99

    
100
                //need to cancel pollLoadBalancerTask so it 
101
                //does not keep polling in a new activity
102
                if(pollLoadBalancerTask != null){
103
                        pollLoadBalancerTask.cancel(true);
104
                }
105
        }
106

    
107
        @Override
108
        protected void onStop(){
109
                super.onStop();
110

    
111
                /*
112
                 * Need to stop running listener task
113
                 * if we exit
114
                 */
115
                if(loggingListenerTask != null){
116
                        loggingListenerTask.cancel(true);
117
                }
118
                
119
                if(sessionPersistenceListenerTask != null){
120
                        sessionPersistenceListenerTask.cancel(true);
121
                }
122
        }
123
        
124
        private void setupButton(int resourceId, OnClickListener onClickListener) {
125
                Button button = (Button) findViewById(resourceId);
126
                button.setOnClickListener(onClickListener);
127
        }
128

    
129
        //change the text on the button depending
130
        //on the state of Connection Logging
131
        private void setLogButtonText(){
132
                Button logs = (Button)findViewById(R.id.connection_log_button);
133
                String loggingEnabled = loadBalancer.getIsConnectionLoggingEnabled();
134
                if(loggingEnabled != null && loggingEnabled.equals("true")){
135
                        logs.setText("Disable Logs");
136
                } else {
137
                        logs.setText("Enable Logs");
138
                }
139
        }
140

    
141
        //change the text on the button depending
142
        //on the state of Session Persistence
143
        private void setSessionButtonText(){
144
                Button sessionPersistence = (Button)findViewById(R.id.session_persistence_button);
145
                //session persistence is null if it is off
146
                if(loadBalancer.getSessionPersistence() != null){
147
                        sessionPersistence.setText("Disable Session Persistence");
148
                } else {
149
                        sessionPersistence.setText("Enable Session Persistence");
150
                }
151
        }
152

    
153
        //if the load balancer state contains DELETE
154
        //then parts of it may be null, so use a different
155
        //onClick in that condition
156
        private void setUpButtons(){
157
                if(loadBalancer != null){
158
                        setupButton(R.id.edit_loadbalancer_button, new OnClickListener() {
159
                                @Override
160
                                public void onClick(View v) {
161
                                        if(!loadBalancer.getStatus().contains(DELETED)){
162
                                                Intent editLoadBalancerIntent = new Intent(getContext(), EditLoadBalancerActivity.class);
163
                                                editLoadBalancerIntent.putExtra("loadBalancer", loadBalancer);
164
                                                startActivityForResult(editLoadBalancerIntent, EDIT_LOAD_BALANCER_CODE);
165
                                        } else {
166
                                                showAlert(loadBalancer.getStatus(), "The load balancer cannot be updated.");
167
                                        }
168
                                }
169
                        });
170

    
171

    
172
                        setupButton(R.id.delete_loadbalancer_button, new OnClickListener() {
173
                                @Override
174
                                public void onClick(View v) {
175
                                        if(!loadBalancer.getStatus().contains(DELETED)){
176
                                                showDialog(R.id.view_server_delete_button);
177
                                        } else {
178
                                                showAlert(loadBalancer.getStatus(), "The load balancer cannot be deleted.");
179
                                        }
180
                                }
181

    
182
                        });
183

    
184
                        setupButton(R.id.edit_nodes_button, new OnClickListener() {
185
                                @Override
186
                                public void onClick(View v) {
187
                                        if(!loadBalancer.getStatus().contains(DELETED)){
188
                                                Intent editLoadBalancerIntent = new Intent(getContext(), EditNodesActivity.class);
189
                                                editLoadBalancerIntent.putExtra("nodes", loadBalancer.getNodes());
190
                                                editLoadBalancerIntent.putExtra("loadBalancer", loadBalancer);
191
                                                startActivityForResult(editLoadBalancerIntent, EDIT_NODES_CODE);
192
                                        } else {
193
                                                showAlert(loadBalancer.getStatus(), "The nodes cannot be edited.");
194
                                        }
195
                                }
196
                        });
197

    
198
                        setupButton(R.id.connection_log_button, new OnClickListener() {
199
                                @Override
200
                                public void onClick(View v) {
201
                                        if(!loadBalancer.getStatus().contains(DELETED)){
202
                                                showDialog(R.id.connection_log_button);        
203
                                        } else {
204
                                                showAlert(loadBalancer.getStatus(), "Log settings cannot be edited.");        
205
                                        }
206
                                }
207
                        });
208
                        setLogButtonText();
209

    
210
                        setupButton(R.id.session_persistence_button, new OnClickListener() {
211
                                @Override
212
                                public void onClick(View v) {
213
                                        if(!loadBalancer.getStatus().contains(DELETED)){
214
                                                if(!loadBalancer.getProtocol().equals("HTTP")){
215
                                                        showAlert("Error", "Session Persistence cannot be enabled for protocols other than HTTP.");
216
                                                } else {
217
                                                        showDialog(R.id.session_persistence_button);
218
                                                }
219
                                        } else {
220
                                                showAlert(loadBalancer.getStatus(), "Session Persistence cannot be edited.");        
221
                                        }
222
                                }
223
                        });
224
                        setSessionButtonText();
225

    
226
                        setupButton(R.id.connection_throttle_button, new OnClickListener() {
227
                                @Override
228
                                public void onClick(View v) {
229
                                        if(!loadBalancer.getStatus().contains(DELETED)){
230
                                                Intent editLoadBalancerIntent = new Intent(getContext(), ConnectionThrottleActivity.class);
231
                                                editLoadBalancerIntent.putExtra("loadBalancer", loadBalancer);
232
                                                startActivityForResult(editLoadBalancerIntent, EDIT_THROTTLE_CODE);
233
                                        } else {
234
                                                showAlert(loadBalancer.getStatus(), "Connection Throttle cannot be edited.");
235
                                        }
236
                                }
237
                        });
238

    
239
                        setupButton(R.id.access_control_button, new OnClickListener() {
240
                                @Override
241
                                public void onClick(View v) {
242
                                        if(!loadBalancer.getStatus().contains(DELETED)){
243
                                                Intent editLoadBalancerIntent = new Intent(getContext(), AccessControlActivity.class);
244
                                                editLoadBalancerIntent.putExtra("loadBalancer", loadBalancer);
245
                                                startActivityForResult(editLoadBalancerIntent, EDIT_ACCESS_CONTROL_CODE);
246
                                        } else {
247
                                                showAlert(loadBalancer.getStatus(), "Access Control cannot be edited.");
248
                                        }
249
                                }
250
                        });
251
                }
252
        }
253

    
254
        @Override
255
        protected Dialog onCreateDialog(int id) {
256
                switch (id) {
257
                case R.id.view_server_delete_button:
258
                        return new AlertDialog.Builder(ViewLoadBalancerActivity.this)
259
                        .setIcon(R.drawable.alert_dialog_icon)
260
                        .setTitle("Delete Load Balancer")
261
                        .setMessage("Are you sure you want to delete the load balancer?")
262
                        .setPositiveButton("Delete", new DialogInterface.OnClickListener() {
263
                                public void onClick(DialogInterface dialog, int whichButton) {
264
                                        trackEvent(GoogleAnalytics.CATEGORY_LOAD_BALANCER, GoogleAnalytics.EVENT_DELETE, "", -1);
265
                                        new DeleteLoadBalancerTask().execute((Void[]) null);
266
                                }
267
                        })
268
                        .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
269
                                public void onClick(DialogInterface dialog, int whichButton) {
270
                                        // do nothing
271
                                }
272
                        })
273
                        .create();
274
                case R.id.connection_log_button:
275
                        return new AlertDialog.Builder(ViewLoadBalancerActivity.this)
276
                        .setIcon(R.drawable.alert_dialog_icon)
277
                        .setTitle("Disable Logs")
278
                        .setMessage("Are you sure you want to disable logs for this Load Balancer?")
279
                        .setPositiveButton("Enable", new DialogInterface.OnClickListener() {
280
                                public void onClick(DialogInterface dialog, int whichButton) {
281
                                        trackEvent(GoogleAnalytics.CATEGORY_LOAD_BALANCER, GoogleAnalytics.EVENT_LB_CONNECTION_LOGGING, "", -1);
282
                                        new SetLoggingTask().execute();        
283
                                }
284
                        })
285
                        .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
286
                                public void onClick(DialogInterface dialog, int whichButton) {
287
                                        // do nothing
288
                                }
289
                        })
290
                        .create();
291
                case R.id.session_persistence_button:
292
                        return new AlertDialog.Builder(ViewLoadBalancerActivity.this)
293
                        .setIcon(R.drawable.alert_dialog_icon)
294
                        .setTitle("Session Persistence")
295
                        .setMessage("Are you sure you want to disable session persistence for this Load Balancer?")
296
                        .setPositiveButton("Enable", new DialogInterface.OnClickListener() {
297
                                public void onClick(DialogInterface dialog, int whichButton) {
298
                                        //trackEvent(GoogleAnalytics.CATEGORY_LOAD_BALANCER, GoogleAnalytics.EVENT_LB_SESSION_PERSISTENCE, "", -1);
299
                                        new SessionPersistenceTask().execute();
300
                                }
301
                        })
302
                        .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
303
                                public void onClick(DialogInterface dialog, int whichButton) {
304
                                        // do nothing
305
                                }
306
                        })
307
                        .create();
308
                }
309
                return null;
310
        }
311

    
312
        @Override
313
        //Need to show different message depending on the state
314
        //of connection_logs/session_persistence
315
        protected void onPrepareDialog(int id, Dialog dialog){
316
                if(loadBalancer != null){
317
                        switch (id) {
318
                        case R.id.connection_log_button:
319
                                String logTitle;
320
                                String logMessage;
321
                                String logButton;
322
                                if(loadBalancer.getIsConnectionLoggingEnabled().equals("true")){
323
                                        logTitle = "Disable Logs";
324
                                        logMessage = "Are you sure you want to disable logs for this Load Balancer?";
325
                                        logButton = "Disable";
326
                                } else {
327
                                        logTitle = "Enable Logs";
328
                                        logMessage = "Log files will be processed every hour and stored in your Cloud Files account. " +
329
                                        "Standard Cloud Files storage and transfer fees will be accessed for the use of this feature." +
330
                                        "\n\nAre you sure you want to enable logs for this Load Balancer?";
331
                                        logButton = "Enable";
332
                                }
333
                                ((AlertDialog)dialog).setTitle(logTitle);
334
                                ((AlertDialog)dialog).setMessage(logMessage);
335
                                Button sessionLogButton = ((AlertDialog)dialog).getButton(AlertDialog.BUTTON1);
336
                                sessionLogButton.setText(logButton);
337
                                sessionLogButton.invalidate();
338
                                break;
339
                        case R.id.session_persistence_button:
340
                                String sessionMessage;
341
                                String sessionButton;
342
                                if(loadBalancer.getSessionPersistence() != null){
343
                                        Log.d("info", "in sessionpersistence != null");
344
                                        sessionMessage = "Are you sure you want to disable session persistence for this Load Balancer?";
345
                                        sessionButton = "Disable";
346
                                } else {
347
                                        Log.d("info", "in sessionpersistence == null");
348
                                        sessionMessage = "Are you sure you want to enable session persistence for this Load Balancer?";
349
                                        sessionButton = "Enable";
350
                                }
351
                                ((AlertDialog)dialog).setMessage(sessionMessage);
352
                                Button sessionPersistButton = ((AlertDialog)dialog).getButton(AlertDialog.BUTTON1);
353
                                sessionPersistButton.setText(sessionButton);
354
                                sessionPersistButton.invalidate();
355
                                break;
356
                        }
357
                }
358
        }
359

    
360
        //Displays all the load balancer data
361
        private void loadLoadBalancerData() {
362
                if(loadBalancer != null){
363
                        /*
364
                         * need to update the text on button if 
365
                         * it has changed
366
                         */
367
                        setLogButtonText();
368
                        setSessionButtonText();
369

    
370

    
371
                        TextView name = (TextView) findViewById(R.id.view_name);
372
                        name.setText(loadBalancer.getName());
373

    
374
                        TextView id = (TextView) findViewById(R.id.view_lb_id);
375
                        id.setText(loadBalancer.getId());
376

    
377
                        TextView protocol = (TextView) findViewById(R.id.view_protocol);
378
                        protocol.setText(loadBalancer.getProtocol());
379

    
380
                        TextView port = (TextView) findViewById(R.id.view_port);
381
                        port.setText(loadBalancer.getPort());
382

    
383
                        TextView algorithm = (TextView) findViewById(R.id.view_algorithm);
384
                        algorithm.setText(getPrettyAlgoName(loadBalancer.getAlgorithm()));
385

    
386
                        TextView status = (TextView) findViewById(R.id.view_status);
387
                        if (!"ACTIVE".equals(loadBalancer.getStatus())) {
388
                                status.setText(loadBalancer.getStatus());
389
                                pollLoadBalancerTask = new PollLoadBalancerTask();
390
                                pollLoadBalancerTask.execute((Void[]) null);
391
                        } else {
392
                                status.setText(loadBalancer.getStatus());
393
                        }
394

    
395
                        status.setText(loadBalancer.getStatus());
396

    
397
                        TextView connectionLogging = (TextView) findViewById(R.id.view_islogging);
398
                        String isConnectionLogging = loadBalancer.getIsConnectionLoggingEnabled();
399
                        if(isConnectionLogging != null && isConnectionLogging.equals("true")){
400
                                connectionLogging.setText("Enabled");
401
                        } else {
402
                                connectionLogging.setText("Disabled");
403
                        }
404

    
405
                        loadVirutalIpData();
406
                }
407
        }
408

    
409
        private String getPrettyAlgoName(String name){
410
                if(name == null || name.length() == 0){
411
                        return "";
412
                } else {
413
                        String result = name.charAt(0) + "";
414
                        boolean previousWasSpace = false;;
415
                        for(int i = 1; i < name.length(); i++){
416
                                char curLetter = name.charAt(i);
417
                                if(curLetter == '_'){
418
                                        result += " ";
419
                                        previousWasSpace = true;
420
                                } else {
421
                                        if(previousWasSpace){
422
                                                result += Character.toUpperCase(curLetter);
423
                                        } else {
424
                                                result += Character.toLowerCase(curLetter);
425
                                        }
426
                                        previousWasSpace = false;
427
                                }
428
                        }
429
                        return result;
430
                }
431
        }
432

    
433
        private void loadVirutalIpData() {
434
                int layoutIndex = 0;
435
                LinearLayout layout = (LinearLayout) this.findViewById(R.id.vip_addresses);    
436
                layout.removeAllViews();
437
                ArrayList<VirtualIp> virtualIps = loadBalancer.getVirtualIps();
438
                //maybe null if the lb has been deleted
439
                if(virtualIps != null){
440
                        for (int i = 0; i < virtualIps.size(); i++) {
441
                                TextView tv = new TextView(this.getBaseContext());
442
                                //tv.setLayoutParams(((TextView)findViewById(R.id.view_port)).getLayoutParams()); // easy quick styling! :)
443
                                tv.setTypeface(tv.getTypeface(), 1); // 1 == bold
444
                                tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
445
                                tv.setTextColor(Color.WHITE);
446
                                tv.setText(virtualIps.get(i).getAddress());
447
                                HorizontalScrollView scroll = new HorizontalScrollView(this.getBaseContext());
448
                                scroll.setLayoutParams(((TextView)findViewById(R.id.view_port)).getLayoutParams()); // easy quick styling! :)
449
                                scroll.setScrollbarFadingEnabled(true);
450
                                scroll.addView(tv);
451
                                layout.addView(scroll, layoutIndex++);
452
                        }
453
                }
454

    
455
                loadNodeData();
456
        }
457

    
458
        private void loadNodeData() {
459
                int layoutIndex = 0; // public IPs start here
460
                LinearLayout layout = (LinearLayout) this.findViewById(R.id.node_addresses);   
461
                layout.removeAllViews();
462
                ArrayList<Node> nodeIps = loadBalancer.getNodes();
463
                if(nodeIps == null){
464
                        nodeIps = new ArrayList<Node>();
465
                }
466

    
467
                /*
468
                 * need to sort the addresses because during polling
469
                 * their order can change and the display becomes
470
                 * jumpy
471
                 */
472
                ArrayList<String> addresses = new ArrayList<String>();
473
                for(Node n : nodeIps){
474
                        addresses.add(n.getAddress());
475
                }
476

    
477
                Collections.sort(addresses);
478

    
479
                //may be null if lb has been deleted
480
                if(nodeIps != null){
481
                        for (int i = 0; i < nodeIps.size(); i++) {
482
                                TextView tv = new TextView(this.getBaseContext());
483
                                tv.setLayoutParams(((TextView)findViewById(R.id.view_port)).getLayoutParams()); // easy quick styling! :)
484
                                tv.setTypeface(tv.getTypeface(), 1); // 1 == bold
485
                                tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
486
                                tv.setTextColor(Color.WHITE);
487
                                tv.setText(addresses.get(i));
488
                                HorizontalScrollView scroll = new HorizontalScrollView(this.getBaseContext());
489
                                scroll.setLayoutParams(((TextView)findViewById(R.id.view_port)).getLayoutParams()); // easy quick styling! :)
490
                                scroll.setScrollbarFadingEnabled(true);
491
                                scroll.addView(tv);
492
                                layout.addView(scroll, layoutIndex++);
493
                        }
494
                }
495
        }
496

    
497
        //setup menu for when menu button is pressed
498
        public boolean onCreateOptionsMenu(Menu menu) {
499
                super.onCreateOptionsMenu(menu);
500
                MenuInflater inflater = getMenuInflater();
501
                inflater.inflate(R.menu.view_loadbalancer_menu, menu);
502
                return true;
503
        } 
504

    
505
        @Override 
506
        public boolean onOptionsItemSelected(MenuItem item) {
507
                switch (item.getItemId()) {
508
                case R.id.refresh_loadbalancer:
509
                        new LoadLoadBalancerTask().execute((Void[]) null);   
510
                        return true;
511
                }        
512
                return false;
513
        } 
514

    
515
        @Override
516
        //have been kicked back from another activity,
517
        //so refresh the load balancer data
518
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
519
                super.onActivityResult(requestCode, resultCode, data);
520
                if (resultCode == RESULT_OK) {
521
                        new LoadLoadBalancerTask().execute((Void[]) null);   
522
                }
523
        }
524

    
525
        // HTTP request tasks
526
        private class PollLoadBalancerTask extends AsyncTask<Void, Void, LoadBalancer> {
527

    
528
                @Override
529
                protected LoadBalancer doInBackground(Void... arg0) {
530
                        if(pollLoadBalancerTask.isCancelled()){
531
                                return null;
532
                        }
533
                        try {
534
                                loadBalancer = (new LoadBalancerManager(getContext())).getLoadBalancerById(Integer.parseInt(loadBalancer.getId()));
535
                        } catch (NumberFormatException e) {
536
                                // we're polling, so need to show exceptions
537
                        } catch (LoadBalancersException e) {
538
                                // we're polling, so need to show exceptions
539
                        }
540
                        return loadBalancer;
541
                }
542

    
543
                @Override
544
                protected void onPostExecute(LoadBalancer result) {
545
                        loadBalancer = result;
546
                        loadLoadBalancerData();
547
                }
548
        }
549

    
550
        private class LoadLoadBalancerTask extends AsyncTask<Void, Void, LoadBalancer> {
551

    
552
                private LoadBalancersException exception;
553
                private String loadBalancerId;
554

    
555
                protected void onPreExecute() {
556
                        loadBalancerId = loadBalancer.getId();
557
                        /*
558
                         * set to null, so if config change occurs
559
                         * it will be reloaded in onCreate()
560
                         */
561
                        loadBalancer = null;
562
                        showDialog();
563
                }
564

    
565
                @Override
566
                protected LoadBalancer doInBackground(Void... arg0) {
567
                        LoadBalancer result = null;
568
                        try {
569
                                result = (new LoadBalancerManager(getContext())).getLoadBalancerById(Integer.parseInt(loadBalancerId));
570
                        } catch (LoadBalancersException e) {
571
                                exception = e;
572
                        }
573
                        return result;
574
                }
575

    
576
                @Override
577
                protected void onPostExecute(LoadBalancer result) {
578
                        hideDialog();
579
                        if (exception != null) {
580
                                showAlert("Error", exception.getMessage());
581
                        }
582
                        loadBalancer = result;
583

    
584
                        setUpButtons();
585
                        loadLoadBalancerData();
586
                }
587
        } 
588

    
589
        public class DeleteLoadBalancerTask extends AsyncTask<Void, Void, HttpBundle> {
590

    
591
                private CloudServersException exception;
592

    
593
                @Override
594
                protected void onPreExecute(){
595
                        showDialog();
596
                }
597

    
598
                @Override
599
                protected HttpBundle doInBackground(Void... arg0) {
600
                        HttpBundle bundle = null;
601
                        try {
602
                                bundle = (new LoadBalancerManager(getContext())).delete(loadBalancer);
603
                        } catch (CloudServersException e) {
604
                                exception = e;
605
                        }
606
                        return bundle;
607
                }
608

    
609
                @Override
610
                protected void onPostExecute(HttpBundle bundle) {
611
                        hideDialog();
612
                        HttpResponse response = bundle.getResponse();
613
                        if (response != null) {
614
                                int statusCode = response.getStatusLine().getStatusCode();
615
                                if (statusCode == 202) {
616
                                        setResult(Activity.RESULT_OK);
617
                                        finish();
618
                                } else {
619
                                        CloudServersException cse = parseCloudServersException(response);
620
                                        if ("".equals(cse.getMessage())) {
621
                                                showError("There was a problem deleting your load balancer.", bundle);
622
                                        } else {
623
                                                showError("There was a problem deleting your load balancer: " + cse.getMessage(), bundle);
624
                                        }
625
                                }
626
                        } else if (exception != null) {
627
                                showError("There was a problem deleting your load balancer: " + exception.getMessage(), bundle);                                
628
                        }                        
629
                }
630
        }
631

    
632

    
633
        private class SetLoggingTask extends AsyncTask<Void, Void, HttpBundle> {
634

    
635
                private CloudServersException exception;
636

    
637
                @Override
638
                protected void onPreExecute(){
639
                        //showDialog();
640
                        app.setIsSettingLogs(true);
641
                        loggingListenerTask = new LoggingListenerTask();
642
                        loggingListenerTask.execute();
643
                }
644

    
645
                @Override
646
                protected HttpBundle doInBackground(Void... arg0) {
647
                        HttpBundle bundle = null;        
648
                        try {
649
                                bundle = (new LoadBalancerManager(getContext())).setLogging(loadBalancer, !Boolean.valueOf(loadBalancer.getIsConnectionLoggingEnabled()));
650
                        } catch (CloudServersException e) {
651
                                exception = e;
652
                        }
653
                        return bundle;
654
                }
655

    
656
                @Override
657
                protected void onPostExecute(HttpBundle bundle) {
658
                        //hideDialog();
659
                        app.setIsSettingLogs(false);
660
                        HttpResponse response = bundle.getResponse();
661
                        if (response != null) {
662
                                int statusCode = response.getStatusLine().getStatusCode();                        
663
                                if (statusCode == 202 || statusCode == 204) {
664
                                        if(Boolean.valueOf(loadBalancer.getIsConnectionLoggingEnabled())){
665
                                                showToast("Logging has been disabled");
666
                                        } else {
667
                                                showToast("Logging has been enabled");
668
                                        }
669
                                } else {                                        
670
                                        CloudServersException cse = parseCloudServersException(response);
671
                                        if ("".equals(cse.getMessage())) {
672
                                                showError("There was a problem changing your log settings.", bundle);
673
                                        } else {
674
                                                showError("There was a problem changing your log settings: " + cse.getMessage(), bundle);
675
                                        }                                        
676
                                }
677
                        } else if (exception != null) {
678
                                showError("There was a problem changing your log settings: " + exception.getMessage(), bundle);
679

    
680
                        }
681

    
682
                }
683
        }
684

    
685
        /*
686
         * listens for the application to change isSettingLogs
687
         * listens so activity knows when it should display
688
         * the new settings
689
         */
690
        private class LoggingListenerTask extends
691
        AsyncTask<Void, Void, Void> {
692

    
693
                @Override
694
                protected Void doInBackground(Void... arg1) {
695

    
696
                        while(app.isSettingLogs()){
697
                                // wait for process to finish
698
                                // or have it be canceled
699
                                if(loggingListenerTask.isCancelled()){
700
                                        return null;
701
                                }
702
                        }
703
                        return null;
704
                }
705

    
706
                /*
707
                 * when no longer processing, time to load
708
                 * the new files
709
                 */
710
                @Override
711
                protected void onPostExecute(Void arg1) {
712
                        pollLoadBalancerTask = new PollLoadBalancerTask();
713
                        pollLoadBalancerTask.execute((Void[]) null);
714
                }
715
        }
716
        
717
        private class SessionPersistenceTask extends AsyncTask<Void, Void, HttpBundle> {
718

    
719
                private CloudServersException exception;
720
                
721
                @Override
722
                protected void onPreExecute(){
723
                        app.setSettingSessionPersistence(true);
724
                        sessionPersistenceListenerTask = new SessionPersistenceListenerTask();
725
                        sessionPersistenceListenerTask.execute();
726
                }
727

    
728
                @Override
729
                protected HttpBundle doInBackground(Void... arg0) {
730
                        HttpBundle bundle = null;        
731
                        try {
732
                                String currentSetting = loadBalancer.getSessionPersistence();
733
                                if(currentSetting == null){
734
                                        bundle = (new LoadBalancerManager(getContext())).setSessionPersistence(loadBalancer, "HTTP_COOKIE");
735
                                } else {
736
                                        bundle = (new LoadBalancerManager(getContext())).disableSessionPersistence(loadBalancer);
737
                                }
738
                        } catch (CloudServersException e) {
739
                                exception = e;
740
                        }
741
                        return bundle;
742
                }
743
                
744
                @Override
745
                protected void onPostExecute(HttpBundle bundle) {
746
                        //hideDialog();
747
                        app.setSettingSessionPersistence(false);
748
                        HttpResponse response = bundle.getResponse();
749
                        if (response != null) {
750
                                int statusCode = response.getStatusLine().getStatusCode();                        
751
                                if (statusCode == 202 || statusCode == 200) {
752
                                        if(loadBalancer.getSessionPersistence() != null){
753
                                                showToast("Session Persistence has been disabled");
754
                                        } else {
755
                                                showToast("Session Persistence has been enabled");
756
                                        }
757
                                        pollLoadBalancerTask = new PollLoadBalancerTask();
758
                                        pollLoadBalancerTask.execute((Void[]) null);
759
                                } else {                                        
760
                                        CloudServersException cse = parseCloudServersException(response);
761
                                        if ("".equals(cse.getMessage())) {
762
                                                showError("There was a problem changing your session persistence settings.", bundle);
763
                                        } else {
764
                                                showError("There was a problem changing your session persistence settings: " + cse.getMessage(), bundle);
765
                                        }                                        
766
                                }
767
                        } else if (exception != null) {
768
                                showError("There was a problem changing your session persistence settings: " + exception.getMessage(), bundle);
769

    
770
                        }
771

    
772
                }
773
        }
774
        
775
        /*
776
         * listens for the application to change isSettingSessionPersistence
777
         * listens so activity knows when it should display
778
         * the new settings
779
         */
780
        private class SessionPersistenceListenerTask extends
781
        AsyncTask<Void, Void, Void> {
782

    
783
                @Override
784
                protected Void doInBackground(Void... arg1) {
785

    
786
                        while(app.isSettingSessionPersistence()){
787
                                // wait for process to finish
788
                                // or have it be canceled
789
                                if(sessionPersistenceListenerTask.isCancelled()){
790
                                        return null;
791
                                }
792
                        }
793
                        return null;
794
                }
795

    
796
                /*
797
                 * when no longer processing, time to load
798
                 * the new files
799
                 */
800
                @Override
801
                protected void onPostExecute(Void arg1) {
802
                        pollLoadBalancerTask = new PollLoadBalancerTask();
803
                        pollLoadBalancerTask.execute((Void[]) null);
804
                }
805
        }
806

    
807

    
808
}