Search in sources :

Example 6 with Feature

use of org.apache.sling.provisioning.model.Feature 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 7 with Feature

use of org.apache.sling.provisioning.model.Feature 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)

Example 8 with Feature

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

the class ModelTransformer method transform.

@Override
public TransformationResult[] transform(final RegisteredResource resource) {
    Model model = null;
    File baseDir = null;
    if (resource.getType().equals(InstallableResource.TYPE_FILE) && resource.getURL().endsWith(".model")) {
        try (final Reader reader = new InputStreamReader(resource.getInputStream(), "UTF-8")) {
            model = ModelReader.read(reader, resource.getURL());
        } catch (final IOException ioe) {
            logger.info("Unable to read model from " + resource.getURL(), ioe);
        }
    }
    if (resource.getType().equals(InstallableResource.TYPE_FILE) && resource.getURL().endsWith(".mar")) {
        baseDir = this.bundleContext.getDataFile("");
        try (final InputStream is = resource.getInputStream()) {
            model = ModelArchiveReader.read(is, new ModelArchiveReader.ArtifactConsumer() {

                @Override
                public void consume(final Artifact artifact, final InputStream is) throws IOException {
                // nothing to do, install task does extraction
                }
            });
        } catch (final IOException ioe) {
            logger.info("Unable to read model from " + resource.getURL(), ioe);
        }
    }
    if (model != null) {
        Map<Traceable, String> errors = ModelUtility.validate(model);
        if (errors == null) {
            try {
                final Model effectiveModel = ModelUtility.getEffectiveModel(model);
                errors = ModelUtility.validateIncludingVersion(effectiveModel);
                if (errors == null) {
                    String modelTxt = null;
                    try (final StringWriter sw = new StringWriter()) {
                        ModelWriter.write(sw, effectiveModel);
                        modelTxt = sw.toString();
                    } catch (final IOException ioe) {
                        logger.info("Unable to read model from " + resource.getURL(), ioe);
                    }
                    if (modelTxt != null) {
                        final TransformationResult[] result = new TransformationResult[effectiveModel.getFeatures().size()];
                        int index = 0;
                        for (final Feature f : effectiveModel.getFeatures()) {
                            final TransformationResult tr = new TransformationResult();
                            tr.setResourceType(TYPE_PROV_MODEL);
                            tr.setId(f.getName());
                            tr.setVersion(new Version(f.getVersion()));
                            final Map<String, Object> attributes = new HashMap<>();
                            attributes.put(ATTR_MODEL, modelTxt);
                            attributes.put(ATTR_FEATURE_INDEX, index);
                            attributes.put(ATTR_FEATURE_NAME, f.getName() + "-" + f.getVersion());
                            if (baseDir != null) {
                                final File dir = new File(baseDir, f.getName() + "-" + f.getVersion());
                                attributes.put(ATTR_BASE_PATH, dir.getAbsolutePath());
                            }
                            tr.setAttributes(attributes);
                            result[index] = tr;
                            index++;
                        }
                        return result;
                    }
                }
            } catch (final IllegalArgumentException iae) {
                errors = Collections.singletonMap((Traceable) model, iae.getMessage());
            }
        }
        if (errors != null) {
            logger.warn("Errors during parsing model at {} : {}", resource.getURL(), errors.values());
        }
    }
    return null;
}
Also used : InputStreamReader(java.io.InputStreamReader) HashMap(java.util.HashMap) InputStream(java.io.InputStream) ModelArchiveReader(org.apache.sling.provisioning.model.io.ModelArchiveReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) ModelReader(org.apache.sling.provisioning.model.io.ModelReader) IOException(java.io.IOException) Feature(org.apache.sling.provisioning.model.Feature) Artifact(org.apache.sling.provisioning.model.Artifact) StringWriter(java.io.StringWriter) Version(org.osgi.framework.Version) Model(org.apache.sling.provisioning.model.Model) Traceable(org.apache.sling.provisioning.model.Traceable) TransformationResult(org.apache.sling.installer.api.tasks.TransformationResult) File(java.io.File)

Example 9 with Feature

use of org.apache.sling.provisioning.model.Feature 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 10 with Feature

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

the class ModelWriter method write.

/**
     * Writes the model to the writer.
     * The writer is not closed.
     * @param writer Writer
     * @param model Model
     * @throws IOException
     */
public static void write(final Writer writer, final Model model) throws IOException {
    final PrintWriter pw = new PrintWriter(writer);
    boolean firstFeature = true;
    // features
    for (final Feature feature : model.getFeatures()) {
        if (firstFeature) {
            firstFeature = false;
        } else {
            pw.println();
        }
        writeComment(pw, feature);
        pw.print("[feature name=");
        pw.print(feature.getName());
        if (!FeatureTypes.PLAIN.equals(feature.getType())) {
            pw.print(" type=");
            pw.print(feature.getType());
        }
        if (feature.getVersion() != null) {
            pw.print(" version=");
            pw.print(feature.getVersion());
        }
        pw.println("]");
        // variables
        if (!feature.getVariables().isEmpty()) {
            pw.println();
            writeComment(pw, feature.getVariables());
            pw.println("[variables]");
            for (final Map.Entry<String, String> entry : feature.getVariables()) {
                pw.print("  ");
                pw.print(entry.getKey());
                pw.print("=");
                pw.println(entry.getValue());
            }
        }
        // run modes
        for (final RunMode runMode : feature.getRunModes()) {
            // settings
            if (!runMode.getSettings().isEmpty()) {
                pw.println();
                writeComment(pw, runMode.getSettings());
                pw.print("[settings");
                writeRunMode(pw, runMode);
                pw.println("]");
                for (final Map.Entry<String, String> entry : runMode.getSettings()) {
                    pw.print("  ");
                    pw.print(entry.getKey());
                    pw.print("=");
                    pw.println(entry.getValue());
                }
            }
            // artifact groups
            for (final ArtifactGroup group : runMode.getArtifactGroups()) {
                // skip empty groups
                if (group.isEmpty()) {
                    continue;
                }
                pw.println();
                writeComment(pw, group);
                pw.print("[artifacts");
                if (group.getStartLevel() > 0) {
                    pw.print(" startLevel=");
                    pw.print(String.valueOf(group.getStartLevel()));
                }
                writeRunMode(pw, runMode);
                pw.println("]");
                // artifacts
                for (final Artifact ad : group) {
                    writeComment(pw, ad);
                    pw.print("  ");
                    pw.print(ad.toMvnUrl().substring(4));
                    if (!ad.getMetadata().isEmpty()) {
                        boolean first = true;
                        for (final Map.Entry<String, String> entry : ad.getMetadata().entrySet()) {
                            if (first) {
                                first = false;
                                pw.print(" [");
                            } else {
                                pw.print(", ");
                            }
                            pw.print(entry.getKey());
                            pw.print("=");
                            pw.print(entry.getValue());
                        }
                        pw.print("]");
                    }
                    pw.println();
                }
            }
            // configurations
            if (!runMode.getConfigurations().isEmpty()) {
                pw.println();
                writeComment(pw, runMode.getConfigurations());
                pw.print("[configurations");
                writeRunMode(pw, runMode);
                pw.println("]");
                boolean firstConfig = true;
                for (final Configuration config : runMode.getConfigurations()) {
                    if (firstConfig) {
                        firstConfig = false;
                    } else {
                        pw.println();
                    }
                    writeComment(pw, config);
                    final String raw = (String) config.getProperties().get(ModelConstants.CFG_UNPROCESSED);
                    String format = (String) config.getProperties().get(ModelConstants.CFG_UNPROCESSED_FORMAT);
                    if (format == null) {
                        format = ModelConstants.CFG_FORMAT_FELIX_CA;
                    }
                    String cfgMode = (String) config.getProperties().get(ModelConstants.CFG_UNPROCESSED_MODE);
                    if (cfgMode == null) {
                        cfgMode = ModelConstants.CFG_MODE_OVERWRITE;
                    }
                    pw.print("  ");
                    if (config.getFactoryPid() != null) {
                        pw.print(config.getFactoryPid());
                        pw.print("-");
                    }
                    pw.print(config.getPid());
                    final boolean isDefaultFormat = ModelConstants.CFG_FORMAT_FELIX_CA.equals(format);
                    final boolean isDefaultMode = ModelConstants.CFG_MODE_OVERWRITE.equals(cfgMode);
                    if (!isDefaultFormat || !isDefaultMode) {
                        pw.print(" [");
                        if (!isDefaultFormat) {
                            pw.print("format=");
                            pw.print(format);
                            if (!isDefaultMode) {
                                pw.print(",");
                            }
                        }
                        if (!isDefaultMode) {
                            pw.print("mode=");
                            pw.print(cfgMode);
                        }
                        pw.print("]");
                    }
                    pw.println();
                    final String configString;
                    if (raw != null) {
                        configString = raw;
                    } else if (config.isSpecial()) {
                        configString = config.getProperties().get(config.getPid()).toString();
                    } else {
                        final ByteArrayOutputStream os = new ByteArrayOutputStream();
                        try {
                            ConfigurationHandler.write(os, config.getProperties());
                        } finally {
                            os.close();
                        }
                        configString = new String(os.toByteArray(), "UTF-8");
                    }
                    // we have to read the configuration line by line to properly indent
                    final LineNumberReader lnr = new LineNumberReader(new StringReader(configString));
                    String line = null;
                    while ((line = lnr.readLine()) != null) {
                        if (line.trim().isEmpty()) {
                            continue;
                        }
                        pw.print("    ");
                        pw.println(line.trim());
                    }
                }
            }
        }
        // additional sections
        for (final Section section : feature.getAdditionalSections()) {
            pw.println();
            pw.print("  [:");
            pw.print(section.getName());
            for (final Map.Entry<String, String> entry : section.getAttributes().entrySet()) {
                pw.print(' ');
                pw.print(entry.getKey());
                pw.print('=');
                pw.print(entry.getValue());
            }
            pw.println("]");
            if (section.getContents() != null) {
                pw.println(section.getContents());
            }
        }
    }
}
Also used : Configuration(org.apache.sling.provisioning.model.Configuration) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Feature(org.apache.sling.provisioning.model.Feature) Section(org.apache.sling.provisioning.model.Section) Artifact(org.apache.sling.provisioning.model.Artifact) LineNumberReader(java.io.LineNumberReader) RunMode(org.apache.sling.provisioning.model.RunMode) StringReader(java.io.StringReader) Map(java.util.Map) ArtifactGroup(org.apache.sling.provisioning.model.ArtifactGroup) PrintWriter(java.io.PrintWriter)

Aggregations

Feature (org.apache.sling.provisioning.model.Feature)19 RunMode (org.apache.sling.provisioning.model.RunMode)15 File (java.io.File)9 IOException (java.io.IOException)9 ArtifactGroup (org.apache.sling.provisioning.model.ArtifactGroup)9 Model (org.apache.sling.provisioning.model.Model)7 Artifact (org.apache.sling.provisioning.model.Artifact)6 Traceable (org.apache.sling.provisioning.model.Traceable)6 HashMap (java.util.HashMap)5 Configuration (org.apache.sling.provisioning.model.Configuration)5 Map (java.util.Map)4 MavenExecutionException (org.apache.maven.MavenExecutionException)4 Artifact (org.apache.maven.artifact.Artifact)4 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)4 InputStream (java.io.InputStream)3 StringReader (java.io.StringReader)3 ArrayList (java.util.ArrayList)3 Section (org.apache.sling.provisioning.model.Section)3 FileWriter (java.io.FileWriter)2 Reader (java.io.Reader)2