Search in sources :

Example 1 with Configuration

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

the class PreparePackageMojo method prepareWebapp.

/**
     * Prepare the web application.
     */
private void prepareWebapp(final Model model) throws MojoExecutionException {
    if (this.createWebapp) {
        final Map<String, File> contentsMap = new HashMap<String, File>();
        this.project.setContextValue(BuildConstants.CONTEXT_WEBAPP, contentsMap);
        // unpack base artifact and create settings
        final File outputDir = new File(this.project.getBuild().getDirectory(), BuildConstants.WEBAPP_OUTDIR);
        final File webappDir = new File(outputDir, "WEB-INF");
        unpackBaseArtifact(model, outputDir, ModelConstants.RUN_MODE_WEBAPP);
        // check for web.xml
        final Feature webappF = model.getFeature(ModelConstants.FEATURE_LAUNCHPAD);
        if (webappF != null) {
            final RunMode webappRM = webappF.getRunMode();
            if (webappRM != null) {
                final Configuration webConfig = webappRM.getConfiguration(ModelConstants.CFG_LAUNCHPAD_WEB_XML);
                if (webConfig != null) {
                    final File webXML = new File(webappDir, "web.xml");
                    try {
                        FileUtils.fileWrite(webXML, webConfig.getProperties().get(ModelConstants.CFG_LAUNCHPAD_WEB_XML).toString());
                    } catch (final IOException e) {
                        throw new MojoExecutionException("Unable to write configuration to " + webXML, e);
                    }
                }
            }
        }
        this.buildSettings(model, ModelConstants.RUN_MODE_WEBAPP, webappDir);
        this.buildBootstrapFile(model, ModelConstants.RUN_MODE_WEBAPP, webappDir);
        this.embedModel(model, webappDir);
        this.buildContentsMap(model, ModelConstants.RUN_MODE_WEBAPP, contentsMap);
    }
}
Also used : Configuration(org.apache.sling.provisioning.model.Configuration) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) HashMap(java.util.HashMap) RunMode(org.apache.sling.provisioning.model.RunMode) IOException(java.io.IOException) File(java.io.File) Feature(org.apache.sling.provisioning.model.Feature)

Example 2 with Configuration

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

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

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

Example 5 with Configuration

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

the class IOTest method testMultilineConfiguration.

@Test
public void testMultilineConfiguration() throws Exception {
    final Model m = ModelUtility.getEffectiveModel(U.readCompleteTestModel(new String[] { "configadmin.txt" }));
    final List<Configuration> configs = new ArrayList<Configuration>();
    for (final Configuration c : m.getFeature("configadmin").getRunMode().getConfigurations()) {
        configs.add(c);
    }
    assertEquals(5, configs.size());
    final Configuration cfgA = configs.get(0);
    assertEquals("org.apache.test.A", cfgA.getPid());
    assertNull(cfgA.getFactoryPid());
    assertEquals(1, cfgA.getProperties().size());
    assertEquals("A", cfgA.getProperties().get("name"));
    final Configuration cfgB = configs.get(1);
    assertEquals("org.apache.test.B", cfgB.getPid());
    assertNull(cfgB.getFactoryPid());
    assertEquals(2, cfgB.getProperties().size());
    assertEquals("B", cfgB.getProperties().get("name"));
    assertArrayEquals(new String[] { "one", "two", "three" }, (String[]) cfgB.getProperties().get("array"));
    final Configuration cfgC = configs.get(2);
    assertEquals("org.apache.test.C", cfgC.getPid());
    assertNull(cfgC.getFactoryPid());
    assertEquals(2, cfgC.getProperties().size());
    assertEquals("C", cfgC.getProperties().get("name"));
    assertArrayEquals(new Integer[] { 1, 2, 3 }, (Integer[]) cfgC.getProperties().get("array"));
    final Configuration cfgD = configs.get(3);
    assertEquals("org.apache.test.D", cfgD.getPid());
    assertEquals("Here is\na multiline\nstring", cfgD.getProperties().get("textA"));
    assertEquals("Another one\nusing\nescaped newlines", cfgD.getProperties().get("textB"));
    final Configuration cfgE = configs.get(4);
    assertEquals("org.apache.test.E", cfgE.getPid());
    assertNull(cfgE.getFactoryPid());
    assertEquals(4, cfgE.getProperties().size());
    // TODO values will need to change once SLING-5914 is fixed
    assertEquals(6.0995758E-316, cfgE.getProperties().get("doubleValue"));
    assertEquals(6.461264E-31f, cfgE.getProperties().get("floatValue"));
    assertArrayEquals(new Double[] { 1.598088874E-315d, 2.09215452E-315d }, (Double[]) cfgE.getProperties().get("doubles"));
    assertArrayEquals(new Float[] { 3.7971794E-20f, 1.4675382E-16f }, (Float[]) cfgE.getProperties().get("floats"));
}
Also used : Configuration(org.apache.sling.provisioning.model.Configuration) Model(org.apache.sling.provisioning.model.Model) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Aggregations

Configuration (org.apache.sling.provisioning.model.Configuration)7 Feature (org.apache.sling.provisioning.model.Feature)5 RunMode (org.apache.sling.provisioning.model.RunMode)5 File (java.io.File)4 IOException (java.io.IOException)3 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)3 ArtifactGroup (org.apache.sling.provisioning.model.ArtifactGroup)3 HashMap (java.util.HashMap)2 Artifact (org.apache.sling.provisioning.model.Artifact)2 Section (org.apache.sling.provisioning.model.Section)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 FileOutputStream (java.io.FileOutputStream)1 LineNumberReader (java.io.LineNumberReader)1 PrintWriter (java.io.PrintWriter)1 StringReader (java.io.StringReader)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 Artifact (org.apache.maven.artifact.Artifact)1 BSNRenamer (org.apache.sling.commons.osgi.BSNRenamer)1