KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > orm > ojb > OjbAccessor


1 /*
2  * Copyright 2002-2005 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.orm.ojb;
18
19 import java.sql.SQLException JavaDoc;
20
21 import org.apache.ojb.broker.PBKey;
22 import org.apache.ojb.broker.PersistenceBrokerException;
23 import org.apache.ojb.broker.PersistenceBrokerFactory;
24
25 import org.springframework.dao.DataAccessException;
26 import org.springframework.jdbc.support.JdbcAccessor;
27
28 /**
29  * Base class for OJB-accessing classes, defining common properties like PBKey.
30  * Extends JdbcAccessor to inherit SQLException translation capabilities.
31  *
32  * <p>Not intended to be used directly. See PersistenceBrokerTemplate.
33  *
34  * @author Juergen Hoeller
35  * @since 1.1
36  * @see PersistenceBrokerTemplate
37  */

38 public class OjbAccessor extends JdbcAccessor {
39
40     private PBKey pbKey = PersistenceBrokerFactory.getDefaultKey();
41
42
43     /**
44      * Set the JDBC Connection Descriptor alias of the PersistenceBroker
45      * configuration to use. Default is the default connection configured for OJB.
46      */

47     public void setJcdAlias(String JavaDoc jcdAlias) {
48         this.pbKey = new PBKey(jcdAlias);
49     }
50
51     /**
52      * Return the JDBC Connection Descriptor alias of the PersistenceBroker
53      * configuration to use.
54      */

55     public String JavaDoc getJcdAlias() {
56         return (this.pbKey != null ? this.pbKey.getAlias() : null);
57     }
58
59     /**
60      * Set the PBKey of the PersistenceBroker configuration to use.
61      * Default is the default connection configured for OJB.
62      */

63     public void setPbKey(PBKey pbKey) {
64         this.pbKey = pbKey;
65     }
66
67     /**
68      * Return the PBKey of the PersistenceBroker configuration used.
69      */

70     public PBKey getPbKey() {
71         return pbKey;
72     }
73
74
75     /**
76      * Convert the given PersistenceBrokerException to an appropriate exception
77      * from the org.springframework.dao hierarchy. In case of a wrapped SQLException,
78      * the SQLExceptionTranslator inherited from the superclass gets applied.
79      * May be overridden in subclasses.
80      * @param ex PersistenceBrokerException that occured
81      * @return the corresponding DataAccessException instance
82      * @see #setExceptionTranslator
83      */

84     public DataAccessException convertOjbAccessException(PersistenceBrokerException ex) {
85         if (ex.getCause() instanceof PersistenceBrokerException) {
86             return convertOjbAccessException((PersistenceBrokerException) ex.getCause());
87         }
88         else if (ex.getCause() instanceof SQLException JavaDoc) {
89             return convertJdbcAccessException((SQLException JavaDoc) ex.getCause());
90         }
91         else {
92             throw new OjbOperationException(ex);
93         }
94     }
95
96     /**
97      * Convert the given SQLException to an appropriate exception from the
98      * org.springframework.dao hierarchy. Can be overridden in subclasses.
99      * <p>Note that SQLException can just occur here when callback code
100      * performs direct JDBC access via ConnectionManagerIF.getConnection().
101      * @param ex SQLException that occured
102      * @return the corresponding DataAccessException instance
103      * @see #setExceptionTranslator
104      * @see org.apache.ojb.broker.accesslayer.ConnectionManagerIF#getConnection
105      */

106     protected DataAccessException convertJdbcAccessException(SQLException JavaDoc ex) {
107         return getExceptionTranslator().translate("OJB operation", null, ex);
108     }
109
110 }
111
Popular Tags