use of org.apache.jackrabbit.vault.packaging.PackageId in project sling by apache.
the class PackageTransformer method createTask.
/**
* @see org.apache.sling.installer.api.tasks.InstallTaskFactory#createTask(org.apache.sling.installer.api.tasks.TaskResourceGroup)
*/
@Override
public InstallTask createTask(final TaskResourceGroup toActivate) {
final TaskResource resource = toActivate.getActiveResource();
if (resource == null || !resource.getType().equals(RESOURCE_TYPE)) {
return null;
}
// extract the package id
final String id = (String) resource.getAttribute(ATTR_PCK_ID);
final PackageId pkgId = PackageId.fromString(id);
if (pkgId == null) {
logger.error("Error during processing of {}: Package id is wrong/null.", resource);
return new ChangeStateTask(toActivate, ResourceState.IGNORED);
}
if (resource.getState() == ResourceState.INSTALL) {
return new InstallPackageTask(pkgId, toActivate);
}
return new UninstallPackageTask(pkgId, toActivate);
}
use of org.apache.jackrabbit.vault.packaging.PackageId 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.PackageId in project acs-aem-commons by Adobe-Consulting-Services.
the class PackageHelperImplTest method testRemovePackage.
@Test
public void testRemovePackage() throws Exception {
final PackageId packageId = new PackageId(packageGroup, packageName, packageOneVersion);
when(jcrPackageManager.open(packageId)).thenReturn(packageOne);
when(packageOneNode.getSession()).thenReturn(mock(Session.class));
packageHelper.removePackage(jcrPackageManager, packageGroup, packageName, packageOneVersion);
verify(packageOneNode, times(1)).remove();
}
use of org.apache.jackrabbit.vault.packaging.PackageId in project acs-aem-commons by Adobe-Consulting-Services.
the class PackageHelperImpl method removePackage.
/**
* {@inheritDoc}
*/
public void removePackage(final JcrPackageManager jcrPackageManager, final String groupName, final String name, final String version) throws RepositoryException {
final PackageId packageId = new PackageId(groupName, name, version);
final JcrPackage jcrPackage = jcrPackageManager.open(packageId);
if (jcrPackage != null && jcrPackage.getNode() != null) {
jcrPackage.getNode().remove();
jcrPackage.getNode().getSession().save();
} else {
log.debug("Nothing to remove at: ", packageId.getInstallationPath());
}
}
use of org.apache.jackrabbit.vault.packaging.PackageId 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);
}
}
}
Aggregations