KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > injection > PcEncInjector


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.injection;
23
24 import org.jboss.ejb3.entity.ManagedEntityManagerFactory;
25 import org.jboss.ejb3.entity.ExtendedEntityManager;
26 import org.jboss.ejb3.entity.ExtendedHibernateSession;
27 import org.jboss.ejb3.entity.TransactionScopedEntityManager;
28 import org.jboss.ejb3.entity.TransactionScopedHibernateSession;
29 import org.jboss.ejb3.stateful.StatefulContainer;
30 import org.jboss.naming.Util;
31
32 import javax.persistence.PersistenceContextType;
33 import javax.persistence.EntityManager;
34 import javax.naming.NameNotFoundException JavaDoc;
35 import javax.naming.NamingException JavaDoc;
36
37 /**
38  * Comment
39  *
40  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
41  * @version $Revision: 56518 $
42  */

43 public class PcEncInjector implements EncInjector
44 {
45    private String JavaDoc encName;
46    private String JavaDoc unitName;
47    private PersistenceContextType type;
48    private Class JavaDoc injectionType;
49    private String JavaDoc error;
50
51    public PcEncInjector(String JavaDoc encName, String JavaDoc unitName, PersistenceContextType type, Class JavaDoc injectionType, String JavaDoc error)
52    {
53       this.encName = encName;
54       this.unitName = unitName;
55       this.type = type;
56       this.injectionType = injectionType;
57       this.error = error;
58    }
59
60    public void inject(InjectionContainer container)
61    {
62       String JavaDoc error1 = error;
63       ManagedEntityManagerFactory factory = null;
64       try
65       {
66          factory = PersistenceUnitHandler.getManagedEntityManagerFactory(
67                  container, unitName);
68       }
69       catch (NameNotFoundException JavaDoc e)
70       {
71          error1 += " " + e.getMessage();
72       }
73       if (factory == null)
74       {
75          throw new RuntimeException JavaDoc(error1);
76       }
77       if (type == PersistenceContextType.EXTENDED)
78       {
79          if (!(container instanceof StatefulContainer))
80             throw new RuntimeException JavaDoc("It is illegal to inject an EXTENDED PC into something other than a SFSB");
81          container.getInjectors().add(0, new ExtendedPersistenceContextInjector(factory));
82          Object JavaDoc extendedPc = null;
83          if (injectionType == null
84                  || injectionType.getName().equals(EntityManager.class.getName()))
85          {
86             extendedPc = new ExtendedEntityManager(factory.getKernelName());
87          }
88          else
89          {
90             extendedPc = new ExtendedHibernateSession(factory.getKernelName());
91          }
92          try
93          {
94             Util.rebind(container.getEnc(), encName, extendedPc);
95          }
96          catch (NamingException JavaDoc e)
97          {
98             throw new RuntimeException JavaDoc(error1, e);
99          }
100       }
101       else
102       {
103          Object JavaDoc entityManager = null;
104          if (injectionType == null
105                  || injectionType.getName().equals(EntityManager.class.getName()))
106          {
107             entityManager = new TransactionScopedEntityManager(factory);
108          }
109          else
110          {
111             entityManager = new TransactionScopedHibernateSession(factory);
112          }
113          try
114          {
115             Util.rebind(container.getEnc(), encName, entityManager);
116          }
117          catch (NamingException JavaDoc e)
118          {
119             throw new RuntimeException JavaDoc(error1, e);
120          }
121       }
122    }
123 }
124
Popular Tags