001/*
002 * Copyright 2002-2013 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 *      https://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 exprShess or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.orm.jdo.support;
018
019import javax.jdo.PersistenceManager;
020import javax.jdo.PersistenceManagerFactory;
021
022import org.springframework.beans.factory.FactoryBean;
023import org.springframework.util.Assert;
024
025/**
026 * Proxy that implements the {@link javax.jdo.PersistenceManager} interface,
027 * delegating to a thread-bound PersistenceManager on each invocation -
028 * as defined by the JDO 3.0 specification. This class makes such a standard
029 * JDO PersistenceManager proxy available for bean references.
030 *
031 * <p>The main advantage of this proxy is that it allows DAOs to work with a
032 * plain JDO PersistenceManager reference in JDO 3.0 style
033 * (see {@link javax.jdo.PersistenceManagerFactory#getPersistenceManagerProxy()}),
034 * exposing the exact behavior that the target JDO provider implements.
035 *
036 * @author Juergen Hoeller
037 * @since 3.0
038 * @see SpringPersistenceManagerProxyBean
039 * @see javax.jdo.PersistenceManagerFactory#getPersistenceManagerProxy()
040 */
041public class StandardPersistenceManagerProxyBean implements FactoryBean<PersistenceManager> {
042
043        private PersistenceManager proxy;
044
045
046        /**
047         * Set the target JDO PersistenceManagerFactory that this proxy should
048         * delegate to. This should be the raw PersistenceManagerFactory, as
049         * accessed by JdoTransactionManager.
050         * @see org.springframework.orm.jdo.JdoTransactionManager
051         */
052        public void setPersistenceManagerFactory(PersistenceManagerFactory pmf) {
053                Assert.notNull(pmf, "PersistenceManagerFactory must not be null");
054                this.proxy = pmf.getPersistenceManagerProxy();
055        }
056
057
058        @Override
059        public PersistenceManager getObject() {
060                return this.proxy;
061        }
062
063        @Override
064        public Class<? extends PersistenceManager> getObjectType() {
065                return (this.proxy != null ? this.proxy.getClass() : PersistenceManager.class);
066        }
067
068        @Override
069        public boolean isSingleton() {
070                return true;
071        }
072
073}