· 9 years ago · Dec 10, 2016, 10:56 AM
1package com.corenttech.engine.cloud.amazonvpc;
2
3import com.amazonaws.AmazonServiceException;
4import com.amazonaws.ClientConfiguration;
5import com.amazonaws.auth.AWSCredentials;
6import com.amazonaws.auth.BasicAWSCredentials;
7import com.amazonaws.auth.PropertiesCredentials;
8import com.amazonaws.services.ec2.AmazonEC2Client;
9import com.amazonaws.services.s3.AmazonS3;
10import com.amazonaws.services.s3.AmazonS3Client;
11import com.corenttech.core.log.LoggingUtility;
12import com.corenttech.engine.utility.StringUtility;
13import java.io.ByteArrayInputStream;
14import java.io.IOException;
15import java.io.InputStream;
16import java.io.PrintStream;
17
18public class AmazonClientConnection
19{
20 private static LoggingUtility log = LoggingUtility.getInstance(AmazonClientConnection.class);
21
22 public static AmazonEC2Client establishConnection(AmazonCredentials creds)
23 {
24 System.out.println("Establish Connection");
25 AmazonEC2Client client = null;
26 int conAttempts = 0;
27 try
28 {
29 client = establishNewConnection(creds);
30 }
31 catch (Exception ex)
32 {
33 client = reTryConnection(ex.getMessage(), creds, ++conAttempts);
34 }
35 return client;
36 }
37
38 public static AmazonEC2Client establishNewConnection(AmazonCredentials creds)
39 {
40 AmazonEC2Client ec2Client = null;
41 try
42 {
43 System.out.println("Inside Establish Connection");
44
45 String credentialsString = "accessKey = " + creds.getAccessKeyId() + "\n";
46 credentialsString = credentialsString + "secretKey = " + creds.getSecretKey() + "\n";
47 InputStream is = new ByteArrayInputStream(credentialsString.getBytes());
48
49 AWSCredentials credentials = new PropertiesCredentials(is);
50
51 ec2Client = new AmazonEC2Client(credentials);
52
53 ec2Client.setEndpoint("ec2.us-east-1.amazonaws.com");
54
55 ec2Client.describeAvailabilityZones();
56 return ec2Client;
57 }
58 catch (IOException ex)
59 {
60 System.out.println("<< IOException at EstablishNewConnection >>" + ex.getMessage());
61 }
62 catch (AmazonServiceException ex)
63 {
64 throw new AmazonServiceException(ex.getMessage());
65 }
66 System.out.println("Eccccc---------------" + ec2Client);
67 return null;
68 }
69
70 public static AWSCredentials establishAWSCredentials(AmazonCredentials creds)
71 {
72 try
73 {
74 System.out.println("Establish AWS Credential");
75
76 String credentialsString = "accessKey = " + creds.getAccessKeyId() + "\n";
77 credentialsString = credentialsString + "secretKey = " + creds.getSecretKey() + "\n";
78
79 InputStream is = new ByteArrayInputStream(credentialsString.getBytes());
80 System.out.println("Input Stream-----------------" + is);
81 System.out.println("Establish CONNECTION" + is);
82 AWSCredentials credentials = new PropertiesCredentials(is);
83 System.out.println("Establish CONNECTION" + credentials);
84 return credentials;
85 }
86 catch (IOException ex)
87 {
88 System.out.println("<< IOException at establishAWSCredentials >>" + ex.getMessage());
89 }
90 catch (AmazonServiceException ex)
91 {
92 throw new AmazonServiceException(ex.getMessage());
93 }
94 return null;
95 }
96
97 private static AmazonEC2Client reTryConnection(String errorMsg, AmazonCredentials creds, int conAttempts)
98 {
99 AmazonEC2Client client = null;
100 if (!StringUtility.isNullOrEmpty(errorMsg))
101 {
102 System.out.println("<< reTryConnection >>" + errorMsg);
103 if ("<response success=\"false\" code=\"500\">".equalsIgnoreCase(errorMsg)) {
104 try
105 {
106 System.out.println("<< reTryConnection 1st attempt >>");
107 Thread.sleep(10000L);
108 client = establishNewConnection(creds);
109 }
110 catch (InterruptedException localInterruptedException) {}catch (AmazonServiceException ex)
111 {
112 if (conAttempts < 3) {
113 reTryConnection(errorMsg, creds, ++conAttempts);
114 } else {
115 throw new AmazonServiceException(ex.getMessage());
116 }
117 }
118 } else {
119 throw new AmazonServiceException(errorMsg);
120 }
121 }
122 return client;
123 } public static AmazonS3 S3establishNewConnection(AmazonCredentials creds) {
124 AmazonS3 s3client = null;
125 try {
126 System.out.println("Inside Establish S3Connection");
127 String credentialsString;
128 credentialsString = "accessKey = " + creds.getAccessKeyId() + "\n";
129 credentialsString = "secretKey = " + creds.getSecretKey() + "\n";
130 ClientConfiguration clientConfig = new ClientConfiguration();
131 AWSCredentials credentials = new BasicAWSCredentials(creds.getAccessKeyId(), creds.getSecretKey());
132 s3client = new AmazonS3Client(credentials, clientConfig);
133 } catch (AmazonServiceException ex) {
134 throw new AmazonServiceException(ex.getMessage());
135 }
136 return s3client;
137 }
138}