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.jpa.support;
018:
019: import javax.persistence.EntityManager;
020: import javax.persistence.EntityManagerFactory;
021:
022: import org.springframework.beans.factory.FactoryBean;
023: import org.springframework.beans.factory.InitializingBean;
024: import org.springframework.orm.jpa.EntityManagerFactoryAccessor;
025: import org.springframework.orm.jpa.EntityManagerFactoryInfo;
026: import org.springframework.orm.jpa.EntityManagerPlus;
027: import org.springframework.orm.jpa.JpaDialect;
028: import org.springframework.orm.jpa.SharedEntityManagerCreator;
029: import org.springframework.util.Assert;
030:
031: /**
032: * FactoryBeans that exposes a shared JPA EntityManager reference for a
033: * given EntityManagerFactory. Typically used for an EntityManagerFactory
034: * created by LocalEntityManagerFactoryBean, as direct alternative to a
035: * JndiObjectFactoryBean definition for a Java EE 5 server's EntityManager.
036: *
037: * <p>The shared EntityManager will behave just like an EntityManager fetched
038: * from an application server's JNDI environment, as defined by the JPA
039: * specification. It will delegate all calls to the current transactional
040: * EntityManager, if any; else, it will fall back to a newly created
041: * EntityManager per operation.
042: *
043: * <p>Can be passed to DAOs that expect a shared EntityManager reference
044: * rather than an EntityManagerFactory reference. Note that Spring's
045: * JpaTransactionManager always needs an EntityManagerFactory reference,
046: * to be able to create new transactional EntityManager instances.
047: *
048: * @author Juergen Hoeller
049: * @since 2.0
050: * @see #setEntityManagerFactory
051: * @see #setEntityManagerInterface
052: * @see org.springframework.orm.jpa.LocalEntityManagerFactoryBean
053: * @see org.springframework.orm.jpa.JpaTransactionManager
054: */
055: public class SharedEntityManagerBean extends
056: EntityManagerFactoryAccessor implements FactoryBean,
057: InitializingBean {
058:
059: private Class entityManagerInterface;
060:
061: private EntityManager shared;
062:
063: /**
064: * Specify the EntityManager interface to expose.
065: * <p>Default is the the EntityManager interface as defined by the
066: * EntityManagerFactoryInfo, if available. Else, the standard
067: * <code>javax.persistence.EntityManager</code> interface will be used.
068: * @see org.springframework.orm.jpa.EntityManagerFactoryInfo#getEntityManagerInterface()
069: * @see javax.persistence.EntityManager
070: */
071: public void setEntityManagerInterface(Class entityManagerInterface) {
072: Assert.notNull(entityManagerInterface,
073: "entityManagerInterface must not be null");
074: Assert
075: .isAssignable(EntityManager.class,
076: entityManagerInterface);
077: this .entityManagerInterface = entityManagerInterface;
078: }
079:
080: public final void afterPropertiesSet() {
081: EntityManagerFactory emf = getEntityManagerFactory();
082: if (emf == null) {
083: throw new IllegalArgumentException(
084: "entityManagerFactory is required");
085: }
086: Class[] ifcs = null;
087: if (emf instanceof EntityManagerFactoryInfo) {
088: EntityManagerFactoryInfo emfInfo = (EntityManagerFactoryInfo) emf;
089: if (this .entityManagerInterface == null) {
090: this .entityManagerInterface = emfInfo
091: .getEntityManagerInterface();
092: }
093: JpaDialect jpaDialect = emfInfo.getJpaDialect();
094: if (jpaDialect != null
095: && jpaDialect.supportsEntityManagerPlusOperations()) {
096: ifcs = new Class[] { this .entityManagerInterface,
097: EntityManagerPlus.class };
098: } else {
099: ifcs = new Class[] { this .entityManagerInterface };
100: }
101: } else {
102: if (this .entityManagerInterface == null) {
103: this .entityManagerInterface = EntityManager.class;
104: }
105: ifcs = new Class[] { this .entityManagerInterface };
106: }
107: this .shared = SharedEntityManagerCreator
108: .createSharedEntityManager(emf, getJpaPropertyMap(),
109: ifcs);
110: }
111:
112: public EntityManager getObject() {
113: return this .shared;
114: }
115:
116: public Class getObjectType() {
117: return this .entityManagerInterface;
118: }
119:
120: public boolean isSingleton() {
121: return true;
122: }
123:
124: }
|