Add exception translator for EclipseLink exceptions

Issue: SPR-9541
This commit is contained in:
Jan Stamer
2012-06-25 12:52:59 +02:00
committed by Stephane Nicoll
parent 658f7f58df
commit 371e3a7ac0
6 changed files with 266 additions and 0 deletions

View File

@@ -0,0 +1,51 @@
/*
* Copyright 2002-2012 the original author or authors.
*
* 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.springframework.orm.eclipselink;
import org.eclipse.persistence.exceptions.EclipseLinkException;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.support.PersistenceExceptionTranslator;
/**
* {@link PersistenceExceptionTranslator} capable of translating {@link EclipseLinkException}
* instances to Spring's {@link DataAccessException} hierarchy.
*
* @author Jan Stamer
* @since 3.2
* @see org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor
*/
public class EclipseLinkExceptionTranslator implements PersistenceExceptionTranslator {
public DataAccessException translateExceptionIfPossible(RuntimeException ex) {
if (ex instanceof EclipseLinkException) {
return convertEclipseLinkAccessException((EclipseLinkException) ex);
}
return null;
}
/**
* Convert the given EclipseLinkException to an appropriate exception from
* the {@code org.springframework.dao} hierarchy.
* @param ex EclipseLinkException that occurred
* @return a corresponding DataAccessException
* @see SessionFactoryUtils#convertEclipseLinkAccessException
*/
protected DataAccessException convertEclipseLinkAccessException(EclipseLinkException ex) {
return EclipseLinkUtils.convertEclipseLinkAccessException(ex);
}
}

View File

@@ -0,0 +1,42 @@
/*
* Copyright 2002-2012 the original author or authors.
*
* 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.springframework.orm.eclipselink;
import org.eclipse.persistence.exceptions.EclipseLinkException;
import org.springframework.dao.UncategorizedDataAccessException;
/**
* EclipseLink-specific subclass of UncategorizedDataAccessException, for
* EclipseLink system errors that do not match any concrete
* <code>org.springframework.dao</code> exceptions.
*
* @author Jan Stamer
* @since 3.2
* @see EclipseLinkUtils#convertEclipseLinkAccessException(EclipseLinkException)
*/
public class EclipseLinkSystemException extends UncategorizedDataAccessException {
/**
* Create a new HibernateSystemException, wrapping an arbitrary
* HibernateException.
* @param cause the HibernateException thrown
*/
public EclipseLinkSystemException(EclipseLinkException cause) {
super(cause != null ? cause.getMessage() : null, cause);
}
}

View File

@@ -0,0 +1,42 @@
/*
* Copyright 2002-2012 the original author or authors.
*
* 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.springframework.orm.eclipselink;
import org.eclipse.persistence.exceptions.EclipseLinkException;
import org.springframework.dao.DataAccessException;
/**
* Helper class featuring methods for Eclipse Link. Also provides support for
* exception translation.
*
* @author Jan Stamer
* @since 3.2
*/
public abstract class EclipseLinkUtils {
/**
* Convert the given EclipseLinkException to an appropriate exception from
* the <code>org.springframework.dao</code> hierarchy.
* @param ex EclipseLinkException that occured
* @return the corresponding DataAccessException instance
* @see EclipseLinkExceptionTranslator#convertEclipseLinkAccessException(EclipseLinkException)
*/
public static DataAccessException convertEclipseLinkAccessException(EclipseLinkException ex) {
return new EclipseLinkSystemException(ex);
}
}