use of org.apache.sling.provisioning.model.ArtifactGroup in project sling by apache.
the class PreparePackageMojo method getRunModesManifest.
private Manifest getRunModesManifest(Feature feature) throws MojoExecutionException {
Map<String, StringBuilder> runModes = new HashMap<>();
for (RunMode rm : feature.getRunModes()) {
for (ArtifactGroup ag : rm.getArtifactGroups()) {
// For subsystems the start level on the artifact group is used as start order.
int startOrder = ag.getStartLevel();
for (org.apache.sling.provisioning.model.Artifact a : ag) {
Artifact artifact = ModelUtils.getArtifact(this.project, this.mavenSession, this.artifactHandlerManager, this.resolver, a.getGroupId(), a.getArtifactId(), a.getVersion(), a.getType(), a.getClassifier());
File artifactFile = artifact.getFile();
String entryName = getEntryName(artifactFile, startOrder);
String[] runModeNames = rm.getNames();
if (runModeNames == null)
runModeNames = new String[] { ALL_RUNMODES_KEY };
for (String runModeName : runModeNames) {
StringBuilder sb = runModes.get(runModeName);
if (sb == null) {
sb = new StringBuilder();
runModes.put(runModeName, sb);
} else {
sb.append('|');
}
sb.append(entryName);
}
}
}
}
Manifest mf = new Manifest();
Attributes attrs = mf.getMainAttributes();
// Manifest does not work without this value
attrs.putValue("Manifest-Version", "1.0");
attrs.putValue("About-This-Manifest", "This is not a real manifest, it is used as information when this archive is transformed into a real subsystem .esa file");
for (Map.Entry<String, StringBuilder> entry : runModes.entrySet()) {
attrs.putValue(entry.getKey().replace(':', '_'), entry.getValue().toString());
}
return mf;
}
use of org.apache.sling.provisioning.model.ArtifactGroup in project sling by apache.
the class PreparePackageMojo method createSubsystemBaseFile.
private File createSubsystemBaseFile(Feature feature, AtomicInteger startLevelHolder) throws MojoExecutionException {
File subsystemFile = new File(getTmpDir(), feature.getName() + ".subsystem-base");
if (subsystemFile.exists()) {
// TODO is there a better way to avoid calling this multiple times?
return null;
}
startLevelHolder.set(-1);
// The runmodes information has to be the first item in the archive so that we always have it available when the
// archive is being processed. For this reason a Jar file is used here as it's guaranteed to store the manifest
// first.
Manifest runModesManifest = getRunModesManifest(feature);
getLog().info("Creating subsystem base file: " + subsystemFile.getName());
boolean created = subsystemFile.getParentFile().mkdirs();
if (!created) {
throw new MojoExecutionException("Failed creating " + subsystemFile.getParentFile().getAbsolutePath());
}
try (JarOutputStream os = new JarOutputStream(new FileOutputStream(subsystemFile), runModesManifest)) {
Map<String, Integer> bsnStartOrderMap = new HashMap<>();
for (RunMode rm : feature.getRunModes()) {
for (ArtifactGroup ag : rm.getArtifactGroups()) {
// For subsystems the start level on the artifact group is used as start order.
int startOrder = ag.getStartLevel();
for (org.apache.sling.provisioning.model.Artifact a : ag) {
Artifact artifact = ModelUtils.getArtifact(this.project, this.mavenSession, this.artifactHandlerManager, this.resolver, a.getGroupId(), a.getArtifactId(), a.getVersion(), a.getType(), a.getClassifier());
File artifactFile = artifact.getFile();
String entryName = getEntryName(artifactFile, startOrder);
ZipEntry ze = new ZipEntry(entryName);
try {
os.putNextEntry(ze);
Files.copy(artifactFile.toPath(), os);
} finally {
os.closeEntry();
}
}
}
}
int sl = createSubsystemManifest(feature, bsnStartOrderMap, os);
if (sl != -1)
startLevelHolder.set(sl);
addReadme(os);
} catch (IOException ioe) {
throw new MojoExecutionException("Problem creating subsystem .esa file " + subsystemFile, ioe);
}
return subsystemFile;
}
use of org.apache.sling.provisioning.model.ArtifactGroup 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.ArtifactGroup 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.ArtifactGroup 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