Search in sources :

Example 6 with Artifact

use of org.apache.sling.provisioning.model.Artifact in project sling by apache.

the class ModelArchiveReader method read.

/**
     * Read a model archive.
     * The input stream is not closed. It is up to the caller to close the input stream.
     * @param in The input stream to read from.
     * @return The model
     * @throws IOException If anything goes wrong
     */
@SuppressWarnings("resource")
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.");
    }
    return model;
}
Also used : InputStreamReader(java.io.InputStreamReader) JarInputStream(java.util.jar.JarInputStream) Model(org.apache.sling.provisioning.model.Model) IOException(java.io.IOException) Manifest(java.util.jar.Manifest) JarEntry(java.util.jar.JarEntry) Artifact(org.apache.sling.provisioning.model.Artifact)

Example 7 with Artifact

use of org.apache.sling.provisioning.model.Artifact in project sling by apache.

the class ModelArchiveWriter method write.

/**
     * Create a model archive.
     * The output stream will not be closed by this method. The caller
     * must call {@link JarOutputStream#close()} or {@link JarOutputStream#finish()}
     * on the return output stream. The caller can add additional files through
     * the return stream.
     *
     * In order to create an archive for a model, each feature in the model must
     * have a name and a version and the model must be valid, therefore {@link ModelUtility#validateIncludingVersion(Model)}
     * is called first. If the model is invalid an {@code IOException} is thrown.
     *
     * @param out The output stream to write to
     * @param model The model to write
     * @param baseManifest Optional base manifest used for creating the manifest.
     * @param provider The artifact provider
     * @return The jar output stream.
     * @throws IOException If anything goes wrong
     */
public static JarOutputStream write(final OutputStream out, final Model model, final Manifest baseManifest, final ArtifactProvider provider) throws IOException {
    // check model
    final Map<Traceable, String> errors = ModelUtility.validate(model);
    if (errors != null) {
        throw new IOException("Model is not valid: " + errors);
    }
    // create manifest
    final Manifest manifest = (baseManifest == null ? new Manifest() : new Manifest(baseManifest));
    manifest.getMainAttributes().putValue("Manifest-Version", "1.0");
    manifest.getMainAttributes().putValue(MANIFEST_HEADER, String.valueOf(ARCHIVE_VERSION));
    // create archive
    final JarOutputStream jos = new JarOutputStream(out, manifest);
    // write model first
    final JarEntry entry = new JarEntry(MODEL_NAME);
    jos.putNextEntry(entry);
    final Writer writer = new OutputStreamWriter(jos, "UTF-8");
    ModelWriter.write(writer, model);
    writer.flush();
    jos.closeEntry();
    final byte[] buffer = new byte[1024 * 1024 * 256];
    for (final Feature f : model.getFeatures()) {
        for (final RunMode rm : f.getRunModes()) {
            for (final ArtifactGroup g : rm.getArtifactGroups()) {
                for (final Artifact a : g) {
                    final JarEntry artifactEntry = new JarEntry(ARTIFACTS_PREFIX + a.getRepositoryPath());
                    jos.putNextEntry(artifactEntry);
                    try (final InputStream is = provider.getInputStream(a)) {
                        int l = 0;
                        while ((l = is.read(buffer)) > 0) {
                            jos.write(buffer, 0, l);
                        }
                    }
                    jos.closeEntry();
                }
            }
        }
    }
    return jos;
}
Also used : InputStream(java.io.InputStream) JarOutputStream(java.util.jar.JarOutputStream) IOException(java.io.IOException) Manifest(java.util.jar.Manifest) JarEntry(java.util.jar.JarEntry) Feature(org.apache.sling.provisioning.model.Feature) Artifact(org.apache.sling.provisioning.model.Artifact) RunMode(org.apache.sling.provisioning.model.RunMode) OutputStreamWriter(java.io.OutputStreamWriter) Traceable(org.apache.sling.provisioning.model.Traceable) ArtifactGroup(org.apache.sling.provisioning.model.ArtifactGroup) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter)

Example 8 with Artifact

use of org.apache.sling.provisioning.model.Artifact in project sling by apache.

the class LaunchpadComparer method outputFormatted.

private static void outputFormatted(Map.Entry<ArtifactKey, VersionChange> e) {
    ArtifactKey artifact = e.getKey();
    VersionChange versionChange = e.getValue();
    System.out.format("    %-30s : %-55s : %s -> %s%n", artifact.getGroupId(), artifact.getArtifactId(), versionChange.getFrom(), versionChange.getTo());
    if (!artifact.getGroupId().equals("org.apache.sling")) {
        return;
    }
    SvnChangeLogFinder svn = new SvnChangeLogFinder();
    String fromTag = artifact.getArtifactId() + "-" + versionChange.getFrom();
    String toTag = artifact.getArtifactId() + "-" + versionChange.getTo();
    try {
        List<String> issues = svn.getChanges(fromTag, toTag).stream().map(LaunchpadComparer::toJiraKey).filter(k -> k != null).collect(Collectors.toList());
        IssueFinder issueFinder = new IssueFinder();
        issueFinder.findIssues(issues).forEach(i -> System.out.format("        %-10s - %s%n", i.getKey(), i.getSummary()));
    } catch (SVNException | IOException e1) {
        System.err.println("Failed retrieving changes : " + e1.getMessage());
    }
}
Also used : IssueFinder(org.apache.sling.tooling.lc.jira.IssueFinder) AetherSetup(org.apache.sling.tooling.lc.aether.AetherSetup) ArtifactKey(org.apache.sling.tooling.lc.aether.ArtifactKey) BundleList(org.apache.sling.maven.projectsupport.bundlelist.v1_0_0.BundleList) Function(java.util.function.Function) BundleListUtils(org.apache.sling.maven.projectsupport.BundleListUtils) Artifact(org.apache.sling.provisioning.model.Artifact) Matcher(java.util.regex.Matcher) Map(java.util.Map) StreamSupport(java.util.stream.StreamSupport) VersionChange(org.apache.sling.tooling.lc.aether.VersionChange) Files(java.nio.file.Files) SVNException(org.tmatesoft.svn.core.SVNException) Set(java.util.Set) IOException(java.io.IOException) Artifacts(org.apache.sling.tooling.lc.aether.Artifacts) Collectors(java.util.stream.Collectors) File(java.io.File) Sets(com.google.common.collect.Sets) Objects(java.util.Objects) List(java.util.List) ModelUtility(org.apache.sling.provisioning.model.ModelUtility) SvnChangeLogFinder(org.apache.sling.tooling.lc.svn.SvnChangeLogFinder) Model(org.apache.sling.provisioning.model.Model) BufferedReader(java.io.BufferedReader) Pattern(java.util.regex.Pattern) Bundle(org.apache.sling.maven.projectsupport.bundlelist.v1_0_0.Bundle) ModelReader(org.apache.sling.provisioning.model.io.ModelReader) IssueFinder(org.apache.sling.tooling.lc.jira.IssueFinder) ArtifactKey(org.apache.sling.tooling.lc.aether.ArtifactKey) VersionChange(org.apache.sling.tooling.lc.aether.VersionChange) SvnChangeLogFinder(org.apache.sling.tooling.lc.svn.SvnChangeLogFinder) SVNException(org.tmatesoft.svn.core.SVNException) IOException(java.io.IOException)

Example 9 with Artifact

use of org.apache.sling.provisioning.model.Artifact in project sling by apache.

the class LaunchpadComparer method run.

public void run() throws Exception {
    System.out.format("Computing differences between Launchpad versions %s and %s...%n", firstVersion, secondVersion);
    // 1. download artifacts
    AetherSetup aether = new AetherSetup();
    File fromFile = aether.download(Artifacts.launchpadCoordinates(firstVersion));
    File toFile = aether.download(Artifacts.launchpadCoordinates(secondVersion));
    // 2. parse artifact definitions
    Model model;
    try (BufferedReader reader = Files.newBufferedReader(toFile.toPath())) {
        model = ModelUtility.getEffectiveModel(ModelReader.read(reader, null));
    }
    Map<ArtifactKey, Artifact> to = model.getFeatures().stream().flatMap(f -> f.getRunModes().stream()).flatMap(r -> r.getArtifactGroups().stream()).flatMap(g -> StreamSupport.stream(g.spliterator(), false)).collect(Collectors.toMap(a -> new ArtifactKey(a), Function.identity()));
    BundleList readBundleList = BundleListUtils.readBundleList(fromFile);
    Map<ArtifactKey, Artifact> from = readBundleList.getStartLevels().stream().flatMap(sl -> sl.getBundles().stream()).collect(Collectors.toMap(b -> new ArtifactKey(b), LaunchpadComparer::newArtifact));
    // 3. generate added / removed / changed
    Set<Artifact> removed = Sets.difference(from.keySet(), to.keySet()).stream().map(k -> from.get(k)).collect(Collectors.toSet());
    Set<Artifact> added = Sets.difference(to.keySet(), from.keySet()).stream().map(k -> to.get(k)).collect(Collectors.toSet());
    Map<ArtifactKey, VersionChange> changed = to.values().stream().filter(k -> !added.contains(k) && !removed.contains(k)).map(k -> new ArtifactKey(k)).filter(k -> !Objects.equals(to.get(k).getVersion(), from.get(k).getVersion())).collect(Collectors.toMap(Function.identity(), k -> new VersionChange(from.get(k).getVersion(), to.get(k).getVersion())));
    // 4. output changes
    System.out.println("Added ");
    added.stream().sorted().forEach(LaunchpadComparer::outputFormatted);
    System.out.println("Removed ");
    removed.stream().sorted().forEach(LaunchpadComparer::outputFormatted);
    System.out.println("Changed");
    changed.entrySet().stream().sorted((a, b) -> a.getKey().compareTo(b.getKey())).forEach(LaunchpadComparer::outputFormatted);
}
Also used : IssueFinder(org.apache.sling.tooling.lc.jira.IssueFinder) AetherSetup(org.apache.sling.tooling.lc.aether.AetherSetup) ArtifactKey(org.apache.sling.tooling.lc.aether.ArtifactKey) BundleList(org.apache.sling.maven.projectsupport.bundlelist.v1_0_0.BundleList) Function(java.util.function.Function) BundleListUtils(org.apache.sling.maven.projectsupport.BundleListUtils) Artifact(org.apache.sling.provisioning.model.Artifact) Matcher(java.util.regex.Matcher) Map(java.util.Map) StreamSupport(java.util.stream.StreamSupport) VersionChange(org.apache.sling.tooling.lc.aether.VersionChange) Files(java.nio.file.Files) SVNException(org.tmatesoft.svn.core.SVNException) Set(java.util.Set) IOException(java.io.IOException) Artifacts(org.apache.sling.tooling.lc.aether.Artifacts) Collectors(java.util.stream.Collectors) File(java.io.File) Sets(com.google.common.collect.Sets) Objects(java.util.Objects) List(java.util.List) ModelUtility(org.apache.sling.provisioning.model.ModelUtility) SvnChangeLogFinder(org.apache.sling.tooling.lc.svn.SvnChangeLogFinder) Model(org.apache.sling.provisioning.model.Model) BufferedReader(java.io.BufferedReader) Pattern(java.util.regex.Pattern) Bundle(org.apache.sling.maven.projectsupport.bundlelist.v1_0_0.Bundle) ModelReader(org.apache.sling.provisioning.model.io.ModelReader) ArtifactKey(org.apache.sling.tooling.lc.aether.ArtifactKey) BundleList(org.apache.sling.maven.projectsupport.bundlelist.v1_0_0.BundleList) VersionChange(org.apache.sling.tooling.lc.aether.VersionChange) Artifact(org.apache.sling.provisioning.model.Artifact) Model(org.apache.sling.provisioning.model.Model) BufferedReader(java.io.BufferedReader) File(java.io.File) AetherSetup(org.apache.sling.tooling.lc.aether.AetherSetup)

Example 10 with Artifact

use of org.apache.sling.provisioning.model.Artifact in project sling by apache.

the class ArtifactKey method compareTo.

@Override
public int compareTo(ArtifactKey o) {
    Artifact us = new Artifact(groupId, artifactId, "0.0.0", classifier, type);
    Artifact them = new Artifact(o.groupId, o.artifactId, "0.0.0", o.classifier, o.type);
    return us.compareTo(them);
}
Also used : Artifact(org.apache.sling.provisioning.model.Artifact)

Aggregations

Artifact (org.apache.sling.provisioning.model.Artifact)14 IOException (java.io.IOException)8 Model (org.apache.sling.provisioning.model.Model)7 File (java.io.File)6 Feature (org.apache.sling.provisioning.model.Feature)6 InputStream (java.io.InputStream)5 ArtifactGroup (org.apache.sling.provisioning.model.ArtifactGroup)5 RunMode (org.apache.sling.provisioning.model.RunMode)5 Map (java.util.Map)4 Traceable (org.apache.sling.provisioning.model.Traceable)4 ModelReader (org.apache.sling.provisioning.model.io.ModelReader)4 InputStreamReader (java.io.InputStreamReader)3 Reader (java.io.Reader)3 HashMap (java.util.HashMap)3 JarEntry (java.util.jar.JarEntry)3 Section (org.apache.sling.provisioning.model.Section)3 Sets (com.google.common.collect.Sets)2 BufferedReader (java.io.BufferedReader)2 StringReader (java.io.StringReader)2 StringWriter (java.io.StringWriter)2