1 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 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 EJB_JAR_PREFIX = DDXmiConstants.EJBJAR_HREF_PREFIX; 42 43 46 47 public EjbSynchronizer(File ejbjarFilePar, File ibmejbjarbndFilePar) { 48 try { 49 xpath = XPathFactory.newInstance(XPathConstants.DOM_OBJECT_MODEL).newXPath(); 50 } catch (Exception 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 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 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 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 e) { 135 e.printStackTrace(); 136 } 137 } 138 } 139 private String getBeanIdFromBinding(Node beanNode, NodeList bindingsList) throws XPathFactoryConfigurationException, XPathExpressionException { 140 String 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 href = hrefAttrNode.getTextContent(); 149 String 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 getBeanName(Node beanNode) throws XPathFactoryConfigurationException, XPathExpressionException { 158 return ((Node) xpath.compile("child::ejb-name").evaluate(beanNode, XPathConstants.NODE)).getTextContent(); 159 } 160 161 private String 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 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 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 createBeanId(Node beanNode) throws XPathFactoryConfigurationException, XPathExpressionException { 194 String name = getBeanName(beanNode); 195 196 return name + BINDING_SEPARATOR + new Date().getTime(); 197 } 198 199 private boolean beanExists(Document document, String id) throws XPathExpressionException, XPathFactoryConfigurationException { 200 201 String 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 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 id) throws XPathFactoryConfigurationException, XPathExpressionException { 219 220 String 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 name, String id, String 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 |