use of org.apache.jackrabbit.vault.packaging.JcrPackageManager 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.jackrabbit.vault.packaging.JcrPackageManager in project acs-aem-commons by Adobe-Consulting-Services.
the class AutomaticPackageReplicatorJob method excute.
public void excute() throws RepositoryException, PackageException, IOException, ReplicationException {
boolean succeeded = false;
ResourceResolver resolver = null;
try {
resolver = ConfigurationUpdateListener.getResourceResolver(resolverFactory);
Session session = resolver.adaptTo(Session.class);
JcrPackageManager pkgMgr = PackagingService.getPackageManager(session);
PackageId packageId = new PackageId(packagePath);
// check if the package exists
JcrPackage jcrPackage = pkgMgr.open(packageId);
if (jcrPackage == null || jcrPackage.getNode() == null) {
log.warn("Package at path " + packagePath + " does not exist");
throw new IllegalArgumentException("Package at path " + packagePath + " does not exist");
}
log.debug("Assembling package {}", packagePath);
pkgMgr.assemble(jcrPackage, null);
log.debug("Replicating package {}", packagePath);
replicator.replicate(session, ReplicationActionType.ACTIVATE, jcrPackage.getNode().getPath());
log.debug("Package {} replicated successfully!", packagePath);
fireEvent(OSGI_EVENT_REPLICATED_TOPIC);
succeeded = true;
} finally {
if (resolver != null) {
resolver.close();
}
if (!succeeded) {
fireEvent(OSGI_EVENT_FAILED_TOPIC);
}
}
}
use of org.apache.jackrabbit.vault.packaging.JcrPackageManager in project acs-aem-commons by Adobe-Consulting-Services.
the class PackageHelperImpl method createPackageFromPathFilterSets.
/**
* {@inheritDoc}
*/
public JcrPackage createPackageFromPathFilterSets(final Collection<PathFilterSet> pathFilterSets, final Session session, final String groupName, final String name, String version, final ConflictResolution conflictResolution, final Map<String, String> packageDefinitionProperties) throws IOException, RepositoryException {
final JcrPackageManager jcrPackageManager = packaging.getPackageManager(session);
if (ConflictResolution.Replace.equals(conflictResolution)) {
this.removePackage(jcrPackageManager, groupName, name, version);
} else if (ConflictResolution.IncrementVersion.equals(conflictResolution)) {
version = this.getNextVersion(jcrPackageManager, groupName, name, version).toString();
}
final JcrPackage jcrPackage = jcrPackageManager.create(groupName, name, version);
final JcrPackageDefinition jcrPackageDefinition = jcrPackage.getDefinition();
final DefaultWorkspaceFilter workspaceFilter = new DefaultWorkspaceFilter();
for (final PathFilterSet pathFilterSet : pathFilterSets) {
workspaceFilter.add(pathFilterSet);
}
jcrPackageDefinition.setFilter(workspaceFilter, true);
for (final Map.Entry<String, String> entry : packageDefinitionProperties.entrySet()) {
jcrPackageDefinition.set(entry.getKey(), entry.getValue(), false);
}
session.save();
return jcrPackage;
}
Aggregations