1414import java .io .InputStream ;
1515import java .io .InputStreamReader ;
1616import java .io .OutputStream ;
17+ import java .net .ConnectException ;
1718import java .net .HttpURLConnection ;
1819import java .net .URL ;
1920import java .net .URLEncoder ;
4748
4849public class VmwareContext {
4950 private static final Logger s_logger = Logger .getLogger (VmwareContext .class );
50-
51+
52+ private static int MAX_CONNECT_RETRY = 5 ;
53+ private static int CONNECT_RETRY_INTERVAL = 1000 ;
54+
5155 private ExtendedAppUtil _appUtil ;
5256 private String _serverAddress ;
5357
5458 private Map <String , Object > _stockMap = new HashMap <String , Object >();
55- private int _CHUNKSIZE = 1 *1024 *1024 ; // 1M
59+ private int _CHUNKSIZE = 1 *1024 *1024 ; // 1M
60+
5661
5762 static {
5863 try {
@@ -328,7 +333,7 @@ public void uploadVmdkFile(String httpMethod, String urlString, String localFile
328333 conn .setRequestProperty ("Connection" , "Keep-Alive" );
329334 conn .setRequestProperty ("Content-Type" , "application/x-vnd.vmware-streamVmdk" );
330335 conn .setRequestProperty ("Content-Length" , Long .toString (new File (localFileName ).length ()));
331- conn . connect ( );
336+ connectWithRetry ( conn );
332337
333338 BufferedOutputStream bos = null ;
334339 BufferedInputStream is = null ;
@@ -367,8 +372,8 @@ public long downloadVmdkFile(String urlString, String localFileName,
367372 conn .setRequestProperty (org .apache .axis .transport .http .HTTPConstants .HEADER_COOKIE , cookieString );
368373 conn .setDoInput (true );
369374 conn .setDoOutput (true );
370- conn .setAllowUserInteraction (true );
371- conn . connect ( );
375+ conn .setAllowUserInteraction (true );
376+ connectWithRetry ( conn );
372377
373378 long bytesWritten = 0 ;
374379 InputStream in = null ;
@@ -387,8 +392,6 @@ public long downloadVmdkFile(String urlString, String localFileName,
387392 if (progressUpdater != null )
388393 progressUpdater .action (new Long (totalBytesDownloaded ));
389394 }
390- } catch (Throwable e ) {
391- s_logger .error ("Unexpected exception " , e );
392395 } finally {
393396 if (in != null )
394397 in .close ();
@@ -533,7 +536,7 @@ public boolean verify(String urlHostName, SSLSession session) {
533536 conn .setAllowUserInteraction (true );
534537 conn .setRequestProperty (org .apache .axis .transport .http .HTTPConstants .HEADER_COOKIE , cookieString );
535538 conn .setRequestMethod (httpMethod );
536- conn . connect ( );
539+ connectWithRetry ( conn );
537540 return conn ;
538541 }
539542
@@ -556,6 +559,27 @@ private String getServiceCookie() throws Exception {
556559 org .apache .axis .MessageContext msgContext = callObj .getMessageContext ();
557560 String cookieString = (String )msgContext .getProperty (org .apache .axis .transport .http .HTTPConstants .HEADER_COOKIE );
558561 return cookieString ;
562+ }
563+
564+ private static void connectWithRetry (HttpURLConnection conn ) throws Exception {
565+ boolean connected = false ;
566+ for (int i = 0 ; i < MAX_CONNECT_RETRY && !connected ; i ++) {
567+ try {
568+ conn .connect ();
569+ connected = true ;
570+ s_logger .info ("Connected, conn: " + conn .toString () + ", retry: " + i );
571+ } catch (Exception e ) {
572+ s_logger .warn ("Unable to connect, conn: " + conn .toString () + ", message: " + e .toString () + ", retry: " + i );
573+
574+ try {
575+ Thread .sleep (CONNECT_RETRY_INTERVAL );
576+ } catch (InterruptedException ex ) {
577+ }
578+ }
579+ }
580+
581+ if (!connected )
582+ throw new Exception ("Unable to connect to " + conn .toString ());
559583 }
560584
561585 public void close () {
0 commit comments