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