use of io.jans.orm.exception.EntryPersistenceException in project jans by JanssenProject.
the class SpannerEntryManager method find.
@Override
protected List<AttributeData> find(String dn, String[] objectClasses, Map<String, PropertyAnnotation> propertiesAnnotationsMap, String... ldapReturnAttributes) {
try {
// Load entry
ParsedKey keyWithInum = toSQLKey(dn);
List<AttributeData> result = getOperationService().lookup(keyWithInum.getKey(), objectClasses[0], toInternalAttributes(ldapReturnAttributes));
if (result != null) {
return result;
}
} catch (Exception ex) {
throw new EntryPersistenceException(String.format("Failed to find entry: '%s'", dn), ex);
}
throw new EntryPersistenceException(String.format("Failed to find entry: '%s'", dn));
}
use of io.jans.orm.exception.EntryPersistenceException in project jans by JanssenProject.
the class ManualSpannerEntryManagerTest method testBatchJob.
@Test(dependsOnMethods = "deleteSessionId", enabled = false)
public void testBatchJob() {
String outsideSid = UUID.randomUUID().toString();
for (int i = 0; i < 200; i++) {
SessionId sessionId = buildSessionId();
sessionId.setOutsideSid(outsideSid);
Pair<Date, Integer> expirarion = expirationDate(new Date());
sessionId.setExpirationDate(expirarion.getFirst());
sessionId.setTtl(expirarion.getSecond());
manager.persist(sessionId);
}
totalProcessedCount = 0;
ProcessBatchOperation<SessionId> sessionBatchOperation = new ProcessBatchOperation<SessionId>() {
int processedCount = 0;
@Override
public void performAction(List<SessionId> objects) {
for (SessionId simpleSession : objects) {
try {
Calendar calendar = Calendar.getInstance();
Date jansLastAccessTimeDate = simpleSession.getExpirationDate();
calendar.setTime(jansLastAccessTimeDate);
calendar.add(Calendar.SECOND, -1);
simpleSession.setExpirationDate(calendar.getTime());
manager.merge(simpleSession);
processedCount++;
} catch (EntryPersistenceException ex) {
System.err.println("Failed to update entry: " + ex.getMessage());
}
}
System.out.println("Total processed: " + processedCount);
assertEquals(processedCount, 100);
totalProcessedCount += processedCount;
}
};
Filter filter1 = Filter.createANDFilter(Filter.createPresenceFilter("exp"), Filter.createEqualityFilter("sid", outsideSid));
manager.findEntries("o=jans", SessionId.class, filter1, SearchScope.SUB, new String[] { "exp" }, sessionBatchOperation, 0, 500, 100);
assertEquals(totalProcessedCount, 200);
}
use of io.jans.orm.exception.EntryPersistenceException in project jans by JanssenProject.
the class SqlBatchJobSample method main.
public static void main(String[] args) {
// Prepare sample connection details
SqlEntryManagerSample sqlEntryManagerSample = new SqlEntryManagerSample();
// Create SQL entry manager
final SqlEntryManager sqlEntryManager = sqlEntryManagerSample.createSqlEntryManager();
BatchOperation<SimpleToken> tokenSQLBatchOperation = new ProcessBatchOperation<SimpleToken>() {
private int processedCount = 0;
@Override
public void performAction(List<SimpleToken> objects) {
for (SimpleToken simpleTokenSQL : objects) {
try {
CustomAttribute customAttribute = getUpdatedAttribute(sqlEntryManager, simpleTokenSQL.getDn(), "exp", simpleTokenSQL.getAttribute("exp"));
simpleTokenSQL.setCustomAttributes(Arrays.asList(new CustomAttribute[] { customAttribute }));
sqlEntryManager.merge(simpleTokenSQL);
processedCount++;
} catch (EntryPersistenceException ex) {
LOG.error("Failed to update entry", ex);
}
}
LOG.info("Total processed tokens: " + processedCount);
}
};
final Filter filter1 = Filter.createPresenceFilter("exp");
sqlEntryManager.findEntries("o=jans", SimpleToken.class, filter1, SearchScope.SUB, new String[] { "exp" }, tokenSQLBatchOperation, 0, 0, 100);
BatchOperation<SimpleSession> sessionBatchOperation = new ProcessBatchOperation<SimpleSession>() {
private int processedCount = 0;
@Override
public void performAction(List<SimpleSession> objects) {
int currentProcessedCount = 0;
for (SimpleSession simpleSession : objects) {
try {
CustomAttribute customAttribute = getUpdatedAttribute(sqlEntryManager, simpleSession.getDn(), "jansLastAccessTime", simpleSession.getAttribute("jansLastAccessTime"));
simpleSession.setCustomAttributes(Arrays.asList(new CustomAttribute[] { customAttribute }));
sqlEntryManager.merge(simpleSession);
processedCount++;
currentProcessedCount++;
} catch (EntryPersistenceException ex) {
LOG.error("Failed to update entry", ex);
}
}
LOG.info("Currnet batch count processed sessions: " + currentProcessedCount);
LOG.info("Total processed sessions: " + processedCount);
}
};
final Filter filter2 = Filter.createPresenceFilter("jansLastAccessTime");
sqlEntryManager.findEntries("o=jans", SimpleSession.class, filter2, SearchScope.SUB, new String[] { "jansLastAccessTime" }, 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 clients: " + processedCount);
}
};
final Filter filter3 = Filter.createPresenceFilter("exp");
List<SimpleClient> result3 = sqlEntryManager.findEntries("o=jans", SimpleClient.class, filter3, SearchScope.SUB, new String[] { "exp" }, 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 clients: " + processedCount);
}
};
final Filter filter4 = Filter.createPresenceFilter("exp");
List<SimpleClient> result4 = sqlEntryManager.findEntries("o=jans", SimpleClient.class, filter4, SearchScope.SUB, new String[] { "exp" }, clientBatchOperation2, 0, 0, 1000);
LOG.info("Result count (with collecting results): " + result4.size());
}
use of io.jans.orm.exception.EntryPersistenceException in project jans by JanssenProject.
the class SqlEntryManager method countEntries.
@Override
public <T> int countEntries(String baseDN, Class<T> entryClass, Filter filter, SearchScope scope) {
if (StringHelper.isEmptyString(baseDN)) {
throw new MappingException("Base DN to find entries is null");
}
// Check entry class
checkEntryClass(entryClass, false);
String[] objectClasses = getTypeObjectClasses(entryClass);
List<PropertyAnnotation> propertiesAnnotations = getEntryPropertyAnnotations(entryClass);
// Find entries
Filter searchFilter;
if (objectClasses.length > 0) {
searchFilter = addObjectClassFilter(filter, objectClasses);
} else {
searchFilter = filter;
}
// Prepare properties types to allow build filter properly
Map<String, PropertyAnnotation> propertiesAnnotationsMap = prepareEntryPropertiesTypes(entryClass, propertiesAnnotations);
ConvertedExpression convertedExpression;
try {
convertedExpression = toSqlFilter(searchFilter, propertiesAnnotationsMap);
} catch (SearchException ex) {
throw new EntryPersistenceException(String.format("Failed to convert filter '%s' to expression", searchFilter));
}
PagedResult<EntryData> searchResult;
try {
searchResult = searchImpl(toSQLKey(baseDN).getKey(), objectClasses[0], convertedExpression, scope, null, null, null, SearchReturnDataType.COUNT, 0, 0, 0);
} catch (Exception ex) {
throw new EntryPersistenceException(String.format("Failed to calculate the number of entries with baseDN: '%s', filter: '%s'", baseDN, searchFilter), ex);
}
return searchResult.getTotalEntriesCount();
}
use of io.jans.orm.exception.EntryPersistenceException in project jans by JanssenProject.
the class SqlEntryManager method contains.
@Override
protected <T> boolean contains(String baseDN, String[] objectClasses, Class<T> entryClass, List<PropertyAnnotation> propertiesAnnotations, Filter filter, String[] ldapReturnAttributes) {
if (StringHelper.isEmptyString(baseDN)) {
throw new MappingException("Base DN to check contain entries is null");
}
// Create filter
Filter searchFilter;
if (objectClasses.length > 0) {
searchFilter = addObjectClassFilter(filter, objectClasses);
} else {
searchFilter = filter;
}
// Prepare properties types to allow build filter properly
Map<String, PropertyAnnotation> propertiesAnnotationsMap = prepareEntryPropertiesTypes(entryClass, propertiesAnnotations);
ConvertedExpression convertedExpression;
try {
convertedExpression = toSqlFilter(searchFilter, propertiesAnnotationsMap);
} catch (SearchException ex) {
throw new EntryPersistenceException(String.format("Failed to convert filter '%s' to expression", searchFilter));
}
PagedResult<EntryData> searchResult = null;
try {
ParsedKey keyWithInum = toSQLKey(baseDN);
searchResult = searchImpl(keyWithInum.getKey(), objectClasses[0], convertedExpression, SearchScope.SUB, ldapReturnAttributes, null, null, SearchReturnDataType.SEARCH, 0, 1, 0);
if (searchResult == null) {
throw new EntryPersistenceException(String.format("Failed to find entry with baseDN: '%s', filter: '%s'", baseDN, searchFilter));
}
} catch (Exception ex) {
throw new EntryPersistenceException(String.format("Failed to find entry with baseDN: '%s', filter: '%s'", baseDN, searchFilter), ex);
}
return (searchResult != null) && (searchResult.getEntriesCount() > 0);
}
Aggregations