public File download(final File workDir, final URL url, final String displayName,
        final Boolean overwrite, final boolean checkAttributes, final boolean failOnValidation,
        final boolean downloadFeeback) {
        LOGGER.debug("Downloading [" + url + "]");
        File downloadedBundlesFile = new File(workDir, "downloaded_bundles.properties");
        Properties fileNamesForUrls = loadProperties(downloadedBundlesFile);

        String downloadedFileName = fileNamesForUrls.getProperty(url.toExternalForm());
        String hashFileName = "" + url.toExternalForm().hashCode();
        if (downloadedFileName == null) {
            // destination will be made based on the hashcode of the url to be downloaded
            downloadedFileName = hashFileName + ".jar";

        }
        File destination = new File(workDir, downloadedFileName);

        // download the bundle only if is a forced overwrite or the file does not exist or the file
        // is there but is
        // invalid
        boolean forceOverwrite = overwrite || !destination.exists();
        if (!forceOverwrite) {
            try {
                String cachingName = determineCachingName(destination, hashFileName);
                if (!destination.getName().equals(cachingName)) {
                    throw new TestContainerException("File " + destination + " should have name "
                        + cachingName);
                }
            }
            catch (TestContainerException ignore) {
                forceOverwrite = true;
            }
        }
        if (forceOverwrite) {
            try {
                LOGGER.debug("Creating new file at destination: " + destination.getAbsolutePath());
                destination.getParentFile().mkdirs();
                destination.createNewFile();
                FileOutputStream os = null;
                try {
                    os = new FileOutputStream(destination);
                    FileChannel fileChannel = os.getChannel();
                    StreamUtils.ProgressBar progressBar = null;
                    if (LOGGER.isInfoEnabled()) {
                        if (downloadFeeback) {
                            progressBar = new StreamUtils.FineGrainedProgressBar(displayName);
                        }
                        else {
                            progressBar = new StreamUtils.CoarseGrainedProgressBar(displayName);
                        }
                    }
                    // Check if this is an exploded bundle...
                    if (url.getPath().endsWith("/") && "file".equals(url.getProtocol())) {
                        StreamUtils.streamCopy(new URL("assembly:" + url.toExternalForm()),
                            fileChannel, progressBar);
                    }
                    else {
                        StreamUtils.streamCopy(url, fileChannel, progressBar);
                    }

                    fileChannel.close();
                    LOGGER.debug("Successfully downloaded to [" + destination + "]");
                }
                finally {
                    if (os != null) {
                        os.close();
                    }
                }
            }
            catch (IOException e) {
                throw new TestContainerException("[" + url + "] could not be downloaded", e);
            }
        }
        if (checkAttributes) {
            try {
                validateBundle(url, destination);
            }
            catch (TestContainerException e) {
                if (failOnValidation) {
                    throw e;
                }
                return null;
            }
        }
        String cachingName = determineCachingName(destination, hashFileName);
        File newDestination = new File(destination.getParentFile(), cachingName);
        if (!cachingName.equals(destination.getName())) {
            if (newDestination.exists()) {
                if (!newDestination.delete()) {
                    throw new TestContainerException("Cannot delete " + newDestination);
                }
            }
            if (!destination.renameTo(newDestination)) {
                throw new TestContainerException("Cannot rename " + destination + " to "
                    + newDestination);
            }
            fileNamesForUrls.setProperty(url.toExternalForm(), cachingName);
            saveProperties(fileNamesForUrls, downloadedBundlesFile);
        }

        return newDestination;
    }
public File download ( final File workDir , final URL url , final String displayName , final Boolean overwrite , final boolean checkAttributes , final boolean failOnValidation , final boolean downloadFeeback ) { LOGGER . debug ( "Downloading [" + url + "]" ) ; File downloadedBundlesFile = new File ( workDir , "downloaded_bundles.properties" ) ; Properties fileNamesForUrls = loadProperties ( downloadedBundlesFile ) ; String downloadedFileName = fileNamesForUrls . getProperty ( url . toExternalForm ( ) ) ; String hashFileName = "" + url . toExternalForm ( ) . hashCode ( ) ; if ( downloadedFileName == null ) { downloadedFileName = hashFileName + ".jar" ; } File destination = new File ( workDir , downloadedFileName ) ; boolean forceOverwrite = overwrite || ! destination . exists ( ) ; if ( ! forceOverwrite ) { try { String cachingName = determineCachingName ( destination , hashFileName ) ; if ( ! destination . getName ( ) . equals ( cachingName ) ) { throw new TestContainerException ( "File " + destination + " should have name " + cachingName ) ; } } catch ( TestContainerException ignore ) { forceOverwrite = true ; } } if ( forceOverwrite ) { try { LOGGER . debug ( "Creating new file at destination: " + destination . getAbsolutePath ( ) ) ; destination . getParentFile ( ) . mkdirs ( ) ; destination . createNewFile ( ) ; FileOutputStream os = null ; try { os = new FileOutputStream ( destination ) ; FileChannel fileChannel = os . getChannel ( ) ; StreamUtils . ProgressBar progressBar = null ; if ( LOGGER . isInfoEnabled ( ) ) { if ( downloadFeeback ) { progressBar = new StreamUtils . FineGrainedProgressBar ( displayName ) ; } else { progressBar = new StreamUtils . CoarseGrainedProgressBar ( displayName ) ; } } if ( url . getPath ( ) . endsWith ( "/" ) && "file" . equals ( url . getProtocol ( ) ) ) { StreamUtils . streamCopy ( new URL ( "assembly:" + url . toExternalForm ( ) ) , fileChannel , progressBar ) ; } else { StreamUtils . streamCopy ( url , fileChannel , progressBar ) ; } fileChannel . close ( ) ; LOGGER . debug ( "Successfully downloaded to [" + destination + "]" ) ; } finally { if ( os != null ) { os . close ( ) ; } } } catch ( IOException e ) { throw new TestContainerException ( "[" + url + "] could not be downloaded" , e ) ; } } if ( checkAttributes ) { try { validateBundle ( url , destination ) ; } catch ( TestContainerException e ) { if ( failOnValidation ) { throw e ; } return null ; } } String cachingName = determineCachingName ( destination , hashFileName ) ; File newDestination = new File ( destination . getParentFile ( ) , cachingName ) ; if ( ! cachingName . equals ( destination . getName ( ) ) ) { if ( newDestination . exists ( ) ) { if ( ! newDestination . delete ( ) ) { throw new TestContainerException ( "Cannot delete " + newDestination ) ; } } if ( ! destination . renameTo ( newDestination ) ) { throw new TestContainerException ( "Cannot rename " + destination + " to " + newDestination ) ; } fileNamesForUrls . setProperty ( url . toExternalForm ( ) , cachingName ) ; saveProperties ( fileNamesForUrls , downloadedBundlesFile ) ; } return newDestination ; }
Downloads files from urls.

@param workDir
the directory where to download bundles
@param url
of the file to be downloaded
@param displayName
to be shown during download
@param overwrite
if the bundles should be overwritten
@param checkAttributes
whether or not to check attributes in the manifest
@param failOnValidation
if validation fails should or not fail with an exception (or just return null)
@param downloadFeeback
whether or not downloading process should display fine grained progres info

@return the File corresponding to the downloaded file, or null if the bundle is invalid (not
an osgi bundle)

@throws TestContainerException
if the url could not be downloaded