Search in sources :

Example 11 with SqlEntryManagerSample

use of io.jans.orm.sql.persistence.SqlEntryManagerSample in project jans by JanssenProject.

the class SqlSimpleClientSample method main.

public static void main(String[] args) {
    // Prepare sample connection details
    SqlEntryManagerSample sqlEntryManagerSample = new SqlEntryManagerSample();
    // Create SQL entry manager
    SqlEntryManager sqlEntryManager = sqlEntryManagerSample.createSqlEntryManager();
    SimpleClient newClient = new SimpleClient();
    newClient.setDn("inum=test_clnt2,ou=client,o=gluu");
    newClient.setDefaultAcrValues(new String[] { "test_clnt2_acr" });
    newClient.setClientName("test_clnt2");
    sqlEntryManager.persist(newClient);
    Filter presenceFilter = Filter.createEqualityFilter("displayName", "test_clnt2");
    List<SimpleClient> results = sqlEntryManager.findEntries("ou=test_clnt2,o=gluu", SimpleClient.class, presenceFilter);
    for (SimpleClient client : results) {
        String[] acrs = client.getDefaultAcrValues();
        if (ArrayHelper.isNotEmpty(acrs)) {
            System.out.println(Arrays.toString(acrs));
        }
    }
    sqlEntryManager.remove(newClient.getDn(), SimpleClient.class);
}
Also used : Filter(io.jans.orm.search.filter.Filter) SimpleClient(io.jans.orm.sql.model.SimpleClient) SqlEntryManagerSample(io.jans.orm.sql.persistence.SqlEntryManagerSample) SqlEntryManager(io.jans.orm.sql.impl.SqlEntryManager)

Example 12 with SqlEntryManagerSample

use of io.jans.orm.sql.persistence.SqlEntryManagerSample in project jans by JanssenProject.

the class UmaResourceSample method main.

public static void main(String[] args) {
    // Prepare sample connection details
    SqlEntryManagerSample sqlEntryManagerSample = new SqlEntryManagerSample();
    // Create SQL entry manager
    SqlEntryManager sqlEntryManager = sqlEntryManagerSample.createSqlEntryManager();
    final Filter filter = Filter.createEqualityFilter("jansAssociatedClnt", "inum=AB77-1A2B,ou=clients,o=jans");
    List<UmaResource> umaResource = sqlEntryManager.findEntries("ou=resources,ou=uma,o=jans", UmaResource.class, filter);
    LOG.info("Found umaResources: " + umaResource);
}
Also used : Filter(io.jans.orm.search.filter.Filter) SqlEntryManagerSample(io.jans.orm.sql.persistence.SqlEntryManagerSample) SqlEntryManager(io.jans.orm.sql.impl.SqlEntryManager) UmaResource(io.jans.orm.sql.model.UmaResource)

Example 13 with SqlEntryManagerSample

use of io.jans.orm.sql.persistence.SqlEntryManagerSample in project jans by JanssenProject.

the class SqlSample method main.

public static void main(String[] args) {
    // Prepare sample connection details
    SqlEntryManagerSample sqlEntryManagerSample = new SqlEntryManagerSample();
    // Create SQL entry manager
    SqlEntryManager sqlEntryManager = sqlEntryManagerSample.createSqlEntryManager();
    SimpleUser newUser = new SimpleUser();
    newUser.setDn(String.format("inum=%s,ou=people,o=jans", System.currentTimeMillis()));
    newUser.setUserId("sample_user_" + System.currentTimeMillis());
    newUser.setUserPassword("pwd");
    newUser.getCustomAttributes().add(new CustomObjectAttribute("address", Arrays.asList("London", "Texas", "Kiev")));
    newUser.getCustomAttributes().add(new CustomObjectAttribute("transientId", "transientId"));
    sqlEntryManager.persist(newUser);
    SimpleUser dummyUser = sqlEntryManager.find(SimpleUser.class, newUser.getDn());
    LOG.info("Dummy User '{}'", dummyUser);
    // Find all users which have specified object classes defined in SimpleUser
    List<SimpleUser> users = sqlEntryManager.findEntries("ou=people,o=jans", SimpleUser.class, null);
    for (SimpleUser user : users) {
        LOG.info("User with uid: '{}' with DN: '{}'", user.getUserId(), user.getDn());
    }
    if (users.size() > 0) {
        // Add attribute "address" to first user
        SimpleUser user = users.get(0);
        LOG.info("Updating: " + user.getUserId());
        String[] values = new String[] { "Somewhere: " + System.currentTimeMillis(), "Somewhere2: " + System.currentTimeMillis() };
        user.getCustomAttributes().add(new CustomObjectAttribute("address", Arrays.asList(values)));
        user.getCustomAttributes().add(new CustomObjectAttribute("transientId", "new_transientId"));
        user.getCustomAttributes().add(new CustomObjectAttribute("jansGuid", "test_guid"));
        user.setUserId("user1");
        user.setUserPassword("test_pwd");
        sqlEntryManager.merge(user);
    }
    for (SimpleUser user : users) {
        boolean result1 = sqlEntryManager.authenticate(user.getDn(), SimpleUser.class, "test_pwd");
        boolean result2 = sqlEntryManager.authenticate("ou=people,o=jans", SimpleUser.class, user.getUserId(), "test");
        System.out.println("authetication result: " + result1 + ", " + result2);
    }
    Filter filter = Filter.createEqualityFilter("jansStatus", "active");
    List<SimpleAttribute> attributes = sqlEntryManager.findEntries("o=jans", SimpleAttribute.class, filter, SearchScope.SUB, null, null, 10, 0, 0);
    for (SimpleAttribute attribute : attributes) {
        LOG.info("Attribute with displayName: " + attribute.getCustomAttributes().get(1));
    }
    Filter filter2 = Filter.createEqualityFilter("jansState", "authenticated");
    List<SimpleSession> sessions = sqlEntryManager.findEntries("o=jans", SimpleSession.class, filter2, SearchScope.SUB, null, null, 10, 0, 0);
    LOG.info("Found sessions: " + sessions.size());
    List<SimpleGrant> grants = sqlEntryManager.findEntries("o=jans", SimpleGrant.class, null, SearchScope.SUB, new String[] { "grtId" }, null, 1, 0, 0);
    LOG.info("Found grants: " + grants.size());
    try {
        PagedResult<SimpleUser> listViewResponse = sqlEntryManager.findPagedEntries("o=jans", SimpleUser.class, null, new String[] { "uid", "displayName", "jansStatus" }, "uid", SortOrder.ASCENDING, 0, 6, 4);
        LOG.info("Found persons: " + listViewResponse.getEntriesCount() + ", total persons: " + listViewResponse.getTotalEntriesCount());
        for (SimpleUser user : listViewResponse.getEntries()) {
            System.out.println(user.getUserId());
        }
    } catch (Exception ex) {
        LOG.info("Failed to search", ex);
    }
    try {
        PagedResult<SimpleUser> listViewResponse = sqlEntryManager.findPagedEntries("o=jans", SimpleUser.class, null, new String[] { "uid", "displayName", "jansStatus" }, "uid", SortOrder.DESCENDING, 0, 6, 4);
        LOG.info("Found persons: " + listViewResponse.getEntriesCount() + ", total persons: " + listViewResponse.getTotalEntriesCount());
        for (SimpleUser user : listViewResponse.getEntries()) {
            System.out.println(user.getUserId());
        }
    } catch (Exception ex) {
        LOG.info("Failed to search", ex);
    }
}
Also used : CustomObjectAttribute(io.jans.orm.model.base.CustomObjectAttribute) SimpleUser(io.jans.orm.sql.model.SimpleUser) SimpleAttribute(io.jans.orm.sql.model.SimpleAttribute) SqlEntryManagerSample(io.jans.orm.sql.persistence.SqlEntryManagerSample) SqlEntryManager(io.jans.orm.sql.impl.SqlEntryManager) SimpleGrant(io.jans.orm.sql.model.SimpleGrant) Filter(io.jans.orm.search.filter.Filter) SimpleSession(io.jans.orm.sql.model.SimpleSession)

Example 14 with SqlEntryManagerSample

use of io.jans.orm.sql.persistence.SqlEntryManagerSample in project jans by JanssenProject.

the class SqlSimpleClientWithDeleteNotifierSample method main.

public static void main(String[] args) {
    // Prepare sample connection details
    SqlEntryManagerSample sqlEntryManagerSample = new SqlEntryManagerSample();
    // Create SQL entry manager
    SqlEntryManager sqlEntryManager = sqlEntryManagerSample.createSqlEntryManager();
    sqlEntryManager.addDeleteSubscriber(new DeleteNotifier() {

        @Override
        public void onBeforeRemove(String dn, String[] objectClasses) {
            System.out.println(Arrays.asList(objectClasses));
            System.out.println(sqlEntryManager.exportEntry(dn, objectClasses[0]));
        }

        @Override
        public void onAfterRemove(String dn, String[] objectClasses) {
            System.out.println(Arrays.asList(objectClasses));
        }
    });
    SimpleClient newClient = new SimpleClient();
    newClient.setDn("inum=test_clnt3,ou=client,o=gluu");
    newClient.setDefaultAcrValues(new String[] { "test_clnt3_acr" });
    newClient.setClientName("test_clnt3");
    sqlEntryManager.persist(newClient);
    Filter presenceFilter = Filter.createEqualityFilter("displayName", "test_clnt3");
    List<SimpleClient> results = sqlEntryManager.findEntries("ou=test_clnt3,o=gluu", SimpleClient.class, presenceFilter);
    for (SimpleClient client : results) {
        String[] acrs = client.getDefaultAcrValues();
        if (ArrayHelper.isNotEmpty(acrs)) {
            System.out.println(Arrays.toString(acrs));
        }
    }
    sqlEntryManager.remove(newClient.getDn(), SimpleClient.class);
}
Also used : Filter(io.jans.orm.search.filter.Filter) DeleteNotifier(io.jans.orm.event.DeleteNotifier) SimpleClient(io.jans.orm.sql.model.SimpleClient) SqlEntryManagerSample(io.jans.orm.sql.persistence.SqlEntryManagerSample) SqlEntryManager(io.jans.orm.sql.impl.SqlEntryManager)

Example 15 with SqlEntryManagerSample

use of io.jans.orm.sql.persistence.SqlEntryManagerSample in project jans by JanssenProject.

the class SqlUserSearchSample method main.

public static void main(String[] args) throws InterruptedException {
    // Prepare sample connection details
    final SqlEntryManagerSample sqlEntryManagerSample = new SqlEntryManagerSample();
    final SqlEntryManager sqlEntryManager = sqlEntryManagerSample.createSqlEntryManager();
    Filter filter1 = Filter.createEqualityFilter("uid", "test_user");
    List<SimpleUser> users = sqlEntryManager.findEntries("ou=people,o=jans", SimpleUser.class, filter1);
    System.out.println(users);
    int countUsers = 2000000;
    int threadCount = 200;
    int threadIterationCount = 200;
    Filter filter = Filter.createEqualityFilter(Filter.createLowercaseFilter("uid"), String.format("user%06d", countUsers));
    boolean foundUser = sqlEntryManager.contains("ou=people,o=jans", SimpleUser.class, filter);
    if (!foundUser) {
        addTestUsers(sqlEntryManager, countUsers);
    }
    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 = "user" + userUid;
                        /*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 = sqlEntryManager.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 {
        sqlEntryManager.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()));
}
Also used : SimpleUser(io.jans.orm.sql.model.SimpleUser) SqlEntryManagerSample(io.jans.orm.sql.persistence.SqlEntryManagerSample) SqlEntryManager(io.jans.orm.sql.impl.SqlEntryManager) Filter(io.jans.orm.search.filter.Filter) ExecutorService(java.util.concurrent.ExecutorService) List(java.util.List)

Aggregations

SqlEntryManager (io.jans.orm.sql.impl.SqlEntryManager)15 SqlEntryManagerSample (io.jans.orm.sql.persistence.SqlEntryManagerSample)15 Filter (io.jans.orm.search.filter.Filter)11 Date (java.util.Date)5 SimpleUser (io.jans.orm.sql.model.SimpleUser)4 CustomObjectAttribute (io.jans.orm.model.base.CustomObjectAttribute)3 SimpleClient (io.jans.orm.sql.model.SimpleClient)3 EntryPersistenceException (io.jans.orm.exception.EntryPersistenceException)2 CustomAttribute (io.jans.orm.model.base.CustomAttribute)2 SimpleSession (io.jans.orm.sql.model.SimpleSession)2 SimpleSessionState (io.jans.orm.sql.model.SimpleSessionState)2 List (java.util.List)2 ExecutorService (java.util.concurrent.ExecutorService)2 DeleteNotifier (io.jans.orm.event.DeleteNotifier)1 DefaultBatchOperation (io.jans.orm.model.DefaultBatchOperation)1 ProcessBatchOperation (io.jans.orm.model.ProcessBatchOperation)1 JansConfiguration (io.jans.orm.sql.model.JansConfiguration)1 SimpleAttribute (io.jans.orm.sql.model.SimpleAttribute)1 SimpleCacheEntry (io.jans.orm.sql.model.SimpleCacheEntry)1 SimpleCustomStringUser (io.jans.orm.sql.model.SimpleCustomStringUser)1