KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > deployment > node > ejb > InterceptorBindingNode


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
24 package com.sun.enterprise.deployment.node.ejb;
25
26 import java.util.Map JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.LinkedList JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import org.w3c.dom.Node JavaDoc;
32 import org.xml.sax.Attributes JavaDoc;
33
34 import com.sun.enterprise.deployment.node.XMLElement;
35 import com.sun.enterprise.deployment.node.MethodNode;
36 import com.sun.enterprise.deployment.node.DeploymentDescriptorNode;
37
38 import com.sun.enterprise.deployment.Descriptor;
39 import com.sun.enterprise.deployment.EjbBundleDescriptor;
40 import com.sun.enterprise.deployment.InterceptorBindingDescriptor;
41 import com.sun.enterprise.deployment.MethodDescriptor;
42 import com.sun.enterprise.deployment.EjbDescriptor;
43 import com.sun.enterprise.deployment.EjbInterceptor;
44 import com.sun.enterprise.deployment.xml.EjbTagNames;
45
46 public class InterceptorBindingNode extends DeploymentDescriptorNode {
47
48     private MethodDescriptor businessMethod = null;
49     private boolean needsOverloadResolution = false;
50
51     public InterceptorBindingNode() {
52         super();
53     }
54     
55     public void startElement(XMLElement element, Attributes JavaDoc attributes) {
56
57         if( EjbTagNames.METHOD.equals(element.getQName()) ) {
58
59             businessMethod = new MethodDescriptor();
60             // Assume we need overloaded method resolution until we
61
// encounter at least one method-param element.
62
needsOverloadResolution = true;
63
64         } else if( EjbTagNames.METHOD_PARAMS.equals(element.getQName()) ) {
65
66             // If there's a method-params element, regardless of whether there
67
// are any <method-param> sub-elements, there's no overload
68
// resolution needed.
69
needsOverloadResolution = false;
70
71         }
72           
73         super.startElement(element, attributes);
74     }
75    
76     public void setElementValue(XMLElement element, String JavaDoc value) {
77
78         if( EjbTagNames.METHOD_NAME.equals(element.getQName()) ) {
79             businessMethod.setName(value);
80         } else if( EjbTagNames.METHOD_PARAM.equals(element.getQName()) ) {
81             if( (value != null) && (value.trim().length() > 0) ) {
82                 businessMethod.addParameterClass(value.trim());
83             }
84         } else {
85             super.setElementValue(element, value);
86         }
87
88     }
89
90     /**
91      * receives notification of the end of an XML element by the Parser
92      *
93      * @param element the xml tag identification
94      * @return true if this node is done processing the XML sub tree
95      */

96     public boolean endElement(XMLElement element) {
97
98         if (EjbTagNames.INTERCEPTOR_ORDER.equals(element.getQName())) {
99
100             InterceptorBindingDescriptor desc = (InterceptorBindingDescriptor)
101                 getDescriptor();
102             desc.setIsTotalOrdering(true);
103
104         } else if( EjbTagNames.METHOD.equals(element.getQName()) ) {
105
106             InterceptorBindingDescriptor bindingDesc =
107                 (InterceptorBindingDescriptor) getDescriptor();
108             businessMethod.setEjbClassSymbol(MethodDescriptor.EJB_BEAN);
109             bindingDesc.setBusinessMethod(businessMethod);
110             
111             if( needsOverloadResolution ) {
112                 bindingDesc.setNeedsOverloadResolution(true);
113             }
114
115             businessMethod = null;
116             needsOverloadResolution = false;
117
118         }
119
120         return super.endElement(element);
121     }
122
123     /**
124      * all sub-implementation of this class can use a dispatch table to map xml element to
125      * method name on the descriptor class for setting the element value.
126      *
127      * @return the map with the element name as a key, the setter method as a value
128      */

129     protected Map JavaDoc getDispatchTable() {
130         Map JavaDoc table = super.getDispatchTable();
131
132         table.put(EjbTagNames.EJB_NAME, "setEjbName");
133         table.put(EjbTagNames.INTERCEPTOR_CLASS, "appendInterceptorClass");
134         table.put(EjbTagNames.EXCLUDE_DEFAULT_INTERCEPTORS,
135                   "setExcludeDefaultInterceptors");
136         table.put(EjbTagNames.EXCLUDE_CLASS_INTERCEPTORS,
137                   "setExcludeClassInterceptors");
138
139         return table;
140     }
141     
142     /**
143      * Write interceptor bindings for this ejb.
144      *
145      * @param parent node in the DOM tree
146      * @param the descriptor to write
147      */

148     public void writeBindings(Node JavaDoc parent, EjbDescriptor ejbDesc) {
149
150         List JavaDoc<EjbInterceptor> classInterceptors = ejbDesc.getInterceptorChain();
151         if( classInterceptors.size() > 0 ) {
152             writeTotalOrdering(parent, classInterceptors, ejbDesc, null);
153         }
154
155         Map JavaDoc<MethodDescriptor, List JavaDoc<EjbInterceptor>> methodInterceptorsMap =
156             ejbDesc.getMethodInterceptorsMap();
157
158         for(MethodDescriptor nextMethod : methodInterceptorsMap.keySet()) {
159
160             List JavaDoc<EjbInterceptor> interceptors =
161                 methodInterceptorsMap.get(nextMethod);
162             
163             if(interceptors.isEmpty()) {
164                 writeExclusionBinding(parent, ejbDesc, nextMethod);
165             } else {
166                 writeTotalOrdering(parent, interceptors, ejbDesc, nextMethod);
167             }
168
169         }
170
171         return;
172     }
173
174     private void writeTotalOrdering(Node JavaDoc parent,
175                                     List JavaDoc<EjbInterceptor> interceptors,
176                                     EjbDescriptor ejbDesc,
177                                     MethodDescriptor method) {
178
179         Node JavaDoc bindingNode = appendChild(parent,
180                                        EjbTagNames.INTERCEPTOR_BINDING);
181
182         appendTextChild(bindingNode, EjbTagNames.EJB_NAME,
183                         ejbDesc.getName());
184
185         Node JavaDoc totalOrderingNode = appendChild(bindingNode,
186                                              EjbTagNames.INTERCEPTOR_ORDER);
187         for(EjbInterceptor next : interceptors) {
188
189             appendTextChild(totalOrderingNode, EjbTagNames.INTERCEPTOR_CLASS,
190                             next.getInterceptorClassName());
191         }
192
193         if( method != null ) {
194             
195             MethodNode methodNode = new MethodNode();
196
197             // Write out method description. void methods will be written
198
// out using an empty method-params element so they will not
199
// be interpreted as overloaded when processed.
200
methodNode.writeJavaMethodDescriptor
201                 (bindingNode, EjbTagNames.INTERCEPTOR_BUSINESS_METHOD, method,
202                  true);
203
204         }
205
206     }
207
208     private void writeExclusionBinding(Node JavaDoc parent, EjbDescriptor ejbDesc,
209                                        MethodDescriptor method) {
210
211         Node JavaDoc bindingNode = appendChild(parent,
212                                        EjbTagNames.INTERCEPTOR_BINDING);
213
214         appendTextChild(bindingNode, EjbTagNames.EJB_NAME,
215                         ejbDesc.getName());
216
217         appendTextChild(bindingNode, EjbTagNames.EXCLUDE_CLASS_INTERCEPTORS,
218                         "true");
219
220         MethodNode methodNode = new MethodNode();
221
222         // Write out method description. void methods will be written
223
// out using an empty method-params element so they will not
224
// be interpreted as overloaded when processed.
225
methodNode.writeJavaMethodDescriptor
226             (bindingNode, EjbTagNames.INTERCEPTOR_BUSINESS_METHOD, method,
227              true);
228                                              
229     }
230 }
231
Popular Tags