Skip to content

Commit

Permalink
Improve to use cross component transaction.
Browse files Browse the repository at this point in the history
  • Loading branch information
Thisara-Welmilla committed Dec 11, 2024
1 parent 4a0a0b0 commit 980a36b
Show file tree
Hide file tree
Showing 9 changed files with 185 additions and 166 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.identity.application.common.dao.impl.AuthenticatorManagementDAOImpl;
import org.wso2.carbon.identity.application.common.dao.AuthenticatorManagementDAO;
import org.wso2.carbon.identity.application.common.dao.impl.AuthenticatorManagementDAOImpl;
import org.wso2.carbon.identity.application.common.dao.impl.CacheBackedAuthenticatorMgtDAO;
import org.wso2.carbon.identity.application.common.exception.AuthenticatorMgtException;
import org.wso2.carbon.identity.application.common.model.FederatedAuthenticatorConfig;
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,18 @@

package org.wso2.carbon.identity.application.common.dao.impl;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.database.utils.jdbc.NamedJdbcTemplate;
import org.wso2.carbon.database.utils.jdbc.exceptions.TransactionException;
import org.wso2.carbon.identity.application.common.dao.AuthenticatorManagementDAO;
import org.wso2.carbon.identity.application.common.exception.AuthenticatorMgtClientException;
import org.wso2.carbon.identity.application.common.exception.AuthenticatorMgtException;
import org.wso2.carbon.identity.application.common.exception.AuthenticatorMgtServerException;
import org.wso2.carbon.identity.application.common.model.UserDefinedLocalAuthenticatorConfig;
import org.wso2.carbon.identity.application.common.util.UserDefinedAuthenticatorEndpointConfigManager;
import org.wso2.carbon.identity.core.util.IdentityDatabaseUtil;
import org.wso2.carbon.identity.core.util.IdentityTenantUtil;

import java.util.List;

Expand All @@ -31,6 +39,8 @@
*/
public class AuthenticatorManagementFacade implements AuthenticatorManagementDAO {

private static final Log LOG = LogFactory.getLog(AuthenticatorManagementFacade.class);

private final AuthenticatorManagementDAO dao;
private UserDefinedAuthenticatorEndpointConfigManager endpointConfigManager =
new UserDefinedAuthenticatorEndpointConfigManager();
Expand All @@ -44,13 +54,20 @@ public AuthenticatorManagementFacade(AuthenticatorManagementDAO dao) {
public UserDefinedLocalAuthenticatorConfig addUserDefinedLocalAuthenticator(
UserDefinedLocalAuthenticatorConfig authenticatorConfig, int tenantId) throws AuthenticatorMgtException {

endpointConfigManager.addEndpointConfigurations(authenticatorConfig, tenantId);

NamedJdbcTemplate jdbcTemplate = new NamedJdbcTemplate(IdentityDatabaseUtil.getDataSource());
try {
return endpointConfigManager.resolveEndpointConfigurations(
dao.addUserDefinedLocalAuthenticator(authenticatorConfig, tenantId), tenantId);
} catch (AuthenticatorMgtException e) {
endpointConfigManager.deleteEndpointConfigurations(authenticatorConfig, tenantId);
throw e;
return jdbcTemplate.withTransaction(template -> {
endpointConfigManager.addEndpointConfigurations(authenticatorConfig, tenantId);
return endpointConfigManager.resolveEndpointConfigurations(
dao.addUserDefinedLocalAuthenticator(authenticatorConfig, tenantId), tenantId);
});
} catch (TransactionException e) {
// Since exceptions thrown are wrapped with TransactionException, extracting the actual cause.
LOG.debug("Error while creating the user defined local authenticator: " + authenticatorConfig.getName() +
" in Tenant Domain: " + IdentityTenantUtil.getTenantDomain(tenantId) +
". Rolling back created authenticator information, and associated action.");
throw handleAuthenticatorMgtException(e);
}
}

Expand All @@ -59,16 +76,22 @@ public UserDefinedLocalAuthenticatorConfig updateUserDefinedLocalAuthenticator(U
existingAuthenticatorConfig, UserDefinedLocalAuthenticatorConfig newAuthenticatorConfig,
int tenantId) throws AuthenticatorMgtException {

endpointConfigManager.updateEndpointConfigurations(newAuthenticatorConfig, existingAuthenticatorConfig,
tenantId);
NamedJdbcTemplate jdbcTemplate = new NamedJdbcTemplate(IdentityDatabaseUtil.getDataSource());
try {
return endpointConfigManager.resolveEndpointConfigurations(
dao.updateUserDefinedLocalAuthenticator(existingAuthenticatorConfig, newAuthenticatorConfig,
tenantId), tenantId);
} catch (AuthenticatorMgtException e) {
endpointConfigManager.updateEndpointConfigurations(existingAuthenticatorConfig, newAuthenticatorConfig,
tenantId);
throw e;
return jdbcTemplate.withTransaction(template -> {
endpointConfigManager.updateEndpointConfigurations(newAuthenticatorConfig, existingAuthenticatorConfig,
tenantId);
return endpointConfigManager.resolveEndpointConfigurations(
dao.updateUserDefinedLocalAuthenticator(existingAuthenticatorConfig, newAuthenticatorConfig,
tenantId), tenantId);
});
} catch (TransactionException e) {
// Since exceptions thrown are wrapped with TransactionException, extracting the actual cause.
LOG.debug("Error while updating the user defined local authenticator: " +
newAuthenticatorConfig.getName() + " in Tenant Domain: " +
IdentityTenantUtil.getTenantDomain(tenantId) +
". Rolling back updated authenticator information, and associated action.");
throw handleAuthenticatorMgtException(e);
}
}

Expand Down Expand Up @@ -96,12 +119,37 @@ public List<UserDefinedLocalAuthenticatorConfig> getAllUserDefinedLocalAuthentic
public void deleteUserDefinedLocalAuthenticator(String authenticatorConfigName, UserDefinedLocalAuthenticatorConfig
authenticatorConfig, int tenantId) throws AuthenticatorMgtException {

endpointConfigManager.deleteEndpointConfigurations(authenticatorConfig, tenantId);
NamedJdbcTemplate jdbcTemplate = new NamedJdbcTemplate(IdentityDatabaseUtil.getDataSource());
try {
dao.deleteUserDefinedLocalAuthenticator(authenticatorConfigName, authenticatorConfig, tenantId);
} catch (AuthenticatorMgtException e) {
endpointConfigManager.addEndpointConfigurations(authenticatorConfig, tenantId);
throw e;
jdbcTemplate.withTransaction(template -> {
endpointConfigManager.deleteEndpointConfigurations(authenticatorConfig, tenantId);
dao.deleteUserDefinedLocalAuthenticator(authenticatorConfigName, authenticatorConfig, tenantId);
return null;
});
} catch (TransactionException e) {
// Since exceptions thrown are wrapped with TransactionException, extracting the actual cause.
LOG.debug("Error while deleting the user defined local authenticator: " + authenticatorConfigName +
" in Tenant Domain: " + IdentityTenantUtil.getTenantDomain(tenantId) +
". Rolling back deleted authenticator information, and associated action.");
throw handleAuthenticatorMgtException(e);
}
}

/**
* Handle the authenticator management client exception.
*
* @param throwable Throwable object.
* @throws AuthenticatorMgtClientException If an authenticator management client exception.
*/
private static AuthenticatorMgtException handleAuthenticatorMgtException(Throwable throwable)
throws AuthenticatorMgtException {

if (throwable instanceof AuthenticatorMgtClientException) {
AuthenticatorMgtClientException error = (AuthenticatorMgtClientException) throwable;
throw new AuthenticatorMgtClientException(error.getErrorCode(), error.getMessage(), error.getDescription());
}

AuthenticatorMgtServerException error = (AuthenticatorMgtServerException) throwable;
throw new AuthenticatorMgtServerException(error.getErrorCode(), error.getMessage(), error.getDescription());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import org.osgi.service.component.annotations.Reference;
import org.osgi.service.component.annotations.ReferenceCardinality;
import org.osgi.service.component.annotations.ReferencePolicy;
import org.wso2.carbon.identity.action.management.ActionManagementService;
import org.wso2.carbon.identity.action.management.service.ActionManagementService;
import org.wso2.carbon.identity.application.common.ApplicationAuthenticatorService;

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.wso2.carbon.identity.application.common.internal;

import org.wso2.carbon.identity.action.management.ActionManagementService;
import org.wso2.carbon.identity.action.management.service.ActionManagementService;
import org.wso2.carbon.identity.application.common.ApplicationAuthenticatorService;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,9 @@ public enum AuthenticatorMgtError {
ERROR_CODE_RETRIEVING_ENDPOINT_CONFIG("65010", "Error while resolving endpoint configurations.",
"Error while retrieving endpoint configurations for the user defined local authenticator %s."),
ERROR_CODE_DELETING_ENDPOINT_CONFIG("65011", "Error while managing endpoint configurations.",
"Error while managing endpoint configurations for the user defined local authenticator %s.");
"Error while managing endpoint configurations for the user defined local authenticator %s."),
ERROR_CODE_HAVING_MULTIPLE_PROP("65012", "Multiple properties found", "Only actionId " +
"property is allowed for authenticator: %s.");

private final String code;
private final String message;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@ private ConfigElements() {
*/
public static class Authenticator {

public static final String ACTION_ID_PROPERTY = "actionId";

/**
* OpenId authenticator constants.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,14 @@
import java.util.Map;

import static org.wso2.carbon.identity.application.common.util.AuthenticatorMgtExceptionBuilder.buildServerException;
import static org.wso2.carbon.identity.application.common.util.IdentityApplicationConstants.Authenticator.ACTION_ID_PROPERTY;

/**
* This class responsible for managing authenticator endpoint configurations for the user defined Local
* authenticators.
*/
public class UserDefinedAuthenticatorEndpointConfigManager {

private static final String ACTION_ID_PROPERTY = "actionId";

/**
* Create a new action for given endpoint configurations of the user defined authenticator.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import org.wso2.carbon.identity.action.management.ActionManagementService;
import org.wso2.carbon.identity.action.management.exception.ActionMgtException;
import org.wso2.carbon.identity.action.management.model.Action;
import org.wso2.carbon.identity.action.management.model.Authentication;
import org.wso2.carbon.identity.action.management.model.EndpointConfig;
import org.wso2.carbon.identity.action.management.service.ActionManagementService;
import org.wso2.carbon.identity.application.common.ApplicationAuthenticatorService;
import org.wso2.carbon.identity.application.common.exception.AuthenticatorMgtException;
import org.wso2.carbon.identity.application.common.exception.AuthenticatorMgtServerRuntimeException;
Expand Down

0 comments on commit 980a36b

Please sign in to comment.