KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > deployment > annotation > handlers > ResourceHandler


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23 package com.sun.enterprise.deployment.annotation.handlers;
24
25 import java.lang.annotation.Annotation JavaDoc;
26 import java.lang.annotation.ElementType JavaDoc;
27 import java.lang.reflect.AnnotatedElement JavaDoc;
28 import java.lang.reflect.Field JavaDoc;
29 import java.lang.reflect.Method JavaDoc;
30 import java.util.logging.Level JavaDoc;
31
32 import java.util.Map JavaDoc;
33 import java.util.HashMap JavaDoc;
34 import java.util.Set JavaDoc;
35 import java.util.HashSet JavaDoc;
36
37 import javax.annotation.Resource;
38
39 import javax.jms.Queue JavaDoc;
40 import javax.jms.Topic JavaDoc;
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 /**
56  * This handler is responsible for handling the javax.annotation.Resource
57  *
58  */

59 public class ResourceHandler extends AbstractResourceHandler {
60
61     // Map of all @Resource types that map to env-entries and their
62
// corresponding types.
63
private static final Map JavaDoc<Class JavaDoc, Class JavaDoc> envEntryTypes;
64
65     static {
66
67         envEntryTypes = new HashMap JavaDoc<Class JavaDoc, Class JavaDoc>();
68
69         envEntryTypes.put(String JavaDoc.class, String JavaDoc.class);
70
71         envEntryTypes.put(Character JavaDoc.class, Character JavaDoc.class);
72         envEntryTypes.put(Character.TYPE, Character JavaDoc.class);
73
74         envEntryTypes.put(Byte JavaDoc.class, Byte JavaDoc.class);
75         envEntryTypes.put(Byte.TYPE, Byte JavaDoc.class);
76
77         envEntryTypes.put(Short JavaDoc.class, Short JavaDoc.class);
78         envEntryTypes.put(Short.TYPE, Short JavaDoc.class);
79
80         envEntryTypes.put(Integer JavaDoc.class, Integer JavaDoc.class);
81         envEntryTypes.put(Integer.TYPE, Integer JavaDoc.class);
82
83         envEntryTypes.put(Long JavaDoc.class, Long JavaDoc.class);
84         envEntryTypes.put(Long.TYPE, Long JavaDoc.class);
85
86         envEntryTypes.put(Boolean JavaDoc.class, Boolean JavaDoc.class);
87         envEntryTypes.put(Boolean.TYPE, Boolean JavaDoc.class);
88
89         envEntryTypes.put(Double JavaDoc.class, Double JavaDoc.class);
90         envEntryTypes.put(Double.TYPE, Double JavaDoc.class);
91
92         envEntryTypes.put(Float JavaDoc.class, Float JavaDoc.class);
93         envEntryTypes.put(Float.TYPE, Float JavaDoc.class);
94
95     }
96         
97     public ResourceHandler() {
98     }
99
100     /**
101      * @return the annoation type this annotation handler is handling
102      */

103     public Class JavaDoc<? extends Annotation JavaDoc> getAnnotationType() {
104         return Resource.class;
105     }
106
107     /**
108      * This entry point is used both for a single @EJB and iteratively
109      * from a compound @EJBs processor.
110      */

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 JavaDoc f = (Field JavaDoc)ainfo.getAnnotatedElement();
128             String JavaDoc targetClassName = f.getDeclaringClass().getName();
129
130             String JavaDoc logicalName = resourceAn.name();
131
132             // applying with default
133
if (logicalName.equals("")) {
134                 logicalName = targetClassName + "/" + f.getName();
135             }
136
137             // If specified, beanInterface() overrides parameter type
138
// NOTE that default value is Object.class, not null
139
Class JavaDoc resourceType = (resourceAn.type() == Object JavaDoc.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) { // a new one
153
processNewAnnotation(desc, descriptorInfo.dependencyType,
154                                          descriptorInfo.resourceType,
155                                          logicalName, resourceAn);
156                 }
157             }
158         } else if (ElementType.METHOD.equals(ainfo.getElementType())) {
159
160             Method JavaDoc m = (Method JavaDoc)ainfo.getAnnotatedElement();
161             String JavaDoc targetClassName = m.getDeclaringClass().getName();
162
163             String JavaDoc logicalName = resourceAn.name();
164             if( logicalName.equals("") ) {
165                 // Derive javabean property name.
166
String JavaDoc propertyName =
167                         getInjectionMethodPropertyName(m, ainfo);
168
169                 // prefixing with fully qualified type name
170
logicalName = targetClassName + "/" + propertyName;
171             }
172
173             validateInjectionMethod(m, ainfo);
174
175             Class JavaDoc[] params = m.getParameterTypes();
176             // If specified, beanInterface() overrides parameter type
177
// NOTE that default value is Object.class, not null
178
Class JavaDoc resourceType = (resourceAn.type() == Object JavaDoc.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) { // a new one
193
processNewAnnotation(desc,
194                                          descriptorInfo.dependencyType,
195                                          descriptorInfo.resourceType,
196                                          logicalName, resourceAn);
197                 }
198             }
199
200         } else if( ElementType.TYPE.equals(ainfo.getElementType()) ) {
201             // name() and type() are required for TYPE-level @Resource
202
String JavaDoc logicalName = resourceAn.name();
203             Class JavaDoc resourceType = resourceAn.type();
204
205             if( "".equals(logicalName) || resourceType == Object JavaDoc.class ) {
206                 Class JavaDoc c = (Class JavaDoc) 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 JavaDoc[] { 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) { // a new one
221
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 JavaDoc 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 JavaDoc resourceType,
250         String JavaDoc 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 JavaDoc.class) ||
257             (resourceType == javax.jms.Topic JavaDoc.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 JavaDoc.class ||
265                     resourceType == javax.ejb.MessageDrivenContext JavaDoc.class ||
266                     resourceType == javax.ejb.EJBContext JavaDoc.class ||
267                     resourceType == javax.ejb.TimerService JavaDoc.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             // Get corresponding class type. This does the appropriate
278
// mapping for primitives. For everything, the type is
279
// unchanged.
280
descriptorInfo.resourceType = envEntryTypes.get(resourceType);
281         } else {
282             descriptorInfo.descriptors = getResourceReferenceDescriptors
283                 (logicalName, rcContexts);
284         }
285
286         return descriptorInfo;
287     }
288
289     /**
290      * Return ResourceReferenceDescriptors with given name if exists or a new
291      * one without name being set.
292      * @param logicalName
293      * @param rcContexts
294      * @return an array of ResourceReferenceDescriptor
295      */

296     private ResourceReferenceDescriptor[] getResourceReferenceDescriptors(
297             String JavaDoc 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     /**
314      * Return MessageDestinationReferenceDescriptors with given name
315      * if exists or a new one without name being set.
316      * @param logicName
317      * @param rcContexts
318      * @return an array of message destination reference descriptors
319      */

320     private MessageDestinationReferenceDescriptor[]
321         getMessageDestinationReferenceDescriptors
322         (String JavaDoc 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     /**
341      * Return JmsDestinationReferenceDescriptors with given name
342      * if exists or a new one without name being set.
343      * @param logicName
344      * @param rcContexts
345      * @return an array of resource env reference descriptors
346      */

347     private JmsDestinationReferenceDescriptor[]
348         getJmsDestinationReferenceDescriptors
349         (String JavaDoc 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     /**
368      * Return EnvironmentProperty descriptors with given name
369      * if exists or a new one without name being set.
370      * @param logicalName
371      * @param rcContexts
372      * @return an array of EnvironmentProperty descriptors
373      */

374     private EnvironmentProperty[] getEnvironmentPropertyDescriptors
375         (String JavaDoc logicalName, ResourceContainerContext[] rcContexts) {
376             
377         Set JavaDoc<EnvironmentProperty> envEntriesSet =
378             new HashSet JavaDoc<EnvironmentProperty>();
379
380         for (int i = 0; i < rcContexts.length; i++) {
381             EnvironmentProperty envEntry =
382                 rcContexts[i].getEnvEntry(logicalName);
383             // For @Resource declarations that map to env-entries, if there
384
// is no corresponding deployment descriptor entry that has a
385
// value, it's treated as if the declaration doesn't exist.
386
// A common case is that the @Resource is applied to a field
387
// with a default value which was not overridden by the deployer.
388
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 JavaDoc resourceType,
399                                       String JavaDoc 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 JavaDoc authType =
429                 (annotation.authenticationType() ==
430                  Resource.AuthenticationType.CONTAINER) ?
431                 ResourceReferenceDescriptor.CONTAINER_AUTHORIZATION :
432                 ResourceReferenceDescriptor.APPLICATION_AUTHORIZATION;
433             
434             resRef.setAuthorization(authType);
435             
436             String JavaDoc 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