Search in sources :

Example 6 with BatchOperation

use of org.gluu.site.ldap.persistence.BatchOperation in project oxCore by GluuFederation.

the class LdapSampleBatchJob method main.

public static void main(String[] args) {
    // Prepare sample connection details
    LdapSampleEntryManager ldapSampleEntryManager = new LdapSampleEntryManager();
    // Create LDAP entry manager
    final LdapEntryManager ldapEntryManager = ldapSampleEntryManager.createLdapEntryManager();
    BatchOperation<SimpleTokenLdap> tokenLdapBatchOperation = new BatchOperation<SimpleTokenLdap>(ldapEntryManager) {

        private int processedCount = 0;

        @Override
        protected List<SimpleTokenLdap> getChunkOrNull(int batchSize) {
            log.info("Processed: " + processedCount);
            final Filter filter = Filter.createPresenceFilter("oxAuthExpiration");
            return ldapEntryManager.findEntries("o=gluu", SimpleTokenLdap.class, filter, SearchScope.SUB, new String[] { "oxAuthExpiration" }, this, 0, batchSize, batchSize);
        }

        @Override
        protected void performAction(List<SimpleTokenLdap> objects) {
            for (SimpleTokenLdap simpleTokenLdap : objects) {
                try {
                    CustomAttribute customAttribute = getUpdatedAttribute("oxAuthExpiration", simpleTokenLdap.getAttribute("oxAuthExpiration"));
                    simpleTokenLdap.setCustomAttributes(Arrays.asList(new CustomAttribute[] { customAttribute }));
                    ldapEntryManager.merge(simpleTokenLdap);
                    processedCount++;
                } catch (EntryPersistenceException ex) {
                    log.error("Failed to update entry", ex);
                }
            }
        }
    };
    tokenLdapBatchOperation.iterateAllByChunks(100);
    BatchOperation<SimpleSession> sessionBatchOperation = new BatchOperation<SimpleSession>(ldapEntryManager) {

        private int processedCount = 0;

        @Override
        protected List<SimpleSession> getChunkOrNull(int batchSize) {
            log.info("Processed: " + processedCount);
            final Filter filter = Filter.createPresenceFilter("oxLastAccessTime");
            return ldapEntryManager.findEntries("o=gluu", SimpleSession.class, filter, SearchScope.SUB, new String[] { "oxLastAccessTime" }, this, 0, batchSize, batchSize);
        }

        @Override
        protected void performAction(List<SimpleSession> objects) {
            for (SimpleSession simpleSession : objects) {
                try {
                    CustomAttribute customAttribute = getUpdatedAttribute("oxLastAccessTime", simpleSession.getAttribute("oxLastAccessTime"));
                    simpleSession.setCustomAttributes(Arrays.asList(new CustomAttribute[] { customAttribute }));
                    ldapEntryManager.merge(simpleSession);
                    processedCount++;
                } catch (EntryPersistenceException ex) {
                    log.error("Failed to update entry", ex);
                }
            }
        }
    };
    sessionBatchOperation.iterateAllByChunks(100);
}
Also used : LdapEntryManager(org.gluu.site.ldap.persistence.LdapEntryManager) Filter(com.unboundid.ldap.sdk.Filter) CustomAttribute(org.xdi.ldap.model.CustomAttribute) EntryPersistenceException(org.gluu.site.ldap.persistence.exception.EntryPersistenceException) List(java.util.List) BatchOperation(org.gluu.site.ldap.persistence.BatchOperation)

Example 7 with BatchOperation

use of org.gluu.site.ldap.persistence.BatchOperation in project oxCore by GluuFederation.

the class MetricService method removeExpiredMetricEntries.

public void removeExpiredMetricEntries(int batchSize, final Date expirationDate, final ApplicationType applicationType, final String applianceInum) {
    final Set<String> keepBaseDnForPeriod = getBaseDnForPeriod(applicationType, applianceInum, expirationDate, new Date());
    // Remove expired entries
    for (final String baseDnForPeriod : keepBaseDnForPeriod) {
        BatchOperation<MetricEntry> metricEntryBatchOperation = new BatchOperation<MetricEntry>(ldapEntryManager) {

            @Override
            protected List<MetricEntry> getChunkOrNull(int batchSize) {
                return getExpiredMetricEntries(this, batchSize, baseDnForPeriod, expirationDate);
            }

            @Override
            protected void performAction(List<MetricEntry> objects) {
                for (MetricEntry metricEntry : objects) {
                    remove(metricEntry);
                }
            }
        };
        metricEntryBatchOperation.iterateAllByChunks(batchSize);
    }
    BatchOperation<SimpleBranch> batchOperation = new BatchOperation<SimpleBranch>(ldapEntryManager) {

        @Override
        protected List<SimpleBranch> getChunkOrNull(int batchSize) {
            return findAllPeriodBranches(this, batchSize, applicationType, applianceInum);
        }

        @Override
        protected void performAction(List<SimpleBranch> objects) {
            String baseDn = buildDn(null, null, applicationType, applianceInum);
            Set<String> periodBranchesStrings = new HashSet<String>();
            for (SimpleBranch periodBranch : objects) {
                if (!StringHelper.equalsIgnoreCase(baseDn, periodBranch.getDn())) {
                    periodBranchesStrings.add(periodBranch.getDn());
                }
            }
            periodBranchesStrings.removeAll(keepBaseDnForPeriod);
            // Remove expired months
            for (String baseDnForPeriod : periodBranchesStrings) {
                removeBranch(baseDnForPeriod);
            }
        }
    };
    batchOperation.iterateAllByChunks(batchSize);
}
Also used : SimpleBranch(org.xdi.ldap.model.SimpleBranch) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) BatchOperation(org.gluu.site.ldap.persistence.BatchOperation) MetricEntry(org.xdi.model.metric.ldap.MetricEntry) Date(java.util.Date) HashSet(java.util.HashSet)

Example 8 with BatchOperation

use of org.gluu.site.ldap.persistence.BatchOperation in project oxAuth by GluuFederation.

the class CleanerTimer method processRegisteredClients.

private void processRegisteredClients() {
    log.debug("Start Client clean up");
    BatchOperation<Client> clientBatchService = new BatchOperation<Client>(ldapEntryManager) {

        @Override
        protected List<Client> getChunkOrNull(int chunkSize) {
            return clientService.getClientsWithExpirationDate(this, chunkSize, chunkSize);
        }

        @Override
        protected void performAction(List<Client> entries) {
            for (Client client : entries) {
                try {
                    GregorianCalendar now = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
                    GregorianCalendar expirationDate = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
                    expirationDate.setTime(client.getClientSecretExpiresAt());
                    if (expirationDate.before(now)) {
                        List<AuthorizationGrant> toRemove = authorizationGrantList.getAuthorizationGrant(client.getClientId());
                        authorizationGrantList.removeAuthorizationGrants(toRemove);
                        log.debug("Removing Client: {}, Expiration date: {}", client.getClientId(), client.getClientSecretExpiresAt());
                        clientService.remove(client);
                    }
                } catch (Exception e) {
                    log.error("Failed to remove entry", e);
                }
            }
        }
    };
    clientBatchService.iterateAllByChunks(BATCH_SIZE);
    log.debug("End Client clean up");
}
Also used : AuthorizationGrantList(org.xdi.oxauth.model.common.AuthorizationGrantList) BatchOperation(org.gluu.site.ldap.persistence.BatchOperation) Client(org.xdi.oxauth.model.registration.Client) AuthorizationGrant(org.xdi.oxauth.model.common.AuthorizationGrant)

Aggregations

BatchOperation (org.gluu.site.ldap.persistence.BatchOperation)8 List (java.util.List)5 AuthorizationGrantList (org.xdi.oxauth.model.common.AuthorizationGrantList)4 LDAPException (com.unboundid.ldap.sdk.LDAPException)3 ArrayList (java.util.ArrayList)2 Date (java.util.Date)2 AuthorizationGrant (org.xdi.oxauth.model.common.AuthorizationGrant)2 Filter (com.unboundid.ldap.sdk.Filter)1 Calendar (java.util.Calendar)1 HashSet (java.util.HashSet)1 LinkedList (java.util.LinkedList)1 LdapEntryManager (org.gluu.site.ldap.persistence.LdapEntryManager)1 EntryPersistenceException (org.gluu.site.ldap.persistence.exception.EntryPersistenceException)1 CustomAttribute (org.xdi.ldap.model.CustomAttribute)1 SimpleBranch (org.xdi.ldap.model.SimpleBranch)1 MetricEntry (org.xdi.model.metric.ldap.MetricEntry)1 MemcachedGrant (org.xdi.oxauth.model.common.MemcachedGrant)1 UmaRPT (org.xdi.oxauth.model.common.uma.UmaRPT)1 DeviceRegistration (org.xdi.oxauth.model.fido.u2f.DeviceRegistration)1 RequestMessageLdap (org.xdi.oxauth.model.fido.u2f.RequestMessageLdap)1