From 71b1e7577a399ddb537384ff655b7fd64b7338ec Mon Sep 17 00:00:00 2001 From: Sajith Ekanayaka Date: Sat, 25 Sep 2021 19:15:08 +0530 Subject: [PATCH] Improve debug logs and usage of class variables --- .../user/core/jdbc/JDBCUserStoreManager.java | 119 ++++++------------ 1 file changed, 38 insertions(+), 81 deletions(-) diff --git a/core/org.wso2.carbon.user.core/src/main/java/org/wso2/carbon/user/core/jdbc/JDBCUserStoreManager.java b/core/org.wso2.carbon.user.core/src/main/java/org/wso2/carbon/user/core/jdbc/JDBCUserStoreManager.java index 860375c0b89..c13818a34ee 100644 --- a/core/org.wso2.carbon.user.core/src/main/java/org/wso2/carbon/user/core/jdbc/JDBCUserStoreManager.java +++ b/core/org.wso2.carbon.user.core/src/main/java/org/wso2/carbon/user/core/jdbc/JDBCUserStoreManager.java @@ -426,26 +426,8 @@ public String[] doListUsers(String filter, int maxItemLimit) throws UserStoreExc return new String[0]; } - int givenMax = UserCoreConstants.MAX_USER_ROLE_LIST; - - int searchTime = UserCoreConstants.MAX_SEARCH_TIME; - - try { - givenMax = Integer.parseInt(realmConfig - .getUserStoreProperty(UserCoreConstants.RealmConfig.PROPERTY_MAX_USER_LIST)); - } catch (Exception e) { - givenMax = UserCoreConstants.MAX_USER_ROLE_LIST; - } - - try { - searchTime = Integer.parseInt(realmConfig - .getUserStoreProperty(UserCoreConstants.RealmConfig.PROPERTY_MAX_SEARCH_TIME)); - } catch (Exception e) { - searchTime = UserCoreConstants.MAX_SEARCH_TIME; - } - - if (maxItemLimit < 0 || maxItemLimit > givenMax) { - maxItemLimit = givenMax; + if (maxItemLimit < 0 || maxItemLimit > maximumUserNameListLength) { + maxItemLimit = maximumUserNameListLength; } String displayNameAttribute = realmConfig.getUserStoreProperty(JDBCUserStoreConstants.DISPLAY_NAME_ATTRIBUTE); @@ -491,7 +473,7 @@ public String[] doListUsers(String filter, int maxItemLimit) throws UserStoreExc } prepStmt.setMaxRows(maxItemLimit); try { - prepStmt.setQueryTimeout(searchTime); + prepStmt.setQueryTimeout(queryTimeout); } catch (Exception e) { // this can be ignored since timeout method is not implemented log.debug(e); @@ -694,8 +676,6 @@ private void setPSRestrictions(PreparedStatement ps, int maxItemLimit) throws SQ int givenMax = UserCoreConstants.MAX_USER_ROLE_LIST; - int searchTime = UserCoreConstants.MAX_SEARCH_TIME; - try { givenMax = Integer.parseInt(realmConfig.getUserStoreProperty(UserCoreConstants.RealmConfig.PROPERTY_MAX_ROLE_LIST)); @@ -703,20 +683,13 @@ private void setPSRestrictions(PreparedStatement ps, int maxItemLimit) throws SQ givenMax = UserCoreConstants.MAX_USER_ROLE_LIST; } - try { - searchTime = - Integer.parseInt(realmConfig.getUserStoreProperty(UserCoreConstants.RealmConfig.PROPERTY_MAX_SEARCH_TIME)); - } catch (Exception e) { - searchTime = UserCoreConstants.MAX_SEARCH_TIME; - } - if (maxItemLimit < 0 || maxItemLimit > givenMax) { maxItemLimit = givenMax; } ps.setMaxRows(maxItemLimit); try { - ps.setQueryTimeout(searchTime); + ps.setQueryTimeout(queryTimeout); } catch (Exception e) { // this can be ignored since timeout method is not implemented log.debug(e); @@ -3107,27 +3080,13 @@ public String[] getUserListFromProperties(String property, String value, String prepStmt.setInt(5, tenantId); } - int givenMax; - int searchTime; int maxItemLimit = MAX_ITEM_LIMIT_UNLIMITED; - try { - givenMax = Integer.parseInt(realmConfig - .getUserStoreProperty(UserCoreConstants.RealmConfig.PROPERTY_MAX_USER_LIST)); - } catch (Exception e) { - givenMax = UserCoreConstants.MAX_USER_ROLE_LIST; - } - try { - searchTime = Integer.parseInt(realmConfig - .getUserStoreProperty(UserCoreConstants.RealmConfig.PROPERTY_MAX_SEARCH_TIME)); - } catch (Exception e) { - searchTime = UserCoreConstants.MAX_SEARCH_TIME; - } - if (maxItemLimit < 0 || maxItemLimit > givenMax) { - maxItemLimit = givenMax; + if (maxItemLimit < 0 || maxItemLimit > maximumUserNameListLength) { + maxItemLimit = maximumUserNameListLength; } prepStmt.setMaxRows(maxItemLimit); try { - prepStmt.setQueryTimeout(searchTime); + prepStmt.setQueryTimeout(queryTimeout); } catch (Exception e) { // this can be ignored since timeout method is not implemented log.debug(e); @@ -3714,8 +3673,6 @@ protected PaginatedSearchResult doListUsers(String filter, int limit, int offset String sqlStmt; PreparedStatement prepStmt = null; ResultSet rs = null; - int givenMax; - int searchTime; PaginatedSearchResult result = new PaginatedSearchResult(); @@ -3723,22 +3680,8 @@ protected PaginatedSearchResult doListUsers(String filter, int limit, int offset return result; } - try { - givenMax = Integer.parseInt(realmConfig.getUserStoreProperty(UserCoreConstants.RealmConfig - .PROPERTY_MAX_USER_LIST)); - } catch (Exception e) { - givenMax = UserCoreConstants.MAX_USER_ROLE_LIST; - } - - try { - searchTime = Integer.parseInt(realmConfig - .getUserStoreProperty(UserCoreConstants.RealmConfig.PROPERTY_MAX_SEARCH_TIME)); - } catch (Exception e) { - searchTime = UserCoreConstants.MAX_SEARCH_TIME; - } - - if (limit < 0 || limit > givenMax) { - limit = givenMax; + if (limit < 0 || limit > maximumUserNameListLength) { + limit = maximumUserNameListLength; } try { @@ -3804,7 +3747,7 @@ protected PaginatedSearchResult doListUsers(String filter, int limit, int offset } try { - prepStmt.setQueryTimeout(searchTime); + prepStmt.setQueryTimeout(queryTimeout); } catch (Exception e) { // this can be ignored since timeout method is not implemented log.debug(e); @@ -4720,17 +4663,24 @@ private String getExternalRoleListSqlStatement(String caseSensitiveUsernameQuery private int getMaxUserNameListLength() { int maxUserList; - try { - maxUserList = Integer.parseInt(realmConfig.getUserStoreProperty(UserCoreConstants.RealmConfig - .PROPERTY_MAX_USER_LIST)); - } catch (Exception e) { - // The user store property might not be configured. Therefore logging as debug. + if (StringUtils.isEmpty(realmConfig.getUserStoreProperty(UserCoreConstants.RealmConfig + .PROPERTY_MAX_USER_LIST))) { if (log.isDebugEnabled()) { - log.debug("Unable to get the " + UserCoreConstants.RealmConfig.PROPERTY_MAX_USER_LIST + + log.debug(UserCoreConstants.RealmConfig.PROPERTY_MAX_USER_LIST + " is not defined in the realm " + + "configuration. The default value: " + UserCoreConstants.MAX_USER_ROLE_LIST + " is used " + + "instead."); + } + maxUserList = UserCoreConstants.MAX_USER_ROLE_LIST; + } else { + try { + maxUserList = Integer.parseInt(realmConfig.getUserStoreProperty(UserCoreConstants.RealmConfig + .PROPERTY_MAX_USER_LIST)); + } catch (NumberFormatException e) { + log.warn("Unable to get the " + UserCoreConstants.RealmConfig.PROPERTY_MAX_USER_LIST + " from the realm configuration. The default value: " + UserCoreConstants.MAX_USER_ROLE_LIST + " is used instead.", e); + maxUserList = UserCoreConstants.MAX_USER_ROLE_LIST; } - maxUserList = UserCoreConstants.MAX_USER_ROLE_LIST; } return maxUserList; } @@ -4738,17 +4688,24 @@ private int getMaxUserNameListLength() { private int getSQLQueryTimeoutLimit() { int searchTime; - try { - searchTime = Integer.parseInt(realmConfig - .getUserStoreProperty(UserCoreConstants.RealmConfig.PROPERTY_MAX_SEARCH_TIME)); - } catch (Exception e) { - // The user store property might not be configured. Therefore logging as debug. + if (StringUtils.isEmpty(realmConfig.getUserStoreProperty(UserCoreConstants.RealmConfig + .PROPERTY_MAX_SEARCH_TIME))) { if (log.isDebugEnabled()) { - log.debug("Unable to get the " + UserCoreConstants.RealmConfig.PROPERTY_MAX_SEARCH_TIME + + log.debug(UserCoreConstants.RealmConfig.PROPERTY_MAX_SEARCH_TIME + " is not defined in the realm " + + "configuration. The default value: " + UserCoreConstants.MAX_SEARCH_TIME + " is used " + + "instead."); + } + searchTime = UserCoreConstants.MAX_SEARCH_TIME; + } else { + try { + searchTime = Integer.parseInt(realmConfig + .getUserStoreProperty(UserCoreConstants.RealmConfig.PROPERTY_MAX_SEARCH_TIME)); + } catch (NumberFormatException e) { + log.warn("Unable to get the " + UserCoreConstants.RealmConfig.PROPERTY_MAX_SEARCH_TIME + " from the realm configuration. The default value: " + UserCoreConstants.MAX_SEARCH_TIME + " is used instead.", e); + searchTime = UserCoreConstants.MAX_SEARCH_TIME; } - searchTime = UserCoreConstants.MAX_SEARCH_TIME; } return searchTime; }