Search in sources :

Example 1 with DefaultBatchOperation

use of org.gluu.persist.model.DefaultBatchOperation 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 ProcessBatchOperation<SimpleTokenLdap>() {

        private int processedCount = 0;

        @Override
        public void performAction(List<SimpleTokenLdap> objects) {
            for (SimpleTokenLdap simpleTokenLdap : objects) {
                try {
                    CustomAttribute customAttribute = getUpdatedAttribute(ldapEntryManager, "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);
                }
            }
            LOG.info("Total processed: " + processedCount);
        }
    };
    final Filter filter1 = Filter.createPresenceFilter("oxAuthExpiration");
    ldapEntryManager.findEntries("o=gluu", SimpleTokenLdap.class, filter1, SearchScope.SUB, new String[] { "oxAuthExpiration" }, tokenLdapBatchOperation, 0, 0, 100);
    BatchOperation<SimpleSession> sessionBatchOperation = new ProcessBatchOperation<SimpleSession>() {

        private int processedCount = 0;

        @Override
        public void performAction(List<SimpleSession> objects) {
            for (SimpleSession simpleSession : objects) {
                try {
                    CustomAttribute customAttribute = getUpdatedAttribute(ldapEntryManager, "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);
                }
            }
            LOG.info("Total processed: " + processedCount);
        }
    };
    final Filter filter2 = Filter.createPresenceFilter("oxLastAccessTime");
    ldapEntryManager.findEntries("o=gluu", SimpleSession.class, filter2, SearchScope.SUB, new String[] { "oxLastAccessTime" }, sessionBatchOperation, 0, 0, 100);
    BatchOperation<SimpleClient> clientBatchOperation = new ProcessBatchOperation<SimpleClient>() {

        private int processedCount = 0;

        @Override
        public void performAction(List<SimpleClient> objects) {
            for (SimpleClient simpleClient : objects) {
                processedCount++;
            }
            LOG.info("Total processed: " + processedCount);
        }
    };
    final Filter filter3 = Filter.createPresenceFilter("oxAuthClientSecretExpiresAt");
    List<SimpleClient> result3 = ldapEntryManager.findEntries("o=gluu", SimpleClient.class, filter3, SearchScope.SUB, new String[] { "oxAuthClientSecretExpiresAt" }, clientBatchOperation, 0, 0, 1000);
    LOG.info("Result count (without collecting results): " + result3.size());
    BatchOperation<SimpleClient> clientBatchOperation2 = new DefaultBatchOperation<SimpleClient>() {

        private int processedCount = 0;

        @Override
        public void performAction(List<SimpleClient> objects) {
            for (SimpleClient simpleClient : objects) {
                processedCount++;
            }
            LOG.info("Total processed: " + processedCount);
        }
    };
    final Filter filter4 = Filter.createPresenceFilter("oxAuthClientSecretExpiresAt");
    List<SimpleClient> result4 = ldapEntryManager.findEntries("o=gluu", SimpleClient.class, filter4, SearchScope.SUB, new String[] { "oxAuthClientSecretExpiresAt" }, clientBatchOperation2, 0, 0, 1000);
    LOG.info("Result count (with collecting results): " + result4.size());
}
Also used : SimpleTokenLdap(org.gluu.ldap.model.SimpleTokenLdap) CustomAttribute(org.gluu.persist.model.base.CustomAttribute) EntryPersistenceException(org.gluu.persist.exception.mapping.EntryPersistenceException) DefaultBatchOperation(org.gluu.persist.model.DefaultBatchOperation) LdapEntryManager(org.gluu.persist.ldap.impl.LdapEntryManager) Filter(org.gluu.search.filter.Filter) ProcessBatchOperation(org.gluu.persist.model.ProcessBatchOperation) List(java.util.List) SimpleClient(org.gluu.ldap.model.SimpleClient) SimpleSession(org.gluu.ldap.model.SimpleSession)

Example 2 with DefaultBatchOperation

use of org.gluu.persist.model.DefaultBatchOperation in project oxCore by GluuFederation.

the class MetricService method removeExpiredMetricEntries.

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

            @Override
            public boolean collectSearchResult(int size) {
                return false;
            }

            @Override
            public void performAction(List<MetricEntry> entries) {
                for (MetricEntry metricEntry : entries) {
                    remove(metricEntry);
                }
            }
        };
        getExpiredMetricEntries(metricEntryBatchOperation, baseDnForPeriod, expirationDate, sizeLimit, chunkSize);
    }
    DefaultBatchOperation<SimpleBranch> batchOperation = new DefaultBatchOperation<SimpleBranch>() {

        @Override
        public boolean collectSearchResult(int size) {
            return false;
        }

        @Override
        public 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);
            }
        }
    };
    findAllPeriodBranches(batchOperation, applicationType, applianceInum, sizeLimit, chunkSize);
}
Also used : SimpleBranch(org.gluu.persist.model.base.SimpleBranch) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) MetricEntry(org.xdi.model.metric.ldap.MetricEntry) DefaultBatchOperation(org.gluu.persist.model.DefaultBatchOperation) Date(java.util.Date) HashSet(java.util.HashSet)

Aggregations

List (java.util.List)2 DefaultBatchOperation (org.gluu.persist.model.DefaultBatchOperation)2 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 HashSet (java.util.HashSet)1 LinkedList (java.util.LinkedList)1 SimpleClient (org.gluu.ldap.model.SimpleClient)1 SimpleSession (org.gluu.ldap.model.SimpleSession)1 SimpleTokenLdap (org.gluu.ldap.model.SimpleTokenLdap)1 EntryPersistenceException (org.gluu.persist.exception.mapping.EntryPersistenceException)1 LdapEntryManager (org.gluu.persist.ldap.impl.LdapEntryManager)1 ProcessBatchOperation (org.gluu.persist.model.ProcessBatchOperation)1 CustomAttribute (org.gluu.persist.model.base.CustomAttribute)1 SimpleBranch (org.gluu.persist.model.base.SimpleBranch)1 Filter (org.gluu.search.filter.Filter)1 MetricEntry (org.xdi.model.metric.ldap.MetricEntry)1