KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.Map JavaDoc;
26
27 import java.lang.annotation.Annotation JavaDoc;
28 import java.lang.annotation.ElementType JavaDoc;
29 import java.lang.reflect.AnnotatedElement JavaDoc;
30 import java.lang.reflect.Field JavaDoc;
31 import java.lang.reflect.Method JavaDoc;
32
33 import java.util.logging.Level JavaDoc;
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 /**
50  * This handler is responsible for handling the
51  * javax.persistence.PersistenceUnit annotation.
52  *
53  */

54 public class EntityManagerReferenceHandler
55     extends AbstractResourceHandler {
56     
57     public EntityManagerReferenceHandler() {
58     }
59
60     /**
61      * @return the annoation type this annotation handler is handling
62      */

63     public Class JavaDoc<? extends Annotation JavaDoc> getAnnotationType() {
64         return PersistenceContext.class;
65     }
66
67     /**
68      * Process a particular annotation which type is the same as the
69      * one returned by @see getAnnotationType(). All information
70      * pertinent to the annotation and its context is encapsulated
71      * in the passed AnnotationInfo instance.
72      *
73      * @param ainfo the annotation information
74      * @param rcContexts an array of ResourceContainerContext
75      * @return HandlerProcessingResult
76      */

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             // application client does not support @PersistenceContext
85
String JavaDoc 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     /**
97      * Process a particular annotation which type is the same as the
98      * one returned by @see getAnnotationType(). All information
99      * pertinent to the annotation and its context is encapsulated
100      * in the passed AnnotationInfo instance.
101      *
102      */

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 JavaDoc f = (Field JavaDoc)ainfo.getAnnotatedElement();
110             String JavaDoc targetClassName = f.getDeclaringClass().getName();
111
112             String JavaDoc logicalName = emRefAn.name();
113
114             // applying with default
115
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) { // a new one
129
processNewEmRefAnnotation(emRef, logicalName, emRefAn);
130                 }
131             }
132         } else if (ElementType.METHOD.equals(ainfo.getElementType())) {
133
134             Method JavaDoc m = (Method JavaDoc)ainfo.getAnnotatedElement();
135             String JavaDoc targetClassName = m.getDeclaringClass().getName();
136
137             String JavaDoc logicalName = emRefAn.name();
138             if( logicalName.equals("") ) {
139                 // Derive javabean property name.
140
String JavaDoc propertyName =
141                     getInjectionMethodPropertyName(m, ainfo);
142
143                 // prefixing with fully qualified type name
144
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) { // a new one
159

160                     processNewEmRefAnnotation(emRef, logicalName, emRefAn);
161
162                 }
163             }
164         } else if( ElementType.TYPE.equals(ainfo.getElementType()) ) {
165             // name() is required for TYPE-level usage
166
String JavaDoc logicalName = emRefAn.name();
167
168             if( "".equals(logicalName) ) {
169                 Class JavaDoc c = (Class JavaDoc) 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) { // a new one
180

181                     processNewEmRefAnnotation(emRef, logicalName, emRefAn);
182                                             
183                 }
184             }
185         }
186
187         return getDefaultProcessedResult();
188     }
189
190     /**
191      * Return EntityManagerReferenceDescriptors with given name
192      * if exists or a new one without name being set.
193      */

194     private EntityManagerReferenceDescriptor[]
195         getEmReferenceDescriptors(String JavaDoc 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 JavaDoc 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         // Add each property from annotation to descriptor, unless
228
// it has been overridden within the .xml.
229
Map JavaDoc 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