001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.injection;
023:
024: import java.lang.reflect.Proxy;
025: import javax.naming.NameNotFoundException;
026: import javax.naming.NamingException;
027: import javax.persistence.EntityManager;
028: import javax.persistence.PersistenceContextType;
029:
030: import org.hibernate.Session;
031: import org.jboss.ejb3.entity.ExtendedEntityManager;
032: import org.jboss.ejb3.entity.ManagedEntityManagerFactory;
033: import org.jboss.ejb3.entity.TransactionScopedEntityManager;
034: import org.jboss.ejb3.entity.hibernate.ExtendedSessionInvocationHandler;
035: import org.jboss.ejb3.entity.hibernate.TransactionScopedSessionInvocationHandler;
036: import org.jboss.ejb3.stateful.StatefulContainer;
037: import org.jboss.naming.Util;
038:
039: /**
040: * Comment
041: *
042: * @author <a href="mailto:bill@jboss.org">Bill Burke</a>
043: * @version $Revision: 61629 $
044: */
045: public class PcEncInjector implements EncInjector {
046: private static final Class[] SESS_PROXY_INTERFACES = new Class[] {
047: org.hibernate.classic.Session.class,
048: org.hibernate.engine.SessionImplementor.class,
049: org.hibernate.jdbc.JDBCContext.Context.class,
050: org.hibernate.event.EventSource.class };
051:
052: private String encName;
053: private String unitName;
054: private PersistenceContextType type;
055: private Class injectionType;
056: private String error;
057:
058: public PcEncInjector(String encName, String unitName,
059: PersistenceContextType type, Class injectionType,
060: String error) {
061: this .encName = encName;
062: this .unitName = unitName;
063: this .type = type;
064: this .injectionType = injectionType;
065: this .error = error;
066: }
067:
068: public void inject(InjectionContainer container) {
069: String error1 = error;
070: ManagedEntityManagerFactory factory = null;
071: try {
072: factory = PersistenceUnitHandler
073: .getManagedEntityManagerFactory(container, unitName);
074: } catch (NameNotFoundException e) {
075: error1 += " " + e.getMessage();
076: }
077: if (factory == null) {
078: throw new RuntimeException(error1);
079: }
080: if (type == PersistenceContextType.EXTENDED) {
081: if (!(container instanceof StatefulContainer))
082: throw new RuntimeException(
083: "It is illegal to inject an EXTENDED PC into something other than a SFSB");
084: container.getInjectors().add(0,
085: new ExtendedPersistenceContextInjector(factory));
086: Object extendedPc;
087: if (injectionType == null
088: || injectionType.getName().equals(
089: EntityManager.class.getName())) {
090: extendedPc = new ExtendedEntityManager(factory
091: .getKernelName());
092: } else {
093: ExtendedSessionInvocationHandler handler = new ExtendedSessionInvocationHandler(
094: factory.getKernelName());
095: extendedPc = Proxy.newProxyInstance(Session.class
096: .getClassLoader(), //use the Hibernate classloader so the proxy has the same scope as Hibernate
097: SESS_PROXY_INTERFACES, handler);
098: }
099: try {
100: Util.rebind(container.getEnc(), encName, extendedPc);
101: } catch (NamingException e) {
102: throw new RuntimeException(error1, e);
103: }
104: } else {
105: Object entityManager;
106: if (injectionType == null
107: || EntityManager.class
108: .isAssignableFrom(injectionType)) {
109: entityManager = new TransactionScopedEntityManager(
110: factory);
111: } else {
112: TransactionScopedSessionInvocationHandler handler = new TransactionScopedSessionInvocationHandler(
113: factory);
114: entityManager = Proxy.newProxyInstance(Session.class
115: .getClassLoader(), //use the Hibernate classloader so the proxy has the same scope as Hibernate
116: SESS_PROXY_INTERFACES, handler);
117: }
118: try {
119: Util.rebind(container.getEnc(), encName, entityManager);
120: } catch (NamingException e) {
121: throw new RuntimeException(error1, e);
122: }
123: }
124: }
125: }
|