Search in sources :

Example 1 with ManagedImage

use of com.ibm.dtfj.utils.ManagedImage 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)

Example 2 with ManagedImage

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

the class ImageFactory method getImagesFromArchive.

@Override
public Image[] getImagesFromArchive(File archive, boolean extract) throws IOException {
    Objects.requireNonNull(archive);
    if (!FileManager.fileExists(archive)) {
        // $NON-NLS-1$ //$NON-NLS-2$
        throw new FileNotFoundException("Archive '" + archive.getAbsolutePath() + "' not found.");
    }
    if (!FileManager.isArchive(archive)) {
        // $NON-NLS-1$ //$NON-NLS-2$
        throw new IOException("The specified archive " + archive.getAbsolutePath() + " was not recognised");
    }
    // images created from this zip file
    ArrayList<Image> images = new ArrayList<>();
    exceptions.clear();
    // the cast to an ArchiveFileManager is safe as we've already checked earlier with FileManager.isArchive
    CompressedFileManager manager = (CompressedFileManager) FileManager.getManager(archive);
    List<ManagedImageSource> sources = manager.getImageSources();
    if (extract) {
        // create the temporary directory in preparation for the extraction
        File parent = getTempDirParent();
        tmpdir = FileManager.createTempDir(parent);
    }
    for (ManagedImageSource source : sources) {
        ImageInputStream corestream = null;
        ImageInputStream metastream = null;
        if (extract) {
            // extract the files to disk
            manager.extract(source, tmpdir);
            File coreFile = new File(source.getPathToExtractedFile());
            corestream = new J9FileImageInputStream(coreFile, source);
            if (source.hasMetaData()) {
                manager.extract(source.getMetadata(), tmpdir);
                File metaFile = new File(source.getMetadata().getPathToExtractedFile());
                metastream = new J9FileImageInputStream(metaFile, source.getMetadata());
            }
        } else {
            // run from in memory
            corestream = manager.getStream(source);
            if (source.hasMetaData()) {
                metastream = manager.getStream(source.getMetadata());
            }
        }
        try {
            // Try each factory listed for this source file type, checking to see if we find a JRE
            ImageReference imageReference = new ImageReference();
            for (String factory : source.getType().getFactoryNames()) {
                if (foundRuntimeInImage(factory, imageReference, source.toURI(), corestream, metastream)) {
                    if (imageReference.image instanceof ManagedImage) {
                        ((ManagedImage) imageReference.image).setImageSource(source);
                    }
                    images.add(imageReference.image);
                    // quit looking for an image
                    break;
                }
                imageReference.image = null;
            }
            // Final fallback is to get a native-only DDR Image (no JRE), if the source is a core dump
            if (imageReference.image == null && source.getType().equals(ImageSourceType.CORE)) {
                com.ibm.dtfj.image.ImageFactory factory = createImageFactory(FACTORY_DDR);
                if (factory != null) {
                    imageReference.image = factory.getImage(corestream, source.toURI());
                    if (imageReference.image != null) {
                        images.add(imageReference.image);
                    }
                }
            }
        } catch (Exception e) {
            // log exception and attempt to carry on creating images
            exceptions.add(e);
        }
    }
    printExceptions();
    return images.toArray(new Image[images.size()]);
}
Also used : ManagedImage(com.ibm.dtfj.utils.ManagedImage) J9FileImageInputStream(com.ibm.dtfj.utils.file.J9FileImageInputStream) ImageInputStream(javax.imageio.stream.ImageInputStream) FileNotFoundException(java.io.FileNotFoundException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) J9FileImageInputStream(com.ibm.dtfj.utils.file.J9FileImageInputStream) Image(com.ibm.dtfj.image.Image) ManagedImage(com.ibm.dtfj.utils.ManagedImage) MultipleCandidateException(com.ibm.dtfj.utils.file.MultipleCandidateException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) 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)2 CompressedFileManager (com.ibm.dtfj.utils.file.CompressedFileManager)2 ManagedImageSource (com.ibm.dtfj.utils.file.ManagedImageSource)2 MultipleCandidateException (com.ibm.dtfj.utils.file.MultipleCandidateException)2 File (java.io.File)2 FileNotFoundException (java.io.FileNotFoundException)2 IOException (java.io.IOException)2 Image (com.ibm.dtfj.image.Image)1 FileManager (com.ibm.dtfj.utils.file.FileManager)1 J9FileImageInputStream (com.ibm.dtfj.utils.file.J9FileImageInputStream)1 MalformedURLException (java.net.MalformedURLException)1 ArrayList (java.util.ArrayList)1 ImageInputStream (javax.imageio.stream.ImageInputStream)1