Search in sources :

Example 1 with JcrPackage

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;
}
Also used : HashMap(java.util.HashMap) IOException(java.io.IOException) JcrPackageManager(org.apache.jackrabbit.vault.packaging.JcrPackageManager) IOException(java.io.IOException) ZipInputStream(java.util.zip.ZipInputStream) BufferedInputStream(java.io.BufferedInputStream) Version(org.osgi.framework.Version) JcrPackage(org.apache.jackrabbit.vault.packaging.JcrPackage) PackageId(org.apache.jackrabbit.vault.packaging.PackageId) TransformationResult(org.apache.sling.installer.api.tasks.TransformationResult) Session(javax.jcr.Session)

Example 2 with JcrPackage

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);
}
Also used : HashMap(java.util.HashMap) Node(javax.jcr.Node) ArrayList(java.util.ArrayList) Resource(org.apache.sling.api.resource.Resource) ValueMapDecorator(org.apache.sling.api.wrappers.ValueMapDecorator) JcrPackage(org.apache.jackrabbit.vault.packaging.JcrPackage) PackageId(org.apache.jackrabbit.vault.packaging.PackageId) VaultPackage(org.apache.jackrabbit.vault.packaging.VaultPackage) JcrPackageDefinition(org.apache.jackrabbit.vault.packaging.JcrPackageDefinition) Before(org.junit.Before)

Example 3 with JcrPackage

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());
    }
}
Also used : JcrPackage(org.apache.jackrabbit.vault.packaging.JcrPackage) PackageId(org.apache.jackrabbit.vault.packaging.PackageId)

Example 4 with JcrPackage

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;
        }
    }
}
Also used : NodeIterator(javax.jcr.NodeIterator) Version(org.apache.jackrabbit.vault.packaging.Version) Node(javax.jcr.Node) JcrPackage(org.apache.jackrabbit.vault.packaging.JcrPackage)

Example 5 with JcrPackage

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);
        }
    }
}
Also used : ResourceResolver(org.apache.sling.api.resource.ResourceResolver) JcrPackage(org.apache.jackrabbit.vault.packaging.JcrPackage) PackageId(org.apache.jackrabbit.vault.packaging.PackageId) JcrPackageManager(org.apache.jackrabbit.vault.packaging.JcrPackageManager) Session(javax.jcr.Session)

Aggregations

JcrPackage (org.apache.jackrabbit.vault.packaging.JcrPackage)11 HashMap (java.util.HashMap)4 PackageId (org.apache.jackrabbit.vault.packaging.PackageId)4 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)4 JcrPackageManager (org.apache.jackrabbit.vault.packaging.JcrPackageManager)3 Resource (org.apache.sling.api.resource.Resource)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 Node (javax.jcr.Node)2 RepositoryException (javax.jcr.RepositoryException)2 Session (javax.jcr.Session)2 PathFilterSet (org.apache.jackrabbit.vault.fs.api.PathFilterSet)2 JcrPackageDefinition (org.apache.jackrabbit.vault.packaging.JcrPackageDefinition)2 LoginException (org.apache.sling.api.resource.LoginException)2 BufferedInputStream (java.io.BufferedInputStream)1 Map (java.util.Map)1 ZipInputStream (java.util.zip.ZipInputStream)1 NodeIterator (javax.jcr.NodeIterator)1 DefaultWorkspaceFilter (org.apache.jackrabbit.vault.fs.config.DefaultWorkspaceFilter)1 VaultPackage (org.apache.jackrabbit.vault.packaging.VaultPackage)1