/**
   * Download the given resource url and store it in the local cache Directory.
   *
   * @param resource
   * @param destinationDir The URL of the destination Directoy
   */
  public void downloadCacheResource(final StorageResource resource) {
    try {
      if (resource == null) return;
      logger.log(
          Level.INFO,
          "downloadCacheResource "
              + resource.getResourceUrl()
              + " Version:"
              + resource.getVersion());
      storageManager.getCacheDirectoryEntry(
          new Callback<DirectoryEntry, StorageError>() {
            public void onSuccess(DirectoryEntry cacheDir) {
              try {
                FileTransfer fileTransfer = phonegap.getFile().createFileTransfer();
                String localFileName =
                    storageManager.convertFilePathToFileName(resource.getResourceUrl());
                String sourceUrl = storageManager.getRemoteAppBaseUrl() + resource.getResourceUrl();
                String destUrl = cacheDir.toURL() + localFileName;
                // String destUrl =
                // "cdvfile://localhost/persistent/testapp/test.mp4";
                logger.log(
                    Level.INFO, "downloadResource invoked for : " + sourceUrl + " to : " + destUrl);
                fileTransfer.download(sourceUrl, destUrl, getResourceDownloadHandler(resource));
              } catch (Exception lex) {
                logger.log(Level.SEVERE, "Exception in downloadCacheResource success handler", lex);
              }
            }

            public void onFailure(StorageError error) {
              logger.log(
                  Level.WARNING,
                  "Failed to download CacheResource for : " + resource.getResourceUrl());
              if (resource.getDownloadNotification() != null) {
                resource
                    .getDownloadNotification()
                    .onFailure(new TransferError(error.getErrorCode(), error.getErrorReason()));
              }
            }
          });
    } catch (Exception ex) {
      logger.log(Level.SEVERE, "Exception resourceDownload for : " + resource.getResourceUrl(), ex);
    }
  }
  @Override
  public void execute() {
    try {
      Boolean versionCheck = storageManager.checkResourceVersion(storageResource);
      if (versionCheck == null) {
        logger.log(
            Level.WARNING,
            "ResourceCacheReference retrieval no chache entry found : "
                + storageResource.getResourceUrl()
                + " requestedVersion:"
                + storageResource.getVersion()
                + " -> invoke loading");
        downloadCacheResource(storageResource);
      } else if (versionCheck == true) {
        // it should be there already and version is ok
        logger.log(
            Level.INFO,
            "Successful ResourceCacheReference retrieval : " + storageResource.getResourceUrl());
        // check if it really exists but asynch
        String fileName =
            storageManager.convertFilePathToFileName(storageResource.getResourceUrl());
        storageManager.getLocalFileReference(
            storageManager.getCacheDirectory(),
            fileName,
            false,
            new FileCallback<FileEntry, StorageError>() {
              @Override
              public void onSuccess(FileEntry entry) {
                logger.log(
                    Level.INFO,
                    "Successful ResourceCacheFile retrieval : " + storageResource.getResourceUrl());
                // the cache is ok, file is there in right version, we don't have to
                // do something really.
                if (storageResource.getDownloadNotification() != null) {
                  storageResource.getDownloadNotification().onSuccess(entry);
                }
              }

              @Override
              public void onFailure(StorageError error) {
                logger.log(
                    Level.SEVERE,
                    "Failure ResourceCacheReference retrieval : "
                        + storageResource.getResourceUrl()
                        + " : "
                        + error.toString());
                // nothing found, we must try to download again
                downloadCacheResource(storageResource);
              }
            });
      } else if (versionCheck == false) {
        // version doesn't match, invoke reload
        logger.log(
            Level.WARNING,
            "ResourceCacheReference retrieval version mismatch : "
                + storageResource.getResourceUrl()
                + " requestedVersion:"
                + storageResource.getVersion()
                + " -> invoke loading");
        downloadCacheResource(storageResource);
      }
    } catch (Exception ex) {
      logger.log(Level.SEVERE, "Exception checking ResourceCache", ex);
    }
  }