KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > websphere6 > config > sync > EjbSynchronizer


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  * $Id: EjbSynchronizer.java,v 1.1 2007/01/10 12:03:36 dlipin Exp $
19  */

20
21 package org.netbeans.modules.j2ee.websphere6.config.sync;
22
23 import java.io.*;
24 import java.util.*;
25 import javax.xml.parsers.*;
26 import javax.xml.xpath.*;
27 import org.netbeans.modules.j2ee.websphere6.dd.beans.DDXmiConstants;
28 import org.w3c.dom.*;
29
30 /**
31  *
32  * @author Dmitry Lipin
33  */

34 public class EjbSynchronizer extends Synchronizer{
35     private File ejbjarFile ;
36     private File ibmejbjarbndFile;
37     private XPath xpath = null;
38     
39     private boolean saveEjbJarNeeded = false;
40     private boolean saveIbmEjbJarBndNeeded = false;
41     private static final String JavaDoc EJB_JAR_PREFIX = DDXmiConstants.EJBJAR_HREF_PREFIX;
42     
43     /**
44      * Synchronizes the ebj-jar.xml with ibm-ebj-jar-bnd.xmi
45      */

46     
47     public EjbSynchronizer(File ejbjarFilePar, File ibmejbjarbndFilePar) {
48         try {
49             xpath = XPathFactory.newInstance(XPathConstants.DOM_OBJECT_MODEL).newXPath();
50         } catch (Exception JavaDoc ex) {
51             ex.printStackTrace();
52             xpath = null;
53         };
54         this.ibmejbjarbndFile = ibmejbjarbndFilePar;
55         this.ejbjarFile = ejbjarFilePar;
56     }
57     
58     
59     public synchronized void syncDescriptors() {
60         if ((ejbjarFile != null) && (ibmejbjarbndFile != null)) {
61             
62             
63             try {
64                 saveEjbJarNeeded = false;
65                 saveIbmEjbJarBndNeeded = false;
66                 //xpath.setNamespaceContext(new EjbJarNSC());
67
Document ejbjarDocument = loadDocument(ejbjarFile);
68                 Document ibmejbjarbndDocument = loadDocument(ibmejbjarbndFile);
69                 
70                 NodeList beansList = (NodeList) xpath.
71                         compile("/ejb-jar/enterprise-beans/*").
72                         evaluate(ejbjarDocument, XPathConstants.NODESET);
73                 Node bindingsRoot = ibmejbjarbndDocument.getDocumentElement();
74                 
75                 if (bindingsRoot == null) {
76                     return;
77                 }
78                 
79                 NodeList bindingsList = (NodeList) xpath.
80                         compile("./"+ DDXmiConstants.EJB_BINDINGS_ID).
81                         evaluate(bindingsRoot, XPathConstants.NODESET);
82                 
83                 
84                 for (int i = 0; i < beansList.getLength(); i++) {
85                     Node node = beansList.item(i);
86                     
87                     String JavaDoc id = getBeanId(node);
88                     if ((id == null) || (!bindingExists(bindingsRoot, id))) {
89                         boolean neadCreateBinding = false;
90                         if (id == null) {
91                             id = getBeanIdFromBinding(node,bindingsList);
92                             if(id==null) {
93                                 id = createBeanId(node);
94                                 neadCreateBinding = true;
95                             }
96                             Attr attribute = ejbjarDocument.createAttribute("id");
97                             attribute.setValue(id);
98                             node.getAttributes().setNamedItem(attribute);
99                             saveEjbJarNeeded = true;
100                         }
101                         
102                         
103                         if(neadCreateBinding) {
104                             Node binding = constructBinding(ibmejbjarbndDocument,
105                                     getBeanName(node),
106                                     getBeanId(node),
107                                     getBeanType(node));
108                             bindingsRoot.appendChild(ibmejbjarbndDocument.createTextNode(" "));
109                             bindingsRoot.appendChild(binding);
110                             bindingsRoot.appendChild(ibmejbjarbndDocument.createTextNode("\n"));
111                             saveIbmEjbJarBndNeeded = true;
112                         }
113                     }
114                 }
115                 
116                 for (int i = 0; i < bindingsList.getLength(); i++) {
117                     Node node = bindingsList.item(i);
118                     
119                     String JavaDoc id = getBindingId(node);
120                     
121                     if (!beanExists(ejbjarDocument, id)) {
122                         bindingsRoot.removeChild(node);
123                         
124                         saveIbmEjbJarBndNeeded = true;
125                     }
126                 }
127                 
128                 if (saveEjbJarNeeded) {
129                     saveDocument(ejbjarDocument, ejbjarFile);
130                 }
131                 if (saveIbmEjbJarBndNeeded) {
132                     saveDocument(ibmejbjarbndDocument, ibmejbjarbndFile);
133                 }
134             } catch (Exception JavaDoc e) {
135                 e.printStackTrace();
136             }
137         }
138     }
139     private String JavaDoc getBeanIdFromBinding(Node beanNode, NodeList bindingsList) throws XPathFactoryConfigurationException, XPathExpressionException {
140         String JavaDoc name = getBeanName(beanNode);
141         for(int i=0;i<bindingsList.getLength();i++) {
142             Node entBeanNode = ((Node) xpath.compile("./" + DDXmiConstants.ENTERPRISE_BEAN_ID).
143                     evaluate(bindingsList.item(i), XPathConstants.NODE));
144             Node hrefAttrNode = entBeanNode.getAttributes().getNamedItem("href");
145             if(hrefAttrNode==null) {
146                 continue;
147             }
148             String JavaDoc href = hrefAttrNode.getTextContent();
149             String JavaDoc beanId = href.substring(href.indexOf(EJB_JAR_PREFIX) + EJB_JAR_PREFIX.length(),
150                     href.indexOf(BINDING_SEPARATOR));
151             if(beanId.equals(name)) {
152                 return href.substring(href.indexOf(EJB_JAR_PREFIX) + EJB_JAR_PREFIX.length());
153             }
154         }
155         return null;
156     }
157     private String JavaDoc getBeanName(Node beanNode) throws XPathFactoryConfigurationException, XPathExpressionException {
158         return ((Node) xpath.compile("child::ejb-name").evaluate(beanNode, XPathConstants.NODE)).getTextContent();
159     }
160     
161     private String JavaDoc getBeanId(Node beanNode) {
162         Node idNode = beanNode.getAttributes().getNamedItem("id");
163         
164         if (idNode != null) {
165             return idNode.getTextContent();
166         } else {
167             return null;
168         }
169     }
170     
171     private String JavaDoc getBeanType(Node beanNode) throws XPathFactoryConfigurationException, XPathExpressionException {
172         
173         if (beanNode.getNodeName().equals("session")) {
174             return DDXmiConstants.EJB_ENTERPRISE_BEAN_TYPE_SESSION;
175         }
176         
177         if (beanNode.getNodeName().equals("entity")) {
178             String JavaDoc persistenceType = ((Node) xpath.compile("child::persistence-type").evaluate(beanNode, XPathConstants.NODE)).getTextContent();
179             if (persistenceType.equals("Container")) {
180                 return DDXmiConstants.EJB_ENTERPRISE_BEAN_TYPE_CONTAINER_MANAGED_ENTITY;
181             } else {
182                 return "ejb:BeanManagedEntity";
183             }
184         }
185         
186         if (beanNode.getNodeName().equals("message-driven")) {
187             return DDXmiConstants.EJB_ENTERPRISE_BEAN_TYPE_MESSAGEDRIVEN;
188         }
189         
190         return null;
191     }
192     
193     private String JavaDoc createBeanId(Node beanNode) throws XPathFactoryConfigurationException, XPathExpressionException {
194         String JavaDoc name = getBeanName(beanNode);
195         
196         return name + BINDING_SEPARATOR + new Date().getTime();
197     }
198     
199     private boolean beanExists(Document document, String JavaDoc id) throws XPathExpressionException, XPathFactoryConfigurationException {
200         
201         String JavaDoc path = "/ejb-jar/enterprise-beans/*[@id=\"" + id + "\"]";
202         
203         Node node = (Node) xpath.compile(path).evaluate(document, XPathConstants.NODE);
204         
205         return node != null;
206     }
207     
208     private String JavaDoc getBindingId(Node bindingNode) {
209         Node idNode = bindingNode.getAttributes().getNamedItem("xmi:id");
210         
211         if (idNode != null) {
212             return idNode.getTextContent();
213         } else {
214             return null;
215         }
216     }
217     
218     private boolean bindingExists(Node root, String JavaDoc id) throws XPathFactoryConfigurationException, XPathExpressionException {
219         
220         String JavaDoc path = "./" + DDXmiConstants.EJB_BINDINGS_ID + "[@id=\"" + id + "\"]";
221         
222         Node node = (Node) xpath.compile(path).evaluate(root, XPathConstants.NODE);
223         
224         return node != null;
225     }
226     
227     private Node constructBinding(Document document, String JavaDoc name, String JavaDoc id, String JavaDoc type) {
228         Element binding = document.createElement("ejbBindings");
229         binding.setAttribute("jndiName", "ejb/" + id);
230         binding.setAttributeNS("http://www.omg.org/XMI", "xmi:id", id);
231         
232         Element bean = document.createElement(DDXmiConstants.ENTERPRISE_BEAN_ID);
233         
234         bean.setAttribute("href", EJB_JAR_PREFIX + id);
235         bean.setAttributeNS("http://www.omg.org/XMI", "xmi:type", type);
236         binding.appendChild(document.createTextNode("\n "));
237         binding.appendChild(bean);
238         binding.appendChild(document.createTextNode("\n"));
239         return binding;
240     }
241 }
242
Popular Tags