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 ConfigTaskCreator method checkConfiguration.
/**
* Check if the registered resource is a configuration
* @param resource The resource
*/
private TransformationResult[] checkConfiguration(final RegisteredResource resource) {
final String url = separatorsToUnix(resource.getURL());
String lastIdPart = url;
final int pos = lastIdPart.lastIndexOf('/');
if (pos != -1) {
lastIdPart = lastIdPart.substring(pos + 1);
}
final String pid;
// remove extension if known
if (isConfigExtension(getExtension(lastIdPart))) {
final int lastDot = lastIdPart.lastIndexOf('.');
pid = lastIdPart.substring(0, lastDot);
} else {
pid = lastIdPart;
}
// split pid and factory pid alias
final String factoryPid;
final String configPid;
int n = pid.indexOf('-');
if (n > 0) {
// quick check if this is an existing configuration
final String fString = pid.substring(0, n);
final String cString = pid.substring(n + 1);
boolean useExtendedPid = false;
try {
if (ConfigUtil.getConfiguration(this.configAdmin, fString, fString + '.' + cString) != null) {
useExtendedPid = true;
}
} catch (final Exception ignore) {
// ignore this
}
if (useExtendedPid) {
configPid = fString + '.' + cString;
} else {
configPid = pid.substring(n + 1);
}
factoryPid = pid.substring(0, n);
} else {
factoryPid = null;
configPid = pid;
}
final Map<String, Object> attr = new HashMap<String, Object>();
attr.put(Constants.SERVICE_PID, configPid);
// Factory?
if (factoryPid != null) {
attr.put(ConfigurationAdmin.SERVICE_FACTORYPID, factoryPid);
}
final TransformationResult tr = new TransformationResult();
final String id = (factoryPid == null ? "" : factoryPid + ".") + configPid;
tr.setId(id);
tr.setResourceType(InstallableResource.TYPE_CONFIG);
tr.setAttributes(attr);
return new TransformationResult[] { tr };
}
use of org.apache.sling.installer.api.tasks.TransformationResult in project sling by apache.
the class RegisteredResourceComparatorTest method getConfig.
private RegisteredResourceImpl getConfig(String url, Dictionary<String, Object> data, int priority, String digest, ResourceState state) throws IOException {
if (data == null) {
data = new Hashtable<String, Object>();
data.put("foo", "bar");
}
new FileDataStore(new MockBundleContext());
final InstallableResource r = new InstallableResource(url, null, data, digest, null, priority);
final InternalResource internal = InternalResource.create("test", r);
final RegisteredResourceImpl rr = RegisteredResourceImpl.create(internal);
TransformationResult[] tr = new DefaultTransformer().transform(rr);
if (tr == null) {
final TransformationResult result = new TransformationResult();
result.setId(url);
result.setResourceType(InstallableResource.TYPE_CONFIG);
tr = new TransformationResult[] { result };
}
final RegisteredResourceImpl result = (RegisteredResourceImpl) rr.clone(tr[0]);
if (state != null) {
result.setState(state, null);
}
return result;
}
Aggregations