Search in sources :

Example 6 with SqlEntryManagerSample

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

the class SqlUpateCacheEntrySample method main.

public static void main(String[] args) {
    // Prepare sample connection details
    SqlEntryManagerSample sqlEntryManagerSample = new SqlEntryManagerSample();
    // Create SQL entry manager
    SqlEntryManager sqlEntryManager = sqlEntryManagerSample.createSqlEntryManager();
    String key = UUID.randomUUID().toString();
    final String cacheDn = String.format("uuid=%s,%s", key, "ou=cache,o=gluu");
    int expirationInSeconds = 60;
    Calendar expirationDate = Calendar.getInstance();
    expirationDate.setTime(new Date());
    expirationDate.add(Calendar.SECOND, expirationInSeconds);
    SimpleCacheEntry entity = new SimpleCacheEntry();
    entity.setTtl(expirationInSeconds);
    entity.setData("sample_data");
    entity.setId(key);
    entity.setDn(cacheDn);
    entity.setCreationDate(new Date());
    entity.setExpirationDate(expirationDate.getTime());
    entity.setDeletable(true);
    sqlEntryManager.persist(entity);
    // Try to update
    sqlEntryManager.merge(entity);
    // Try to update with removed value
    entity.setData(null);
    sqlEntryManager.merge(entity);
}
Also used : SimpleCacheEntry(io.jans.orm.sql.model.SimpleCacheEntry) Calendar(java.util.Calendar) SqlEntryManagerSample(io.jans.orm.sql.persistence.SqlEntryManagerSample) SqlEntryManager(io.jans.orm.sql.impl.SqlEntryManager) Date(java.util.Date)

Example 7 with SqlEntryManagerSample

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

the class SqlConcurentSessionUpdateSample method main.

public static void main(String[] args) throws InterruptedException {
    // Prepare sample connection details
    SqlEntryManagerSample sqlEntryManagerSample = new SqlEntryManagerSample();
    // Create SQL entry manager
    final SqlEntryManager sqlEntryManager = sqlEntryManagerSample.createSqlEntryManager();
    try {
        String sessionId = UUID.randomUUID().toString();
        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());
        sqlEntryManager.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 simpleSessionStateFromSQL = sqlEntryManager.find(SimpleSessionState.class, sessionDn);
                    String beforeUserDn = simpleSessionStateFromSQL.getUserDn();
                    String randomUserDn = count % 2 == 0 ? userDn : "";
                    try {
                        simpleSessionStateFromSQL.setUserDn(randomUserDn);
                        simpleSessionStateFromSQL.setLastUsedAt(new Date());
                        sqlEntryManager.merge(simpleSessionStateFromSQL);
                        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 {
        sqlEntryManager.destroy();
    }
}
Also used : SimpleSessionState(io.jans.orm.sql.model.SimpleSessionState) ExecutorService(java.util.concurrent.ExecutorService) SqlEntryManagerSample(io.jans.orm.sql.persistence.SqlEntryManagerSample) SqlEntryManager(io.jans.orm.sql.impl.SqlEntryManager) Date(java.util.Date)

Example 8 with SqlEntryManagerSample

use of io.jans.orm.sql.persistence.SqlEntryManagerSample 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());
}
Also used : CustomAttribute(io.jans.orm.model.base.CustomAttribute) EntryPersistenceException(io.jans.orm.exception.EntryPersistenceException) DefaultBatchOperation(io.jans.orm.model.DefaultBatchOperation) SqlEntryManagerSample(io.jans.orm.sql.persistence.SqlEntryManagerSample) SqlEntryManager(io.jans.orm.sql.impl.SqlEntryManager) Filter(io.jans.orm.search.filter.Filter) ProcessBatchOperation(io.jans.orm.model.ProcessBatchOperation) List(java.util.List) SimpleClient(io.jans.orm.sql.model.SimpleClient) SimpleSession(io.jans.orm.sql.model.SimpleSession) SimpleToken(io.jans.orm.sql.model.SimpleToken)

Example 9 with SqlEntryManagerSample

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

the class SqlCustomStringAttributesSample method main.

public static void main(String[] args) {
    // Prepare sample connection details
    SqlEntryManagerSample sqlEntryManagerSample = new SqlEntryManagerSample();
    // Create SQL entry manager
    SqlEntryManager sqlEntryManager = sqlEntryManagerSample.createSqlEntryManager();
    String randomExternalUid = "" + System.currentTimeMillis();
    // String randomExternalUid = "otp:" + System.currentTimeMillis();
    // Add dummy user
    SimpleCustomStringUser newUser = new SimpleCustomStringUser();
    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 CustomAttribute("address", Arrays.asList("London", "Texas", "Kiev")));
    newUser.getCustomAttributes().add((new CustomAttribute("jansExtUid", randomExternalUid)).multiValued());
    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 but use custom class with String values
    SimpleCustomStringUser foundUser = sqlEntryManager.find(SimpleCustomStringUser.class, newUser.getDn());
    LOG.info("Found User '{}' with uid '{}' and key '{}'", foundUser, foundUser.getUserId(), foundUser.getDn());
    LOG.info("Custom attributes '{}'", foundUser.getCustomAttributes());
    for (CustomAttribute customAttribute : foundUser.getCustomAttributes()) {
        LOG.info("Found custom attribute '{}' with value '{}'", customAttribute.getName(), customAttribute.getValue());
    }
    // Find by jsExternalUid
    Filter jsExternalUidFilter = Filter.createEqualityFilter("jansExtUid", randomExternalUid).multiValued();
    List<SimpleCustomStringUser> foundUsers = sqlEntryManager.findEntries("ou=people,o=jans", SimpleCustomStringUser.class, jsExternalUidFilter);
    for (SimpleCustomStringUser foundUser2 : foundUsers) {
        LOG.info("Found User '{}' by jansExtUid with uid '{}' and key '{}'", foundUser2, foundUser2.getUserId(), foundUser2.getDn());
    }
}
Also used : Filter(io.jans.orm.search.filter.Filter) CustomAttribute(io.jans.orm.model.base.CustomAttribute) SimpleCustomStringUser(io.jans.orm.sql.model.SimpleCustomStringUser) SqlEntryManagerSample(io.jans.orm.sql.persistence.SqlEntryManagerSample) SqlEntryManager(io.jans.orm.sql.impl.SqlEntryManager)

Example 10 with SqlEntryManagerSample

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

the class SqlIdpAuthConfSample method main.

public static void main(String[] args) {
    // Prepare sample connection details
    SqlEntryManagerSample sqlEntryManagerSample = new SqlEntryManagerSample();
    // Create SQL entry manager
    SqlEntryManager sqlEntryManager = sqlEntryManagerSample.createSqlEntryManager();
    JansConfiguration jansConfiguration = sqlEntryManager.find(JansConfiguration.class, "ou=configuration,o=jans");
    LOG.info("Found jansConfiguration: " + jansConfiguration);
}
Also used : JansConfiguration(io.jans.orm.sql.model.JansConfiguration) SqlEntryManagerSample(io.jans.orm.sql.persistence.SqlEntryManagerSample) SqlEntryManager(io.jans.orm.sql.impl.SqlEntryManager)

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