use of org.apache.jackrabbit.vault.packaging.JcrPackage 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.JcrPackage in project acs-aem-commons by Adobe-Consulting-Services.
the class JcrPackageReplicationStatusEventHandlerTest method setUp.
@Before
public void setUp() throws Exception {
calendar = Calendar.getInstance();
final List<String> contentPaths = new ArrayList<String>();
contentPaths.add("/content/foo/jcr:content");
contentPaths.add("/content/bar");
contentPaths.add("/content/dam/folder/jcr:content");
final Resource packageResource = mock(Resource.class);
final Node packageNode = mock(Node.class);
final JcrPackage jcrPackage = mock(JcrPackage.class);
final VaultPackage vaultPackage = mock(VaultPackage.class);
final Node jcrPackageNode = mock(Node.class);
final JcrPackageDefinition jcrPackageDefinition = mock(JcrPackageDefinition.class);
final Resource jcrPackageJcrContent = mock(Resource.class);
final Resource contentResource1parent = mock(Resource.class);
final Resource contentResource3parent = mock(Resource.class);
final Node contentNode1 = mock(Node.class);
final Node contentNode1parent = mock(Node.class);
final Node contentNode2 = mock(Node.class);
final Node contentNode3 = mock(Node.class);
final Node contentNode3parent = mock(Node.class);
final String[] paths = new String[] { PACKAGE_PATH };
when(job.getProperty("paths")).thenReturn(paths);
when(resourceResolverFactory.getServiceResourceResolver(anyMap())).thenReturn(resourceResolver);
when(resourceResolver.getResource(PACKAGE_PATH)).thenReturn(packageResource);
when(packageResource.adaptTo(Node.class)).thenReturn(packageNode);
when(packaging.open(packageNode, false)).thenReturn(jcrPackage);
when(packageHelper.getContents(jcrPackage)).thenReturn(contentPaths);
when(jcrPackage.getDefinition()).thenReturn(jcrPackageDefinition);
when(jcrPackageDefinition.getId()).thenReturn(mock(PackageId.class));
when(jcrPackage.getNode()).thenReturn(jcrPackageNode);
when(jcrPackageNode.getPath()).thenReturn(PACKAGE_PATH);
when(packageResource.getChild("jcr:content")).thenReturn(jcrPackageJcrContent);
when(jcrPackage.getPackage()).thenReturn(vaultPackage);
when(vaultPackage.getCreated()).thenReturn(calendar);
Map<String, Object> properties = new HashMap<String, Object>();
properties.put(JcrConstants.JCR_LASTMODIFIED, calendar);
when(jcrPackageJcrContent.adaptTo(ValueMap.class)).thenReturn(new ValueMapDecorator(properties));
when(resourceResolver.getResource("/content/foo/jcr:content")).thenReturn(contentResource1);
when(contentResource1.adaptTo(Node.class)).thenReturn(contentNode1);
when(contentNode1.isNodeType("cq:PageContent")).thenReturn(true);
when(contentResource1.getParent()).thenReturn(contentResource1parent);
when(contentResource1parent.adaptTo(Node.class)).thenReturn(contentNode1parent);
when(contentNode1parent.isNodeType("cq:Page")).thenReturn(true);
when(resourceResolver.getResource("/content/bar")).thenReturn(contentResource2);
when(contentResource2.adaptTo(Node.class)).thenReturn(contentNode2);
when(contentNode2.isNodeType("dam:AssetContent")).thenReturn(true);
when(resourceResolver.getResource("/content/dam/folder/jcr:content")).thenReturn(contentResource3);
when(contentResource3.adaptTo(Node.class)).thenReturn(contentNode3);
when(contentNode3.isNodeType("nt:unstructured")).thenReturn(true);
when(contentResource3.getParent()).thenReturn(contentResource3parent);
when(contentResource3parent.adaptTo(Node.class)).thenReturn(contentNode3parent);
when(contentNode3parent.isNodeType("sling:OrderedFolder")).thenReturn(true);
}
use of org.apache.jackrabbit.vault.packaging.JcrPackage 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.JcrPackage in project acs-aem-commons by Adobe-Consulting-Services.
the class PackageHelperImpl method getNextVersion.
@SuppressWarnings("squid:S3776")
public Version getNextVersion(final JcrPackageManager jcrPackageManager, final String groupName, final String name, final String version) throws RepositoryException {
final Node packageRoot = jcrPackageManager.getPackageRoot(false);
final Version configVersion = Version.create(version);
if (!packageRoot.hasNode(groupName)) {
return configVersion;
}
final Node packageGroupNode = packageRoot.getNode(groupName);
if (packageGroupNode == null) {
return configVersion;
} else {
final NodeIterator children = packageGroupNode.getNodes();
Version latestVersion = configVersion;
boolean configVersionEligible = true;
while (children.hasNext()) {
final Node child = children.nextNode();
final JcrPackage jcrPackage = jcrPackageManager.open(child, true);
if (jcrPackage == null || jcrPackage.getDefinition() == null || jcrPackage.getDefinition().getId() == null) {
log.warn("Could not covert node [ {} ] into a proper JCR Package, moving to next node", child.getPath());
continue;
} else if (!StringUtils.equals(name, jcrPackage.getDefinition().getId().getName())) {
// Name mismatch - so just skip
continue;
}
final Version packageVersion = jcrPackage.getDefinition().getId().getVersion();
log.debug(packageVersion.toString() + " compareTo " + latestVersion.toString() + " = " + packageVersion.compareTo(latestVersion));
if (packageVersion.compareTo(latestVersion) >= 1) {
latestVersion = packageVersion;
log.debug("Found a new latest version: {}", latestVersion.toString());
} else if (packageVersion.compareTo(configVersion) == 0) {
configVersionEligible = false;
log.debug("Found a package with the same version as the config version");
}
}
log.debug("Current latest version: {}", latestVersion.toString());
if (configVersionEligible && latestVersion.equals(configVersion)) {
// If the config-specified version is newer than any existing package, jump to the config version
return configVersion;
} else {
// Else increment the latest known version's minor
latestVersion = this.normalizeVersion(latestVersion);
final String[] segments = latestVersion.getNormalizedSegments();
// Increment minor
segments[1] = String.valueOf(Integer.parseInt(segments[1]) + 1);
final Version nextVersion = Version.create(segments);
log.debug("Next version: {}", nextVersion);
return nextVersion;
}
}
}
use of org.apache.jackrabbit.vault.packaging.JcrPackage 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