Search in sources :

Example 1 with DistributionException

use of org.apache.sling.distribution.common.DistributionException in project sling by apache.

the class KryoContentSerializer method importFromStream.

@Override
public void importFromStream(ResourceResolver resourceResolver, InputStream stream) throws DistributionException {
    Kryo kryo = new Kryo();
    kryo.setInstantiatorStrategy(new Kryo.DefaultInstantiatorStrategy(new StdInstantiatorStrategy()));
    kryo.addDefaultSerializer(Resource.class, new ResourceSerializer(null));
    kryo.addDefaultSerializer(InputStream.class, new InputStreamSerializer());
    try {
        Input input = new Input(stream);
        @SuppressWarnings("unchecked") LinkedList<Resource> resources = (LinkedList<Resource>) kryo.readObject(input, LinkedList.class);
        input.close();
        for (Resource resource : resources) {
            persistResource(resourceResolver, resource);
        }
        resourceResolver.commit();
    } catch (Exception e) {
        throw new DistributionException(e);
    }
}
Also used : Input(com.esotericsoftware.kryo.io.Input) StdInstantiatorStrategy(org.objenesis.strategy.StdInstantiatorStrategy) Resource(org.apache.sling.api.resource.Resource) SyntheticResource(org.apache.sling.api.resource.SyntheticResource) DistributionException(org.apache.sling.distribution.common.DistributionException) Kryo(com.esotericsoftware.kryo.Kryo) LinkedList(java.util.LinkedList) DistributionException(org.apache.sling.distribution.common.DistributionException) PersistenceException(org.apache.sling.api.resource.PersistenceException) IOException(java.io.IOException)

Example 2 with DistributionException

use of org.apache.sling.distribution.common.DistributionException in project sling by apache.

the class FileDistributionPackageBuilder method readPackageInternal.

@Override
protected DistributionPackage readPackageInternal(@Nonnull ResourceResolver resourceResolver, @Nonnull InputStream stream) throws DistributionException {
    DistributionPackage distributionPackage;
    final File file;
    DigestOutputStream outputStream = null;
    try {
        String name;
        // stable id
        Map<String, Object> info = new HashMap<String, Object>();
        DistributionPackageUtils.readInfo(stream, info);
        Object remoteId = info.get(DistributionPackageUtils.PROPERTY_REMOTE_PACKAGE_ID);
        if (remoteId != null) {
            name = remoteId.toString();
            log.debug("preserving remote id {}", name);
        } else {
            name = "distrpck-read-" + System.nanoTime();
            log.debug("generating a new id {}", name);
        }
        file = File.createTempFile(name, "." + getType(), tempDirectory);
        outputStream = openDigestOutputStream(new FileOutputStream(file), digestAlgorithm);
        IOUtils.copy(stream, outputStream);
        outputStream.flush();
        String digestMessage = readDigestMessage(outputStream);
        distributionPackage = new FileDistributionPackage(file, getType(), digestAlgorithm, digestMessage);
    } catch (Exception e) {
        throw new DistributionException(e);
    } finally {
        IOUtils.closeQuietly(outputStream);
    }
    return distributionPackage;
}
Also used : DistributionPackage(org.apache.sling.distribution.packaging.DistributionPackage) HashMap(java.util.HashMap) DigestUtils.openDigestOutputStream(org.apache.sling.distribution.util.impl.DigestUtils.openDigestOutputStream) DigestOutputStream(java.security.DigestOutputStream) FileOutputStream(java.io.FileOutputStream) DistributionException(org.apache.sling.distribution.common.DistributionException) File(java.io.File) DistributionException(org.apache.sling.distribution.common.DistributionException) IOException(java.io.IOException)

Example 3 with DistributionException

use of org.apache.sling.distribution.common.DistributionException in project sling by apache.

the class ResourceDistributionPackageBuilder method readPackageInternal.

@Override
protected DistributionPackage readPackageInternal(@Nonnull ResourceResolver resourceResolver, @Nonnull InputStream inputStream) throws DistributionException {
    try {
        Resource packagesRoot = DistributionPackageUtils.getPackagesRoot(resourceResolver, packagesPath);
        Resource packageResource = uploadStream(resourceResolver, packagesRoot, inputStream, -1);
        return new ResourceDistributionPackage(packageResource, getType(), resourceResolver, null, null);
    } catch (PersistenceException e) {
        throw new DistributionException(e);
    }
}
Also used : Resource(org.apache.sling.api.resource.Resource) PersistenceException(org.apache.sling.api.resource.PersistenceException) DistributionException(org.apache.sling.distribution.common.DistributionException)

Example 4 with DistributionException

use of org.apache.sling.distribution.common.DistributionException in project sling by apache.

the class ResourceDistributionPackageBuilder method createPackageForAdd.

@Override
protected DistributionPackage createPackageForAdd(@Nonnull ResourceResolver resourceResolver, @Nonnull DistributionRequest request) throws DistributionException {
    DistributionPackage distributionPackage;
    try {
        FileBackedMemoryOutputStream outputStream = null;
        DigestOutputStream digestStream = null;
        String digestMessage = null;
        try {
            outputStream = new FileBackedMemoryOutputStream(fileThreshold, memoryUnit, useOffHeapMemory, tempDirectory, "distrpck-create-", "." + getType());
            if (digestAlgorithm != null) {
                digestStream = openDigestOutputStream(outputStream, digestAlgorithm);
                export(resourceResolver, request, digestStream);
            } else {
                export(resourceResolver, request, outputStream);
            }
            outputStream.flush();
            if (digestAlgorithm != null) {
                digestMessage = readDigestMessage(digestStream);
            }
        } finally {
            IOUtils.closeQuietly(outputStream);
            IOUtils.closeQuietly(digestStream);
        }
        Resource packagesRoot = DistributionPackageUtils.getPackagesRoot(resourceResolver, packagesPath);
        InputStream inputStream = null;
        Resource packageResource = null;
        try {
            inputStream = outputStream.openWrittenDataInputStream();
            packageResource = uploadStream(resourceResolver, packagesRoot, inputStream, outputStream.size());
        } finally {
            IOUtils.closeQuietly(inputStream);
            outputStream.clean();
        }
        distributionPackage = new ResourceDistributionPackage(packageResource, getType(), resourceResolver, digestAlgorithm, digestMessage);
    } catch (IOException e) {
        throw new DistributionException(e);
    }
    return distributionPackage;
}
Also used : DistributionPackage(org.apache.sling.distribution.packaging.DistributionPackage) FileBackedMemoryOutputStream(org.apache.sling.distribution.util.impl.FileBackedMemoryOutputStream) DigestUtils.openDigestOutputStream(org.apache.sling.distribution.util.impl.DigestUtils.openDigestOutputStream) DigestOutputStream(java.security.DigestOutputStream) InputStream(java.io.InputStream) Resource(org.apache.sling.api.resource.Resource) DistributionException(org.apache.sling.distribution.common.DistributionException) IOException(java.io.IOException)

Example 5 with DistributionException

use of org.apache.sling.distribution.common.DistributionException in project sling by apache.

the class ResourceDistributionPackageCleanup method run.

public void run() {
    log.debug("Cleaning up {} packages", packageBuilder.getType());
    ResourceResolver serviceResolver = null;
    try {
        int deleted = 0, total = 0;
        serviceResolver = resolverFactory.getServiceResourceResolver(null);
        for (Iterator<ResourceDistributionPackage> pkgs = packageBuilder.getPackages(serviceResolver); pkgs.hasNext(); total++) {
            ResourceDistributionPackage pkg = pkgs.next();
            if (pkg.disposable()) {
                log.debug("Delete package {}", pkg.getId());
                deleted++;
                pkg.delete(false);
            } else {
                log.debug("package {} is not disposable", pkg.getId());
            }
        }
        if (serviceResolver.hasChanges()) {
            serviceResolver.commit();
        }
        log.debug("Cleaned up {}/{} {} packages", new Object[] { deleted, total, packageBuilder.getType() });
    } catch (LoginException e) {
        log.error("Failed to get distribution service resolver: {}", e.getMessage());
    } catch (DistributionException e) {
        log.error("Failed to get the list of packages", e);
    } catch (PersistenceException e) {
        log.error("Failed to delete disposable packages", e);
    } finally {
        if (serviceResolver != null && serviceResolver.isLive()) {
            serviceResolver.close();
        }
    }
}
Also used : ResourceResolver(org.apache.sling.api.resource.ResourceResolver) PersistenceException(org.apache.sling.api.resource.PersistenceException) LoginException(org.apache.sling.api.resource.LoginException) DistributionException(org.apache.sling.distribution.common.DistributionException)

Aggregations

DistributionException (org.apache.sling.distribution.common.DistributionException)35 IOException (java.io.IOException)8 DistributionPackage (org.apache.sling.distribution.packaging.DistributionPackage)8 Session (javax.jcr.Session)6 Nonnull (javax.annotation.Nonnull)5 RepositoryException (javax.jcr.RepositoryException)5 PersistenceException (org.apache.sling.api.resource.PersistenceException)5 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)5 DistributionPackageInfo (org.apache.sling.distribution.packaging.DistributionPackageInfo)5 InputStream (java.io.InputStream)4 Resource (org.apache.sling.api.resource.Resource)4 DistributionQueue (org.apache.sling.distribution.queue.DistributionQueue)4 File (java.io.File)3 FileOutputStream (java.io.FileOutputStream)3 DigestOutputStream (java.security.DigestOutputStream)3 HashMap (java.util.HashMap)3 LinkedList (java.util.LinkedList)3 LoginException (org.apache.sling.api.resource.LoginException)3 SimpleDistributionResponse (org.apache.sling.distribution.impl.SimpleDistributionResponse)3 DistributionQueueEntry (org.apache.sling.distribution.queue.DistributionQueueEntry)3