001/** 002 * GRANITE DATA SERVICES 003 * Copyright (C) 2006-2013 GRANITE DATA SERVICES S.A.S. 004 * 005 * This file is part of the Granite Data Services Platform. 006 * 007 * Granite Data Services is free software; you can redistribute it and/or 008 * modify it under the terms of the GNU Lesser General Public 009 * License as published by the Free Software Foundation; either 010 * version 2.1 of the License, or (at your option) any later version. 011 * 012 * Granite Data Services is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser 015 * General Public License for more details. 016 * 017 * You should have received a copy of the GNU Lesser General Public 018 * License along with this library; if not, write to the Free Software 019 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 020 * USA, or see <http://www.gnu.org/licenses/>. 021 */ 022package org.granite.tide.hibernate4; 023 024import java.util.HashMap; 025import java.util.Map; 026 027import org.granite.hibernate4.HibernateOptimisticLockException; 028import org.granite.messaging.service.ExceptionConverter; 029import org.granite.messaging.service.ExtendedServiceExceptionHandler; 030import org.granite.messaging.service.ServiceException; 031import org.hibernate.HibernateException; 032import org.hibernate.NonUniqueObjectException; 033import org.hibernate.NonUniqueResultException; 034import org.hibernate.ObjectNotFoundException; 035import org.hibernate.StaleObjectStateException; 036import org.hibernate.StaleStateException; 037 038 039public class HibernateExceptionConverter implements ExceptionConverter { 040 041 public static final String ENTITY_EXISTS = "Persistence.EntityExists"; 042 public static final String ENTITY_NOT_FOUND = "Persistence.EntityNotFound"; 043 public static final String NON_UNIQUE_RESULT = "Persistence.NonUnique"; 044 public static final String NO_RESULT = "Persistence.NoResult"; 045 public static final String OPTIMISTIC_LOCK = "Persistence.OptimisticLock"; 046 public static final String TRANSACTION_REQUIRED = "Persistence.TransactionRequired"; 047 public static final String ROLLBACK = "Persistence.Rollback"; 048 public static final String OTHER = "Persistence.Error"; 049 050 051 public boolean accepts(Throwable t, Throwable finalException) { 052 // Check if we could not let the JPA converter handle the exception 053 for (Throwable cause = finalException; cause != null && cause != t; cause = ExtendedServiceExceptionHandler.getCause(cause)) { 054 if (cause.getClass().getName().startsWith("javax.persistence")) 055 return false; 056 } 057 058 return t.getClass().equals(NonUniqueObjectException.class) 059 || t.getClass().equals(ObjectNotFoundException.class) 060 || t.getClass().equals(NonUniqueResultException.class) 061 || StaleStateException.class.isAssignableFrom(t.getClass()) 062 || HibernateException.class.isAssignableFrom(t.getClass()); 063 } 064 065 public ServiceException convert(Throwable t, String detail, Map<String, Object> extendedData) { 066 String error = null; 067 Map<String, Object> ex = null; 068 if (t.getClass().equals(NonUniqueObjectException.class)) 069 error = ENTITY_EXISTS; 070 else if (t.getClass().equals(ObjectNotFoundException.class)) 071 error = ENTITY_NOT_FOUND; 072 else if (t.getClass().equals(NonUniqueResultException.class)) 073 error = NON_UNIQUE_RESULT; 074 else if (t.getClass().equals(StaleStateException.class) || t.getClass().equals(StaleObjectStateException.class) 075 || t.getClass().equals(HibernateOptimisticLockException.class)) { 076 error = OPTIMISTIC_LOCK; 077 if (t instanceof HibernateOptimisticLockException) { 078 ex = new HashMap<String, Object>(); 079 ex.put("entity", ((HibernateOptimisticLockException)t).getEntity()); 080 } 081 else if (t instanceof StaleObjectStateException) { 082 ex = new HashMap<String, Object>(); 083 ex.put("entityName", ((StaleObjectStateException)t).getEntityName()); 084 ex.put("identifier", ((StaleObjectStateException)t).getIdentifier()); 085 } 086 } 087 else 088 error = OTHER; 089 090 ServiceException se = new ServiceException(error, t.getMessage(), detail, t); 091 if (ex != null && !ex.isEmpty()) 092 se.getExtendedData().putAll(ex); 093 return se; 094 } 095 096}