1 23 package com.sun.enterprise.deployment.annotation.handlers; 24 25 import java.util.Map ; 26 27 import java.lang.annotation.Annotation ; 28 import java.lang.annotation.ElementType ; 29 import java.lang.reflect.AnnotatedElement ; 30 import java.lang.reflect.Field ; 31 import java.lang.reflect.Method ; 32 33 import java.util.logging.Level ; 34 35 import javax.persistence.PersistenceContext; 36 import javax.persistence.PersistenceProperty; 37 38 import com.sun.enterprise.deployment.EntityManagerReferenceDescriptor; 39 import com.sun.enterprise.deployment.InjectionTarget; 40 41 import com.sun.enterprise.deployment.annotation.AnnotatedElementHandler; 42 import com.sun.enterprise.deployment.annotation.AnnotationInfo; 43 import com.sun.enterprise.deployment.annotation.AnnotationProcessorException; 44 import com.sun.enterprise.deployment.annotation.HandlerProcessingResult; 45 import com.sun.enterprise.deployment.annotation.context.ResourceContainerContext; 46 import com.sun.enterprise.deployment.annotation.context.AppClientContext; 47 import com.sun.enterprise.deployment.annotation.impl.AnnotationUtils; 48 49 54 public class EntityManagerReferenceHandler 55 extends AbstractResourceHandler { 56 57 public EntityManagerReferenceHandler() { 58 } 59 60 63 public Class <? extends Annotation > getAnnotationType() { 64 return PersistenceContext.class; 65 } 66 67 77 protected HandlerProcessingResult processAnnotation(AnnotationInfo ainfo, 78 ResourceContainerContext[] rcContexts) 79 throws AnnotationProcessorException { 80 81 AnnotatedElementHandler aeHandler = 82 ainfo.getProcessingContext().getHandler(); 83 if (aeHandler instanceof AppClientContext) { 84 String msg = localStrings.getLocalString( 86 "enterprise.deployment.annotation.handlers.invalidaehandler", 87 "Invalid annotation symbol found for this type of class."); 88 log(Level.SEVERE, ainfo, msg); 89 return getDefaultProcessedResult(); 90 } 91 PersistenceContext emRefAn = (PersistenceContext)ainfo.getAnnotation(); 92 return processEmRef(ainfo, rcContexts, emRefAn); 93 } 94 95 96 103 protected HandlerProcessingResult processEmRef(AnnotationInfo ainfo, 104 ResourceContainerContext[] rcContexts, PersistenceContext emRefAn) 105 throws AnnotationProcessorException { 106 EntityManagerReferenceDescriptor emRefs[] = null; 107 108 if (ElementType.FIELD.equals(ainfo.getElementType())) { 109 Field f = (Field )ainfo.getAnnotatedElement(); 110 String targetClassName = f.getDeclaringClass().getName(); 111 112 String logicalName = emRefAn.name(); 113 114 if (logicalName.equals("")) { 116 logicalName = targetClassName + "/" + f.getName(); 117 } 118 119 emRefs = getEmReferenceDescriptors(logicalName, rcContexts); 120 121 InjectionTarget target = new InjectionTarget(); 122 target.setFieldName(f.getName()); 123 target.setClassName(targetClassName); 124 for (EntityManagerReferenceDescriptor emRef : emRefs) { 125 126 emRef.addInjectionTarget(target); 127 128 if (emRef.getName().length() == 0) { processNewEmRefAnnotation(emRef, logicalName, emRefAn); 130 } 131 } 132 } else if (ElementType.METHOD.equals(ainfo.getElementType())) { 133 134 Method m = (Method )ainfo.getAnnotatedElement(); 135 String targetClassName = m.getDeclaringClass().getName(); 136 137 String logicalName = emRefAn.name(); 138 if( logicalName.equals("") ) { 139 String propertyName = 141 getInjectionMethodPropertyName(m, ainfo); 142 143 logicalName = targetClassName + "/" + propertyName; 145 } 146 147 validateInjectionMethod(m, ainfo); 148 149 emRefs = getEmReferenceDescriptors(logicalName, rcContexts); 150 151 InjectionTarget target = new InjectionTarget(); 152 target.setMethodName(m.getName()); 153 target.setClassName(targetClassName); 154 for (EntityManagerReferenceDescriptor emRef : emRefs) { 155 156 emRef.addInjectionTarget(target); 157 158 if (emRef.getName().length() == 0) { 160 processNewEmRefAnnotation(emRef, logicalName, emRefAn); 161 162 } 163 } 164 } else if( ElementType.TYPE.equals(ainfo.getElementType()) ) { 165 String logicalName = emRefAn.name(); 167 168 if( "".equals(logicalName) ) { 169 Class c = (Class ) ainfo.getAnnotatedElement(); 170 log(Level.SEVERE, ainfo, 171 localStrings.getLocalString( 172 "enterprise.deployment.annotation.handlers.nonametypelevel", 173 "TYPE-Level annotation symbol on class must specify name.")); 174 return getDefaultFailedResult(); 175 } 176 177 emRefs = getEmReferenceDescriptors(logicalName, rcContexts); 178 for (EntityManagerReferenceDescriptor emRef : emRefs) { 179 if (emRef.getName().length() == 0) { 181 processNewEmRefAnnotation(emRef, logicalName, emRefAn); 182 183 } 184 } 185 } 186 187 return getDefaultProcessedResult(); 188 } 189 190 194 private EntityManagerReferenceDescriptor[] 195 getEmReferenceDescriptors(String logicalName, 196 ResourceContainerContext[] rcContexts) { 197 198 EntityManagerReferenceDescriptor emRefs[] = 199 new EntityManagerReferenceDescriptor[rcContexts.length]; 200 for (int i = 0; i < rcContexts.length; i++) { 201 EntityManagerReferenceDescriptor emRef = 202 (EntityManagerReferenceDescriptor)rcContexts[i]. 203 getEntityManagerReference(logicalName); 204 if (emRef == null) { 205 emRef = new EntityManagerReferenceDescriptor(); 206 rcContexts[i].addEntityManagerReferenceDescriptor 207 (emRef); 208 } 209 emRefs[i] = emRef; 210 } 211 212 return emRefs; 213 } 214 215 private void processNewEmRefAnnotation 216 (EntityManagerReferenceDescriptor emRef, 217 String logicalName, PersistenceContext annotation) { 218 219 emRef.setName(logicalName); 220 221 if( !(annotation.unitName().equals("")) ) { 222 emRef.setUnitName(annotation.unitName()); 223 } 224 225 emRef.setPersistenceContextType(annotation.type()); 226 227 Map existingProperties = emRef.getProperties(); 230 231 for(PersistenceProperty next : annotation.properties()) { 232 if( !existingProperties.containsKey(next.name()) ) { 233 emRef.addProperty(next.name(), next.value()); 234 } 235 } 236 237 } 238 239 } 240 | Popular Tags |