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;
}
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;
}
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());
}
}
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);
}
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);
}
Aggregations