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 import java.util.logging.Level ; 31 32 import java.util.Map ; 33 import java.util.HashMap ; 34 import java.util.Set ; 35 import java.util.HashSet ; 36 37 import javax.annotation.Resource; 38 39 import javax.jms.Queue ; 40 import javax.jms.Topic ; 41 42 import com.sun.enterprise.deployment.MethodDescriptor; 43 import com.sun.enterprise.deployment.ResourceReferenceDescriptor; 44 import com.sun.enterprise.deployment.MessageDestinationReferenceDescriptor; 45 import com.sun.enterprise.deployment.EnvironmentProperty; 46 import com.sun.enterprise.deployment.JmsDestinationReferenceDescriptor; 47 import com.sun.enterprise.deployment.InjectionTarget; 48 import com.sun.enterprise.deployment.annotation.AnnotatedElementHandler; 49 import com.sun.enterprise.deployment.annotation.AnnotationInfo; 50 import com.sun.enterprise.deployment.annotation.AnnotationProcessorException; 51 import com.sun.enterprise.deployment.annotation.HandlerProcessingResult; 52 import com.sun.enterprise.deployment.annotation.context.ResourceContainerContext; 53 import com.sun.enterprise.deployment.annotation.impl.AnnotationUtils; 54 55 59 public class ResourceHandler extends AbstractResourceHandler { 60 61 private static final Map <Class , Class > envEntryTypes; 64 65 static { 66 67 envEntryTypes = new HashMap <Class , Class >(); 68 69 envEntryTypes.put(String .class, String .class); 70 71 envEntryTypes.put(Character .class, Character .class); 72 envEntryTypes.put(Character.TYPE, Character .class); 73 74 envEntryTypes.put(Byte .class, Byte .class); 75 envEntryTypes.put(Byte.TYPE, Byte .class); 76 77 envEntryTypes.put(Short .class, Short .class); 78 envEntryTypes.put(Short.TYPE, Short .class); 79 80 envEntryTypes.put(Integer .class, Integer .class); 81 envEntryTypes.put(Integer.TYPE, Integer .class); 82 83 envEntryTypes.put(Long .class, Long .class); 84 envEntryTypes.put(Long.TYPE, Long .class); 85 86 envEntryTypes.put(Boolean .class, Boolean .class); 87 envEntryTypes.put(Boolean.TYPE, Boolean .class); 88 89 envEntryTypes.put(Double .class, Double .class); 90 envEntryTypes.put(Double.TYPE, Double .class); 91 92 envEntryTypes.put(Float .class, Float .class); 93 envEntryTypes.put(Float.TYPE, Float .class); 94 95 } 96 97 public ResourceHandler() { 98 } 99 100 103 public Class <? extends Annotation > getAnnotationType() { 104 return Resource.class; 105 } 106 107 111 protected HandlerProcessingResult processAnnotation(AnnotationInfo ainfo, 112 ResourceContainerContext[] rcContexts) 113 throws AnnotationProcessorException { 114 115 Resource resourceAn = (Resource)ainfo.getAnnotation(); 116 return processResource(ainfo, rcContexts, resourceAn); 117 } 118 119 protected HandlerProcessingResult processResource(AnnotationInfo ainfo, 120 ResourceContainerContext[] rcContexts, 121 Resource resourceAn) 122 throws AnnotationProcessorException { 123 124 ResourceReferenceDescriptor resourceRefs[] = null; 125 126 if (ElementType.FIELD.equals(ainfo.getElementType())) { 127 Field f = (Field )ainfo.getAnnotatedElement(); 128 String targetClassName = f.getDeclaringClass().getName(); 129 130 String logicalName = resourceAn.name(); 131 132 if (logicalName.equals("")) { 134 logicalName = targetClassName + "/" + f.getName(); 135 } 136 137 Class resourceType = (resourceAn.type() == Object .class) ? 140 f.getType() : resourceAn.type(); 141 142 DescriptorInfo descriptorInfo = getDescriptors 143 (resourceType, logicalName, rcContexts); 144 145 InjectionTarget target = new InjectionTarget(); 146 target.setFieldName(f.getName()); 147 target.setClassName(targetClassName); 148 149 for (EnvironmentProperty desc : descriptorInfo.descriptors) { 150 desc.addInjectionTarget(target); 151 152 if (desc.getName().length() == 0) { processNewAnnotation(desc, descriptorInfo.dependencyType, 154 descriptorInfo.resourceType, 155 logicalName, resourceAn); 156 } 157 } 158 } else if (ElementType.METHOD.equals(ainfo.getElementType())) { 159 160 Method m = (Method )ainfo.getAnnotatedElement(); 161 String targetClassName = m.getDeclaringClass().getName(); 162 163 String logicalName = resourceAn.name(); 164 if( logicalName.equals("") ) { 165 String propertyName = 167 getInjectionMethodPropertyName(m, ainfo); 168 169 logicalName = targetClassName + "/" + propertyName; 171 } 172 173 validateInjectionMethod(m, ainfo); 174 175 Class [] params = m.getParameterTypes(); 176 Class resourceType = (resourceAn.type() == Object .class) ? 179 params[0] : resourceAn.type(); 180 181 182 DescriptorInfo descriptorInfo = getDescriptors 183 (resourceType, logicalName, rcContexts); 184 185 InjectionTarget target = new InjectionTarget(); 186 target.setMethodName(m.getName()); 187 target.setClassName(targetClassName); 188 189 for (EnvironmentProperty desc : descriptorInfo.descriptors) { 190 desc.addInjectionTarget(target); 191 192 if (desc.getName().length() == 0) { processNewAnnotation(desc, 194 descriptorInfo.dependencyType, 195 descriptorInfo.resourceType, 196 logicalName, resourceAn); 197 } 198 } 199 200 } else if( ElementType.TYPE.equals(ainfo.getElementType()) ) { 201 String logicalName = resourceAn.name(); 203 Class resourceType = resourceAn.type(); 204 205 if( "".equals(logicalName) || resourceType == Object .class ) { 206 Class c = (Class ) ainfo.getAnnotatedElement(); 207 log(Level.SEVERE, ainfo, 208 localStrings.getLocalString( 209 "enterprise.deployment.annotation.handlers.invalidtypelevelresource", 210 "Invalid TYPE-level @Resource with name() = [{0}] and type = [{1}] in {2}. Each TYPE-level @Resource must specify both name() and type().", 211 new Object [] { logicalName, resourceType, c })); 212 return getDefaultFailedResult(); 213 } 214 215 DescriptorInfo descriptorInfo = getDescriptors 216 (resourceType, logicalName, rcContexts); 217 218 for (EnvironmentProperty desc : descriptorInfo.descriptors) { 219 220 if (desc.getName().length() == 0) { processNewAnnotation(desc, 222 descriptorInfo.dependencyType, 223 descriptorInfo.resourceType, 224 logicalName, resourceAn); 225 } 226 } 227 } 228 229 return getDefaultProcessedResult(); 230 } 231 232 private class DescriptorInfo { 233 234 public EnvironmentProperty[] descriptors; 235 public DependencyType dependencyType; 236 public Class resourceType; 237 } 238 239 240 private enum DependencyType { 241 ENV_ENTRY, 242 RESOURCE_REF, 243 MESSAGE_DESTINATION_REF, 244 RESOURCE_ENV_REF 245 } 246 247 248 249 private DescriptorInfo getDescriptors(Class resourceType, 250 String logicalName, ResourceContainerContext[] rcContexts) { 251 252 DescriptorInfo descriptorInfo = new DescriptorInfo(); 253 descriptorInfo.dependencyType = DependencyType.RESOURCE_REF; 254 descriptorInfo.resourceType = resourceType; 255 256 if( (resourceType == javax.jms.Queue .class) || 257 (resourceType == javax.jms.Topic .class) ) { 258 259 descriptorInfo.descriptors = 260 getMessageDestinationReferenceDescriptors 261 (logicalName, rcContexts); 262 descriptorInfo.dependencyType = 263 DependencyType.MESSAGE_DESTINATION_REF; 264 } else if ( resourceType == javax.ejb.SessionContext .class || 265 resourceType == javax.ejb.MessageDrivenContext .class || 266 resourceType == javax.ejb.EJBContext .class || 267 resourceType == javax.ejb.TimerService .class) { 268 descriptorInfo.descriptors = 269 getJmsDestinationReferenceDescriptors 270 (logicalName, rcContexts); 271 descriptorInfo.dependencyType = 272 DependencyType.RESOURCE_ENV_REF; 273 } else if( envEntryTypes.containsKey(resourceType) ) { 274 descriptorInfo.descriptors = getEnvironmentPropertyDescriptors 275 (logicalName, rcContexts); 276 descriptorInfo.dependencyType = DependencyType.ENV_ENTRY; 277 descriptorInfo.resourceType = envEntryTypes.get(resourceType); 281 } else { 282 descriptorInfo.descriptors = getResourceReferenceDescriptors 283 (logicalName, rcContexts); 284 } 285 286 return descriptorInfo; 287 } 288 289 296 private ResourceReferenceDescriptor[] getResourceReferenceDescriptors( 297 String logicalName, ResourceContainerContext[] rcContexts) { 298 ResourceReferenceDescriptor resourceRefs[] = 299 new ResourceReferenceDescriptor[rcContexts.length]; 300 for (int i = 0; i < rcContexts.length; i++) { 301 ResourceReferenceDescriptor resourceRef = 302 rcContexts[i].getResourceReference(logicalName); 303 if (resourceRef == null) { 304 resourceRef = new ResourceReferenceDescriptor(); 305 rcContexts[i].addResourceReferenceDescriptor(resourceRef); 306 } 307 resourceRefs[i] = resourceRef; 308 } 309 310 return resourceRefs; 311 } 312 313 320 private MessageDestinationReferenceDescriptor[] 321 getMessageDestinationReferenceDescriptors 322 (String logicName, ResourceContainerContext[] rcContexts) { 323 324 MessageDestinationReferenceDescriptor msgDestRefs[] = 325 new MessageDestinationReferenceDescriptor[rcContexts.length]; 326 for (int i = 0; i < rcContexts.length; i++) { 327 MessageDestinationReferenceDescriptor msgDestRef = 328 rcContexts[i].getMessageDestinationReference(logicName); 329 if (msgDestRef == null) { 330 msgDestRef = new MessageDestinationReferenceDescriptor(); 331 rcContexts[i].addMessageDestinationReferenceDescriptor( 332 msgDestRef); 333 } 334 msgDestRefs[i] = msgDestRef; 335 } 336 337 return msgDestRefs; 338 } 339 340 347 private JmsDestinationReferenceDescriptor[] 348 getJmsDestinationReferenceDescriptors 349 (String logicName, ResourceContainerContext[] rcContexts) { 350 351 JmsDestinationReferenceDescriptor jmsDestRefs[] = 352 new JmsDestinationReferenceDescriptor[rcContexts.length]; 353 for (int i = 0; i < rcContexts.length; i++) { 354 JmsDestinationReferenceDescriptor jmsDestRef = 355 rcContexts[i].getJmsDestinationReference(logicName); 356 if (jmsDestRef == null) { 357 jmsDestRef = new JmsDestinationReferenceDescriptor(); 358 rcContexts[i].addJmsDestinationReferenceDescriptor( 359 jmsDestRef); 360 } 361 jmsDestRefs[i] = jmsDestRef; 362 } 363 364 return jmsDestRefs; 365 } 366 367 374 private EnvironmentProperty[] getEnvironmentPropertyDescriptors 375 (String logicalName, ResourceContainerContext[] rcContexts) { 376 377 Set <EnvironmentProperty> envEntriesSet = 378 new HashSet <EnvironmentProperty>(); 379 380 for (int i = 0; i < rcContexts.length; i++) { 381 EnvironmentProperty envEntry = 382 rcContexts[i].getEnvEntry(logicalName); 383 if ((envEntry != null) && (envEntry.hasAValue()) ) { 389 envEntriesSet.add(envEntry); 390 } 391 } 392 393 return envEntriesSet.toArray(new EnvironmentProperty[] {}); 394 } 395 396 private void processNewAnnotation(EnvironmentProperty desc, 397 DependencyType dependencyType, 398 Class resourceType, 399 String logicalName, Resource annotation){ 400 401 desc.setName(logicalName); 402 desc.setDescription(annotation.description()); 403 404 if( dependencyType == DependencyType.ENV_ENTRY ) { 405 406 desc.setType(resourceType.getName()); 407 desc.setMappedName(annotation.mappedName()); 408 409 } else if( dependencyType == DependencyType.MESSAGE_DESTINATION_REF ) { 410 411 MessageDestinationReferenceDescriptor msgDestRef = 412 (MessageDestinationReferenceDescriptor) desc; 413 msgDestRef.setDestinationType(resourceType.getName()); 414 msgDestRef.setMappedName(annotation.mappedName()); 415 } else if( dependencyType == DependencyType.RESOURCE_ENV_REF ) { 416 417 JmsDestinationReferenceDescriptor jmsDestRef = 418 (JmsDestinationReferenceDescriptor) desc; 419 jmsDestRef.setRefType(resourceType.getName()); 420 jmsDestRef.setMappedName(annotation.mappedName()); 421 } else if( dependencyType == DependencyType.RESOURCE_REF ) { 422 423 desc.setType(resourceType.getName()); 424 425 ResourceReferenceDescriptor resRef = (ResourceReferenceDescriptor) 426 desc; 427 428 String authType = 429 (annotation.authenticationType() == 430 Resource.AuthenticationType.CONTAINER) ? 431 ResourceReferenceDescriptor.CONTAINER_AUTHORIZATION : 432 ResourceReferenceDescriptor.APPLICATION_AUTHORIZATION; 433 434 resRef.setAuthorization(authType); 435 436 String sharable = annotation.shareable() ? 437 ResourceReferenceDescriptor.RESOURCE_SHAREABLE : 438 ResourceReferenceDescriptor.RESOURCE_UNSHAREABLE; 439 440 resRef.setSharingScope(sharable); 441 resRef.setMappedName(annotation.mappedName()); 442 } 443 444 return; 445 } 446 } 447 | Popular Tags |