KickJava   Java API By Example, From Geeks To Geeks.

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


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.PersistenceContext;
18 import javax.persistence.PersistenceContext;
19 import javax.persistence.PersistenceContexts;
20 import javax.persistence.PersistenceContext;
21 import javax.persistence.PersistenceContexts;
22 import javax.persistence.PersistenceContexts;
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 PersistenceContextHandler
37 {
38    private static void loadResourcesAnnotation(Container container) throws Exception JavaDoc
39    {
40       PersistenceContexts resources = (PersistenceContexts) ((Advisor) container).resolveAnnotation(PersistenceContexts.class);
41       if (resources == null) return;
42       for (PersistenceContext 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 @PersistenceContext");
48          }
49
50          ManagedEntityManagerFactory factory = getManagedEntityManagerFactory(ref, container);
51          bindEntityManagerToEnc(factory, encName);
52       }
53       PersistenceContext pc = (PersistenceContext)((Advisor)container).resolveAnnotation(PersistenceContext.class);
54       if (pc != null)
55       {
56          String JavaDoc encName = pc.name();
57          if (encName == null || encName.equals(""))
58          {
59             throw new RuntimeException JavaDoc("JBoss requires name() for class level @PersistenceContext");
60          }
61
62          ManagedEntityManagerFactory factory = getManagedEntityManagerFactory(pc, container);
63          bindEntityManagerToEnc(factory, encName);
64       }
65
66    }
67
68    public static ManagedEntityManagerFactory getManagedEntityManagerFactory(PersistenceContext ref, Container container)
69            throws NamingException JavaDoc
70    {
71       ManagedEntityManagerFactory factory;
72       if (ref.unitName() == null || ref.unitName().equals(""))
73       {
74          factory = container.getManagedEntityManagerFactory();
75       }
76       else
77       {
78          InitialContext JavaDoc ctx = new InitialContext JavaDoc();
79          factory = (ManagedEntityManagerFactory) ctx.lookup("java:/managedEntityFactory." + ref.unitName());
80       }
81       return factory;
82    }
83
84
85    public static EntityManagerFactory getEntityManagerFactory(PersistenceContext ref, Container container)
86            throws NamingException JavaDoc
87    {
88       EntityManagerFactory factory;
89       if (ref.name() == null || ref.name().equals(""))
90       {
91          factory = container.getManagedEntityManagerFactory().getEntityManagerFactory();
92          return new InjectedEntityManagerFactory(factory);
93       }
94       else
95       {
96          InitialContext JavaDoc ctx = new InitialContext JavaDoc();
97          factory = (EntityManagerFactory) ctx.lookup("java:/" + ref.name());
98          return new InjectedEntityManagerFactory(factory, "java:/" + ref.name());
99       }
100    }
101
102
103    public static List JavaDoc loadInjectors(Container container) throws Exception JavaDoc
104    {
105       Class JavaDoc clazz = container.getBeanClass();
106       Method JavaDoc[] methods = clazz.getMethods();
107       ArrayList JavaDoc list = new ArrayList JavaDoc();
108       Context JavaDoc resourceCtx = Util.createSubcontext(new InitialContext JavaDoc(), Container.ENC_CTX_NAME + "/env");
109       for (int i = 0; i < methods.length; i++)
110       {
111          PersistenceContext ref = (PersistenceContext) ((Advisor) container).resolveAnnotation(methods[i], PersistenceContext.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             ManagedEntityManagerFactory factory = getManagedEntityManagerFactory(ref, container);
122             list.add(new EntityManagerMethodInjector(methods[i], container, factory, ref.type()));
123             bindEntityManagerToEnc(factory, encName);
124          }
125       }
126       loadResourcesAnnotation(container);
127       loadFieldInjectors(clazz, container, list, resourceCtx);
128       return list;
129    }
130
131    private static void bindEntityManagerToEnc(ManagedEntityManagerFactory factory, String JavaDoc encName)
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       InjectedEntityManager entityManager = new InjectedEntityManager(factory);
137       entityCtx.bind(encName, entityManager);
138    }
139
140    private static void loadFieldInjectors(Class JavaDoc clazz, Container container, ArrayList JavaDoc list, Context JavaDoc resourceCtx) throws Exception JavaDoc
141    {
142       if (clazz == null || clazz.equals(Object JavaDoc.class)) return;
143       loadFieldInjectors(clazz.getSuperclass(), container, list, resourceCtx);
144       Field JavaDoc[] fields = clazz.getDeclaredFields();
145       for (int i = 0; i < fields.length; i++)
146       {
147          PersistenceContext ref = (PersistenceContext) ((Advisor) container).resolveAnnotation(fields[i], PersistenceContext.class);
148          if (ref != null)
149          {
150             fields[i].setAccessible(true);
151             String JavaDoc encName = ref.name();
152             if (encName == null || encName.equals(""))
153             {
154                encName = fields[i].getName();
155             }
156             ManagedEntityManagerFactory factory = getManagedEntityManagerFactory(ref, container);
157             list.add(new EntityManagerFieldInjector(fields[i], container, factory, ref.type()));
158             bindEntityManagerToEnc(factory, encName);
159          }
160       }
161    }
162 }
163
Popular Tags