Search in sources :

Example 1 with Feature

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

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

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

the class PreparePackageMojo method buildContentsMap.

/**
     * Build a list of all artifacts.
     */
private void buildContentsMap(final Model model, final String packageRunMode, final Map<String, File> contentsMap) throws MojoExecutionException {
    if (packageRunMode == null) {
        // add base jar
        final Artifact artifact = getBaseArtifact(model, null, BuildConstants.TYPE_JAR);
        contentsMap.put(BASE_DESTINATION + "/" + artifact.getArtifactId() + "." + artifact.getArtifactHandler().getExtension(), artifact.getFile());
    }
    for (final Feature feature : model.getFeatures()) {
        if (feature.isSpecial() && !feature.getName().equals(ModelConstants.FEATURE_BOOT)) {
            continue;
        } else if (FeatureTypes.SUBSYSTEM_APPLICATION.equals(feature.getType()) || FeatureTypes.SUBSYSTEM_COMPOSITE.equals(feature.getType()) || FeatureTypes.SUBSYSTEM_FEATURE.equals(feature.getType())) {
            buildSubsystemBase(contentsMap, feature);
        } else {
            for (final RunMode runMode : feature.getRunModes()) {
                if (packageRunMode == null) {
                    if (runMode.isSpecial()) {
                        continue;
                    }
                    this.buildContentsMap(model, runMode, contentsMap, feature.getName().equals(ModelConstants.FEATURE_BOOT));
                } else {
                    if (runMode.isRunMode(packageRunMode)) {
                        this.buildContentsMap(model, runMode, contentsMap, feature.getName().equals(ModelConstants.FEATURE_BOOT));
                    }
                }
            }
        }
    }
}
Also used : RunMode(org.apache.sling.provisioning.model.RunMode) Feature(org.apache.sling.provisioning.model.Feature) Artifact(org.apache.maven.artifact.Artifact)

Example 4 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 5 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)

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