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);
}
}
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);
}
}
}
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));
}
}
}
}
}
}
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;
}
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;
}
Aggregations