public void execute()

in uima-build-helper-maven-plugin/src/main/java/org/apache/uima/buildhelper/CopyFromApacheDist.java [86:225]


  public void execute() throws MojoExecutionException {
     //  repoloc / org/apache/uima  / artifactId / version / artifactId - version - classifier . type
    String targetInLocalFileSystem = String.format("%s/%s/%s/%s/%s-%s%s.%s",
        repository,
        groupId.replace('.', '/'),
        artifactId,
        version,
        artifactId,
        version,
        (classifier.length() > 0) ? ("-" + classifier) : "",
        type);    
    
    File targetFile = new File(targetInLocalFileSystem);
    if (targetFile.exists()) {
      System.out.format("copy-from-apache-dist returning, file %s exists%n", targetInLocalFileSystem);
      return;
    }
     // http://archive.apache.org/dist/uima/  artifactId - version / artifactId  - version - classifier . type
    String remoteLocation = String.format("http://%s.apache.org/dist/uima/%s-%s/%s-%s%s.%s",
        "archive",
//        "www",
        artifactId,
        version,
        artifactId,
        version,
        (classifier.length() > 0) ? ("-" + classifier) : "",
        type);

    // read remote file
    URL remoteURL = null;
    try {
      remoteURL = new URL(remoteLocation);
    } catch (MalformedURLException e) {
      throw new MojoExecutionException("Bad URL internally: " + remoteLocation, e);
    }
  
    FileOutputStream os = null;
    targetFile.getParentFile().mkdirs();
    if (targetFile.exists()) {
      System.out.format(" *** Surprise, file %s exists on 2nd check%n", targetInLocalFileSystem);
      return;
    }
    try {      
      os = new FileOutputStream(targetFile);
    } catch (FileNotFoundException e) {
      throw new MojoExecutionException("While creating local file in location " + targetFile.getAbsolutePath(), e);    
    }
    
    int totalSize = 0;
    int readSoFar = 0;

  retryLoop:
    for (int retry = 0; retry < MAXRETRIES; retry ++) {
    
      HttpURLConnection remoteConnection = null;
      InputStream is = null;
      
      try {
        remoteConnection = (HttpURLConnection) remoteURL.openConnection();
        if (readSoFar > 0) {
          String rangespec = String.format("bytes=%d-", readSoFar);
          System.out.format("Requesting range: %s%n", rangespec);
          remoteConnection.setRequestProperty("Range", rangespec);
        }
        if (totalSize == 0) { 
          totalSize = remoteConnection.getContentLength();
          if (totalSize < MINTOTALSIZE) {
            throw new MojoExecutionException(String.format("File size %d too small for %s%n", totalSize, remoteLocation));
          }
        }

        is =  remoteConnection.getInputStream();
//        if (readSoFar > 0) {
//          System.out.format("Skipping over %,d bytes read so far; this may take some time%n", readSoFar);
//          long skipped = is.skip(readSoFar);
//          if (skipped != readSoFar) {
//            System.out.format("Skipping only skipped %,d out of %,d; retrying", skipped, readSoFar);
//            continue retryLoop;
//          }
//        }
      } catch (IOException e) {
        throw new MojoExecutionException("While reading remote location " + remoteLocation, e);
      }
  
  
      System.out.format("copy-from-apache-dist file %s to %s%n", remoteLocation, targetInLocalFileSystem);
      System.out.format("%,12d of %,12d\r", readSoFar, totalSize);
      
      byte[] buf = new byte[1024*1024];  // buffer size
      long timeToLog = System.currentTimeMillis();
      while(true) {
        int bytesRead;
        try {
          bytesRead = is.read(buf);
        } catch (IOException e) {
          throw new MojoExecutionException("While reading remote file in location " + remoteLocation, e);    
        }
        if (bytesRead < 0 ) {
          if (readSoFar == totalSize) {
            System.out.format("%,12d of %,12d%nFinished%n", readSoFar, totalSize);          
            break;
          }
          System.out.format("%n *** Premature EOF, %,12d read out of %,12d   Retry %d%n", readSoFar, totalSize, retry);
          try {
            is.close();
          } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
          }
          if (retry == (MAXRETRIES - 1)) {
            throw new MojoExecutionException("CopyFromApacheDist retry limit exceeded ");   
          }
          continue retryLoop;
        }
        retry = 0;  // reset retry count because we read some good bytes
        try {
          os.write(buf, 0, bytesRead);
        } catch (IOException e) {
          throw new MojoExecutionException("While writing target file in location " + targetFile.getAbsolutePath(), e);    
        }
        readSoFar = readSoFar + bytesRead;
        if (System.currentTimeMillis() - timeToLog > 1000) {
          timeToLog = System.currentTimeMillis();
          System.out.format("%,12d of %,12d\r", readSoFar, totalSize);          
        }
      }

      try {
        os.close();
      } catch (IOException e) {
        throw new MojoExecutionException("While closing target file in location " + targetFile.getAbsolutePath(), e);    
      }
      try {
        is.close();
      } catch (IOException e) {
        throw new MojoExecutionException("While closing remote file in location " + remoteLocation, e);    
      }
      break;  // out of retry loop
    }
  }