Search in sources :

Example 1 with ArtifactGroup

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

the class PreparePackageMojo method getRunModesManifest.

private Manifest getRunModesManifest(Feature feature) throws MojoExecutionException {
    Map<String, StringBuilder> runModes = new HashMap<>();
    for (RunMode rm : feature.getRunModes()) {
        for (ArtifactGroup ag : rm.getArtifactGroups()) {
            // For subsystems the start level on the artifact group is used as start order.
            int startOrder = ag.getStartLevel();
            for (org.apache.sling.provisioning.model.Artifact a : ag) {
                Artifact artifact = ModelUtils.getArtifact(this.project, this.mavenSession, this.artifactHandlerManager, this.resolver, a.getGroupId(), a.getArtifactId(), a.getVersion(), a.getType(), a.getClassifier());
                File artifactFile = artifact.getFile();
                String entryName = getEntryName(artifactFile, startOrder);
                String[] runModeNames = rm.getNames();
                if (runModeNames == null)
                    runModeNames = new String[] { ALL_RUNMODES_KEY };
                for (String runModeName : runModeNames) {
                    StringBuilder sb = runModes.get(runModeName);
                    if (sb == null) {
                        sb = new StringBuilder();
                        runModes.put(runModeName, sb);
                    } else {
                        sb.append('|');
                    }
                    sb.append(entryName);
                }
            }
        }
    }
    Manifest mf = new Manifest();
    Attributes attrs = mf.getMainAttributes();
    // Manifest does not work without this value
    attrs.putValue("Manifest-Version", "1.0");
    attrs.putValue("About-This-Manifest", "This is not a real manifest, it is used as information when this archive is transformed into a real subsystem .esa file");
    for (Map.Entry<String, StringBuilder> entry : runModes.entrySet()) {
        attrs.putValue(entry.getKey().replace(':', '_'), entry.getValue().toString());
    }
    return mf;
}
Also used : HashMap(java.util.HashMap) Attributes(java.util.jar.Attributes) Manifest(java.util.jar.Manifest) Artifact(org.apache.maven.artifact.Artifact) RunMode(org.apache.sling.provisioning.model.RunMode) ArtifactGroup(org.apache.sling.provisioning.model.ArtifactGroup) File(java.io.File) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with ArtifactGroup

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

the class PreparePackageMojo method createSubsystemBaseFile.

private File createSubsystemBaseFile(Feature feature, AtomicInteger startLevelHolder) throws MojoExecutionException {
    File subsystemFile = new File(getTmpDir(), feature.getName() + ".subsystem-base");
    if (subsystemFile.exists()) {
        // TODO is there a better way to avoid calling this multiple times?
        return null;
    }
    startLevelHolder.set(-1);
    // The runmodes information has to be the first item in the archive so that we always have it available when the
    // archive is being processed. For this reason a Jar file is used here as it's guaranteed to store the manifest
    // first.
    Manifest runModesManifest = getRunModesManifest(feature);
    getLog().info("Creating subsystem base file: " + subsystemFile.getName());
    boolean created = subsystemFile.getParentFile().mkdirs();
    if (!created) {
        throw new MojoExecutionException("Failed creating " + subsystemFile.getParentFile().getAbsolutePath());
    }
    try (JarOutputStream os = new JarOutputStream(new FileOutputStream(subsystemFile), runModesManifest)) {
        Map<String, Integer> bsnStartOrderMap = new HashMap<>();
        for (RunMode rm : feature.getRunModes()) {
            for (ArtifactGroup ag : rm.getArtifactGroups()) {
                // For subsystems the start level on the artifact group is used as start order.
                int startOrder = ag.getStartLevel();
                for (org.apache.sling.provisioning.model.Artifact a : ag) {
                    Artifact artifact = ModelUtils.getArtifact(this.project, this.mavenSession, this.artifactHandlerManager, this.resolver, a.getGroupId(), a.getArtifactId(), a.getVersion(), a.getType(), a.getClassifier());
                    File artifactFile = artifact.getFile();
                    String entryName = getEntryName(artifactFile, startOrder);
                    ZipEntry ze = new ZipEntry(entryName);
                    try {
                        os.putNextEntry(ze);
                        Files.copy(artifactFile.toPath(), os);
                    } finally {
                        os.closeEntry();
                    }
                }
            }
        }
        int sl = createSubsystemManifest(feature, bsnStartOrderMap, os);
        if (sl != -1)
            startLevelHolder.set(sl);
        addReadme(os);
    } catch (IOException ioe) {
        throw new MojoExecutionException("Problem creating subsystem .esa file " + subsystemFile, ioe);
    }
    return subsystemFile;
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) HashMap(java.util.HashMap) ZipEntry(java.util.zip.ZipEntry) JarOutputStream(java.util.jar.JarOutputStream) IOException(java.io.IOException) Manifest(java.util.jar.Manifest) Artifact(org.apache.maven.artifact.Artifact) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RunMode(org.apache.sling.provisioning.model.RunMode) FileOutputStream(java.io.FileOutputStream) File(java.io.File) ArtifactGroup(org.apache.sling.provisioning.model.ArtifactGroup)

Example 3 with ArtifactGroup

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

the class ModelPreprocessor method processAttachments.

private void processAttachments(final Environment env, final ProjectInfo info) throws MavenExecutionException {
    final Xpp3Dom config = info.plugin == null ? null : (Xpp3Dom) info.plugin.getConfiguration();
    final Xpp3Dom[] nodes = (config == null ? null : config.getChildren("attach"));
    if (nodes != null) {
        for (final Xpp3Dom node : nodes) {
            final String type = nodeValue(node, "type", null);
            if (type == null) {
                throw new MavenExecutionException("Attachment for provisioning model has no type.", (File) null);
            }
            final String classifier = nodeValue(node, "classifier", null);
            final String featureName = nodeValue(node, "feature", null);
            int startLevel = 0;
            final String level = nodeValue(node, "startLevel", null);
            if (level != null) {
                startLevel = Integer.valueOf(level);
            }
            final Feature f;
            if (featureName != null) {
                f = info.localModel.getFeature(featureName);
            } else if (info.localModel.getFeatures().isEmpty()) {
                f = null;
            } else {
                f = info.localModel.getFeatures().get(0);
            }
            if (f == null) {
                if (featureName == null) {
                    throw new MavenExecutionException("No feature found in provisioning model for attachment.", (File) null);
                }
                throw new MavenExecutionException("Feature with name '" + featureName + "' not found in provisioning model for attachment.", (File) null);
            }
            final RunMode runMode = f.getOrCreateRunMode(null);
            final ArtifactGroup group = runMode.getOrCreateArtifactGroup(startLevel);
            final org.apache.sling.provisioning.model.Artifact artifact = new org.apache.sling.provisioning.model.Artifact(info.project.getGroupId(), info.project.getArtifactId(), info.project.getVersion(), classifier, type);
            env.logger.debug("Attaching " + artifact + " to feature " + f.getName());
            group.add(artifact);
        }
    }
}
Also used : Xpp3Dom(org.codehaus.plexus.util.xml.Xpp3Dom) Feature(org.apache.sling.provisioning.model.Feature) Artifact(org.apache.maven.artifact.Artifact) DefaultArtifact(org.apache.maven.artifact.DefaultArtifact) MavenExecutionException(org.apache.maven.MavenExecutionException) RunMode(org.apache.sling.provisioning.model.RunMode) ArtifactGroup(org.apache.sling.provisioning.model.ArtifactGroup)

Example 4 with ArtifactGroup

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

the class InstallModelTask method transform.

private Result transform(final String name, final String modelText, final int featureIndex, final TaskResource rsrc, final File baseDir) {
    Model model = null;
    try (final Reader reader = new StringReader(modelText)) {
        model = ModelUtility.getEffectiveModel(ModelReader.read(reader, name));
    } catch (final IOException ioe) {
        logger.warn("Unable to read model file for feature " + name, ioe);
    }
    if (model == null) {
        return null;
    }
    int index = 0;
    final Iterator<Feature> iter = model.getFeatures().iterator();
    while (iter.hasNext()) {
        iter.next();
        if (index != featureIndex) {
            iter.remove();
        }
        index++;
    }
    if (baseDir != null) {
        final List<Artifact> artifacts = new ArrayList<>();
        final Feature feature = model.getFeatures().get(0);
        for (final RunMode rm : feature.getRunModes()) {
            for (final ArtifactGroup group : rm.getArtifactGroups()) {
                for (final Artifact a : group) {
                    artifacts.add(a);
                }
            }
        }
        // extract artifacts
        final byte[] buffer = new byte[1024 * 1024 * 256];
        try (final InputStream is = rsrc.getInputStream()) {
            ModelArchiveReader.read(is, new ModelArchiveReader.ArtifactConsumer() {

                @Override
                public void consume(final Artifact artifact, final InputStream is) throws IOException {
                    if (artifacts.contains(artifact)) {
                        final File artifactFile = new File(baseDir, artifact.getRepositoryPath().replace('/', File.separatorChar));
                        if (!artifactFile.exists()) {
                            artifactFile.getParentFile().mkdirs();
                            try (final OutputStream os = new FileOutputStream(artifactFile)) {
                                int l = 0;
                                while ((l = is.read(buffer)) > 0) {
                                    os.write(buffer, 0, l);
                                }
                            }
                        }
                    }
                }
            });
        } catch (final IOException ioe) {
            logger.warn("Unable to extract artifacts from model " + name, ioe);
            return null;
        }
    }
    final List<ArtifactDescription> files = new ArrayList<>();
    Map<Traceable, String> errors = collectArtifacts(model, files, baseDir);
    if (errors == null) {
        final Result result = new Result();
        for (final ArtifactDescription desc : files) {
            if (desc.artifactFile != null) {
                try {
                    final InputStream is = new FileInputStream(desc.artifactFile);
                    final String digest = String.valueOf(desc.artifactFile.lastModified());
                    // handle start level
                    final Dictionary<String, Object> dict = new Hashtable<String, Object>();
                    if (desc.startLevel > 0) {
                        dict.put(InstallableResource.BUNDLE_START_LEVEL, desc.startLevel);
                    }
                    dict.put(InstallableResource.RESOURCE_URI_HINT, desc.artifactFile.toURI().toString());
                    result.resources.add(new InstallableResource("/" + desc.artifactFile.getName(), is, dict, digest, InstallableResource.TYPE_FILE, null));
                } catch (final IOException ioe) {
                    logger.warn("Unable to read artifact " + desc.artifactFile, ioe);
                    return null;
                }
            } else if (desc.cfg != null) {
                final String id = (desc.cfg.getFactoryPid() != null ? desc.cfg.getFactoryPid() + "-" + desc.cfg.getPid() : desc.cfg.getPid());
                result.resources.add(new InstallableResource("/" + id + ".config", null, desc.cfg.getProperties(), null, InstallableResource.TYPE_CONFIG, null));
            } else if (desc.section != null) {
                result.repoinit = desc.section.getContents();
            }
        }
        return result;
    }
    logger.warn("Errors during parsing model file {} : {}", name, errors.values());
    return null;
}
Also used : InstallableResource(org.apache.sling.installer.api.InstallableResource) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) ArrayList(java.util.ArrayList) ModelArchiveReader(org.apache.sling.provisioning.model.io.ModelArchiveReader) Reader(java.io.Reader) StringReader(java.io.StringReader) ModelReader(org.apache.sling.provisioning.model.io.ModelReader) Feature(org.apache.sling.provisioning.model.Feature) StringReader(java.io.StringReader) Traceable(org.apache.sling.provisioning.model.Traceable) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Hashtable(java.util.Hashtable) IOException(java.io.IOException) Artifact(org.apache.sling.provisioning.model.Artifact) FileInputStream(java.io.FileInputStream) ModelArchiveReader(org.apache.sling.provisioning.model.io.ModelArchiveReader) RunMode(org.apache.sling.provisioning.model.RunMode) FileOutputStream(java.io.FileOutputStream) Model(org.apache.sling.provisioning.model.Model) ArtifactGroup(org.apache.sling.provisioning.model.ArtifactGroup) File(java.io.File)

Example 5 with ArtifactGroup

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

the class InstallModelTask method collectArtifacts.

private Map<Traceable, String> collectArtifacts(final Model effectiveModel, final List<ArtifactDescription> files, final File baseDir) {
    final RepositoryAccess repo = new RepositoryAccess();
    final Map<Traceable, String> errors = new HashMap<>();
    for (final Feature f : effectiveModel.getFeatures()) {
        if (f.isSpecial()) {
            continue;
        }
        for (final Section section : f.getAdditionalSections()) {
            final ArtifactDescription desc = new ArtifactDescription();
            desc.section = section;
            files.add(desc);
        }
        for (final RunMode mode : f.getRunModes()) {
            if (mode.isSpecial()) {
                continue;
            }
            if (mode.isActive(this.activeRunModes)) {
                for (final ArtifactGroup group : mode.getArtifactGroups()) {
                    for (final Artifact artifact : group) {
                        File file = (baseDir == null ? null : new File(baseDir, artifact.getRepositoryPath().replace('/', File.separatorChar)));
                        if (file == null || !file.exists()) {
                            file = repo.get(artifact);
                        }
                        if (file == null) {
                            errors.put(artifact, "Artifact " + artifact.toMvnUrl() + " not found.");
                        } else {
                            final ArtifactDescription desc = new ArtifactDescription();
                            desc.artifactFile = file;
                            desc.startLevel = group.getStartLevel();
                            files.add(desc);
                        }
                    }
                }
                for (final Configuration cfg : mode.getConfigurations()) {
                    if (cfg.isSpecial()) {
                        continue;
                    }
                    final ArtifactDescription desc = new ArtifactDescription();
                    desc.cfg = cfg;
                    files.add(desc);
                }
            }
        }
    }
    return errors.isEmpty() ? null : errors;
}
Also used : Configuration(org.apache.sling.provisioning.model.Configuration) HashMap(java.util.HashMap) Feature(org.apache.sling.provisioning.model.Feature) Section(org.apache.sling.provisioning.model.Section) Artifact(org.apache.sling.provisioning.model.Artifact) RunMode(org.apache.sling.provisioning.model.RunMode) Traceable(org.apache.sling.provisioning.model.Traceable) ArtifactGroup(org.apache.sling.provisioning.model.ArtifactGroup) File(java.io.File)

Aggregations

ArtifactGroup (org.apache.sling.provisioning.model.ArtifactGroup)12 RunMode (org.apache.sling.provisioning.model.RunMode)11 Feature (org.apache.sling.provisioning.model.Feature)9 File (java.io.File)7 IOException (java.io.IOException)6 Artifact (org.apache.maven.artifact.Artifact)6 Artifact (org.apache.sling.provisioning.model.Artifact)5 Traceable (org.apache.sling.provisioning.model.Traceable)4 FileOutputStream (java.io.FileOutputStream)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 Manifest (java.util.jar.Manifest)3 MavenExecutionException (org.apache.maven.MavenExecutionException)3 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)3 Configuration (org.apache.sling.provisioning.model.Configuration)3 Model (org.apache.sling.provisioning.model.Model)3 InputStream (java.io.InputStream)2 StringReader (java.io.StringReader)2 Writer (java.io.Writer)2