use of org.apache.sling.installer.api.tasks.TransformationResult in project sling by apache.
the class DeploymentPackageInstaller method transform.
/**
* @see org.apache.sling.installer.api.tasks.ResourceTransformer#transform(org.apache.sling.installer.api.tasks.RegisteredResource)
*/
public TransformationResult[] transform(final RegisteredResource resource) {
if (resource.getType().equals(InstallableResource.TYPE_FILE)) {
try {
final Manifest m = getManifest(resource.getInputStream());
if (m != null) {
final String sn = m.getMainAttributes().getValue(DEPLOYMENTPACKAGE_SYMBOLICMAME);
if (sn != null) {
final String v = m.getMainAttributes().getValue(DEPLOYMENTPACKAGE_VERSION);
if (v != null) {
final Map<String, Object> attr = new HashMap<String, Object>();
attr.put(DEPLOYMENTPACKAGE_SYMBOLICMAME, sn);
final TransformationResult tr = new TransformationResult();
tr.setId(sn);
tr.setVersion(new Version(v));
tr.setResourceType(TYPE_DP);
tr.setAttributes(attr);
return new TransformationResult[] { tr };
}
}
}
} catch (final IOException ignore) {
// ignore
}
}
return null;
}
use of org.apache.sling.installer.api.tasks.TransformationResult in project sling by apache.
the class ModelTransformer method transform.
@Override
public TransformationResult[] transform(final RegisteredResource resource) {
Model model = null;
File baseDir = null;
if (resource.getType().equals(InstallableResource.TYPE_FILE) && resource.getURL().endsWith(".model")) {
try (final Reader reader = new InputStreamReader(resource.getInputStream(), "UTF-8")) {
model = ModelReader.read(reader, resource.getURL());
} catch (final IOException ioe) {
logger.info("Unable to read model from " + resource.getURL(), ioe);
}
}
if (resource.getType().equals(InstallableResource.TYPE_FILE) && resource.getURL().endsWith(".mar")) {
baseDir = this.bundleContext.getDataFile("");
try (final InputStream is = resource.getInputStream()) {
model = ModelArchiveReader.read(is, new ModelArchiveReader.ArtifactConsumer() {
@Override
public void consume(final Artifact artifact, final InputStream is) throws IOException {
// nothing to do, install task does extraction
}
});
} catch (final IOException ioe) {
logger.info("Unable to read model from " + resource.getURL(), ioe);
}
}
if (model != null) {
Map<Traceable, String> errors = ModelUtility.validate(model);
if (errors == null) {
try {
final Model effectiveModel = ModelUtility.getEffectiveModel(model);
errors = ModelUtility.validateIncludingVersion(effectiveModel);
if (errors == null) {
String modelTxt = null;
try (final StringWriter sw = new StringWriter()) {
ModelWriter.write(sw, effectiveModel);
modelTxt = sw.toString();
} catch (final IOException ioe) {
logger.info("Unable to read model from " + resource.getURL(), ioe);
}
if (modelTxt != null) {
final TransformationResult[] result = new TransformationResult[effectiveModel.getFeatures().size()];
int index = 0;
for (final Feature f : effectiveModel.getFeatures()) {
final TransformationResult tr = new TransformationResult();
tr.setResourceType(TYPE_PROV_MODEL);
tr.setId(f.getName());
tr.setVersion(new Version(f.getVersion()));
final Map<String, Object> attributes = new HashMap<>();
attributes.put(ATTR_MODEL, modelTxt);
attributes.put(ATTR_FEATURE_INDEX, index);
attributes.put(ATTR_FEATURE_NAME, f.getName() + "-" + f.getVersion());
if (baseDir != null) {
final File dir = new File(baseDir, f.getName() + "-" + f.getVersion());
attributes.put(ATTR_BASE_PATH, dir.getAbsolutePath());
}
tr.setAttributes(attributes);
result[index] = tr;
index++;
}
return result;
}
}
} catch (final IllegalArgumentException iae) {
errors = Collections.singletonMap((Traceable) model, iae.getMessage());
}
}
if (errors != null) {
logger.warn("Errors during parsing model at {} : {}", resource.getURL(), errors.values());
}
}
return null;
}
use of org.apache.sling.installer.api.tasks.TransformationResult in project sling by apache.
the class PackageTransformer method checkForPackage.
/**
* Check if the resource is a content package
* @param resource The resource
* @return {@code null} if not a content package, a result otherwise
*/
private TransformationResult[] checkForPackage(final RegisteredResource resource) {
// first check if this is a zip archive
try (final ZipInputStream zin = new ZipInputStream(new BufferedInputStream(resource.getInputStream()))) {
if (zin.getNextEntry() == null) {
return null;
}
} catch (final IOException ioe) {
logger.debug("Unable to read resource.", ioe);
return null;
}
Session session = null;
JcrPackage pck = null;
try {
// create an admin session
session = repository.loginAdministrative(null);
final JcrPackageManager pckMgr = pkgSvc.getPackageManager(session);
pck = pckMgr.upload(resource.getInputStream(), true, true);
if (pck.isValid()) {
final PackageId pid = pck.getDefinition().getId();
final Map<String, Object> attrs = new HashMap<String, Object>();
attrs.put(ATTR_PCK_ID, pid.toString());
final TransformationResult tr = new TransformationResult();
tr.setId(pid.getGroup() + ':' + pid.getName());
tr.setResourceType(RESOURCE_TYPE);
tr.setAttributes(attrs);
// version
final String version = pid.getVersionString();
if (version.length() > 0) {
tr.setVersion(new Version(cleanupVersion(version)));
}
return new TransformationResult[] { tr };
}
} catch (final Exception ioe) {
logger.debug("Unable to check content package " + resource.getURL(), ioe);
} finally {
if (pck != null) {
pck.close();
}
if (session != null) {
session.logout();
}
}
return null;
}
use of org.apache.sling.installer.api.tasks.TransformationResult in project sling by apache.
the class SubsystemBaseTransformer method transform.
public TransformationResult[] transform(RegisteredResource resource) {
// TODO start level of the subsystem
if (resource.getType().equals(InstallableResource.TYPE_FILE)) {
if (resource.getURL().endsWith("." + TYPE_SUBSYSTEM_BASE)) {
logger.info("Found subsystem-base resource {}", resource);
try {
SubsystemData ssd = createSubsystemFile(resource);
TransformationResult tr = new TransformationResult();
Attributes mfAttributes = ssd.manifest.getMainAttributes();
tr.setId(mfAttributes.getValue(SubsystemConstants.SUBSYSTEM_SYMBOLICNAME));
tr.setVersion(new Version(mfAttributes.getValue(SubsystemConstants.SUBSYSTEM_VERSION)));
tr.setResourceType("esa");
tr.setInputStream(new DeleteOnCloseFileInputStream(ssd.file));
Map<String, Object> attr = new HashMap<String, Object>();
attr.put(SubsystemConstants.SUBSYSTEM_SYMBOLICNAME, mfAttributes.getValue(SubsystemConstants.SUBSYSTEM_SYMBOLICNAME));
attr.put(SubsystemConstants.SUBSYSTEM_VERSION, mfAttributes.getValue(SubsystemConstants.SUBSYSTEM_VERSION));
tr.setAttributes(attr);
return new TransformationResult[] { tr };
} catch (IOException ioe) {
logger.error("Problem processing subsystem-base file " + resource, ioe);
}
}
}
return null;
}
use of org.apache.sling.installer.api.tasks.TransformationResult in project sling by apache.
the class OsgiInstallerImpl method transformResources.
/**
* Invoke the transformers on the resources.
*/
private void transformResources() {
boolean changed = false;
final List<ServiceReference> serviceRefs = this.transformerTracker.getSortedServiceReferences();
if (serviceRefs.size() > 0) {
synchronized (this.resourcesLock) {
// Walk the list of unknown resources and invoke all transformers
int index = 0;
final List<RegisteredResource> unknownList = this.persistentList.getUntransformedResources();
while (index < unknownList.size()) {
final RegisteredResource resource = unknownList.get(index);
for (final ServiceReference reference : serviceRefs) {
final Long id = (Long) reference.getProperty(Constants.SERVICE_ID);
// check if this transformer has already been invoked for the resource
final String transformers = (String) ((RegisteredResourceImpl) resource).getAttribute(ResourceTransformer.class.getName());
if (id == null || (transformers != null && transformers.contains(":" + id + ':'))) {
continue;
}
final ResourceTransformer transformer = (ResourceTransformer) this.transformerTracker.getService(reference);
if (transformer != null) {
try {
final TransformationResult[] result = transformer.transform(resource);
final String newTransformers = (transformers == null ? ":" + id + ':' : transformers + id + ':');
((RegisteredResourceImpl) resource).setAttribute(ResourceTransformer.class.getName(), newTransformers);
if (logger.isDebugEnabled()) {
logger.debug("Invoked transformer {} on {} : {}", new Object[] { transformer, resource, Arrays.toString(result) });
}
if (result != null && result.length > 0) {
this.persistentList.transform(resource, result);
changed = true;
index--;
break;
}
} catch (final Throwable t) {
logger.error("Uncaught exception during resource transformation!", t);
}
}
}
index++;
}
}
if (changed) {
this.persistentList.save();
printResources("Transformed");
}
}
}
Aggregations