Search in sources :

Example 1 with PackageId

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);
}
Also used : TaskResource(org.apache.sling.installer.api.tasks.TaskResource) PackageId(org.apache.jackrabbit.vault.packaging.PackageId) ChangeStateTask(org.apache.sling.installer.api.tasks.ChangeStateTask)

Example 2 with PackageId

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;
}
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 3 with PackageId

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();
}
Also used : PackageId(org.apache.jackrabbit.vault.packaging.PackageId) Session(javax.jcr.Session) Test(org.junit.Test)

Example 4 with PackageId

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

Example 5 with PackageId

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

PackageId (org.apache.jackrabbit.vault.packaging.PackageId)5 Session (javax.jcr.Session)3 JcrPackage (org.apache.jackrabbit.vault.packaging.JcrPackage)3 JcrPackageManager (org.apache.jackrabbit.vault.packaging.JcrPackageManager)2 BufferedInputStream (java.io.BufferedInputStream)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 ZipInputStream (java.util.zip.ZipInputStream)1 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)1 ChangeStateTask (org.apache.sling.installer.api.tasks.ChangeStateTask)1 TaskResource (org.apache.sling.installer.api.tasks.TaskResource)1 TransformationResult (org.apache.sling.installer.api.tasks.TransformationResult)1 Test (org.junit.Test)1 Version (org.osgi.framework.Version)1