Search in sources :

Example 1 with J9FileImageInputStream

use of com.ibm.dtfj.utils.file.J9FileImageInputStream 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

Image (com.ibm.dtfj.image.Image)1 ManagedImage (com.ibm.dtfj.utils.ManagedImage)1 CompressedFileManager (com.ibm.dtfj.utils.file.CompressedFileManager)1 J9FileImageInputStream (com.ibm.dtfj.utils.file.J9FileImageInputStream)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 MalformedURLException (java.net.MalformedURLException)1 ArrayList (java.util.ArrayList)1 ImageInputStream (javax.imageio.stream.ImageInputStream)1