Search in sources :

Example 1 with JcrPackageManager

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

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

Example 3 with JcrPackageManager

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;
}
Also used : DefaultWorkspaceFilter(org.apache.jackrabbit.vault.fs.config.DefaultWorkspaceFilter) PathFilterSet(org.apache.jackrabbit.vault.fs.api.PathFilterSet) JcrPackage(org.apache.jackrabbit.vault.packaging.JcrPackage) JcrPackageManager(org.apache.jackrabbit.vault.packaging.JcrPackageManager) Map(java.util.Map) JcrPackageDefinition(org.apache.jackrabbit.vault.packaging.JcrPackageDefinition)

Aggregations

JcrPackage (org.apache.jackrabbit.vault.packaging.JcrPackage)3 JcrPackageManager (org.apache.jackrabbit.vault.packaging.JcrPackageManager)3 Session (javax.jcr.Session)2 PackageId (org.apache.jackrabbit.vault.packaging.PackageId)2 BufferedInputStream (java.io.BufferedInputStream)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ZipInputStream (java.util.zip.ZipInputStream)1 PathFilterSet (org.apache.jackrabbit.vault.fs.api.PathFilterSet)1 DefaultWorkspaceFilter (org.apache.jackrabbit.vault.fs.config.DefaultWorkspaceFilter)1 JcrPackageDefinition (org.apache.jackrabbit.vault.packaging.JcrPackageDefinition)1 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)1 TransformationResult (org.apache.sling.installer.api.tasks.TransformationResult)1 Version (org.osgi.framework.Version)1