KickJava   Java API By Example, From Geeks To Geeks.

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


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
31 import java.util.logging.Level JavaDoc;
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 /**
46  * This handler is responsible for handling the
47  * javax.persistence.PersistenceUnit annotation.
48  *
49  */

50 public class EntityManagerFactoryReferenceHandler
51     extends AbstractResourceHandler {
52     
53     public EntityManagerFactoryReferenceHandler() {
54     }
55
56     /**
57      * @return the annoation type this annotation handler is handling
58      */

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

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     /**
83      * Process a particular annotation which type is the same as the
84      * one returned by @see getAnnotationType(). All information
85      * pertinent to the annotation and its context is encapsulated
86      * in the passed AnnotationInfo instance.
87      *
88      */

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 JavaDoc f = (Field JavaDoc)ainfo.getAnnotatedElement();
96             String JavaDoc targetClassName = f.getDeclaringClass().getName();
97
98             String JavaDoc logicalName = emfRefAn.name();
99                 
100             // applying with default
101
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) { // a new one
114
processNewEmfRefAnnotation(emfRef, logicalName, emfRefAn);
115                 }
116             }
117         } else if (ElementType.METHOD.equals(ainfo.getElementType())) {
118
119             Method JavaDoc m = (Method JavaDoc)ainfo.getAnnotatedElement();
120             String JavaDoc targetClassName = m.getDeclaringClass().getName();
121
122             String JavaDoc logicalName = emfRefAn.name();
123             if( logicalName.equals("") ) {
124                 // Derive javabean property name.
125
String JavaDoc propertyName =
126                     getInjectionMethodPropertyName(m, ainfo);
127
128                 // prefixing with fully qualified type name
129
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) { // a new one
144

145                     processNewEmfRefAnnotation(emfRef, logicalName, emfRefAn);
146
147                 }
148             }
149         } else if( ElementType.TYPE.equals(ainfo.getElementType()) ) {
150             // name() is required for TYPE-level usage
151
String JavaDoc logicalName = emfRefAn.name();
152
153             if( "".equals(logicalName) ) {
154                 Class JavaDoc c = (Class JavaDoc) 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) { // a new one
165

166                     processNewEmfRefAnnotation(emfRef, logicalName, emfRefAn);
167                                             
168                 }
169             }
170         }
171
172         return getDefaultProcessedResult();
173     }
174
175     /**
176      * Return EntityManagerFactoryReferenceDescriptors with given name
177      * if exists or a new one without name being set.
178      */

179     private EntityManagerFactoryReferenceDescriptor[]
180         getEmfReferenceDescriptors(String JavaDoc 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 JavaDoc 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