/* * [Upload.java] * * Summary: Upload files to website. Works by spawning custom command. * * Copyright: (c) 2003-2017 Roedy Green, Canadian Mind Products, http://mindprod.com * * Licence: This software may be copied and used freely for any purpose but military. * http://mindprod.com/contact/nonmil.html * * Requires: JDK 1.8+ * * Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/ * * Version History: * 10.2 2009-04-03 tidy up code to check presence of necessary files to make it more WORA. */ package com.mindprod.replicatorsender; import com.mindprod.replicatorcommon.ReplicatorCommon; import java.io.IOException; import static java.lang.System.*; /** * Upload files to website. Works by spawning custom command. * * @author Roedy Green, Canadian Mind Products * @version 10.2 2009-04-03 tidy up code to check presence of necessary files to make it more WORA. * @since 2003-10-11 */ final class Upload { /** * wrapper for Runtime.exec. You will not see the console output of the spawned command. * * @param command fully qualified *.exe or *.com command * @param wait true if should wait until child has terminated. * * @return Process object * @throws IOException if can't exec. */ @SuppressWarnings( { "SameParameterValue" } ) private static Process exec( String command, boolean wait ) throws IOException { Process p = Runtime.getRuntime().exec( command ); // don't catch IO Exception here if ( wait ) { try { p.waitFor(); } catch ( InterruptedException e ) { Thread.currentThread().interrupt(); } } // end if // You must close these fool things even if you never use them! p.getInputStream().close(); p.getOutputStream().close(); p.getErrorStream().close(); return p; } // end exec /** * upload files to website. E.g. website files, *.zips, manifests. */ private static void upload() { for ( int i = 0; i < ConfigForSender.MAX_UPLOAD_TRIES; i++ ) { try { Process p = exec( ConfigForSender.UPLOAD, true/* wait */ ); int retCode = p.exitValue(); if ( retCode <= ConfigForSender.HIGHEST_ACCEPTABLE_EXIT_CODE || ConfigForSender .HIGHEST_ACCEPTABLE_EXIT_CODE == -1 ) { return; } ReplicatorSender.warn( ConfigForSender.UPLOAD + "\n" + "upload failed with code " + retCode ); } catch ( IOException e ) { ReplicatorSender.warn( ConfigForSender.UPLOAD + "\n" + "upload failed" + "\n" + e.getMessage() ); } // recover from failed upload if ( ConfigForSender.RECOVER.length() > 0 ) { try { err.println( "recovering from failed upload." ); exec( ConfigForSender.RECOVER, true/* wait */ ); } catch ( IOException e ) { ReplicatorSender.warn( ConfigForSender.RECOVER + "\n" + "recover failed" + "\n" + e.getMessage() ); } } } // fell out bottom of retry loop without success. ReplicatorCommon.fatal( ConfigForSender.UPLOAD + "\n" + "unable to successfully upload after " + ConfigForSender.MAX_UPLOAD_TRIES + " tries." ); } // end upload /** * upload recently generated manifest files */ public static void uploadManifests() { // upload files have prepared so far to the website. // also deletes emaciated zips if ( ConfigForSender.UPLOAD.length() != 0 ) { out.println( "uploading manifests..." ); Upload.upload(); } else { out.println( "manifests not uploaded yet." ); } } /** * upload recently generated zip files, and any other files that upload wants to upload. */ public static void uploadZips() { // upload files and zips have prepared so far to the website. if ( ConfigForSender.UPLOAD.length() != 0 ) { out.println( "uploading zips..." ); Upload.upload(); } else { out.println( "zips not uploaded yet." ); } } }