KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > injection > PersistenceUnitHandler


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.ejb3.injection;
8
9 import java.lang.reflect.Field JavaDoc;
10 import java.lang.reflect.Method JavaDoc;
11 import java.util.ArrayList JavaDoc;
12 import java.util.List JavaDoc;
13 import javax.naming.Context JavaDoc;
14 import javax.naming.InitialContext JavaDoc;
15 import javax.naming.NamingException JavaDoc;
16 import javax.persistence.EntityManagerFactory;
17 import javax.persistence.PersistenceUnit;
18 import javax.persistence.PersistenceUnit;
19 import javax.persistence.PersistenceUnits;
20 import javax.persistence.PersistenceUnit;
21 import javax.persistence.PersistenceUnits;
22 import javax.persistence.PersistenceUnits;
23 import org.jboss.aop.Advisor;
24 import org.jboss.ejb3.Container;
25 import org.jboss.ejb3.entity.InjectedEntityManager;
26 import org.jboss.ejb3.entity.InjectedEntityManagerFactory;
27 import org.jboss.ejb3.entity.ManagedEntityManagerFactory;
28 import org.jboss.naming.Util;
29
30 /**
31  * Searches bean class for all @Inject and create Injectors
32  *
33  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
34  * @version $Revision: 1.1.2.2 $
35  */

36 public class PersistenceUnitHandler
37 {
38    private static void loadPUsAnnotation(Container container) throws Exception JavaDoc
39    {
40       PersistenceUnits resources = (PersistenceUnits) ((Advisor) container).resolveAnnotation(PersistenceUnits.class);
41       if (resources == null) return;
42       for (PersistenceUnit ref : resources.value())
43       {
44          String JavaDoc encName = ref.name();
45          if (encName == null || encName.equals(""))
46          {
47             throw new RuntimeException JavaDoc("JBoss requires name() for class level @PersistenceUnit");
48          }
49
50          EntityManagerFactory factory = getEntityManagerFactory(ref, container);
51          bindEntityManagerFactoryToEnc(encName, factory);
52       }
53       PersistenceUnit pu = (PersistenceUnit)((Advisor)container).resolveAnnotation(PersistenceUnit.class);
54       if (pu != null)
55       {
56          String JavaDoc encName = pu.name();
57          if (encName == null || encName.equals(""))
58          {
59             throw new RuntimeException JavaDoc("JBoss requires name() for class level @PersistenceUnit");
60          }
61
62          ManagedEntityManagerFactory factory = getManagedEntityManagerFactory(pu, container);
63          bindEntityManagerToEnc(factory, encName);
64       }
65    }
66
67    public static ManagedEntityManagerFactory getManagedEntityManagerFactory(PersistenceUnit ref, Container container)
68            throws NamingException JavaDoc
69    {
70       ManagedEntityManagerFactory factory;
71       if (ref.unitName() == null || ref.unitName().equals(""))
72       {
73          factory = container.getManagedEntityManagerFactory();
74       }
75       else
76       {
77          InitialContext JavaDoc ctx = new InitialContext JavaDoc();
78          factory = (ManagedEntityManagerFactory) ctx.lookup("java:/managedEntityFactory." + ref.unitName());
79       }
80       return factory;
81    }
82
83
84    public static EntityManagerFactory getEntityManagerFactory(PersistenceUnit ref, Container container)
85            throws NamingException JavaDoc
86    {
87       EntityManagerFactory factory;
88       if (ref.unitName() == null || ref.unitName().equals(""))
89       {
90          factory = container.getManagedEntityManagerFactory().getEntityManagerFactory();
91          return new InjectedEntityManagerFactory(factory);
92       }
93       else
94       {
95          InitialContext JavaDoc ctx = new InitialContext JavaDoc();
96          factory = (EntityManagerFactory) ctx.lookup("java:/" + ref.unitName());
97          return new InjectedEntityManagerFactory(factory, "java:/" + ref.unitName());
98       }
99    }
100
101
102    public static List JavaDoc loadInjectors(Container container) throws Exception JavaDoc
103    {
104       Class JavaDoc clazz = container.getBeanClass();
105       Method JavaDoc[] methods = clazz.getMethods();
106       ArrayList JavaDoc list = new ArrayList JavaDoc();
107       InitialContext JavaDoc ctx = new InitialContext JavaDoc();
108       Context JavaDoc resourceCtx = Util.createSubcontext(new InitialContext JavaDoc(), Container.ENC_CTX_NAME + "/env/resource");
109       for (int i = 0; i < methods.length; i++)
110       {
111          PersistenceUnit ref = (PersistenceUnit) ((Advisor) container).resolveAnnotation(methods[i], PersistenceUnit.class);
112          if (ref != null)
113          {
114             if (!methods[i].getName().startsWith("set")) throw new RuntimeException JavaDoc("@PersistenceContext can only be used with a set method: " + methods[i]);
115             if (methods[i].getParameterTypes().length != 1) throw new RuntimeException JavaDoc("@PersistenceContext can only be used with a set method of one parameter: " + methods[i]);
116             String JavaDoc encName = ref.name();
117             if (encName == null || encName.equals(""))
118             {
119                encName = methods[i].getName().substring(3);
120             }
121             EntityManagerFactory factory = getEntityManagerFactory(ref, container);
122             list.add(new EntityManagerFactoryMethodInjector(methods[i], container, factory));
123             bindEntityManagerFactoryToEnc(encName, factory);
124          }
125       }
126       loadPUsAnnotation(container);
127       loadFieldInjectors(clazz, container, list, resourceCtx);
128       return list;
129    }
130
131    private static void bindEntityManagerFactoryToEnc(String JavaDoc encName, EntityManagerFactory factory)
132            throws NamingException JavaDoc
133    {
134       Context JavaDoc entityCtx1 = Util.createSubcontext(new InitialContext JavaDoc(), Container.ENC_CTX_NAME + "/env");
135       Context JavaDoc entityCtx = entityCtx1;
136       entityCtx.bind(encName, factory);
137    }
138
139    private static void bindEntityManagerToEnc(ManagedEntityManagerFactory factory, String JavaDoc encName)
140            throws NamingException JavaDoc
141    {
142       Context JavaDoc entityCtx1 = Util.createSubcontext(new InitialContext JavaDoc(), Container.ENC_CTX_NAME + "/env");
143       Context JavaDoc entityCtx = entityCtx1;
144       InjectedEntityManager entityManager = new InjectedEntityManager(factory);
145       entityCtx.bind(encName, entityManager);
146    }
147
148    private static void loadFieldInjectors(Class JavaDoc clazz, Container container, ArrayList JavaDoc list, Context JavaDoc resourceCtx) throws Exception JavaDoc
149    {
150       if (clazz == null || clazz.equals(Object JavaDoc.class)) return;
151       loadFieldInjectors(clazz.getSuperclass(), container, list, resourceCtx);
152       Field JavaDoc[] fields = clazz.getDeclaredFields();
153       InitialContext JavaDoc ctx = new InitialContext JavaDoc();
154       for (int i = 0; i < fields.length; i++)
155       {
156          PersistenceUnit ref = (PersistenceUnit) ((Advisor) container).resolveAnnotation(fields[i], PersistenceUnit.class);
157          if (ref != null)
158          {
159             fields[i].setAccessible(true);
160             String JavaDoc encName = ref.name();
161             if (encName == null || encName.equals(""))
162             {
163                encName = fields[i].getName();
164             }
165             EntityManagerFactory factory = getEntityManagerFactory(ref, container);
166             list.add(new EntityManagerFactoryFieldInjector(fields[i], container, factory));
167             bindEntityManagerFactoryToEnc(encName, factory);
168          }
169       }
170    }
171 }
172
Popular Tags