Search in sources :

Example 6 with RunMode

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

the class Configurations method maybeConfigure.

/** Activate our configurations if possible, and if not done already.
     *  Can be called as many times as convenient, to make sure this happens
     *  as early as possible.
     */
public synchronized void maybeConfigure() {
    if (proxy != null) {
        log.debug("Configurations already activated, doing nothing");
        return;
    }
    final Object service = tracker.getService();
    if (service == null) {
        log.debug("ConfigurationAdmin service not yet available, doing nothing");
        return;
    }
    proxy = new ConfigAdminProxy(service);
    log.info("Activating configurations from provisioning model");
    for (Feature f : model.getFeatures()) {
        for (RunMode r : f.getRunModes()) {
            if (!rmFilter.runModeActive(r)) {
                log.info("RunMode is not active, ignored: {}", Arrays.asList(r.getNames()));
                continue;
            }
            for (Configuration c : r.getConfigurations()) {
                try {
                    setConfig(c);
                } catch (Exception e) {
                    log.warn("Failed to activate configuration " + c, e);
                }
            }
        }
    }
}
Also used : Configuration(org.apache.sling.provisioning.model.Configuration) RunMode(org.apache.sling.provisioning.model.RunMode) Feature(org.apache.sling.provisioning.model.Feature) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 7 with RunMode

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

the class FrameworkProperties method getProperties.

public synchronized Map<String, String> getProperties(FeatureFilter filter) {
    if (fprops == null) {
        fprops = new HashMap<String, String>();
        for (Feature f : model.getFeatures()) {
            if (filter != null && filter.ignoreFeature(f)) {
                continue;
            }
            for (RunMode rm : f.getRunModes()) {
                final KeyValueMap<String> settings = rm.getSettings();
                if (settings.size() > 0) {
                    log.info("Using settings from Feature {}, RunMode {} as framework properties", f.getName(), rm.getNames());
                    for (Map.Entry<String, String> e : settings) {
                        log.info("framework property set from provisioning model: {}={}", e.getKey(), e.getValue());
                        fprops.put(e.getKey(), e.getValue());
                    }
                }
            }
        }
    }
    return fprops;
}
Also used : RunMode(org.apache.sling.provisioning.model.RunMode) Feature(org.apache.sling.provisioning.model.Feature) KeyValueMap(org.apache.sling.provisioning.model.KeyValueMap) Map(java.util.Map) HashMap(java.util.HashMap)

Example 8 with RunMode

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

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

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

Aggregations

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