KickJava   Java API By Example, From Geeks To Geeks.

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


1
2 /*
3  * The contents of this file are subject to the terms
4  * of the Common Development and Distribution License
5  * (the License). You may not use this file except in
6  * compliance with the License.
7  *
8  * You can obtain a copy of the license at
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
10  * glassfish/bootstrap/legal/CDDLv1.0.txt.
11  * See the License for the specific language governing
12  * permissions and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL
15  * Header Notice in each file and include the License file
16  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
17  * If applicable, add the following below the CDDL Header,
18  * with the fields enclosed by brackets [] replaced by
19  * you own identifying information:
20  * "Portions Copyrighted [year] [name of copyright owner]"
21  *
22  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
23  */

24
25 package com.sun.enterprise.deployment.node.ejb;
26
27 import java.util.ArrayList JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.Map JavaDoc;
32 import java.util.Set JavaDoc;
33 import java.util.Vector JavaDoc;
34 import org.w3c.dom.Node JavaDoc;
35
36 import com.sun.enterprise.deployment.node.XMLElement;
37 import com.sun.enterprise.deployment.node.BundleNode;
38 import com.sun.enterprise.deployment.node.RootXMLNode;
39 import com.sun.enterprise.deployment.node.SecurityRoleNode;
40 import com.sun.enterprise.deployment.node.ContainerTransactionNode;
41 import com.sun.enterprise.deployment.node.DescriptorFactory;
42 import com.sun.enterprise.deployment.node.MessageDestinationNode;
43
44 import com.sun.enterprise.deployment.xml.TagNames;
45 import com.sun.enterprise.deployment.xml.EjbTagNames;
46 import com.sun.enterprise.deployment.Descriptor;
47 import com.sun.enterprise.deployment.EjbDescriptor;
48 import com.sun.enterprise.deployment.EjbSessionDescriptor;
49 import com.sun.enterprise.deployment.EjbEntityDescriptor;
50 import com.sun.enterprise.deployment.EjbMessageBeanDescriptor;
51 import com.sun.enterprise.deployment.EjbBundleDescriptor;
52 import com.sun.enterprise.deployment.MethodDescriptor;
53 import com.sun.enterprise.deployment.MethodPermissionDescriptor;
54 import com.sun.enterprise.deployment.MethodPermission;
55 import com.sun.enterprise.deployment.SecurityRoleDescriptor;
56 import com.sun.enterprise.deployment.RelationshipDescriptor;
57 import com.sun.enterprise.deployment.EjbApplicationExceptionInfo;
58 import com.sun.enterprise.deployment.EjbInterceptor;
59
60 import com.sun.enterprise.deployment.util.DOLUtils;
61
62 import com.sun.enterprise.deployment.Role;
63
64 /**
65  * This class handles ejb bundle xml files
66  *
67  * @author Jerome Dochez
68  * @version
69  */

70 public class EjbBundleNode extends BundleNode implements RootXMLNode {
71
72     public final static XMLElement tag = new XMLElement(EjbTagNames.EJB_BUNDLE_TAG);
73     public final static String JavaDoc PUBLIC_DTD_ID = "-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN";
74     public final static String JavaDoc PUBLIC_DTD_ID_12 = "-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 1.1//EN";
75     
76     /** The system ID of an ejb-jar document.*/
77     public final static String JavaDoc SYSTEM_ID = "http://java.sun.com/dtd/ejb-jar_2_0.dtd";
78     public final static String JavaDoc SYSTEM_ID_12 = "http://java.sun.com/dtd/ejb-jar_1_1.dtd";
79     public final static String JavaDoc SCHEMA_ID_21 = "ejb-jar_2_1.xsd";
80     public final static String JavaDoc SCHEMA_ID = "ejb-jar_3_0.xsd";
81     public final static String JavaDoc SPEC_VERSION = "3.0";
82     private static List JavaDoc<String JavaDoc> systemIDs = null;
83
84    /**
85     * register this node as a root node capable of loading entire DD files
86     *
87     * @param publicIDToDTD is a mapping between xml Public-ID to DTD
88     * @return the doctype tag name
89     */

90    public static String JavaDoc registerBundle(Map JavaDoc publicIDToDTD) {
91         publicIDToDTD.put(PUBLIC_DTD_ID, SYSTEM_ID);
92         publicIDToDTD.put(PUBLIC_DTD_ID_12, SYSTEM_ID_12);
93         return tag.getQName();
94    }
95     
96     // Descriptor class we are using
97
private EjbBundleDescriptor descriptor;
98       
99    public EjbBundleNode() {
100        super();
101        // register sub XMLNodes
102
registerElementHandler(new XMLElement(EjbTagNames.SESSION),
103                                                             EjbSessionNode.class);
104        registerElementHandler(new XMLElement(EjbTagNames.ENTITY),
105                                                             EjbEntityNode.class);
106        registerElementHandler(new XMLElement(EjbTagNames.MESSAGE_DRIVEN),
107                                                             MessageDrivenBeanNode.class);
108        registerElementHandler(new XMLElement(EjbTagNames.METHOD_PERMISSION),
109                                                             MethodPermissionNode.class);
110        registerElementHandler(new XMLElement(EjbTagNames.ROLE),
111                                                             SecurityRoleNode.class, "addRole");
112        registerElementHandler(new XMLElement(EjbTagNames.CONTAINER_TRANSACTION),
113                                                             ContainerTransactionNode.class);
114        registerElementHandler(new XMLElement(EjbTagNames.EXCLUDE_LIST),
115                                                             ExcludeListNode.class);
116        registerElementHandler(new XMLElement(EjbTagNames.RELATIONSHIPS),
117                                                             RelationshipsNode.class);
118        registerElementHandler(new XMLElement(TagNames.MESSAGE_DESTINATION),
119                                              MessageDestinationNode.class,
120                                              "addMessageDestination");
121        registerElementHandler(new XMLElement(EjbTagNames.APPLICATION_EXCEPTION),
122                                              EjbApplicationExceptionNode.class,
123                                              "addApplicationException");
124        registerElementHandler(new XMLElement(EjbTagNames.INTERCEPTOR),
125                               EjbInterceptorNode.class,
126                               "addInterceptor");
127
128        registerElementHandler(new XMLElement(EjbTagNames.INTERCEPTOR_BINDING),
129                               InterceptorBindingNode.class,
130                               "appendInterceptorBinding");
131    }
132    
133     /**
134      * Adds a new DOL descriptor instance to the descriptor instance associated with
135      * this XMLNode
136      *
137      * @param descriptor the new descriptor
138      */

139    public void addDescriptor(Object JavaDoc newDescriptor) {
140        if (newDescriptor instanceof EjbDescriptor) {
141            descriptor.addEjb((EjbDescriptor) newDescriptor);
142        } else if (newDescriptor instanceof RelationshipDescriptor) {
143            descriptor.addRelationship((RelationshipDescriptor) newDescriptor);
144        } else if (newDescriptor instanceof MethodPermissionDescriptor) {
145            MethodPermissionDescriptor nd = (MethodPermissionDescriptor) newDescriptor;
146            MethodDescriptor[] array = nd.getMethods();
147            for (int i=0;i<array.length;i++) {
148                 EjbDescriptor target = descriptor.getEjbByName(array[i].getEjbName());
149                 MethodPermission[] mps = nd.getMethodPermissions();
150                 for (int j=0;j<mps.length;j++) {
151                     DOLUtils.getDefaultLogger().fine("Adding mp " + mps[j] + " to " + array[i] + " for ejb " + array[i].getEjbName());
152                     target.addPermissionedMethod(mps[j], array[i]);
153                 }
154             }
155        } else super.addDescriptor(newDescriptor);
156    }
157
158    /**
159     * @return the descriptor instance to associate with this XMLNode
160     */

161     public Object JavaDoc getDescriptor() {
162         
163         if (descriptor==null) {
164             descriptor = (EjbBundleDescriptor) DescriptorFactory.getDescriptor(getXMLPath());
165         }
166         return descriptor;
167     }
168        
169    /**
170      * @return the XML tag associated with this XMLNode
171      */

172     protected XMLElement getXMLRootTag() {
173         return tag;
174     }
175     
176    /**
177      * all sub-implementation of this class can use a dispatch table to map xml element to
178      * method name on the descriptor class for setting the element value.
179      *
180      * @return the map with the element name as a key, the setter method as a value
181      */

182     protected Map JavaDoc getDispatchTable() {
183         // no need to be synchronized for now
184
Map JavaDoc table = super.getDispatchTable();
185         table.put(EjbTagNames.EJB_CLIENT_JAR, "setEjbClientJarUri");
186         return table;
187     }
188         
189     /**
190      * write the descriptor class to a DOM tree and return it
191      *
192      * @param parent node for the DOM tree
193      * @param the descriptor to write
194      * @return the DOM tree top node
195      */

196     public Node JavaDoc writeDescriptor(Node JavaDoc parent, Descriptor descriptor) {
197         if (! (descriptor instanceof EjbBundleDescriptor)) {
198             throw new IllegalArgumentException JavaDoc(getClass() + " cannot handles descriptors of type " + descriptor.getClass());
199         }
200         EjbBundleDescriptor ejbDesc = (EjbBundleDescriptor) descriptor;
201         Node JavaDoc jarNode = super.writeDescriptor(parent, ejbDesc);
202         Node JavaDoc entrepriseBeansNode = appendChild(jarNode, EjbTagNames.EJBS);
203         for (Iterator JavaDoc ejbs = ejbDesc.getEjbs().iterator();ejbs.hasNext();) {
204             EjbDescriptor ejb = (EjbDescriptor) ejbs.next();
205             if (EjbSessionDescriptor.TYPE.equals(ejb.getType())) {
206                 EjbNode subNode = new EjbSessionNode();
207                 subNode.writeDescriptor(entrepriseBeansNode,
208                                                                             EjbTagNames.SESSION, ejb);
209             } else if (EjbEntityDescriptor.TYPE.equals(ejb.getType())) {
210                 EjbNode subNode = new EjbEntityNode();
211                 subNode.writeDescriptor(entrepriseBeansNode,
212                                                                             EjbTagNames.ENTITY, ejb);
213             } else if (EjbMessageBeanDescriptor.TYPE.equals(ejb.getType())) {
214                 EjbNode subNode = new MessageDrivenBeanNode();
215                 subNode.writeDescriptor(entrepriseBeansNode,
216                                                                             EjbTagNames.MESSAGE_DRIVEN, ejb);
217             } else {
218                 throw new IllegalStateException JavaDoc("Unknow ejb type " + ejb.getType());
219             }
220         }
221
222         if( ejbDesc.hasInterceptors() ) {
223
224             Node JavaDoc interceptorsNode = appendChild(jarNode,
225                                                 EjbTagNames.INTERCEPTORS);
226             EjbInterceptorNode interceptorNode = new EjbInterceptorNode();
227             for(EjbInterceptor next : ejbDesc.getInterceptors()) {
228                 interceptorNode.writeDescriptor( interceptorsNode,
229                                                  EjbTagNames.INTERCEPTOR, next);
230             }
231
232         }
233         
234         // relationships*
235
if (ejbDesc.hasRelationships()) {
236             (new RelationshipsNode()).writeDescriptor(jarNode, EjbTagNames.RELATIONSHIPS, ejbDesc);
237         }
238         
239         // assembly-descriptor
240
writeAssemblyDescriptor(jarNode, ejbDesc);
241         
242         appendTextChild(jarNode, EjbTagNames.EJB_CLIENT_JAR, ejbDesc.getEjbClientJarUri());
243         return jarNode;
244     }
245     
246     /**
247      * @return the DOCTYPE of the XML file
248      */

249     public String JavaDoc getDocType() {
250         return null;
251     }
252     
253     /**
254      * @return the SystemID of the XML file
255      */

256     public String JavaDoc getSystemID() {
257         return SCHEMA_ID;
258     }
259
260     /**
261      * @return the list of SystemID of the XML schema supported
262      */

263     public List JavaDoc<String JavaDoc> getSystemIDs() {
264         if (systemIDs != null) {
265             return systemIDs;
266         }
267
268         systemIDs = new ArrayList JavaDoc<String JavaDoc>();
269         systemIDs.add(SCHEMA_ID);
270         systemIDs.add(SCHEMA_ID_21);
271         return systemIDs;
272     }
273     
274     /**
275      * write assembly-descriptor related xml information to the DOM tree
276      */

277     private void writeAssemblyDescriptor(Node JavaDoc parentNode, EjbBundleDescriptor bundleDescriptor) {
278        Node JavaDoc assemblyNode = parentNode.getOwnerDocument().createElement(EjbTagNames.ASSEMBLY_DESCRIPTOR);
279        
280        // security-role*
281
SecurityRoleNode roleNode = new SecurityRoleNode();
282        for (Iterator JavaDoc e = bundleDescriptor.getRoles().iterator();e.hasNext();) {
283            roleNode.writeDescriptor(assemblyNode, EjbTagNames.ROLE, (Role) e.next());
284        }
285        
286        // method-permission*
287
Map JavaDoc excludedMethodsByEjb = new HashMap JavaDoc();
288        MethodPermissionNode mpNode = new MethodPermissionNode();
289        for (Iterator JavaDoc e = bundleDescriptor.getEjbs().iterator();e.hasNext();) {
290            EjbDescriptor ejbDesc = (EjbDescriptor) e.next();
291            if (ejbDesc instanceof EjbMessageBeanDescriptor)
292                continue;
293            Vector JavaDoc excludedMethods = new Vector JavaDoc();
294            addMethodPermissions(ejbDesc, ejbDesc.getPermissionedMethodsByPermission(), excludedMethods, mpNode, assemblyNode);
295            addMethodPermissions(ejbDesc, ejbDesc.getStyledPermissionedMethodsByPermission(), excludedMethods, mpNode, assemblyNode);
296            if (excludedMethods.size()>0) {
297                excludedMethodsByEjb.put(ejbDesc, excludedMethods);
298            }
299        }
300        
301        // container-transaction*
302
ContainerTransactionNode ctNode = new ContainerTransactionNode();
303        for (Iterator JavaDoc e = bundleDescriptor.getEjbs().iterator();e.hasNext();) {
304            EjbDescriptor ejbDesc = (EjbDescriptor) e.next();
305            ctNode.writeDescriptor(assemblyNode, EjbTagNames.CONTAINER_TRANSACTION, ejbDesc);
306        }
307
308        // interceptor-binding*
309
Set JavaDoc ejbsForInterceptors = bundleDescriptor.getEjbs();
310        InterceptorBindingNode interceptorBindingNode = new
311            InterceptorBindingNode();
312
313         for(Iterator JavaDoc itr = ejbsForInterceptors.iterator(); itr.hasNext();) {
314
315             EjbDescriptor ejbDesc = (EjbDescriptor) itr.next();
316             if( ejbDesc.getInterceptorClasses().size() > 0 ) {
317                 interceptorBindingNode.writeBindings(assemblyNode,
318                                                      ejbDesc);
319             }
320
321         }
322        
323
324        // message-destination*
325
writeMessageDestinations
326            (assemblyNode, bundleDescriptor.getMessageDestinations().iterator());
327                                 
328        // exclude-list*
329
if (excludedMethodsByEjb.size()>0) {
330            Node JavaDoc excludeListNode = this.appendChild(assemblyNode, EjbTagNames.EXCLUDE_LIST);
331            for (Iterator JavaDoc ejbs = excludedMethodsByEjb.keySet().iterator(); ejbs.hasNext();) {
332                EjbDescriptor ejbDesc = (EjbDescriptor) ejbs.next();
333                Vector JavaDoc excludedMethods = (Vector JavaDoc) excludedMethodsByEjb.get(ejbDesc);
334                
335                MethodPermissionDescriptor mpd = new MethodPermissionDescriptor();
336                mpd.addMethodPermission(MethodPermission.getExcludedMethodPermission());
337                mpd.addMethods(excludedMethods);
338                mpNode.writeDescriptorInNode(excludeListNode, mpd, ejbDesc);
339            }
340        }
341        
342        for(EjbApplicationExceptionInfo next :
343                bundleDescriptor.getApplicationExceptions()) {
344
345            EjbApplicationExceptionNode node = new EjbApplicationExceptionNode();
346            
347            node.writeDescriptor(assemblyNode, EjbTagNames.APPLICATION_EXCEPTION,
348                                 next);
349
350        }
351
352        if (assemblyNode.hasChildNodes()) {
353            parentNode.appendChild(assemblyNode);
354        }
355     }
356     
357     private void addMethodPermissions(
358             EjbDescriptor ejb,
359             Map JavaDoc mpToMethods,
360             Vector JavaDoc excludedMethods,
361             MethodPermissionNode mpNode,
362             Node JavaDoc assemblyNode) {
363                 
364         if (mpToMethods==null || mpToMethods.size()==0) {
365             return;
366         }
367         
368         for (Iterator JavaDoc mpIterator = mpToMethods.keySet().iterator();mpIterator.hasNext();) {
369             MethodPermission mp = (MethodPermission) mpIterator.next();
370             if (mp.isExcluded()) {
371                 // we need to be sure the method descriptors knows who owns them
372
Set JavaDoc methods = (Set JavaDoc) mpToMethods.get(mp);
373                 excludedMethods.addAll(methods);
374             } else {
375                 MethodPermissionDescriptor mpd = new MethodPermissionDescriptor();
376                 mpd.addMethodPermission(mp);
377                 mpd.addMethods((Set JavaDoc) mpToMethods.get(mp));
378                 mpNode.writeDescriptor(assemblyNode, EjbTagNames.METHOD_PERMISSION, mpd, ejb);
379             }
380         }
381     }
382     
383     /**
384      * @return the default spec version level this node complies to
385      */

386     public String JavaDoc getSpecVersion() {
387         return SPEC_VERSION;
388     }
389     
390 }
391
Popular Tags