KEYCLOAK-3617 KEYCLOAK-4117 KEYCLOAK-4118
This commit is contained in:
parent
f9eeecf836
commit
d075172fd2
8 changed files with 164 additions and 19 deletions
|
@ -14,24 +14,25 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.keycloak.transaction;
|
||||
package org.keycloak.connections.jpa;
|
||||
|
||||
import org.keycloak.models.KeycloakSession;
|
||||
import org.keycloak.provider.ExceptionConverter;
|
||||
|
||||
import javax.transaction.TransactionManager;
|
||||
import javax.persistence.PersistenceException;
|
||||
|
||||
/**
|
||||
* @author <a href="mailto:bill@burkecentral.com">Bill Burke</a>
|
||||
* @version $Revision: 1 $
|
||||
*/
|
||||
public class JtaRegistration {
|
||||
public class JpaExceptionConverter implements ExceptionConverter {
|
||||
@Override
|
||||
public Throwable convert(Throwable e) {
|
||||
if (!(e instanceof PersistenceException)) return null;
|
||||
return PersistenceExceptionConverter.convert(e.getCause() != null ? e.getCause() : e);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void begin(KeycloakSession session) {
|
||||
TransactionManager tm = session.getProvider(JtaTransactionManagerLookup.class).getTransactionManager();
|
||||
if (tm == null) return;
|
||||
|
||||
session.getTransactionManager().enlist(new JtaTransactionWrapper(tm));
|
||||
@Override
|
||||
public String getId() {
|
||||
return "jpa";
|
||||
}
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
org.keycloak.connections.jpa.JpaExceptionConverter
|
|
@ -0,0 +1,61 @@
|
|||
/*
|
||||
* Copyright 2016 Red Hat, Inc. and/or its affiliates
|
||||
* and other contributors as indicated by the @author tags.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.keycloak.provider;
|
||||
|
||||
import org.keycloak.Config;
|
||||
import org.keycloak.models.KeycloakSession;
|
||||
import org.keycloak.models.KeycloakSessionFactory;
|
||||
|
||||
/**
|
||||
* Use to unwrap exceptions specifically if there is an exception at JTA commit
|
||||
*
|
||||
* @author <a href="mailto:bill@burkecentral.com">Bill Burke</a>
|
||||
* @version $Revision: 1 $
|
||||
*/
|
||||
public interface ExceptionConverter extends Provider, ProviderFactory<ExceptionConverter> {
|
||||
|
||||
/**
|
||||
* Return null if the provider doesn't handle this type
|
||||
*
|
||||
* @param t
|
||||
* @return
|
||||
*/
|
||||
Throwable convert(Throwable t);
|
||||
|
||||
@Override
|
||||
default ExceptionConverter create(KeycloakSession session) {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
default void init(Config.Scope config) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
default void postInit(KeycloakSessionFactory factory) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
default void close() {
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,46 @@
|
|||
/*
|
||||
* Copyright 2016 Red Hat, Inc. and/or its affiliates
|
||||
* and other contributors as indicated by the @author tags.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.keycloak.provider;
|
||||
|
||||
|
||||
/**
|
||||
* @author <a href="mailto:bburke@redhat.com">Bill Burke</a>
|
||||
*/
|
||||
public class ExceptionConverterSpi implements Spi {
|
||||
|
||||
@Override
|
||||
public boolean isInternal() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "exception-converter";
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<? extends Provider> getProviderClass() {
|
||||
return ExceptionConverter.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<? extends ProviderFactory> getProviderFactoryClass() {
|
||||
return ExceptionConverter.class;
|
||||
}
|
||||
|
||||
}
|
|
@ -15,6 +15,7 @@
|
|||
# limitations under the License.
|
||||
#
|
||||
|
||||
org.keycloak.provider.ExceptionConverterSpi
|
||||
org.keycloak.storage.UserStorageProviderSpi
|
||||
org.keycloak.storage.federated.UserFederatedStorageProviderSpi
|
||||
org.keycloak.models.RealmSpi
|
||||
|
|
|
@ -20,6 +20,7 @@ package org.keycloak.partialimport;
|
|||
import org.keycloak.events.admin.OperationType;
|
||||
import org.keycloak.models.KeycloakSession;
|
||||
import org.keycloak.models.ModelDuplicateException;
|
||||
import org.keycloak.models.ModelException;
|
||||
import org.keycloak.models.RealmModel;
|
||||
import org.keycloak.representations.idm.PartialImportRepresentation;
|
||||
import org.keycloak.services.ErrorResponse;
|
||||
|
@ -90,7 +91,7 @@ public class PartialImportManager {
|
|||
if (session.getTransactionManager().isActive()) {
|
||||
try {
|
||||
session.getTransactionManager().commit();
|
||||
} catch (ModelDuplicateException e) {
|
||||
} catch (ModelException e) {
|
||||
return ErrorResponse.exists(e.getLocalizedMessage());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -95,7 +95,7 @@ public class DefaultKeycloakTransactionManager implements KeycloakTransactionMan
|
|||
if (jtaLookup != null) {
|
||||
TransactionManager tm = jtaLookup.getTransactionManager();
|
||||
if (tm != null) {
|
||||
enlist(new JtaTransactionWrapper(tm));
|
||||
enlist(new JtaTransactionWrapper(session.getKeycloakSessionFactory(), tm));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -17,8 +17,12 @@
|
|||
package org.keycloak.transaction;
|
||||
|
||||
import org.jboss.logging.Logger;
|
||||
import org.keycloak.models.KeycloakSessionFactory;
|
||||
import org.keycloak.models.KeycloakTransaction;
|
||||
import org.keycloak.provider.ExceptionConverter;
|
||||
import org.keycloak.provider.ProviderFactory;
|
||||
|
||||
import javax.transaction.RollbackException;
|
||||
import javax.transaction.Status;
|
||||
import javax.transaction.Transaction;
|
||||
import javax.transaction.TransactionManager;
|
||||
|
@ -33,9 +37,11 @@ public class JtaTransactionWrapper implements KeycloakTransaction {
|
|||
protected Transaction ut;
|
||||
protected Transaction suspended;
|
||||
protected Exception ended;
|
||||
protected KeycloakSessionFactory factory;
|
||||
|
||||
public JtaTransactionWrapper(TransactionManager tm) {
|
||||
public JtaTransactionWrapper(KeycloakSessionFactory factory, TransactionManager tm) {
|
||||
this.tm = tm;
|
||||
this.factory = factory;
|
||||
try {
|
||||
|
||||
suspended = tm.suspend();
|
||||
|
@ -49,6 +55,32 @@ public class JtaTransactionWrapper implements KeycloakTransaction {
|
|||
}
|
||||
}
|
||||
|
||||
public void handleException(Throwable e) {
|
||||
if (e instanceof RollbackException) {
|
||||
e = e.getCause() != null ? e.getCause() : e;
|
||||
}
|
||||
|
||||
for (ProviderFactory factory : this.factory.getProviderFactories(ExceptionConverter.class)) {
|
||||
ExceptionConverter converter = (ExceptionConverter)factory;
|
||||
Throwable throwable = converter.convert(e);
|
||||
if (throwable == null) continue;
|
||||
if (throwable instanceof RuntimeException) {
|
||||
throw (RuntimeException)throwable;
|
||||
} else {
|
||||
throw new RuntimeException(throwable);
|
||||
}
|
||||
}
|
||||
|
||||
if (e instanceof RuntimeException) {
|
||||
throw (RuntimeException)e;
|
||||
} else {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void begin() {
|
||||
}
|
||||
|
@ -59,7 +91,7 @@ public class JtaTransactionWrapper implements KeycloakTransaction {
|
|||
logger.debug("JtaTransactionWrapper commit");
|
||||
tm.commit();
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
handleException(e);
|
||||
} finally {
|
||||
end();
|
||||
}
|
||||
|
@ -71,7 +103,7 @@ public class JtaTransactionWrapper implements KeycloakTransaction {
|
|||
logger.debug("JtaTransactionWrapper rollback");
|
||||
tm.rollback();
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
handleException(e);
|
||||
} finally {
|
||||
end();
|
||||
}
|
||||
|
@ -83,7 +115,7 @@ public class JtaTransactionWrapper implements KeycloakTransaction {
|
|||
try {
|
||||
tm.setRollbackOnly();
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
handleException(e);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -92,8 +124,9 @@ public class JtaTransactionWrapper implements KeycloakTransaction {
|
|||
try {
|
||||
return tm.getStatus() == Status.STATUS_MARKED_ROLLBACK;
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
handleException(e);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -101,8 +134,9 @@ public class JtaTransactionWrapper implements KeycloakTransaction {
|
|||
try {
|
||||
return tm.getStatus() == Status.STATUS_ACTIVE;
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
handleException(e);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
/*
|
||||
|
||||
|
|
Loading…
Reference in a new issue