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);
}
}
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;
}
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);
}
}
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;
}
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();
}
}
}
Aggregations