001: /*
002: * Copyright 2002-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.orm.jdo.support;
018:
019: import javax.jdo.JDOException;
020: import javax.jdo.PersistenceManager;
021: import javax.jdo.PersistenceManagerFactory;
022:
023: import org.springframework.dao.DataAccessException;
024: import org.springframework.dao.DataAccessResourceFailureException;
025: import org.springframework.dao.support.DaoSupport;
026: import org.springframework.orm.jdo.JdoTemplate;
027: import org.springframework.orm.jdo.PersistenceManagerFactoryUtils;
028:
029: /**
030: * Convenient super class for JDO data access objects.
031: *
032: * <p>Requires a PersistenceManagerFactory to be set, providing a JdoTemplate
033: * based on it to subclasses. Can alternatively be initialized directly with a
034: * JdoTemplate, to reuse the latter's settings such as the PersistenceManagerFactory,
035: * JdoDialect, flush mode, etc.
036: *
037: * <p>This base class is mainly intended for JdoTemplate usage but can also
038: * be used when working with PersistenceManagerFactoryUtils directly, for example
039: * in combination with JdoInterceptor-managed PersistenceManagers. Convenience
040: * <code>getPersistenceManager</code> and <code>releasePersistenceManager</code>
041: * methods are provided for that usage style.
042: *
043: * <p>This class will create its own JdoTemplate if only a PersistenceManagerFactory
044: * is passed in. The "allowCreate" flag on that JdoTemplate will be "true" by default.
045: * A custom JdoTemplate instance can be used through overriding <code>createJdoTemplate</code>.
046: *
047: * @author Juergen Hoeller
048: * @since 28.07.2003
049: * @see #setPersistenceManagerFactory
050: * @see #setJdoTemplate
051: * @see #createJdoTemplate
052: * @see #getPersistenceManager
053: * @see #releasePersistenceManager
054: * @see org.springframework.orm.jdo.JdoTemplate
055: * @see org.springframework.orm.jdo.JdoInterceptor
056: */
057: public abstract class JdoDaoSupport extends DaoSupport {
058:
059: private JdoTemplate jdoTemplate;
060:
061: /**
062: * Set the JDO PersistenceManagerFactory to be used by this DAO.
063: * Will automatically create a JdoTemplate for the given PersistenceManagerFactory.
064: * @see #createJdoTemplate
065: * @see #setJdoTemplate
066: */
067: public final void setPersistenceManagerFactory(
068: PersistenceManagerFactory persistenceManagerFactory) {
069: this .jdoTemplate = createJdoTemplate(persistenceManagerFactory);
070: }
071:
072: /**
073: * Create a JdoTemplate for the given PersistenceManagerFactory.
074: * Only invoked if populating the DAO with a PersistenceManagerFactory reference!
075: * <p>Can be overridden in subclasses to provide a JdoTemplate instance
076: * with different configuration, or a custom JdoTemplate subclass.
077: * @param persistenceManagerFactory the JDO PersistenceManagerFactoryto create a JdoTemplate for
078: * @return the new JdoTemplate instance
079: * @see #setPersistenceManagerFactory
080: */
081: protected JdoTemplate createJdoTemplate(
082: PersistenceManagerFactory persistenceManagerFactory) {
083: return new JdoTemplate(persistenceManagerFactory);
084: }
085:
086: /**
087: * Return the JDO PersistenceManagerFactory used by this DAO.
088: */
089: public final PersistenceManagerFactory getPersistenceManagerFactory() {
090: return (this .jdoTemplate != null ? this .jdoTemplate
091: .getPersistenceManagerFactory() : null);
092: }
093:
094: /**
095: * Set the JdoTemplate for this DAO explicitly,
096: * as an alternative to specifying a PersistenceManagerFactory.
097: * @see #setPersistenceManagerFactory
098: */
099: public final void setJdoTemplate(JdoTemplate jdoTemplate) {
100: this .jdoTemplate = jdoTemplate;
101: }
102:
103: /**
104: * Return the JdoTemplate for this DAO, pre-initialized
105: * with the PersistenceManagerFactory or set explicitly.
106: */
107: public final JdoTemplate getJdoTemplate() {
108: return jdoTemplate;
109: }
110:
111: protected final void checkDaoConfig() {
112: if (this .jdoTemplate == null) {
113: throw new IllegalArgumentException(
114: "persistenceManagerFactory or jdoTemplate is required");
115: }
116: }
117:
118: /**
119: * Get a JDO PersistenceManager, either from the current transaction or
120: * a new one. The latter is only allowed if the "allowCreate" setting
121: * of this bean's JdoTemplate is true.
122: * @return the JDO PersistenceManager
123: * @throws DataAccessResourceFailureException if the PersistenceManager couldn't be created
124: * @throws IllegalStateException if no thread-bound PersistenceManager found and allowCreate false
125: * @see org.springframework.orm.jdo.PersistenceManagerFactoryUtils#getPersistenceManager
126: */
127: protected final PersistenceManager getPersistenceManager() {
128: return getPersistenceManager(this .jdoTemplate.isAllowCreate());
129: }
130:
131: /**
132: * Get a JDO PersistenceManager, either from the current transaction or
133: * a new one. The latter is only allowed if "allowCreate" is true.
134: * @param allowCreate if a non-transactional PersistenceManager should be created
135: * when no transactional PersistenceManager can be found for the current thread
136: * @return the JDO PersistenceManager
137: * @throws DataAccessResourceFailureException if the PersistenceManager couldn't be created
138: * @throws IllegalStateException if no thread-bound PersistenceManager found and allowCreate false
139: * @see org.springframework.orm.jdo.PersistenceManagerFactoryUtils#getPersistenceManager
140: */
141: protected final PersistenceManager getPersistenceManager(
142: boolean allowCreate)
143: throws DataAccessResourceFailureException,
144: IllegalStateException {
145:
146: return PersistenceManagerFactoryUtils.getPersistenceManager(
147: getPersistenceManagerFactory(), allowCreate);
148: }
149:
150: /**
151: * Convert the given JDOException to an appropriate exception from the
152: * org.springframework.dao hierarchy.
153: * <p>Delegates to the convertJdoAccessException method of this DAO's JdoTemplate.
154: * @param ex JDOException that occured
155: * @return the corresponding DataAccessException instance
156: * @see #setJdoTemplate
157: * @see org.springframework.orm.jdo.JdoTemplate#convertJdoAccessException
158: */
159: protected final DataAccessException convertJdoAccessException(
160: JDOException ex) {
161: return this .jdoTemplate.convertJdoAccessException(ex);
162: }
163:
164: /**
165: * Close the given JDO PersistenceManager, created via this DAO's
166: * PersistenceManagerFactory, if it isn't bound to the thread.
167: * @param pm PersistenceManager to close
168: * @see org.springframework.orm.jdo.PersistenceManagerFactoryUtils#releasePersistenceManager
169: */
170: protected final void releasePersistenceManager(PersistenceManager pm) {
171: PersistenceManagerFactoryUtils.releasePersistenceManager(pm,
172: getPersistenceManagerFactory());
173: }
174:
175: }
|