Search in sources :

Example 1 with TransformationResult

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;
}
Also used : HashMap(java.util.HashMap) Version(org.osgi.framework.Version) IOException(java.io.IOException) Manifest(java.util.jar.Manifest) TransformationResult(org.apache.sling.installer.api.tasks.TransformationResult)

Example 2 with TransformationResult

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;
}
Also used : InputStreamReader(java.io.InputStreamReader) HashMap(java.util.HashMap) InputStream(java.io.InputStream) ModelArchiveReader(org.apache.sling.provisioning.model.io.ModelArchiveReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) ModelReader(org.apache.sling.provisioning.model.io.ModelReader) IOException(java.io.IOException) Feature(org.apache.sling.provisioning.model.Feature) Artifact(org.apache.sling.provisioning.model.Artifact) StringWriter(java.io.StringWriter) Version(org.osgi.framework.Version) Model(org.apache.sling.provisioning.model.Model) Traceable(org.apache.sling.provisioning.model.Traceable) TransformationResult(org.apache.sling.installer.api.tasks.TransformationResult) File(java.io.File)

Example 3 with TransformationResult

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;
}
Also used : HashMap(java.util.HashMap) IOException(java.io.IOException) JcrPackageManager(org.apache.jackrabbit.vault.packaging.JcrPackageManager) IOException(java.io.IOException) ZipInputStream(java.util.zip.ZipInputStream) BufferedInputStream(java.io.BufferedInputStream) Version(org.osgi.framework.Version) JcrPackage(org.apache.jackrabbit.vault.packaging.JcrPackage) PackageId(org.apache.jackrabbit.vault.packaging.PackageId) TransformationResult(org.apache.sling.installer.api.tasks.TransformationResult) Session(javax.jcr.Session)

Example 4 with TransformationResult

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;
}
Also used : Version(org.osgi.framework.Version) HashMap(java.util.HashMap) Attributes(java.util.jar.Attributes) IOException(java.io.IOException) TransformationResult(org.apache.sling.installer.api.tasks.TransformationResult)

Example 5 with TransformationResult

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");
        }
    }
}
Also used : RegisteredResource(org.apache.sling.installer.api.tasks.RegisteredResource) ServiceReference(org.osgi.framework.ServiceReference) AtomicLong(java.util.concurrent.atomic.AtomicLong) ResourceTransformer(org.apache.sling.installer.api.tasks.ResourceTransformer) TransformationResult(org.apache.sling.installer.api.tasks.TransformationResult)

Aggregations

TransformationResult (org.apache.sling.installer.api.tasks.TransformationResult)18 HashMap (java.util.HashMap)9 IOException (java.io.IOException)6 Version (org.osgi.framework.Version)6 RegisteredResource (org.apache.sling.installer.api.tasks.RegisteredResource)5 HashSet (java.util.HashSet)3 Attributes (java.util.jar.Attributes)3 Manifest (java.util.jar.Manifest)3 ZipInputStream (java.util.zip.ZipInputStream)3 InstallableResource (org.apache.sling.installer.api.InstallableResource)3 InputStream (java.io.InputStream)2 URL (java.net.URL)2 Hashtable (java.util.Hashtable)2 JarFile (java.util.jar.JarFile)2 ZipEntry (java.util.zip.ZipEntry)2 ResourceTransformer (org.apache.sling.installer.api.tasks.ResourceTransformer)2 TaskResource (org.apache.sling.installer.api.tasks.TaskResource)2 DeleteOnCloseFileInputStream (org.apache.sling.installer.factories.subsystems.base.impl.SubsystemBaseTransformer.DeleteOnCloseFileInputStream)2 SlingSettingsService (org.apache.sling.settings.SlingSettingsService)2 Test (org.junit.Test)2