Search in sources :

Example 6 with BatchProcessor

use of org.alfresco.repo.batch.BatchProcessor in project alfresco-repository by Alfresco.

the class NodeArchiveServiceImpl method doBulkOperation.

/**
 * Do batch-controlled work
 */
private void doBulkOperation(final String user, StoreRef originalStoreRef, BatchProcessWorker<NodeRef> worker) {
    String lockToken = null;
    try {
        // Get a lock to keep refreshing
        lockToken = jobLockService.getLock(LOCK_QNAME, LOCK_TTL);
        // TODO: Should merely trigger a background job i.e. perhaps it should not be
        // triggered by a user-based thread
        BatchProcessor<NodeRef> batchProcessor = new BatchProcessor<NodeRef>("ArchiveBulkPurgeOrRestore", transactionService.getRetryingTransactionHelper(), getArchivedNodesWorkProvider(originalStoreRef, lockToken), 2, 20, null, null, 1000);
        batchProcessor.process(worker, true);
    } catch (LockAcquisitionException e) {
        throw new AlfrescoRuntimeException(MSG_BUSY);
    } finally {
        try {
            if (lockToken != null) {
                jobLockService.releaseLock(lockToken, LOCK_QNAME);
            }
        } catch (LockAcquisitionException e) {
        // Ignore
        }
    }
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) BatchProcessor(org.alfresco.repo.batch.BatchProcessor) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) LockAcquisitionException(org.alfresco.repo.lock.LockAcquisitionException)

Example 7 with BatchProcessor

use of org.alfresco.repo.batch.BatchProcessor in project alfresco-repository by Alfresco.

the class RenameSiteAuthorityDisplayName method renameDispayNames.

/**
 * Rename display names of authorities of sites.
 *
 * @param siteInfos
 *            list of sites
 */
private void renameDispayNames(final List<SiteInfo> siteInfos) {
    final String tenantDomain = tenantAdminService.getCurrentUserDomain();
    final Iterator<SiteInfo> pathItr = siteInfos.listIterator();
    BatchProcessWorkProvider<SiteInfo> siteWorkProvider = new BatchProcessWorkProvider<SiteInfo>() {

        @Override
        public int getTotalEstimatedWorkSize() {
            return siteInfos.size();
        }

        @Override
        public Collection<SiteInfo> getNextWork() {
            int batchCount = 0;
            List<SiteInfo> nodes = new ArrayList<SiteInfo>(BATCH_SIZE);
            while (pathItr.hasNext() && batchCount++ != BATCH_SIZE) {
                nodes.add(pathItr.next());
            }
            return nodes;
        }
    };
    // prepare the batch processor and worker object
    BatchProcessor<SiteInfo> siteBatchProcessor = new BatchProcessor<SiteInfo>("RenameSiteAuthorityDisplayName", this.transactionHelper, siteWorkProvider, BATCH_THREADS, BATCH_SIZE, this.applicationEventPublisher, progress_logger, BATCH_SIZE * 10);
    BatchProcessWorker<SiteInfo> worker = new BatchProcessWorker<SiteInfo>() {

        @Override
        public String getIdentifier(SiteInfo entry) {
            return entry.getShortName();
        }

        @Override
        public void beforeProcess() throws Throwable {
            // Disable rules
            ruleService.disableRules();
            // Authentication
            String systemUser = AuthenticationUtil.getSystemUserName();
            systemUser = tenantAdminService.getDomainUser(systemUser, tenantDomain);
            AuthenticationUtil.setRunAsUser(systemUser);
        }

        @Override
        public void afterProcess() throws Throwable {
            // Enable rules
            ruleService.enableRules();
            // Clear authentication
            AuthenticationUtil.clearCurrentSecurityContext();
        }

        @Override
        public void process(SiteInfo siteInfo) throws Throwable {
            // Set all the permissions of site
            Set<AccessPermission> sitePermissions = permissionService.getAllSetPermissions(siteInfo.getNodeRef());
            for (AccessPermission sitePermission : sitePermissions) {
                // Use only GROUP authority
                if (sitePermission.getAuthorityType() == AuthorityType.GROUP) {
                    String authorityName = sitePermission.getAuthority();
                    String currDisplayName = authorityService.getAuthorityDisplayName(authorityName);
                    String necessaryName = ((SiteServiceImpl) siteService).getSiteRoleGroup(siteInfo.getShortName(), sitePermission.getPermission(), false);
                    String alternativeName = ((SiteServiceImpl) siteService).getSiteRoleGroup(siteInfo.getShortName(), sitePermission.getPermission(), true);
                    // check for correct displayName
                    if ((!necessaryName.equalsIgnoreCase(currDisplayName)) || (!alternativeName.equalsIgnoreCase(currDisplayName))) {
                        // fix incorrect display name
                        authorityService.setAuthorityDisplayName(authorityName, necessaryName);
                    }
                }
            }
        }
    };
    siteBatchProcessor.process(worker, true);
}
Also used : SiteInfo(org.alfresco.service.cmr.site.SiteInfo) ArrayList(java.util.ArrayList) AccessPermission(org.alfresco.service.cmr.security.AccessPermission) SiteServiceImpl(org.alfresco.repo.site.SiteServiceImpl) BatchProcessWorkProvider(org.alfresco.repo.batch.BatchProcessWorkProvider) BatchProcessWorker(org.alfresco.repo.batch.BatchProcessor.BatchProcessWorker) BatchProcessor(org.alfresco.repo.batch.BatchProcessor)

Example 8 with BatchProcessor

use of org.alfresco.repo.batch.BatchProcessor in project alfresco-repository by Alfresco.

the class ImapUnsubscribedAspectPatch method applyInternal.

@Override
protected String applyInternal() throws Exception {
    final List<ChildAssociationRef> users = nodeService.getChildAssocs(personService.getPeopleContainer(), ContentModel.ASSOC_CHILDREN, RegexQNamePattern.MATCH_ALL);
    final long maxNodeId = patchDAO.getMaxAdmNodeID();
    BatchProcessWorkProvider<NodeRef> workProvider = new BatchProcessWorkProvider<NodeRef>() {

        final List<NodeRef> result = new ArrayList<NodeRef>();

        public int getTotalEstimatedWorkSize() {
            return result.size();
        }

        public Collection<NodeRef> getNextWork() {
            result.clear();
            while (result.isEmpty() && minSearchNodeId < maxNodeId) {
                nodeDAO.getNodesWithAspects(Collections.singleton(ASPECT_NON_SUBSCRIBED), minSearchNodeId, minSearchNodeId + count, new NodeRefQueryCallback() {

                    public boolean handle(Pair<Long, NodeRef> nodePair) {
                        result.add(nodePair.getSecond());
                        return true;
                    }
                });
                minSearchNodeId = minSearchNodeId + count + 1;
            }
            return result;
        }
    };
    BatchProcessor<NodeRef> batchProcessor = new BatchProcessor<NodeRef>("ImapUnsubscribedAspectPatch", transactionService.getRetryingTransactionHelper(), workProvider, batchThreads, batchSize, applicationEventPublisher, null, 1000);
    BatchProcessWorker<NodeRef> worker = new BatchProcessWorker<NodeRef>() {

        public void afterProcess() throws Throwable {
        }

        public void beforeProcess() throws Throwable {
        }

        public String getIdentifier(NodeRef entry) {
            return entry.toString();
        }

        public void process(NodeRef entry) throws Throwable {
            nodeService.removeAspect(entry, ImapModel.ASPECT_IMAP_FOLDER_NONSUBSCRIBED);
            for (ChildAssociationRef userRef : users) {
                nodeService.createAssociation(userRef.getChildRef(), entry, ImapModel.ASSOC_IMAP_UNSUBSCRIBED);
            }
        }
    };
    batchProcessor.process(worker, true);
    return I18NUtil.getMessage(MSG_NONSUBSCRIBED_ASPECT_REMOVED);
}
Also used : NodeRefQueryCallback(org.alfresco.repo.domain.node.NodeDAO.NodeRefQueryCallback) ChildAssociationRef(org.alfresco.service.cmr.repository.ChildAssociationRef) BatchProcessWorkProvider(org.alfresco.repo.batch.BatchProcessWorkProvider) BatchProcessWorker(org.alfresco.repo.batch.BatchProcessor.BatchProcessWorker) NodeRef(org.alfresco.service.cmr.repository.NodeRef) BatchProcessor(org.alfresco.repo.batch.BatchProcessor) ArrayList(java.util.ArrayList) List(java.util.List)

Example 9 with BatchProcessor

use of org.alfresco.repo.batch.BatchProcessor in project alfresco-repository by Alfresco.

the class ReEncryptor method reEncryptProperties.

protected void reEncryptProperties(final List<NodePropertyEntity> properties, final String lockToken) {
    final Iterator<NodePropertyEntity> it = properties.iterator();
    // TODO use BatchProcessWorkerAdaptor?
    BatchProcessor.BatchProcessWorker<NodePropertyEntity> worker = new BatchProcessor.BatchProcessWorker<NodePropertyEntity>() {

        public String getIdentifier(NodePropertyEntity entity) {
            return String.valueOf(entity.getNodeId());
        }

        public void beforeProcess() throws Throwable {
            refreshLock(lockToken, chunkSize * 100L);
        }

        public void afterProcess() throws Throwable {
        }

        public void process(final NodePropertyEntity entity) throws Throwable {
            NodePropertyValue nodePropValue = entity.getValue();
            // TODO check that we have the correct type i.e. can be cast to Serializable
            Serializable value = nodePropValue.getSerializableValue();
            if (value instanceof SealedObject) {
                SealedObject sealed = (SealedObject) value;
                NodePropertyKey propertyKey = entity.getKey();
                QName propertyQName = qnameDAO.getQName(propertyKey.getQnameId()).getSecond();
                // decrypt...
                Serializable decrypted = metadataEncryptor.decrypt(propertyQName, sealed);
                // ...and then re-encrypt. The new key will be used.
                Serializable resealed = metadataEncryptor.encrypt(propertyQName, decrypted);
                // TODO update resealed using batch update?
                // does the node DAO do batch updating?
                nodeDAO.setNodeProperties(entity.getNodeId(), Collections.singletonMap(propertyQName, resealed));
            } else {
                NodePropertyKey nodeKey = entity.getKey();
                QName propertyQName = qnameDAO.getQName(nodeKey.getQnameId()).getSecond();
                logger.warn("Encountered an encrypted property that is not a SealedObject, for node id " + entity.getNodeId() + ", property " + propertyQName);
            }
        }
    };
    BatchProcessWorkProvider<NodePropertyEntity> provider = new BatchProcessWorkProvider<NodePropertyEntity>() {

        @Override
        public int getTotalEstimatedWorkSize() {
            return properties.size();
        }

        @Override
        public Collection<NodePropertyEntity> getNextWork() {
            List<NodePropertyEntity> sublist = new ArrayList<NodePropertyEntity>(chunkSize);
            synchronized (it) {
                int count = 0;
                while (it.hasNext() && count < chunkSize) {
                    sublist.add(it.next());
                    count++;
                }
            }
            return sublist;
        }
    };
    new BatchProcessor<NodePropertyEntity>("Reencryptor", transactionHelper, provider, numThreads, chunkSize, applicationContext, logger, 100).process(worker, splitTxns);
}
Also used : Serializable(java.io.Serializable) QName(org.alfresco.service.namespace.QName) NodePropertyEntity(org.alfresco.repo.domain.node.NodePropertyEntity) ArrayList(java.util.ArrayList) SealedObject(javax.crypto.SealedObject) BatchProcessWorkProvider(org.alfresco.repo.batch.BatchProcessWorkProvider) BatchProcessor(org.alfresco.repo.batch.BatchProcessor) NodePropertyValue(org.alfresco.repo.domain.node.NodePropertyValue) NodePropertyKey(org.alfresco.repo.domain.node.NodePropertyKey)

Example 10 with BatchProcessor

use of org.alfresco.repo.batch.BatchProcessor in project alfresco-repository by Alfresco.

the class StripingBulkFilesystemImporter method bulkImportImpl.

/**
 * Method that does the work of importing a filesystem using the BatchProcessor.
 *
 * @param bulkImportParameters  The bulk import parameters to apply to this bulk import.
 * @param nodeImporter          The node importer implementation that will import each node.
 * @param lockToken             The lock token to use during the bulk import.
 */
@Override
protected void bulkImportImpl(final BulkImportParameters bulkImportParameters, final NodeImporter nodeImporter, final String lockToken) {
    super.bulkImportImpl(bulkImportParameters, nodeImporter, lockToken);
    final File sourceFolder = nodeImporter.getSourceFolder();
    final int batchSize = getBatchSize(bulkImportParameters);
    final int loggingInterval = getLoggingInterval(bulkImportParameters);
    final StripingFilesystemTracker tracker = new StripingFilesystemTracker(directoryAnalyser, bulkImportParameters.getTarget(), sourceFolder, batchSize);
    final BatchProcessor<ImportableItem> batchProcessor = getBatchProcessor(bulkImportParameters, tracker.getWorkProvider(), loggingInterval);
    final BatchProcessor.BatchProcessWorker<ImportableItem> worker = getWorker(bulkImportParameters, lockToken, nodeImporter, tracker);
    do {
        batchProcessor.process(worker, true);
        if (batchProcessor.getLastError() != null) {
            throw new AlfrescoRuntimeException(batchProcessor.getLastError());
        }
    } while (tracker.moreLevels());
}
Also used : ImportableItem(org.alfresco.repo.bulkimport.ImportableItem) BatchProcessor(org.alfresco.repo.batch.BatchProcessor) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) File(java.io.File)

Aggregations

BatchProcessor (org.alfresco.repo.batch.BatchProcessor)15 BatchProcessWorkProvider (org.alfresco.repo.batch.BatchProcessWorkProvider)10 ArrayList (java.util.ArrayList)7 RetryingTransactionHelper (org.alfresco.repo.transaction.RetryingTransactionHelper)7 NodeRef (org.alfresco.service.cmr.repository.NodeRef)7 List (java.util.List)4 BatchProcessWorker (org.alfresco.repo.batch.BatchProcessor.BatchProcessWorker)4 Serializable (java.io.Serializable)2 Iterator (java.util.Iterator)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 AlfrescoRuntimeException (org.alfresco.error.AlfrescoRuntimeException)2 NodePropertyEntity (org.alfresco.repo.domain.node.NodePropertyEntity)2 LockAcquisitionException (org.alfresco.repo.lock.LockAcquisitionException)2 RunAsWork (org.alfresco.repo.security.authentication.AuthenticationUtil.RunAsWork)2 ChildAssociationRef (org.alfresco.service.cmr.repository.ChildAssociationRef)2 InvalidNodeRefException (org.alfresco.service.cmr.repository.InvalidNodeRefException)2 QName (org.alfresco.service.namespace.QName)2 File (java.io.File)1 HashMap (java.util.HashMap)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1