KickJava   Java API By Example, From Geeks To Geeks.

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


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.Method JavaDoc;
29 import java.lang.NoSuchMethodException JavaDoc;
30 import java.util.logging.Level JavaDoc;
31
32 import javax.ejb.Init JavaDoc;
33
34 import com.sun.enterprise.deployment.EjbDescriptor;
35 import com.sun.enterprise.deployment.EjbInitInfo;
36 import com.sun.enterprise.deployment.EjbSessionDescriptor;
37 import com.sun.enterprise.deployment.MethodDescriptor;
38 import com.sun.enterprise.util.TypeUtil;
39
40 import com.sun.enterprise.deployment.annotation.AnnotationInfo;
41 import com.sun.enterprise.deployment.annotation.AnnotationProcessorException;
42 import com.sun.enterprise.deployment.annotation.HandlerProcessingResult;
43 import com.sun.enterprise.deployment.annotation.context.EjbContext;
44
45 /**
46  * This handler is responsible for handling the javax.ejb.Init attribute
47  *
48  */

49 public class InitHandler extends AbstractAttributeHandler {
50     
51     public InitHandler() {
52     }
53     
54     /**
55      * @return the annoation type this annotation handler is handling
56      */

57     public Class JavaDoc<? extends Annotation JavaDoc> getAnnotationType() {
58         return Init JavaDoc.class;
59     }
60         
61     protected HandlerProcessingResult processAnnotation(AnnotationInfo ainfo,
62             EjbContext[] ejbContexts) throws AnnotationProcessorException {
63
64         Init JavaDoc init = (Init JavaDoc) ainfo.getAnnotation();
65
66         for(EjbContext next : ejbContexts) {
67             
68             EjbSessionDescriptor sessionDescriptor =
69                 (EjbSessionDescriptor) next.getDescriptor();
70
71             Method JavaDoc m = (Method JavaDoc) ainfo.getAnnotatedElement();
72
73             // Check for matching method on home and/or local home interface.
74

75             int numMatches = 0;
76
77             
78             String JavaDoc adaptedCreateMethodName = init.value();
79
80             try {
81                 if( sessionDescriptor.isRemoteInterfacesSupported() ) {
82                     addInitMethod(sessionDescriptor, m,
83                                   adaptedCreateMethodName, false);
84                     numMatches++;
85                 }
86             } catch(Exception JavaDoc e) {
87             }
88
89             try {
90                 if( sessionDescriptor.isLocalInterfacesSupported() ) {
91                     addInitMethod(sessionDescriptor, m,
92                                   adaptedCreateMethodName, true);
93                     numMatches++;
94                 }
95             } catch(Exception JavaDoc e) {
96             }
97
98             if( numMatches == 0 ) {
99                 log(Level.SEVERE, ainfo,
100                     localStrings.getLocalString(
101                     "enterprise.deployment.annotation.handlers.notmatchcreate",
102                     "Unable to find matching Home create method for Init method {0} on bean {1}.",
103                     new Object JavaDoc[] { m, sessionDescriptor.getName() }));
104                 return getDefaultFailedResult();
105             }
106         }
107
108         return getDefaultProcessedResult();
109     }
110
111     private void addInitMethod(EjbSessionDescriptor descriptor,
112                                Method JavaDoc beanMethod,
113                                String JavaDoc adaptedCreateMethodName, boolean local)
114         throws Exception JavaDoc {
115
116         String JavaDoc homeIntfName = local ? descriptor.getLocalHomeClassName() :
117             descriptor.getHomeClassName();
118
119         ClassLoader JavaDoc cl = descriptor.getEjbBundleDescriptor().getClassLoader();
120
121         Class JavaDoc homeIntf = cl.loadClass(homeIntfName);
122         
123         Method JavaDoc createMethod = null;
124         if( (adaptedCreateMethodName == null) ||
125             (adaptedCreateMethodName.equals("")) ) {
126             // Can't make any assumptions about matching method name. Could
127
// be "create" or some form of create<METHOD>, so match based on
128
// signature.
129
for(Method JavaDoc next : homeIntf.getMethods()) {
130                 if( next.getName().startsWith("create") &&
131                     TypeUtil.sameParamTypes(next, beanMethod) ) {
132                     createMethod = next;
133                     break;
134                 }
135             }
136             if( createMethod == null ) {
137                 throw new NoSuchMethodException JavaDoc("No matching adapted home " +
138                                                 "method found for @Init " +
139                                                 " method " + beanMethod);
140             }
141         } else {
142             createMethod = homeIntf.getMethod(adaptedCreateMethodName,
143                                               beanMethod.getParameterTypes());
144         }
145         
146         MethodDescriptor beanMethodDescriptor =
147             new MethodDescriptor(beanMethod, MethodDescriptor.EJB_BEAN);
148         
149         MethodDescriptor createMethodDescriptor =
150             new MethodDescriptor(createMethod,
151                                  ( local ?
152                                    MethodDescriptor.EJB_HOME :
153                                    MethodDescriptor.EJB_LOCALHOME ));
154         
155         EjbInitInfo initInfo = new EjbInitInfo();
156                     
157         initInfo.setBeanMethod(beanMethodDescriptor);
158         initInfo.setCreateMethod(createMethodDescriptor);
159
160         descriptor.addInitMethod(initInfo);
161         
162     }
163
164     /**
165      * @return an array of annotation types this annotation handler would
166      * require to be processed (if present) before it processes it's own
167      * annotation type.
168      */

169     public Class JavaDoc<? extends Annotation JavaDoc>[] getTypeDependencies() {
170         return getEjbAnnotationTypes();
171     }
172 }
173
Popular Tags