KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > dao > support > ChainedPersistenceExceptionTranslator


1 /*
2  * Copyright 2002-2006 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.dao.support;
18
19 import java.util.ArrayList JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.List JavaDoc;
22
23 import org.springframework.dao.DataAccessException;
24 import org.springframework.util.Assert;
25
26 /**
27  * Implementation of PersistenceExceptionTranslator that supports chaining,
28  * allowing the addition of PersistenceExceptionTranslator instances in order.
29  * Returns <code>non-null</code> on the first (if any) match.
30  *
31  * @author Rod Johnson
32  * @author Juergen Hoeller
33  * @since 2.0
34  */

35 public class ChainedPersistenceExceptionTranslator implements PersistenceExceptionTranslator {
36     
37     /** List of PersistenceExceptionTranslators */
38     private final List JavaDoc delegates = new ArrayList JavaDoc(4);
39
40
41     /**
42      * Add a PersistenceExceptionTranslator to the chained delegate list.
43      */

44     public final void addDelegate(PersistenceExceptionTranslator pet) {
45         Assert.notNull(pet, "PersistenceExceptionTranslator must not be null");
46         this.delegates.add(pet);
47     }
48
49     /**
50      * Return all registered PersistenceExceptionTranslator delegates (as array).
51      */

52     public final PersistenceExceptionTranslator[] getDelegates() {
53         return (PersistenceExceptionTranslator[])
54                 this.delegates.toArray(new PersistenceExceptionTranslator[this.delegates.size()]);
55     }
56
57
58     public DataAccessException translateExceptionIfPossible(RuntimeException JavaDoc ex) {
59         DataAccessException translatedDex = null;
60         for (Iterator JavaDoc it = this.delegates.iterator(); translatedDex == null && it.hasNext(); ) {
61             PersistenceExceptionTranslator pet = (PersistenceExceptionTranslator) it.next();
62             translatedDex = pet.translateExceptionIfPossible(ex);
63         }
64         return translatedDex;
65     }
66
67 }
68
Popular Tags