Exemplo n.º 1
0
  /**
   * Starts the download component.
   *
   * @throws SmartFrogException in case of error in starting
   * @throws RemoteException in case of network/emi error
   */
  @Override
  public synchronized void sfStart() throws SmartFrogException, RemoteException {
    super.sfStart();
    log = sfGetApplicationLog();
    String url = "NOT YET SET";
    String localFile = "NOT YET SET";
    try {
      url = (String) sfResolve(ATTR_URL);
      localFile = FileSystem.lookupAbsolutePath(this, ATTR_LOCALFILE, null, null, true, null);

      int blocksize = ((Integer) sfResolve(ATTR_BLOCKSIZE)).intValue();
      int maxCacheAge = -1;
      maxCacheAge = sfResolve(ATTR_MAX_CACHE_AGE, maxCacheAge, false);
      if (sfLog().isInfoEnabled()) {
        sfLog().info(" Downloading '" + url + "' to '" + localFile + "'. Blocksize: " + blocksize);
      }
      File target = new File(localFile);
      bind(target);
      download(url, target, blocksize, maxCacheAge);
      if (sfLog().isInfoEnabled()) {
        sfLog().info(" Download complete. File in: " + target);
      }

      new ComponentHelper(this)
          .sfSelfDetachAndOrTerminate(null, "Download completed. File in: " + target, null, null);

    } catch (Exception e) {
      String errStr = ERROR_IN_DOWNLOAD + url + " to " + localFile;
      if (log.isErrorEnabled()) {
        log.error(errStr, e);
      }
      throw SmartFrogLifecycleException.forward(errStr, e, this);
    }
  }
Exemplo n.º 2
0
  /**
   * Simple Download
   *
   * @param url url to download from
   * @param localFile local file name
   * @param blocksize block size to download
   * @param maxCacheAge the max time (in seconds) that proxies should cache things. -1 = forever.
   * @throws IOException for IO error
   */
  public static void download(String url, File localFile, int blocksize, int maxCacheAge)
      throws IOException {
    // FileOutputStream object.
    FileOutputStream fs = null;
    // InputStream.
    InputStream is = null;

    byte[] b = new byte[blocksize];
    int bytesRead;
    boolean finished = false;
    // create our output directories.
    localFile.getParentFile().mkdirs();
    try {
      // open the URL,
      URL endpoint = new URL(url);
      URLConnection connection = endpoint.openConnection();
      if (maxCacheAge >= 0) {
        connection.addRequestProperty(CACHE_CONTROL, Integer.toString(maxCacheAge));
      }
      is = connection.getInputStream();

      // open the file,
      fs = new FileOutputStream(localFile);

      // transfer the data
      do {
        bytesRead = is.read(b, 0, blocksize);
        if (bytesRead > 0) {
          fs.write(b, 0, bytesRead);
        }
      } while (bytesRead > 0);
      // mark as finished
      finished = true;
      fs.close();
    } finally {
      FileSystem.close(fs);
      FileSystem.close(is);
      // delete any half-downloaded local file if
      // something went wrong during download.
      if (!finished && localFile.exists()) {
        localFile.delete();
      }
    }
  }