KickJava   Java API By Example, From Geeks To Geeks.

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


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.reflect.AnnotatedElement JavaDoc;
27 import java.util.logging.Level JavaDoc;
28
29 import javax.ejb.MessageDriven JavaDoc;
30 import javax.ejb.ActivationConfigProperty JavaDoc;
31
32 import com.sun.enterprise.deployment.EjbBundleDescriptor;
33 import com.sun.enterprise.deployment.EjbDescriptor;
34 import com.sun.enterprise.deployment.EjbMessageBeanDescriptor;
35 import com.sun.enterprise.deployment.EnvironmentProperty;
36
37 import com.sun.enterprise.deployment.annotation.AnnotationInfo;
38 import com.sun.enterprise.deployment.annotation.AnnotationProcessorException;
39 import com.sun.enterprise.deployment.annotation.HandlerProcessingResult;
40 import com.sun.enterprise.deployment.annotation.context.EjbBundleContext;
41
42 /**
43  * This handler is responsible for handling the javax.ejb.MessageDriven
44  *
45  * @author Shing Wai Chan
46  */

47 public class MessageDrivenHandler extends AbstractEjbHandler {
48     
49     /** Creates a new instance of MessageDrivenHandler */
50     public MessageDrivenHandler() {
51     }
52     
53     /**
54      * @return the annoation type this annotation handler is handling
55      */

56     public Class JavaDoc<? extends Annotation JavaDoc> getAnnotationType() {
57         return MessageDriven JavaDoc.class;
58     }
59         
60     /**
61      * Return the name attribute of given annotation.
62      * @param annotation
63      * @return name
64      */

65     protected String JavaDoc getAnnotatedName(Annotation JavaDoc annotation) {
66         MessageDriven JavaDoc mdAn = (MessageDriven JavaDoc)annotation;
67         return mdAn.name();
68     }
69
70     /**
71      * Check if the given EjbDescriptor matches the given Annotation.
72      * @param ejbDesc
73      * @param annotation
74      * @return boolean check for validity of EjbDescriptor
75      */

76     protected boolean isValidEjbDescriptor(EjbDescriptor ejbDesc,
77             Annotation JavaDoc annotation) {
78         return EjbMessageBeanDescriptor.TYPE.equals(ejbDesc.getType());
79     }
80
81     /**
82      * Create a new EjbDescriptor for a given elementName and AnnotationInfo.
83      * @param elementName
84      * @param ainfo
85      * @return a new EjbDescriptor
86      */

87     protected EjbDescriptor createEjbDescriptor(String JavaDoc elementName,
88             AnnotationInfo ainfo) throws AnnotationProcessorException {
89
90         AnnotatedElement JavaDoc ae = ainfo.getAnnotatedElement();
91         EjbMessageBeanDescriptor newDescriptor = new EjbMessageBeanDescriptor();
92         Class JavaDoc ejbClass = (Class JavaDoc)ae;
93         newDescriptor.setName(elementName);
94         newDescriptor.setEjbClassName(ejbClass.getName());
95         return newDescriptor;
96     }
97
98     /**
99      * Set Annotation information to Descriptor.
100      * This method will also be invoked for an existing descriptor with
101      * annotation as user may not specific a complete xml.
102      * @param ejbDesc
103      * @param ainfo
104      * @return HandlerProcessingResult
105      */

106     protected HandlerProcessingResult setEjbDescriptorInfo(
107             EjbDescriptor ejbDesc, AnnotationInfo ainfo)
108             throws AnnotationProcessorException {
109
110         MessageDriven JavaDoc mdAn = (MessageDriven JavaDoc)ainfo.getAnnotation();
111         Class JavaDoc ejbClass = (Class JavaDoc)ainfo.getAnnotatedElement();
112         EjbMessageBeanDescriptor ejbMsgBeanDesc =
113                 (EjbMessageBeanDescriptor)ejbDesc;
114    
115         HandlerProcessingResult procResult =
116             setMessageListenerInterface(
117                     mdAn, ejbMsgBeanDesc, ejbClass, ainfo);
118
119         doDescriptionProcessing(mdAn.description(), ejbMsgBeanDesc);
120         doMappedNameProcessing(mdAn.mappedName(), ejbMsgBeanDesc);
121
122         for (ActivationConfigProperty JavaDoc acProp : mdAn.activationConfig()) {
123             EnvironmentProperty envProp = new EnvironmentProperty(
124                     acProp.propertyName(), acProp.propertyValue(), "");
125                                                 // with empty description
126
// xml override
127
if (ejbMsgBeanDesc.getActivationConfigValue(envProp.getName()) == null) {
128                 ejbMsgBeanDesc.putActivationConfigProperty(envProp);
129             }
130         }
131
132         return procResult;
133     }
134
135     private HandlerProcessingResult setMessageListenerInterface(
136             MessageDriven JavaDoc mdAn, EjbMessageBeanDescriptor msgEjbDesc,
137             Class JavaDoc ejbClass, AnnotationInfo ainfo)
138             throws AnnotationProcessorException {
139
140         String JavaDoc intfName = null;
141
142         // If @MessageDriven contains message listener interface, that takes
143
// precedence. Otherwise, the message listener interface is derived
144
// from the implements clause.
145

146         if( mdAn.messageListenerInterface() != Object JavaDoc.class ) {
147             intfName = mdAn.messageListenerInterface().getName();
148         } else {
149             for(Class JavaDoc next : ejbClass.getInterfaces()) {
150                 if( !excludedFromImplementsClause(next) ) {
151                     if( intfName == null ) {
152                         intfName = next.getName();
153                     } else {
154                         EjbBundleDescriptor currentBundle =
155                         ((EjbBundleContext)ainfo.getProcessingContext().getHandler()).getDescriptor();
156                         log(Level.SEVERE, ainfo,
157                             localStrings.getLocalString(
158                             "enterprise.deployment.annotation.handlers.ambiguousimplementsclausemdb",
159                             "Implements clause for 3.x message driven bean class {0} in {1} declares more than one potential message-listener interface. In this case, the @MessageDriven.messageListenerInterface() attribute must be used to specify the message listener interface.",
160                              new Object JavaDoc[] { ejbClass,
161                              currentBundle.getModuleDescriptor().getArchiveUri() }));
162                         return getDefaultFailedResult();
163                     }
164                 }
165             }
166         }
167
168         // if it's still null, check whether it's defined through
169
// deployment descriptor
170
// note: the descriptor class has a default value
171
// for the interface: javax.jms.MessageListener
172
// so intfName after this set, will never be null
173
if (intfName == null) {
174             intfName = msgEjbDesc.getMessageListenerType();
175         }
176
177         msgEjbDesc.setMessageListenerType(intfName);
178
179         return getDefaultProcessedResult();
180     }
181 }
182
Popular Tags