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.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 36 public class PersistenceUnitHandler 37 { 38 private static void loadPUsAnnotation(Container container) throws Exception 39 { 40 PersistenceUnits resources = (PersistenceUnits) ((Advisor) container).resolveAnnotation(PersistenceUnits.class); 41 if (resources == null) return; 42 for (PersistenceUnit 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 @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 encName = pu.name(); 57 if (encName == null || encName.equals("")) 58 { 59 throw new RuntimeException ("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 69 { 70 ManagedEntityManagerFactory factory; 71 if (ref.unitName() == null || ref.unitName().equals("")) 72 { 73 factory = container.getManagedEntityManagerFactory(); 74 } 75 else 76 { 77 InitialContext ctx = new InitialContext (); 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 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 ctx = new InitialContext (); 96 factory = (EntityManagerFactory) ctx.lookup("java:/" + ref.unitName()); 97 return new InjectedEntityManagerFactory(factory, "java:/" + ref.unitName()); 98 } 99 } 100 101 102 public static List loadInjectors(Container container) throws Exception 103 { 104 Class clazz = container.getBeanClass(); 105 Method [] methods = clazz.getMethods(); 106 ArrayList list = new ArrayList (); 107 InitialContext ctx = new InitialContext (); 108 Context resourceCtx = Util.createSubcontext(new InitialContext (), 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 ("@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 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 encName, EntityManagerFactory factory) 132 throws NamingException 133 { 134 Context entityCtx1 = Util.createSubcontext(new InitialContext (), Container.ENC_CTX_NAME + "/env"); 135 Context entityCtx = entityCtx1; 136 entityCtx.bind(encName, factory); 137 } 138 139 private static void bindEntityManagerToEnc(ManagedEntityManagerFactory factory, String encName) 140 throws NamingException 141 { 142 Context entityCtx1 = Util.createSubcontext(new InitialContext (), Container.ENC_CTX_NAME + "/env"); 143 Context entityCtx = entityCtx1; 144 InjectedEntityManager entityManager = new InjectedEntityManager(factory); 145 entityCtx.bind(encName, entityManager); 146 } 147 148 private static void loadFieldInjectors(Class clazz, Container container, ArrayList list, Context resourceCtx) throws Exception 149 { 150 if (clazz == null || clazz.equals(Object .class)) return; 151 loadFieldInjectors(clazz.getSuperclass(), container, list, resourceCtx); 152 Field [] fields = clazz.getDeclaredFields(); 153 InitialContext ctx = new InitialContext (); 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 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 |