public static Model read()

in src/main/java/org/apache/sling/provisioning/model/io/ModelArchiveReader.java [57:102]


    public static Model read(final InputStream in,
                             final ArtifactConsumer consumer)
    throws IOException {
        Model model = null;

        final JarInputStream jis = new JarInputStream(in);

        // check manifest
        final Manifest manifest = jis.getManifest();
        if ( manifest == null ) {
            throw new IOException("Not a model archive - manifest is missing.");
        }
        // check manifest header
        final String version = manifest.getMainAttributes().getValue(ModelArchiveWriter.MANIFEST_HEADER);
        if ( version == null ) {
            throw new IOException("Not a model archive - manifest header is missing.");
        }
        // validate manifest header
        try {
            final int number = Integer.valueOf(version);
            if ( number < 1 || number > ModelArchiveWriter.ARCHIVE_VERSION ) {
                throw new IOException("Not a model archive - invalid manifest header value: " + version);
            }
        } catch (final NumberFormatException nfe) {
            throw new IOException("Not a model archive - invalid manifest header value: " + version);
        }

        // read contents
        JarEntry entry = null;
        while ( ( entry = jis.getNextJarEntry() ) != null ) {
            if ( ModelArchiveWriter.MODEL_NAME.equals(entry.getName()) ) {
                model = ModelUtility.getEffectiveModel(ModelReader.read(new InputStreamReader(jis, "UTF-8"), null));
            } else if ( !entry.isDirectory() && entry.getName().startsWith(ModelArchiveWriter.ARTIFACTS_PREFIX) ) { // artifact
                final Artifact artifact = Artifact.fromMvnUrl("mvn:" + entry.getName().substring(ModelArchiveWriter.ARTIFACTS_PREFIX.length()));
                consumer.consume(artifact, jis);
            }
            jis.closeEntry();
        }
        if ( model == null ) {
            throw new IOException("Not a model archive - model file is missing.");
        }

        // TODO - we could check whether all artifacts from the model are in the archive

        return model;
    }