Revision 6ecf99bb
b/src/com/rackspace/cloud/loadbalancer/api/client/Algorithm.java | ||
---|---|---|
1 |
package com.rackspace.cloud.loadbalancer.api.client; |
|
2 |
|
|
3 |
import java.util.ArrayList; |
|
4 |
|
|
5 |
public class Algorithm extends Entity{ |
|
6 |
|
|
7 |
/** |
|
8 |
* |
|
9 |
*/ |
|
10 |
private static final long serialVersionUID = -6728669291005268796L; |
|
11 |
private static ArrayList<Algorithm> algorithms; |
|
12 |
private String name; |
|
13 |
|
|
14 |
/** |
|
15 |
* @param name the name to set |
|
16 |
*/ |
|
17 |
public void setName(String name) { |
|
18 |
this.name = name; |
|
19 |
} |
|
20 |
/** |
|
21 |
* @return the name |
|
22 |
*/ |
|
23 |
public String getName() { |
|
24 |
return name; |
|
25 |
} |
|
26 |
|
|
27 |
/** |
|
28 |
* @return the algorithms |
|
29 |
*/ |
|
30 |
public static ArrayList<Algorithm> getAlgorithms() { |
|
31 |
return algorithms; |
|
32 |
} |
|
33 |
/** |
|
34 |
* @param algorithms the algorithms to set |
|
35 |
*/ |
|
36 |
public static void setAlgorithms(ArrayList<Algorithm> algorithms) { |
|
37 |
Algorithm.algorithms = algorithms; |
|
38 |
} |
|
39 |
} |
b/src/com/rackspace/cloud/loadbalancer/api/client/AlgorithmManager.java | ||
---|---|---|
1 |
/** |
|
2 |
* |
|
3 |
*/ |
|
4 |
package com.rackspace.cloud.loadbalancer.api.client; |
|
5 |
|
|
6 |
import java.io.IOException; |
|
7 |
import java.io.StringReader; |
|
8 |
import java.util.ArrayList; |
|
9 |
|
|
10 |
import javax.xml.parsers.FactoryConfigurationError; |
|
11 |
import javax.xml.parsers.ParserConfigurationException; |
|
12 |
import javax.xml.parsers.SAXParser; |
|
13 |
import javax.xml.parsers.SAXParserFactory; |
|
14 |
|
|
15 |
import org.apache.http.HttpResponse; |
|
16 |
import org.apache.http.client.ClientProtocolException; |
|
17 |
import org.apache.http.client.methods.HttpGet; |
|
18 |
import org.apache.http.impl.client.BasicResponseHandler; |
|
19 |
import org.xml.sax.InputSource; |
|
20 |
import org.xml.sax.SAXException; |
|
21 |
import org.xml.sax.XMLReader; |
|
22 |
|
|
23 |
import android.content.Context; |
|
24 |
|
|
25 |
import com.rackspace.cloud.files.api.client.CustomHttpClient; |
|
26 |
import com.rackspace.cloud.loadbalancer.api.parsers.AlgorithmsXMLParser; |
|
27 |
import com.rackspace.cloud.servers.api.client.Account; |
|
28 |
|
|
29 |
public class AlgorithmManager extends EntityManager { |
|
30 |
|
|
31 |
public ArrayList<Algorithm> createList(Context context) { |
|
32 |
CustomHttpClient httpclient = new CustomHttpClient(context); |
|
33 |
HttpGet get = new HttpGet(Account.getAccount().getLoadBalancerDFWUrl() + Account.getAccount().getAccountId() + "/loadbalancers/algorithms.xml"); |
|
34 |
ArrayList<Algorithm> algorithms = new ArrayList<Algorithm>(); |
|
35 |
|
|
36 |
get.addHeader("X-Auth-Token", Account.getAccount().getAuthToken()); |
|
37 |
try { |
|
38 |
HttpResponse resp = httpclient.execute(get); |
|
39 |
BasicResponseHandler responseHandler = new BasicResponseHandler(); |
|
40 |
String body = responseHandler.handleResponse(resp); |
|
41 |
|
|
42 |
if (resp.getStatusLine().getStatusCode() == 200) { |
|
43 |
AlgorithmsXMLParser algorithmsXMLParser = new AlgorithmsXMLParser(); |
|
44 |
SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser(); |
|
45 |
XMLReader xmlReader = saxParser.getXMLReader(); |
|
46 |
xmlReader.setContentHandler(algorithmsXMLParser); |
|
47 |
xmlReader.parse(new InputSource(new StringReader(body))); |
|
48 |
algorithms = algorithmsXMLParser.getAlgorithms(); |
|
49 |
} |
|
50 |
} catch (ClientProtocolException cpe) { |
|
51 |
cpe.printStackTrace(); |
|
52 |
// we'll end up with an empty list; that's good enough |
|
53 |
} catch (IOException e) { |
|
54 |
e.printStackTrace(); |
|
55 |
// we'll end up with an empty list; that's good enough |
|
56 |
} catch (ParserConfigurationException e) { |
|
57 |
e.printStackTrace(); |
|
58 |
// we'll end up with an empty list; that's good enough |
|
59 |
} catch (SAXException e) { |
|
60 |
e.printStackTrace(); |
|
61 |
// we'll end up with an empty list; that's good enough |
|
62 |
} catch (FactoryConfigurationError e) { |
|
63 |
e.printStackTrace(); |
|
64 |
// we'll end up with an empty list; that's good enough |
|
65 |
} |
|
66 |
|
|
67 |
return algorithms; |
|
68 |
} |
|
69 |
|
|
70 |
} |
b/src/com/rackspace/cloud/loadbalancer/api/client/Protocol.java | ||
---|---|---|
1 | 1 |
package com.rackspace.cloud.loadbalancer.api.client; |
2 | 2 |
|
3 | 3 |
import java.util.ArrayList; |
4 |
import java.util.TreeMap; |
|
5 | 4 |
|
6 | 5 |
public class Protocol extends Entity{ |
7 | 6 |
|
b/src/com/rackspace/cloud/loadbalancer/api/client/ProtocolManager.java | ||
---|---|---|
39 | 39 |
HttpResponse resp = httpclient.execute(get); |
40 | 40 |
BasicResponseHandler responseHandler = new BasicResponseHandler(); |
41 | 41 |
String body = responseHandler.handleResponse(resp); |
42 |
Log.d("info", body); |
|
43 | 42 |
|
44 | 43 |
if (resp.getStatusLine().getStatusCode() == 200) { |
45 | 44 |
ProtocolsXMLParser protocolsXMLParser = new ProtocolsXMLParser(); |
b/src/com/rackspace/cloud/loadbalancer/api/parsers/AlgorithmsXMLParser.java | ||
---|---|---|
1 |
/** |
|
2 |
* |
|
3 |
*/ |
|
4 |
package com.rackspace.cloud.loadbalancer.api.parsers; |
|
5 |
|
|
6 |
import java.util.ArrayList; |
|
7 |
|
|
8 |
import org.xml.sax.Attributes; |
|
9 |
import org.xml.sax.helpers.DefaultHandler; |
|
10 |
|
|
11 |
import android.util.Log; |
|
12 |
|
|
13 |
import com.rackspace.cloud.loadbalancer.api.client.Algorithm; |
|
14 |
|
|
15 |
public class AlgorithmsXMLParser extends DefaultHandler { |
|
16 |
private Algorithm algorithm; |
|
17 |
private ArrayList<Algorithm> algorithms; |
|
18 |
private StringBuffer currentData; |
|
19 |
|
|
20 |
public void startDocument() { |
|
21 |
} |
|
22 |
|
|
23 |
public void endDocument() { |
|
24 |
} |
|
25 |
|
|
26 |
public void startElement(String uri, String name, String qName, Attributes atts) { |
|
27 |
|
|
28 |
currentData = new StringBuffer(); |
|
29 |
if ("algorithms".equals(name)) { |
|
30 |
algorithms = new ArrayList<Algorithm>(); |
|
31 |
} else if ("algorithm".equals(name)) { |
|
32 |
algorithm = new Algorithm(); |
|
33 |
algorithm.setName(atts.getValue("name")); |
|
34 |
} |
|
35 |
} |
|
36 |
|
|
37 |
public void endElement(String uri, String name, String qName) { |
|
38 |
if ("algorithm".equals(name)) { |
|
39 |
if (algorithms != null) { |
|
40 |
algorithms.add(algorithm); |
|
41 |
} |
|
42 |
} |
|
43 |
} |
|
44 |
|
|
45 |
public void characters(char ch[], int start, int length) { |
|
46 |
Log.d("Rackspace-Cloud", "Characters: \""); |
|
47 |
for (int i = start; i < start + length; i++) { |
|
48 |
switch (ch[i]) { |
|
49 |
case '\\': |
|
50 |
Log.d("Rackspace-Cloud", "\\\\"); |
|
51 |
break; |
|
52 |
case '"': |
|
53 |
Log.d("Rackspace-Cloud", "\\\""); |
|
54 |
break; |
|
55 |
case '\n': |
|
56 |
Log.d("Rackspace-Cloud", "\\n"); |
|
57 |
break; |
|
58 |
case '\r': |
|
59 |
Log.d("Rackspace-Cloud", "\\r"); |
|
60 |
break; |
|
61 |
case '\t': |
|
62 |
Log.d("Rackspace-Cloud", "\\t"); |
|
63 |
break; |
|
64 |
default: |
|
65 |
Log.d("Rackspace-Cloud", String.valueOf(ch[i])); |
|
66 |
break; |
|
67 |
} |
|
68 |
} |
|
69 |
Log.d("Rackspace-Cloud", "\"\n"); |
|
70 |
|
|
71 |
|
|
72 |
for (int i = start; i < (start + length); i++) { |
|
73 |
currentData.append(ch[i]); |
|
74 |
} |
|
75 |
} |
|
76 |
|
|
77 |
/** |
|
78 |
* @return the algorithm |
|
79 |
*/ |
|
80 |
public Algorithm getAlgorithm() { |
|
81 |
return algorithm; |
|
82 |
} |
|
83 |
|
|
84 |
/** |
|
85 |
* @param algorithm the algorithm to set |
|
86 |
*/ |
|
87 |
public void setAlgorithm(Algorithm algorithm) { |
|
88 |
this.algorithm = algorithm; |
|
89 |
} |
|
90 |
|
|
91 |
/** |
|
92 |
* @return the algorithms |
|
93 |
*/ |
|
94 |
public ArrayList<Algorithm> getAlgorithms() { |
|
95 |
return algorithms; |
|
96 |
} |
|
97 |
|
|
98 |
/** |
|
99 |
* @param algorithms the algorithms to set |
|
100 |
*/ |
|
101 |
public void setAlgorithms(ArrayList<Algorithm> algorithms) { |
|
102 |
this.algorithms = algorithms; |
|
103 |
} |
|
104 |
|
|
105 |
} |
b/src/com/rackspacecloud/android/AddLoadBalancerActivity.java | ||
---|---|---|
4 | 4 |
import java.util.Arrays; |
5 | 5 |
import java.util.Iterator; |
6 | 6 |
|
7 |
import com.rackspace.cloud.loadbalancer.api.client.Algorithm; |
|
7 | 8 |
import com.rackspace.cloud.loadbalancer.api.client.LoadBalancer; |
8 | 9 |
import com.rackspace.cloud.loadbalancer.api.client.Protocol; |
9 | 10 |
import com.rackspace.cloud.loadbalancer.api.client.ProtocolManager; |
... | ... | |
26 | 27 |
public class AddLoadBalancerActivity extends Activity implements OnItemSelectedListener { |
27 | 28 |
|
28 | 29 |
private Protocol[] protocols; |
30 |
private Algorithm[] algorithms; |
|
29 | 31 |
private LoadBalancer loadBalancer; |
30 | 32 |
private Context context; |
31 | 33 |
private Spinner protocolSpinner; |
34 |
private Spinner algorithmSpinner; |
|
32 | 35 |
private EditText portText; |
33 | 36 |
private Protocol selectedProtocol; |
34 | 37 |
|
... | ... | |
43 | 46 |
context = getApplicationContext(); |
44 | 47 |
portText = (EditText)findViewById(R.id.edit_port_text); |
45 | 48 |
loadProtocolSpinner(); |
49 |
loadAlgorithmSpinner(); |
|
46 | 50 |
} |
47 |
|
|
51 |
|
|
48 | 52 |
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { |
49 | 53 |
if (parent == protocolSpinner) { |
50 | 54 |
selectedProtocol = protocols[position]; |
... | ... | |
53 | 57 |
} |
54 | 58 |
|
55 | 59 |
public void onNothingSelected(AdapterView<?> parent) { |
56 |
|
|
60 |
|
|
61 |
} |
|
62 |
|
|
63 |
private void loadProtocolSpinner() { |
|
64 |
protocolSpinner = (Spinner) findViewById(R.id.edit_protocol_spinner); |
|
65 |
protocolSpinner.setOnItemSelectedListener(this); |
|
66 |
String protocolNames[] = new String[Protocol.getProtocols().size()]; |
|
67 |
protocols = new Protocol[Protocol.getProtocols().size()]; |
|
68 |
|
|
69 |
for(int i = 0; i < Protocol.getProtocols().size(); i++){ |
|
70 |
protocols[i] = Protocol.getProtocols().get(i); |
|
71 |
protocolNames[i] = Protocol.getProtocols().get(i).getName(); |
|
72 |
} |
|
73 |
|
|
74 |
ArrayAdapter<String> protocolAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, protocolNames); |
|
75 |
protocolAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); |
|
76 |
protocolSpinner.setAdapter(protocolAdapter); |
|
57 | 77 |
} |
58 | 78 |
|
59 |
private void loadProtocolSpinner() {
|
|
60 |
protocolSpinner = (Spinner) findViewById(R.id.edit_protocol_spinner);
|
|
61 |
protocolSpinner.setOnItemSelectedListener(this);
|
|
62 |
String protocolNames[] = new String[Protocol.getProtocols().size()];
|
|
63 |
protocols = new Protocol[Protocol.getProtocols().size()];
|
|
64 |
|
|
65 |
for(int i = 0; i < Protocol.getProtocols().size(); i++){
|
|
66 |
protocols[i] = Protocol.getProtocols().get(i);
|
|
67 |
protocolNames[i] = Protocol.getProtocols().get(i).getName();
|
|
68 |
}
|
|
69 |
|
|
70 |
ArrayAdapter<String> imageAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, protocolNames);
|
|
71 |
imageAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
|
|
72 |
protocolSpinner.setAdapter(imageAdapter);
|
|
73 |
}
|
|
79 |
private void loadAlgorithmSpinner() {
|
|
80 |
algorithmSpinner = (Spinner) findViewById(R.id.edit_algorithm_spinner);
|
|
81 |
algorithmSpinner.setOnItemSelectedListener(this);
|
|
82 |
String algorithmNames[] = new String[Algorithm.getAlgorithms().size()];
|
|
83 |
algorithms = new Algorithm[Algorithm.getAlgorithms().size()];
|
|
84 |
|
|
85 |
for(int i = 0; i < Algorithm.getAlgorithms().size(); i++){
|
|
86 |
algorithms[i] = Algorithm.getAlgorithms().get(i);
|
|
87 |
algorithmNames[i] = Algorithm.getAlgorithms().get(i).getName();
|
|
88 |
} |
|
89 |
|
|
90 |
ArrayAdapter<String> algorithmAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, algorithmNames);
|
|
91 |
algorithmAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
|
|
92 |
algorithmSpinner.setAdapter(algorithmAdapter);
|
|
93 |
} |
|
74 | 94 |
|
75 | 95 |
private void showAlert(String title, String message) { |
76 | 96 |
AlertDialog alert = new AlertDialog.Builder(this).create(); |
b/src/com/rackspacecloud/android/ListAccountsActivity.java | ||
---|---|---|
10 | 10 |
import java.util.ArrayList; |
11 | 11 |
import java.util.TreeMap; |
12 | 12 |
|
13 |
import com.rackspace.cloud.loadbalancer.api.client.Algorithm; |
|
14 |
import com.rackspace.cloud.loadbalancer.api.client.AlgorithmManager; |
|
13 | 15 |
import com.rackspace.cloud.loadbalancer.api.client.Protocol; |
14 | 16 |
import com.rackspace.cloud.loadbalancer.api.client.ProtocolManager; |
15 | 17 |
import com.rackspace.cloud.servers.api.client.Account; |
... | ... | |
482 | 484 |
protected void onPostExecute(ArrayList<Protocol> result) { |
483 | 485 |
if (result != null && result.size() > 0) { |
484 | 486 |
Protocol.setProtocols(result); |
485 |
new LoadFlavorsTask().execute((Void[]) null);
|
|
487 |
new LoadAlgorithmsTask().execute((Void[]) null);
|
|
486 | 488 |
} else { |
487 | 489 |
showAlert("Login Failure", "There was a problem loading load balancer protocols. Please try again."); |
488 | 490 |
} |
489 | 491 |
} |
490 | 492 |
} |
491 | 493 |
|
494 |
private class LoadAlgorithmsTask extends AsyncTask<Void, Void, ArrayList<Algorithm>> { |
|
495 |
|
|
496 |
@Override |
|
497 |
protected ArrayList<Algorithm> doInBackground(Void... arg0) { |
|
498 |
return (new AlgorithmManager()).createList(context); |
|
499 |
} |
|
500 |
|
|
501 |
@Override |
|
502 |
protected void onPostExecute(ArrayList<Algorithm> result) { |
|
503 |
if (result != null && result.size() > 0) { |
|
504 |
Algorithm.setAlgorithms(result); |
|
505 |
new LoadFlavorsTask().execute((Void[]) null); |
|
506 |
} else { |
|
507 |
showAlert("Login Failure", "There was a problem loading load balancer algorithms. Please try again."); |
|
508 |
} |
|
509 |
} |
|
510 |
} |
|
511 |
|
|
492 | 512 |
private class LoadFlavorsTask extends AsyncTask<Void, Void, ArrayList<Flavor>> { |
493 | 513 |
|
494 | 514 |
@Override |
Also available in: Unified diff