1 7 package org.jboss.ejb3.injection; 8 9 import java.lang.reflect.Field ; 10 import java.lang.reflect.Method ; 11 import java.util.ArrayList ; 12 import java.util.List ; 13 import javax.naming.Context ; 14 import javax.naming.InitialContext ; 15 import javax.naming.NamingException ; 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 36 public class PersistenceContextHandler 37 { 38 private static void loadResourcesAnnotation(Container container) throws Exception 39 { 40 PersistenceContexts resources = (PersistenceContexts) ((Advisor) container).resolveAnnotation(PersistenceContexts.class); 41 if (resources == null) return; 42 for (PersistenceContext ref : resources.value()) 43 { 44 String encName = ref.name(); 45 if (encName == null || encName.equals("")) 46 { 47 throw new RuntimeException ("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 encName = pc.name(); 57 if (encName == null || encName.equals("")) 58 { 59 throw new RuntimeException ("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 70 { 71 ManagedEntityManagerFactory factory; 72 if (ref.unitName() == null || ref.unitName().equals("")) 73 { 74 factory = container.getManagedEntityManagerFactory(); 75 } 76 else 77 { 78 InitialContext ctx = new InitialContext (); 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 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 ctx = new InitialContext (); 97 factory = (EntityManagerFactory) ctx.lookup("java:/" + ref.name()); 98 return new InjectedEntityManagerFactory(factory, "java:/" + ref.name()); 99 } 100 } 101 102 103 public static List loadInjectors(Container container) throws Exception 104 { 105 Class clazz = container.getBeanClass(); 106 Method [] methods = clazz.getMethods(); 107 ArrayList list = new ArrayList (); 108 Context resourceCtx = Util.createSubcontext(new InitialContext (), 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 ("@PersistenceContext can only be used with a set method: " + methods[i]); 115 if (methods[i].getParameterTypes().length != 1) throw new RuntimeException ("@PersistenceContext can only be used with a set method of one parameter: " + methods[i]); 116 String 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 encName) 132 throws NamingException 133 { 134 Context entityCtx1 = Util.createSubcontext(new InitialContext (), Container.ENC_CTX_NAME + "/env"); 135 Context entityCtx = entityCtx1; 136 InjectedEntityManager entityManager = new InjectedEntityManager(factory); 137 entityCtx.bind(encName, entityManager); 138 } 139 140 private static void loadFieldInjectors(Class clazz, Container container, ArrayList list, Context resourceCtx) throws Exception 141 { 142 if (clazz == null || clazz.equals(Object .class)) return; 143 loadFieldInjectors(clazz.getSuperclass(), container, list, resourceCtx); 144 Field [] 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 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 |