use of org.eclipse.persistence.exceptions.ExceptionHandler in project eclipselink by eclipse-ee4j.
the class EntityManagerSetupImpl method setExceptionHandler.
/**
* Allow customized exception handler to be added into session.
* The method needs to be called in deploy and pre-deploy stage.
*/
protected void setExceptionHandler(Map m, ClassLoader loader) {
// Set exception handler if it was specified.
String exceptionHandlerClassName = EntityManagerFactoryProvider.getConfigPropertyAsStringLogDebug(PersistenceUnitProperties.EXCEPTION_HANDLER_CLASS, m, session);
if (exceptionHandlerClassName != null) {
try {
Class<? extends ExceptionHandler> exceptionHandlerClass = findClassForProperty(exceptionHandlerClassName, PersistenceUnitProperties.EXCEPTION_HANDLER_CLASS, loader);
ExceptionHandler exceptionHandler = (ExceptionHandler) buildObjectForClass(exceptionHandlerClass, ExceptionHandler.class);
if (exceptionHandler != null) {
session.setExceptionHandler(exceptionHandler);
} else {
session.handleException(ValidationException.invalidExceptionHandlerClass(exceptionHandlerClassName));
}
} catch (IllegalAccessException e) {
session.handleException(ValidationException.cannotInstantiateExceptionHandlerClass(exceptionHandlerClassName, e));
} catch (PrivilegedActionException e) {
session.handleException(ValidationException.cannotInstantiateExceptionHandlerClass(exceptionHandlerClassName, e));
} catch (InstantiationException e) {
session.handleException(ValidationException.cannotInstantiateExceptionHandlerClass(exceptionHandlerClassName, e));
}
}
}
use of org.eclipse.persistence.exceptions.ExceptionHandler in project eclipselink by eclipse-ee4j.
the class SessionsFactory method processSessionConfig.
/**
* INTERNAL:
* Process the common elements from a SessionConfig.
*/
protected void processSessionConfig(SessionConfig sessionConfig, AbstractSession session) {
// Name
session.setName(sessionConfig.getName().trim());
// Session Event Manager
processSessionEventManagerConfig(sessionConfig.getSessionEventManagerConfig(), session);
// server platform
((DatabaseSessionImpl) session).setServerPlatform(buildServerPlatformConfig(sessionConfig.getServerPlatformConfig(), (DatabaseSessionImpl) session));
// Session Log - BUG# 3442865, don't set the log if it is null
SessionLog log = buildSessionLog(sessionConfig.getLogConfig(), session);
if (log != null) {
session.setSessionLog(log);
}
// Remote command manager
buildRemoteCommandManagerConfig(sessionConfig.getRemoteCommandManagerConfig(), session);
// Profiler - XML Schema default is null
if (sessionConfig.getProfiler() != null) {
if (sessionConfig.getProfiler().equals("eclipselink")) {
session.setProfiler(new PerformanceProfiler());
}
}
// Exception handler
String exceptionHandlerClassName = sessionConfig.getExceptionHandlerClass();
if (exceptionHandlerClassName != null) {
try {
@SuppressWarnings({ "unchecked" }) Class<ExceptionHandler> exceptionHandlerClass = (Class<ExceptionHandler>) m_classLoader.loadClass(exceptionHandlerClassName);
if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()) {
session.setExceptionHandler(AccessController.doPrivileged(new PrivilegedNewInstanceFromClass<>(exceptionHandlerClass)));
} else {
session.setExceptionHandler(PrivilegedAccessHelper.newInstanceFromClass(exceptionHandlerClass));
}
} catch (Exception e) {
throw SessionLoaderException.failedToLoadTag("exception-handler-class", exceptionHandlerClassName, e);
}
}
// Session customizer will be processed in the buildSessions method.
// Ensures it is run last.
}
use of org.eclipse.persistence.exceptions.ExceptionHandler in project eclipselink by eclipse-ee4j.
the class EntityManagerSetupImpl method deploy.
/**
* Deploy a persistence session and return an EntityManagerFactory.
*
* Deployment takes a session that was partially created in the predeploy call and makes it whole.
*
* This means doing any configuration that requires the real class definitions for the entities. In
* the predeploy phase we were in a stage where we were not let allowed to load the real classes.
*
* Deploy could be called several times - but only the first call does the actual deploying -
* additional calls allow to update session properties (in case the session is not connected).
*
* Note that there is no need to synchronize deploy method - it doesn't alter factoryCount
* and while deploy is executed no other method can alter the current state
* (predeploy call would just increment factoryCount; undeploy call would not drop factoryCount to 0).
* However precautions should be taken to handle concurrent calls to deploy, because those may
* alter the current state or connect the session.
*
* @param realClassLoader The class loader that was used to load the entity classes. This loader
* will be maintained for the lifespan of the loaded classes.
* @param additionalProperties added to persistence unit properties for updateServerSession overriding existing properties.
* In JSE case it allows to alter properties in main (as opposed to preMain where preDeploy is called).
* @return An EntityManagerFactory to be used by the Container to obtain EntityManagers
*/
public AbstractSession deploy(ClassLoader realClassLoader, Map additionalProperties) {
if (this.state != STATE_PREDEPLOYED && this.state != STATE_DEPLOYED && this.state != STATE_HALF_DEPLOYED) {
if (mustBeCompositeMember()) {
throw new PersistenceException(EntityManagerSetupException.compositeMemberCannotBeUsedStandalone(this.persistenceUnitInfo.getPersistenceUnitName()));
}
throw new PersistenceException(EntityManagerSetupException.cannotDeployWithoutPredeploy(this.persistenceUnitInfo.getPersistenceUnitName(), this.state, this.persistenceException));
}
// state is PREDEPLOYED or DEPLOYED
this.session.log(SessionLog.FINEST, SessionLog.JPA, "deploy_begin", new Object[] { getPersistenceUnitInfo().getPersistenceUnitName(), this.session.getName(), this.state, this.factoryCount });
ClassLoader classLoaderToUse = realClassLoader;
if (additionalProperties.containsKey(PersistenceUnitProperties.CLASSLOADER)) {
classLoaderToUse = (ClassLoader) additionalProperties.get(PersistenceUnitProperties.CLASSLOADER);
} else if ((this.processor != null) && (this.processor.getProject() != null) && (this.processor.getProject().hasVirtualClasses()) && (this.state == STATE_PREDEPLOYED) && (!(classLoaderToUse instanceof DynamicClassLoader))) {
classLoaderToUse = new DynamicClassLoader(classLoaderToUse);
}
// indicates whether session has failed to connect, determines whether HALF_DEPLOYED state should be kept in case of exception.
boolean isLockAcquired = false;
try {
Map deployProperties = mergeMaps(additionalProperties, this.persistenceUnitInfo.getProperties());
updateTunerPreDeploy(deployProperties, classLoaderToUse);
translateOldProperties(deployProperties, this.session);
if (isComposite()) {
updateCompositeMembersProperties(deployProperties);
}
if (this.state == STATE_PREDEPLOYED) {
this.deployLock.acquire();
isLockAcquired = true;
if (this.state == STATE_PREDEPLOYED) {
if (this.shouldBuildProject && !this.isSessionLoadedFromSessionsXML) {
if (isComposite()) {
deployCompositeMembers(deployProperties, classLoaderToUse);
} else {
if (this.processor.getMetadataSource() != null) {
Map metadataProperties = this.processor.getMetadataSource().getPropertyOverrides(deployProperties, classLoaderToUse, this.session.getSessionLog());
if (metadataProperties != null && !metadataProperties.isEmpty()) {
translateOldProperties(metadataProperties, this.session);
deployProperties = mergeMaps(metadataProperties, deployProperties);
}
}
// listeners and queries require the real classes and are therefore built during deploy using the realClassLoader
this.processor.setClassLoader(classLoaderToUse);
this.processor.createDynamicClasses();
this.processor.addEntityListeners();
if (this.projectCacheAccessor != null) {
// cache the project:
this.projectCacheAccessor.storeProject(this.session.getProject(), deployProperties, this.session.getSessionLog());
}
// The project is initially created using class names rather than classes. This call will make the conversion.
// If the session was loaded from sessions.xml this will also convert the descriptor classes to the correct class loader.
this.session.getProject().convertClassNamesToClasses(classLoaderToUse);
if (!isCompositeMember()) {
addBeanValidationListeners(deployProperties, classLoaderToUse);
}
// Process the customizers last.
this.processor.processCustomizers();
}
this.processor = null;
} else {
// The project is initially created using class names rather than classes. This call will make the conversion.
// If the session was loaded from sessions.xml this will also convert the descriptor classes to the correct class loader.
this.session.getProject().convertClassNamesToClasses(classLoaderToUse);
if (!this.shouldBuildProject) {
// process anything that might not have been serialized/cached in the project correctly:
if (!isCompositeMember()) {
addBeanValidationListeners(deployProperties, classLoaderToUse);
}
// process Descriptor customizers:
processDescriptorsFromCachedProject(classLoaderToUse);
}
}
finishProcessingDescriptorEvents(classLoaderToUse);
this.structConverters = getStructConverters(classLoaderToUse);
updateRemote(deployProperties, classLoaderToUse);
initSession();
if (this.session.getIntegrityChecker().hasErrors()) {
this.session.handleException(new IntegrityException(session.getIntegrityChecker()));
}
this.session.getDatasourcePlatform().getConversionManager().setLoader(classLoaderToUse);
this.state = STATE_HALF_DEPLOYED;
// keep deployLock
} else {
// state is HALF_DEPLOYED or DEPLOY_FAILED
this.deployLock.release();
isLockAcquired = false;
if (this.state == STATE_DEPLOY_FAILED) {
// Rethrow the cache PersistenceException, which caused STATE_DEPLOYED_FAILED.
throw persistenceException;
}
}
}
// state is HALF_DEPLOYED or DEPLOYED
if (!isCompositeMember()) {
if (this.session.isDatabaseSession() && !((DatabaseSessionImpl) session).isLoggedIn()) {
// If it's HALF_DEPLOYED then deployLock has been already acquired.
if (!isLockAcquired) {
this.deployLock.acquire();
isLockAcquired = true;
}
if (!((DatabaseSessionImpl) this.session).isLoggedIn()) {
if (this.state == STATE_DEPLOY_FAILED) {
// Rethrow the cache PersistenceException, which caused STATE_DEPLOYED_FAILED.
throw persistenceException;
}
this.session.setProperties(deployProperties);
updateSession(deployProperties, classLoaderToUse);
if (isValidationOnly(deployProperties, false)) {
/**
* for 324213 we could add a session.loginAndDetectDatasource() call
* before calling initializeDescriptors when validation-only is True
* to avoid a native sequence exception on a generic DatabasePlatform
* by auto-detecting the correct DB platform.
* However, this would introduce a DB login when validation is on
* - in opposition to the functionality of the property (to only validate)
*/
if (this.state == STATE_HALF_DEPLOYED) {
getDatabaseSession().initializeDescriptors();
this.state = STATE_DEPLOYED;
}
} else {
try {
updateTunerDeploy(deployProperties, classLoaderToUse);
updateFreeMemory(deployProperties);
if (this.isSessionLoadedFromSessionsXML) {
getDatabaseSession().login();
} else {
login(getDatabaseSession(), deployProperties, requiresConnection);
}
final Platform platform = getDatabaseSession().getDatasourcePlatform();
String dbProperties = getConfigPropertyAsStringLogDebug(PersistenceUnitProperties.TARGET_DATABASE_PROPERTIES, deployProperties, this.session);
PropertiesUtils.set(platform, PersistenceUnitProperties.TARGET_DATABASE_PROPERTIES, dbProperties);
// Make JTA integration throw JPA exceptions.
if (this.session.hasExternalTransactionController()) {
if (this.session.getExternalTransactionController().getExceptionHandler() == null) {
this.session.getExternalTransactionController().setExceptionHandler(new ExceptionHandler() {
@Override
public Object handleException(RuntimeException exception) {
if (exception instanceof org.eclipse.persistence.exceptions.OptimisticLockException) {
throw new OptimisticLockException(exception);
} else if (exception instanceof EclipseLinkException) {
throw new PersistenceException(exception);
} else {
throw exception;
}
}
});
}
}
this.state = STATE_DEPLOYED;
} catch (Throwable loginException) {
if (this.state == STATE_HALF_DEPLOYED) {
if (this.session.isConnected()) {
// Cannot recover from that - the user has to fix the persistence unit and redeploy it.
try {
getDatabaseSession().logout();
} catch (Throwable logoutException) {
// Ignore
}
this.state = STATE_DEPLOY_FAILED;
}
}
throw loginException;
}
if (!this.isSessionLoadedFromSessionsXML) {
addStructConverters();
}
// Generate the DDL using the correct connection.
writeDDL(deployProperties, getDatabaseSession(deployProperties), classLoaderToUse);
}
}
// Initialize platform specific identity sequences.
session.getDatasourcePlatform().initIdentitySequences(getDatabaseSession(), MetadataProject.DEFAULT_IDENTITY_GENERATOR);
updateTunerPostDeploy(deployProperties, classLoaderToUse);
this.deployLock.release();
isLockAcquired = false;
}
// 266912: Initialize the Metamodel, a login should have already occurred.
try {
this.getMetamodel(classLoaderToUse);
} catch (Exception e) {
this.session.log(SessionLog.FINEST, SessionLog.METAMODEL, "metamodel_init_failed", new Object[] { e.getMessage() });
}
}
// Clear the weaver's reference to meta-data information, as it is held by the class loader and will never gc.
if (this.weaver != null) {
this.weaver.clear();
this.weaver = null;
}
return this.session;
} catch (Throwable exception) {
// before releasing deployLock switch to the correct state
if (this.state == STATE_PREDEPLOYED) {
this.state = STATE_DEPLOY_FAILED;
}
PersistenceException persistenceEx;
if (this.state == STATE_DEPLOY_FAILED) {
if (exception == persistenceException) {
persistenceEx = new PersistenceException(EntityManagerSetupException.cannotDeployWithoutPredeploy(this.persistenceUnitInfo.getPersistenceUnitName(), this.state, this.persistenceException));
} else {
// before releasing deployLock cache the exception
persistenceEx = createDeployFailedPersistenceException(exception);
}
} else {
if (exception instanceof PersistenceException) {
persistenceEx = (PersistenceException) exception;
} else {
persistenceEx = new PersistenceException(exception);
}
}
if (isLockAcquired) {
this.deployLock.release();
}
this.session.logThrowable(SessionLog.SEVERE, SessionLog.EJB, exception);
throw persistenceEx;
} finally {
this.session.log(SessionLog.FINEST, SessionLog.JPA, "deploy_end", new Object[] { getPersistenceUnitInfo().getPersistenceUnitName(), this.session.getName(), this.state, this.factoryCount });
}
}
Aggregations