KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > orm > jdo > support > JdoDaoSupport


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.orm.jdo.support;
18
19 import javax.jdo.JDOException;
20 import javax.jdo.PersistenceManager;
21 import javax.jdo.PersistenceManagerFactory;
22
23 import org.springframework.dao.DataAccessException;
24 import org.springframework.dao.DataAccessResourceFailureException;
25 import org.springframework.dao.support.DaoSupport;
26 import org.springframework.orm.jdo.JdoTemplate;
27 import org.springframework.orm.jdo.PersistenceManagerFactoryUtils;
28
29 /**
30  * Convenient super class for JDO data access objects.
31  *
32  * <p>Requires a PersistenceManagerFactory to be set, providing a JdoTemplate
33  * based on it to subclasses. Can alternatively be initialized directly with a
34  * JdoTemplate, to reuse the latter's settings such as the PersistenceManagerFactory,
35  * JdoDialect, flush mode, etc.
36  *
37  * <p>This base class is mainly intended for JdoTemplate usage but can also
38  * be used when working with PersistenceManagerFactoryUtils directly, for example
39  * in combination with JdoInterceptor-managed PersistenceManagers. Convenience
40  * <code>getPersistenceManager</code> and <code>releasePersistenceManager</code>
41  * methods are provided for that usage style.
42  *
43  * <p>This class will create its own JdoTemplate if only a PersistenceManagerFactory
44  * is passed in. The "allowCreate" flag on that JdoTemplate will be "true" by default.
45  * A custom JdoTemplate instance can be used through overriding <code>createJdoTemplate</code>.
46  *
47  * @author Juergen Hoeller
48  * @since 28.07.2003
49  * @see #setPersistenceManagerFactory
50  * @see #setJdoTemplate
51  * @see #createJdoTemplate
52  * @see #getPersistenceManager
53  * @see #releasePersistenceManager
54  * @see org.springframework.orm.jdo.JdoTemplate
55  * @see org.springframework.orm.jdo.JdoInterceptor
56  */

57 public abstract class JdoDaoSupport extends DaoSupport {
58
59     private JdoTemplate jdoTemplate;
60
61
62     /**
63      * Set the JDO PersistenceManagerFactory to be used by this DAO.
64      * Will automatically create a JdoTemplate for the given PersistenceManagerFactory.
65      * @see #createJdoTemplate
66      * @see #setJdoTemplate
67      */

68     public final void setPersistenceManagerFactory(PersistenceManagerFactory persistenceManagerFactory) {
69       this.jdoTemplate = createJdoTemplate(persistenceManagerFactory);
70     }
71
72     /**
73      * Create a JdoTemplate for the given PersistenceManagerFactory.
74      * Only invoked if populating the DAO with a PersistenceManagerFactory reference!
75      * <p>Can be overridden in subclasses to provide a JdoTemplate instance
76      * with different configuration, or a custom JdoTemplate subclass.
77      * @param persistenceManagerFactory the JDO PersistenceManagerFactoryto create a JdoTemplate for
78      * @return the new JdoTemplate instance
79      * @see #setPersistenceManagerFactory
80      */

81     protected JdoTemplate createJdoTemplate(PersistenceManagerFactory persistenceManagerFactory) {
82         return new JdoTemplate(persistenceManagerFactory);
83     }
84
85     /**
86      * Return the JDO PersistenceManagerFactory used by this DAO.
87      */

88     public final PersistenceManagerFactory getPersistenceManagerFactory() {
89         return (this.jdoTemplate != null ? this.jdoTemplate.getPersistenceManagerFactory() : null);
90     }
91
92     /**
93      * Set the JdoTemplate for this DAO explicitly,
94      * as an alternative to specifying a PersistenceManagerFactory.
95      * @see #setPersistenceManagerFactory
96      */

97     public final void setJdoTemplate(JdoTemplate jdoTemplate) {
98         this.jdoTemplate = jdoTemplate;
99     }
100
101     /**
102      * Return the JdoTemplate for this DAO, pre-initialized
103      * with the PersistenceManagerFactory or set explicitly.
104      */

105     public final JdoTemplate getJdoTemplate() {
106       return jdoTemplate;
107     }
108
109     protected final void checkDaoConfig() {
110         if (this.jdoTemplate == null) {
111             throw new IllegalArgumentException JavaDoc("persistenceManagerFactory or jdoTemplate is required");
112         }
113     }
114
115
116     /**
117      * Get a JDO PersistenceManager, either from the current transaction or
118      * a new one. The latter is only allowed if the "allowCreate" setting
119      * of this bean's JdoTemplate is true.
120      * @return the JDO PersistenceManager
121      * @throws DataAccessResourceFailureException if the PersistenceManager couldn't be created
122      * @throws IllegalStateException if no thread-bound PersistenceManager found and allowCreate false
123      * @see org.springframework.orm.jdo.PersistenceManagerFactoryUtils#getPersistenceManager
124      */

125     protected final PersistenceManager getPersistenceManager() {
126         return getPersistenceManager(this.jdoTemplate.isAllowCreate());
127     }
128
129     /**
130      * Get a JDO PersistenceManager, either from the current transaction or
131      * a new one. The latter is only allowed if "allowCreate" is true.
132      * @param allowCreate if a non-transactional PersistenceManager should be created
133      * when no transactional PersistenceManager can be found for the current thread
134      * @return the JDO PersistenceManager
135      * @throws DataAccessResourceFailureException if the PersistenceManager couldn't be created
136      * @throws IllegalStateException if no thread-bound PersistenceManager found and allowCreate false
137      * @see org.springframework.orm.jdo.PersistenceManagerFactoryUtils#getPersistenceManager
138      */

139     protected final PersistenceManager getPersistenceManager(boolean allowCreate)
140         throws DataAccessResourceFailureException, IllegalStateException JavaDoc {
141
142         return PersistenceManagerFactoryUtils.getPersistenceManager(getPersistenceManagerFactory(), allowCreate);
143     }
144
145     /**
146      * Convert the given JDOException to an appropriate exception from the
147      * org.springframework.dao hierarchy.
148      * <p>Delegates to the convertJdoAccessException method of this DAO's JdoTemplate.
149      * @param ex JDOException that occured
150      * @return the corresponding DataAccessException instance
151      * @see #setJdoTemplate
152      * @see org.springframework.orm.jdo.JdoTemplate#convertJdoAccessException
153      */

154     protected final DataAccessException convertJdoAccessException(JDOException ex) {
155         return this.jdoTemplate.convertJdoAccessException(ex);
156     }
157
158     /**
159      * Close the given JDO PersistenceManager, created via this DAO's
160      * PersistenceManagerFactory, if it isn't bound to the thread.
161      * @param pm PersistenceManager to close
162      * @see org.springframework.orm.jdo.PersistenceManagerFactoryUtils#releasePersistenceManager
163      */

164     protected final void releasePersistenceManager(PersistenceManager pm) {
165         PersistenceManagerFactoryUtils.releasePersistenceManager(pm, getPersistenceManagerFactory());
166     }
167
168 }
169
Popular Tags