Search in sources :

Example 1 with MultipleCandidateException

use of com.ibm.dtfj.utils.file.MultipleCandidateException in project openj9 by eclipse.

the class ImageFactory method getImage.

/**
 * Creates a new Image object based on the contents of imageFile
 *
 * @param imageFile a file with Image information, typically a core file but may also be a container such as a zip
 * @return an instance of Image (null if no image can be constructed from the given file)
 * @throws IOException
 */
@Override
public Image getImage(File imageFile) throws IOException {
    Objects.requireNonNull(imageFile);
    ImageReference imageReference = new ImageReference();
    if (!FileManager.fileExists(imageFile)) {
        // $NON-NLS-1$ //$NON-NLS-2$
        throw new FileNotFoundException("Image file '" + imageFile.getAbsolutePath() + "' not found.");
    }
    exceptions.clear();
    FileManager manager = FileManager.getManager(imageFile);
    List<ManagedImageSource> candidates = manager.getImageSources();
    ManagedImageSource source = null;
    File core = null;
    File meta = null;
    if (FileManager.isArchive(imageFile)) {
        // flag to indicate that a core file has been found
        boolean foundCoreFile = false;
        CompressedFileManager archiveManager = (CompressedFileManager) manager;
        for (ManagedImageSource candidate : candidates) {
            if (candidate.getType().equals(ImageSourceType.CORE)) {
                if (foundCoreFile) {
                    // for legacy behaviour compatibility this can only return 1 core when invoked this way
                    throw new MultipleCandidateException(candidates, imageFile);
                }
                // note the core file
                source = candidate;
                foundCoreFile = true;
            }
        }
        if (foundCoreFile) {
            // if a single core file has been located then extract it and any associated meta data into a temp directory
            File parent = getTempDirParent();
            tmpdir = FileManager.createTempDir(parent);
            archiveManager.extract(source, tmpdir);
            core = source.getExtractedTo();
            if (source.hasMetaData()) {
                archiveManager.extract(source.getMetadata(), tmpdir);
                if (source.getType().equals(ImageSourceType.CORE)) {
                    // when extracting from a zip the archive itself is now the metadata file
                    meta = imageFile;
                } else {
                    meta = source.getMetadata().getExtractedTo();
                }
            }
        } else {
            // $NON-NLS-1$
            throw new IOException("No core files were detected in " + imageFile.getAbsolutePath());
        }
    } else {
        if (candidates.size() > 1) {
            // for backwards behavioural compatibility only one image is allowed to be returned from the supplied file
            throw new MultipleCandidateException(candidates, imageFile);
        }
        source = candidates.get(0);
        core = new File(source.getPath());
        if (source.hasMetaData()) {
            // the presence of a metadata file means that this is not a z/OS dataset and so a FileImageInputStream is safe
            meta = new File(source.getMetadata().getPath());
        }
    }
    for (String factory : source.getType().getFactoryNames()) {
        if (foundImage(factory, imageReference, core, meta)) {
            if (imageReference.image instanceof ManagedImage) {
                ((ManagedImage) imageReference.image).setImageSource(source);
            }
            return imageReference.image;
        }
    }
    printExceptions();
    // the final fallback is to try and get just the native aspect if the file is a core file
    if (source.getType().equals(ImageSourceType.CORE)) {
        com.ibm.dtfj.image.ImageFactory f = createImageFactory(FACTORY_DDR);
        // no valid runtime so return DDR factory for use with Image API
        if (null == f) {
            // $NON-NLS-1$
            throw propagateIOException("Could not create a valid ImageFactory");
        }
        return f.getImage(imageFile);
    } else {
        // $NON-NLS-1$ //$NON-NLS-2$
        throw new IOException("The file " + imageFile.getAbsolutePath() + " was not recognised by any reader");
    }
}
Also used : ManagedImage(com.ibm.dtfj.utils.ManagedImage) FileNotFoundException(java.io.FileNotFoundException) MultipleCandidateException(com.ibm.dtfj.utils.file.MultipleCandidateException) IOException(java.io.IOException) FileManager(com.ibm.dtfj.utils.file.FileManager) CompressedFileManager(com.ibm.dtfj.utils.file.CompressedFileManager) ManagedImageSource(com.ibm.dtfj.utils.file.ManagedImageSource) CompressedFileManager(com.ibm.dtfj.utils.file.CompressedFileManager) File(java.io.File)

Aggregations

ManagedImage (com.ibm.dtfj.utils.ManagedImage)1 CompressedFileManager (com.ibm.dtfj.utils.file.CompressedFileManager)1 FileManager (com.ibm.dtfj.utils.file.FileManager)1 ManagedImageSource (com.ibm.dtfj.utils.file.ManagedImageSource)1 MultipleCandidateException (com.ibm.dtfj.utils.file.MultipleCandidateException)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1