use of io.jans.orm.couchbase.impl.CouchbaseEntryManager in project jans by JanssenProject.
the class CouchbaseConcurentSessionUpdateSample method main.
public static void main(String[] args) throws InterruptedException {
// Prepare sample connection details
CouchbaseEntryManagerSample couchbaseEntryManagerSample = new CouchbaseEntryManagerSample();
final CouchbaseEntryManager couchbaseEntryManager = couchbaseEntryManagerSample.createCouchbaseEntryManager();
try {
// Create Couchbase entry manager
String sessionId = "xyzcyzxy-a41a-45ad-8a83-61485dbad561";
final String sessionDn = "uniqueIdentifier=" + sessionId + ",ou=session,o=jans";
final String userDn = "inum=@!E8F2.853B.1E7B.ACE2!0001!39A4.C163!0000!A8F2.DE1E.D7FB,ou=people,o=jans";
final SimpleSessionState simpleSessionState = new SimpleSessionState();
simpleSessionState.setDn(sessionDn);
simpleSessionState.setId(sessionId);
simpleSessionState.setLastUsedAt(new Date());
couchbaseEntryManager.persist(simpleSessionState);
System.out.println("Persisted");
int threadCount = 500;
ExecutorService executorService = Executors.newFixedThreadPool(threadCount, daemonThreadFactory());
for (int i = 0; i < threadCount; i++) {
final int count = i;
executorService.execute(new Runnable() {
@Override
public void run() {
final SimpleSessionState simpleSessionStateFromCouchbase = couchbaseEntryManager.find(SimpleSessionState.class, sessionDn);
String beforeUserDn = simpleSessionStateFromCouchbase.getUserDn();
String randomUserDn = count % 2 == 0 ? userDn : "";
try {
simpleSessionStateFromCouchbase.setUserDn(randomUserDn);
simpleSessionStateFromCouchbase.setLastUsedAt(new Date());
couchbaseEntryManager.merge(simpleSessionStateFromCouchbase);
System.out.println("Merged thread: " + count + ", userDn: " + randomUserDn + ", before userDn: " + beforeUserDn);
} catch (Throwable e) {
System.out.println("ERROR !!!, thread: " + count + ", userDn: " + randomUserDn + ", before userDn: " + beforeUserDn + ", error:" + e.getMessage());
// e.printStackTrace();
}
}
});
}
Thread.sleep(5000L);
} finally {
couchbaseEntryManager.destroy();
}
}
use of io.jans.orm.couchbase.impl.CouchbaseEntryManager in project jans by JanssenProject.
the class CouchbaseCustomObjectAttributesSample method main.
public static void main(String[] args) {
// Prepare sample connection details
CouchbaseEntryManagerSample couchbaseEntryManagerSample = new CouchbaseEntryManagerSample();
// Create SQL entry manager
CouchbaseEntryManager sqlEntryManager = couchbaseEntryManagerSample.createCouchbaseEntryManager();
// Add dummy user
SimpleUser newUser = new SimpleUser();
newUser.setDn(String.format("inum=%s,ou=people,o=jans", System.currentTimeMillis()));
newUser.setUserId("sample_user_" + System.currentTimeMillis());
newUser.setUserPassword("test");
newUser.getCustomAttributes().add(new CustomObjectAttribute("address", Arrays.asList("London", "Texas", "Kiev")));
newUser.getCustomAttributes().add(new CustomObjectAttribute("jansGuid", "test_value"));
newUser.getCustomAttributes().add(new CustomObjectAttribute("birthdate", new Date()));
newUser.getCustomAttributes().add(new CustomObjectAttribute("jansActive", false));
// Require cusom attribute in table with age: INT type
newUser.getCustomAttributes().add(new CustomObjectAttribute("scimCustomThird", 18));
newUser.setUserRole(UserRole.ADMIN);
newUser.setMemberOf(Arrays.asList("group_1", "group_2", "group_3"));
sqlEntryManager.persist(newUser);
LOG.info("Added User '{}' with uid '{}' and key '{}'", newUser, newUser.getUserId(), newUser.getDn());
// Find added dummy user
SimpleUser foundUser = sqlEntryManager.find(SimpleUser.class, newUser.getDn());
LOG.info("Found User '{}' with uid '{}' and key '{}'", foundUser, foundUser.getUserId(), foundUser.getDn());
LOG.info("Custom attributes '{}'", foundUser.getCustomAttributes());
for (CustomObjectAttribute customAttribute : foundUser.getCustomAttributes()) {
if (customAttribute.getValue() instanceof Date) {
LOG.info("Found date custom attribute '{}' with value '{}'", customAttribute.getName(), customAttribute.getValue());
} else if (customAttribute.getValue() instanceof Integer) {
LOG.info("Found integer custom attribute '{}' with value '{}'", customAttribute.getName(), customAttribute.getValue());
} else if (customAttribute.getValue() instanceof Boolean) {
LOG.info("Found boolean custom attribute '{}' with value '{}'", customAttribute.getName(), customAttribute.getValue());
} else if (customAttribute.getValues().size() > 1) {
LOG.info("Found list custom attribute '{}' with value '{}', multiValued: {}", customAttribute.getName(), customAttribute.getValues(), customAttribute.isMultiValued());
}
}
for (Iterator<CustomObjectAttribute> it = foundUser.getCustomAttributes().iterator(); it.hasNext(); ) {
CustomObjectAttribute attr = (CustomObjectAttribute) it.next();
if (StringHelper.equalsIgnoreCase(attr.getName(), "jansGuid")) {
attr.setValue("");
break;
}
}
sqlEntryManager.merge(foundUser);
// Find updated dummy user
SimpleUser foundUser2 = sqlEntryManager.find(SimpleUser.class, newUser.getDn());
LOG.info("Found User '{}' with uid '{}' and key '{}'", foundUser2, foundUser2.getUserId(), foundUser2.getDn());
LOG.info("Custom attributes after merge '{}'", foundUser2.getCustomAttributes());
for (CustomObjectAttribute customAttribute : foundUser2.getCustomAttributes()) {
if (customAttribute.getValue() instanceof Date) {
LOG.info("Found date custom attribute '{}' with value '{}'", customAttribute.getName(), customAttribute.getValue());
} else if (customAttribute.getValue() instanceof Integer) {
LOG.info("Found integer custom attribute '{}' with value '{}'", customAttribute.getName(), customAttribute.getValue());
} else if (customAttribute.getValue() instanceof Boolean) {
LOG.info("Found boolean custom attribute '{}' with value '{}'", customAttribute.getName(), customAttribute.getValue());
} else if (customAttribute.getValues().size() > 1) {
LOG.info("Found list custom attribute '{}' with value '{}', multiValued: {}", customAttribute.getName(), customAttribute.getValues(), customAttribute.isMultiValued());
}
}
// Find added dummy user by numeric attribute
Filter filter = Filter.createGreaterOrEqualFilter("scimCustomThird", 16);
List<SimpleUser> foundUsers = sqlEntryManager.findEntries("ou=people,o=jans", SimpleUser.class, filter);
if (foundUsers.size() > 0) {
foundUser = foundUsers.get(0);
LOG.info("Found User '{}' by filter '{}' with uid '{}' and key '{}'", foundUser, filter, foundUser, foundUser);
} else {
LOG.error("Can't find User by filter '{}'", filter);
}
}
use of io.jans.orm.couchbase.impl.CouchbaseEntryManager in project jans by JanssenProject.
the class CouchbaseUpateMissingEntrySample method main.
public static void main(String[] args) {
// Prepare sample connection details
CouchbaseEntryManagerSample sqlSampleEntryManager = new CouchbaseEntryManagerSample();
// Create SQL entry manager
CouchbaseEntryManager sqlEntryManager = sqlSampleEntryManager.createCouchbaseEntryManager();
String sessionId = UUID.randomUUID().toString();
final String sessionDn = "uniqueIdentifier=" + sessionId + ",ou=session,o=jans";
final SimpleSessionState simpleSessionState = new SimpleSessionState();
simpleSessionState.setDn(sessionDn);
simpleSessionState.setId(sessionId);
simpleSessionState.setLastUsedAt(new Date());
try {
sqlEntryManager.merge(simpleSessionState);
System.out.println("Updated");
} catch (EntryPersistenceException ex) {
LOG.info("Failed to update, root case exception: {}", ex.getCause().getClass(), ex);
}
}
use of io.jans.orm.couchbase.impl.CouchbaseEntryManager in project jans by JanssenProject.
the class CouchbaseUserSearchSample method main.
public static void main(String[] args) throws InterruptedException {
// Prepare sample connection details
CouchbaseEntryManagerSample couchbaseEntryManagerSample = new CouchbaseEntryManagerSample();
final CouchbaseEntryManager couchbaseEntryManager = couchbaseEntryManagerSample.createCouchbaseEntryManager();
int countUsers = 1000000;
int threadCount = 200;
int threadIterationCount = 10;
long totalStart = System.currentTimeMillis();
try {
ExecutorService executorService = Executors.newFixedThreadPool(threadCount, daemonThreadFactory());
for (int i = 0; i < threadCount; i++) {
activeCount.incrementAndGet();
final int count = i;
executorService.execute(new Runnable() {
@Override
public void run() {
long start = System.currentTimeMillis();
for (int j = 0; j < threadIterationCount; j++) {
long userUid = Math.round(Math.random() * countUsers);
String uid = String.format("user%06d", userUid);
try {
Filter filter = Filter.createEqualityFilter(Filter.createLowercaseFilter("uid"), StringHelper.toLowerCase(uid));
// Filter filter = Filter.createEqualityFilter("uid", uid);
List<SimpleUser> foundUsers = couchbaseEntryManager.findEntries("ou=people,o=jans", SimpleUser.class, filter);
if (foundUsers.size() > 0) {
successResult.incrementAndGet();
} else {
LOG.warn("Failed to find user: " + uid);
failedResult.incrementAndGet();
}
} catch (Throwable e) {
errorResult.incrementAndGet();
System.out.println("ERROR !!!, thread: " + count + ", uid: " + uid + ", error:" + e.getMessage());
e.printStackTrace();
}
}
long end = System.currentTimeMillis();
long duration = end - start;
LOG.info("Thread " + count + " execution time: " + duration);
totalTime.addAndGet(duration);
activeCount.decrementAndGet();
}
});
}
while (activeCount.get() != 0) {
Thread.sleep(1000L);
}
} finally {
couchbaseEntryManager.destroy();
}
long totalEnd = System.currentTimeMillis();
long duration = totalEnd - totalStart;
LOG.info("Total execution time: " + duration + " after execution: " + (threadCount * threadIterationCount));
System.out.println(String.format("successResult: '%d', failedResult: '%d', errorResult: '%d'", successResult.get(), failedResult.get(), errorResult.get()));
}
use of io.jans.orm.couchbase.impl.CouchbaseEntryManager in project jans by JanssenProject.
the class ManualCouchbaseEntryManagerTest method createCouchbaseEntryManager.
public static CouchbaseEntryManager createCouchbaseEntryManager() throws IOException {
CouchbaseEntryManagerFactory couchbaseEntryManagerFactory = new CouchbaseEntryManagerFactory();
couchbaseEntryManagerFactory.create();
CouchbaseEntryManager couchbaseEntryManager = couchbaseEntryManagerFactory.createEntryManager(loadProperties());
System.out.println("Created CouchbaseEntryManager: " + couchbaseEntryManager);
return couchbaseEntryManager;
}
Aggregations