1 23 package com.sun.enterprise.deployment.annotation.handlers; 24 25 import java.lang.annotation.Annotation ; 26 import java.lang.annotation.ElementType ; 27 import java.lang.reflect.AnnotatedElement ; 28 import java.lang.reflect.Field ; 29 import java.lang.reflect.Method ; 30 31 import java.util.logging.Level ; 32 33 import javax.persistence.PersistenceUnit; 34 35 import com.sun.enterprise.deployment.EntityManagerFactoryReferenceDescriptor; 36 import com.sun.enterprise.deployment.InjectionTarget; 37 38 import com.sun.enterprise.deployment.annotation.AnnotatedElementHandler; 39 import com.sun.enterprise.deployment.annotation.AnnotationInfo; 40 import com.sun.enterprise.deployment.annotation.AnnotationProcessorException; 41 import com.sun.enterprise.deployment.annotation.HandlerProcessingResult; 42 import com.sun.enterprise.deployment.annotation.context.ResourceContainerContext; 43 import com.sun.enterprise.deployment.annotation.impl.AnnotationUtils; 44 45 50 public class EntityManagerFactoryReferenceHandler 51 extends AbstractResourceHandler { 52 53 public EntityManagerFactoryReferenceHandler() { 54 } 55 56 59 public Class <? extends Annotation > getAnnotationType() { 60 return PersistenceUnit.class; 61 } 62 63 73 protected HandlerProcessingResult processAnnotation(AnnotationInfo ainfo, 74 ResourceContainerContext[] rcContexts) 75 throws AnnotationProcessorException { 76 77 PersistenceUnit emfRefAn = (PersistenceUnit)ainfo.getAnnotation(); 78 return processEmfRef(ainfo, rcContexts, emfRefAn); 79 } 80 81 82 89 protected HandlerProcessingResult processEmfRef(AnnotationInfo ainfo, 90 ResourceContainerContext[] rcContexts, PersistenceUnit emfRefAn) 91 throws AnnotationProcessorException { 92 EntityManagerFactoryReferenceDescriptor emfRefs[] = null; 93 94 if (ElementType.FIELD.equals(ainfo.getElementType())) { 95 Field f = (Field )ainfo.getAnnotatedElement(); 96 String targetClassName = f.getDeclaringClass().getName(); 97 98 String logicalName = emfRefAn.name(); 99 100 if (logicalName.equals("")) { 102 logicalName = targetClassName + "/" + f.getName(); 103 } 104 105 emfRefs = getEmfReferenceDescriptors(logicalName, rcContexts); 106 107 InjectionTarget target = new InjectionTarget(); 108 target.setFieldName(f.getName()); 109 target.setClassName(targetClassName); 110 for (EntityManagerFactoryReferenceDescriptor emfRef : emfRefs) { 111 emfRef.addInjectionTarget(target); 112 113 if (emfRef.getName().length() == 0) { processNewEmfRefAnnotation(emfRef, logicalName, emfRefAn); 115 } 116 } 117 } else if (ElementType.METHOD.equals(ainfo.getElementType())) { 118 119 Method m = (Method )ainfo.getAnnotatedElement(); 120 String targetClassName = m.getDeclaringClass().getName(); 121 122 String logicalName = emfRefAn.name(); 123 if( logicalName.equals("") ) { 124 String propertyName = 126 getInjectionMethodPropertyName(m, ainfo); 127 128 logicalName = targetClassName + "/" + propertyName; 130 } 131 132 validateInjectionMethod(m, ainfo); 133 134 emfRefs = getEmfReferenceDescriptors(logicalName, rcContexts); 135 136 InjectionTarget target = new InjectionTarget(); 137 target.setMethodName(m.getName()); 138 target.setClassName(targetClassName); 139 for (EntityManagerFactoryReferenceDescriptor emfRef : emfRefs) { 140 141 emfRef.addInjectionTarget(target); 142 143 if (emfRef.getName().length() == 0) { 145 processNewEmfRefAnnotation(emfRef, logicalName, emfRefAn); 146 147 } 148 } 149 } else if( ElementType.TYPE.equals(ainfo.getElementType()) ) { 150 String logicalName = emfRefAn.name(); 152 153 if( "".equals(logicalName) ) { 154 Class c = (Class ) ainfo.getAnnotatedElement(); 155 log(Level.SEVERE, ainfo, 156 localStrings.getLocalString( 157 "enterprise.deployment.annotation.handlers.nonametypelevel", 158 "TYPE-Level annotation symbol on class must specify name.")); 159 return getDefaultFailedResult(); 160 } 161 162 emfRefs = getEmfReferenceDescriptors(logicalName, rcContexts); 163 for (EntityManagerFactoryReferenceDescriptor emfRef : emfRefs) { 164 if (emfRef.getName().length() == 0) { 166 processNewEmfRefAnnotation(emfRef, logicalName, emfRefAn); 167 168 } 169 } 170 } 171 172 return getDefaultProcessedResult(); 173 } 174 175 179 private EntityManagerFactoryReferenceDescriptor[] 180 getEmfReferenceDescriptors(String logicalName, 181 ResourceContainerContext[] rcContexts) { 182 183 EntityManagerFactoryReferenceDescriptor emfRefs[] = 184 new EntityManagerFactoryReferenceDescriptor[rcContexts.length]; 185 for (int i = 0; i < rcContexts.length; i++) { 186 EntityManagerFactoryReferenceDescriptor emfRef = 187 (EntityManagerFactoryReferenceDescriptor)rcContexts[i]. 188 getEntityManagerFactoryReference(logicalName); 189 if (emfRef == null) { 190 emfRef = new EntityManagerFactoryReferenceDescriptor(); 191 rcContexts[i].addEntityManagerFactoryReferenceDescriptor 192 (emfRef); 193 } 194 emfRefs[i] = emfRef; 195 } 196 197 return emfRefs; 198 } 199 200 private void processNewEmfRefAnnotation 201 (EntityManagerFactoryReferenceDescriptor emfRef, 202 String logicalName, PersistenceUnit annotation) { 203 204 emfRef.setName(logicalName); 205 206 if( !(annotation.unitName().equals("")) ) { 207 emfRef.setUnitName(annotation.unitName()); 208 } 209 210 } 211 212 } 213 | Popular Tags |