1 package org.objectweb.celtix.tools.extensions.jaxws; 2 3 import java.io.*; 4 import java.net.*; 5 import java.util.*; 6 import java.util.logging.Logger ; 7 8 import javax.wsdl.Binding; 9 import javax.wsdl.BindingOperation; 10 import javax.wsdl.Definition; 11 import javax.wsdl.Operation; 12 import javax.wsdl.PortType; 13 import javax.wsdl.WSDLException; 14 import javax.xml.parsers.DocumentBuilder ; 15 import javax.xml.parsers.DocumentBuilderFactory ; 16 import javax.xml.parsers.FactoryConfigurationError ; 17 import javax.xml.parsers.ParserConfigurationException ; 18 import javax.xml.stream.XMLStreamException; 19 import javax.xml.stream.XMLStreamReader; 20 21 import org.w3c.dom.Element ; 22 import org.w3c.dom.Node ; 23 import org.w3c.dom.NodeList ; 24 import org.xml.sax.ErrorHandler ; 25 import org.xml.sax.InputSource ; 26 import org.xml.sax.SAXException ; 27 import org.xml.sax.SAXParseException ; 28 29 import org.objectweb.celtix.common.i18n.Message; 30 import org.objectweb.celtix.common.logging.LogUtils; 31 import org.objectweb.celtix.common.util.StringUtils; 32 import org.objectweb.celtix.tools.common.ProcessorEnvironment; 33 import org.objectweb.celtix.tools.common.ToolConstants; 34 import org.objectweb.celtix.tools.common.ToolException; 35 import org.objectweb.celtix.tools.utils.ProcessorUtil; 36 import org.objectweb.celtix.tools.utils.StAXUtil; 37 38 public final class CustomizationParser { 39 private static final Logger LOG = LogUtils.getL7dLogger(CustomizationParser.class); 40 private static CustomizationParser parser; 41 private ProcessorEnvironment env; 42 private final Set<Element > jaxwsBindings = new HashSet<Element >(); 43 private Definition definition; 44 private final Map<BindingsNode, JAXWSBinding>definitionExtensions; 45 private final Map<BindingsNode, JAXWSBinding>portTypeExtensions; 46 private final Map<BindingsNode, JAXWSBinding>operationExtensions; 47 private Element handlerChains; 48 49 private CustomizationParser() { 50 definitionExtensions = new HashMap<BindingsNode, JAXWSBinding>(); 51 portTypeExtensions = new HashMap<BindingsNode, JAXWSBinding>(); 52 operationExtensions = new HashMap<BindingsNode, JAXWSBinding>(); 53 54 } 55 56 public static CustomizationParser getInstance() { 57 if (parser == null) { 58 parser = new CustomizationParser(); 59 } 60 return parser; 61 } 62 63 public void clean() { 64 jaxwsBindings.clear(); 65 definitionExtensions.clear(); 66 portTypeExtensions.clear(); 67 operationExtensions.clear(); 68 69 } 70 71 public Element getHandlerChains() { 72 return this.handlerChains; 73 } 74 75 public JAXWSBinding getDefinitionExtension() { 76 if (definitionExtensions.size() > 0) { 77 return definitionExtensions.values().iterator().next(); 78 } else { 79 return null; 80 } 81 } 82 83 public JAXWSBinding getPortTypeExtension(String portTypeName) { 84 Collection<BindingsNode> bindingNodes = portTypeExtensions.keySet(); 85 JAXWSBinding jaxwsBinding = null; 86 for (BindingsNode bindingNode : bindingNodes) { 87 if (portTypeName.equals(bindingNode.getNodeName())) { 88 jaxwsBinding = portTypeExtensions.get(bindingNode); 89 break; 90 } 91 } 92 if (jaxwsBinding == null) { 93 jaxwsBinding = getDefinitionExtension(); 94 } 95 return jaxwsBinding; 96 } 97 98 public JAXWSBinding getPortTypeOperationExtension(String portTypeName, String operationName) { 99 Collection<BindingsNode> bindingNodes = operationExtensions.keySet(); 100 JAXWSBinding jaxwsBinding = null; 101 for (BindingsNode bindingNode : bindingNodes) { 102 if (matchOperation(bindingNode.getXPathExpression(), portTypeName, operationName)) { 103 jaxwsBinding = operationExtensions.get(bindingNode); 104 break; 105 } 106 } 107 if (jaxwsBinding == null) { 108 jaxwsBinding = getPortTypeExtension(portTypeName); 109 } 110 111 return jaxwsBinding; 112 } 113 114 private boolean matchOperation(String xpathExpression, String portTypeName, String operationName) { 115 String regex = ".*" + wrapper(portTypeName) + ".*" + wrapper(operationName) + ".*"; 116 return xpathExpression.matches(regex); 117 } 118 119 public void parse(ProcessorEnvironment pe, Definition def) { 120 this.env = pe; 121 this.definition = def; 122 String [] bindingFiles; 123 try { 124 bindingFiles = (String [])env.get(ToolConstants.CFG_BINDING); 125 } catch (ClassCastException e) { 126 bindingFiles = new String [1]; 127 bindingFiles[0] = (String )env.get(ToolConstants.CFG_BINDING); 128 } 129 130 for (int i = 0; i < bindingFiles.length; i++) { 131 try { 132 addBinding(bindingFiles[i]); 133 } catch (XMLStreamException xse) { 134 Message msg = new Message("STAX_PASER_ERROR", LOG); 135 throw new ToolException(msg, xse); 136 } 137 } 138 139 for (Element jaxwsBinding : jaxwsBindings) { 140 buildTargetNodeMap(jaxwsBinding, "/"); 141 } 142 143 buildHandlerChains(); 144 } 145 146 private void buildHandlerChains() { 147 for (Element jaxwsBinding : jaxwsBindings) { 148 NodeList nl = jaxwsBinding.getElementsByTagNameNS(ToolConstants.HANDLER_CHAINS_URI, 149 ToolConstants.HANDLER_CHAINS); 150 if (nl.getLength() == 0) { 151 continue; 152 } 153 this.handlerChains = (Element )nl.item(0); 155 return; 156 } 157 } 158 159 private void buildTargetNodeMap(Element bindings, String expression) { 160 if (bindings.getAttributeNode("wsdlLocation") != null) { 161 expression = "/"; 162 } 163 164 if (isJAXWSBindings(bindings) && bindings.getAttributeNode("node") != null) { 165 expression = expression + "/" + bindings.getAttribute("node"); 166 try { 167 evaluateBindingsNode(bindings, expression); 168 } catch (WSDLException we) { 169 Message msg = new Message("PARSE_BININDINGFILE_EXCEPTION", LOG); 170 throw new ToolException(msg, we); 171 } 172 } 173 174 Element [] children = getChildElements(bindings, ToolConstants.NS_JAXWS_BINDINGS); 175 for (int i = 0; i < children.length; i++) { 176 buildTargetNodeMap(children[i], expression); 177 } 178 } 179 180 private Element [] getChildElements(Element parent, String nsUri) { 181 List <Element > a = new ArrayList<Element >(); 182 NodeList children = parent.getChildNodes(); 183 for (int i = 0; i < children.getLength(); i++) { 184 Node item = children.item(i); 185 if (!(item instanceof Element )) { 186 continue; 187 } 188 if (nsUri.equals(item.getNamespaceURI())) { 189 a.add((Element )item); 190 } 191 } 192 return (Element [])a.toArray(new Element [a.size()]); 193 } 194 195 private boolean isJAXWSBindings(Node bindings) { 196 return ToolConstants.NS_JAXWS_BINDINGS.equals(bindings.getNamespaceURI()) 197 && "bindings".equals(bindings.getLocalName()); 198 } 199 200 private void addBinding(String bindingFile) throws XMLStreamException { 201 String bindingLocation = ProcessorUtil.absolutize(ProcessorUtil.getFileOrURLName(bindingFile)); 202 203 InputSource is = new InputSource (bindingLocation); 204 XMLStreamReader reader = StAXUtil.createFreshXMLStreamReader(is); 205 206 StAXUtil.toStartTag(reader); 207 208 if (isValidJaxwsBindingFile(bindingFile, reader)) { 209 Element root = parse(is); 210 jaxwsBindings.add(root); 211 } else if (isValidJaxbBindingFile(reader)) { 212 env.addJaxbBindingFile(bindingFile, is); 213 } else { 214 Message msg = new Message("UNKONW_BINDING_FILE", LOG, bindingFile); 215 throw new ToolException(msg); 216 } 217 } 218 219 private boolean isValidJaxbBindingFile(XMLStreamReader reader) { 220 if (ToolConstants.JAXB_BINDINGS.equals(reader.getName())) { 221 return true; 222 } 223 return false; 224 } 225 226 private boolean isValidJaxwsBindingFile(String bindingLocation, XMLStreamReader reader) { 227 try { 228 if (ToolConstants.JAXWS_BINDINGS.equals(reader.getName())) { 229 String wsdlURL = (String )env.get(ToolConstants.CFG_WSDLURL); 230 wsdlURL = ProcessorUtil.absolutize(ProcessorUtil.getFileOrURLName(wsdlURL)); 231 String wsdlLocation = reader.getAttributeValue(null, "wsdlLocation"); 232 if (StringUtils.isFileExist(bindingLocation) && !StringUtils.isFileAbsolute(wsdlLocation)) { 233 String basedir = new File(bindingLocation).getParent(); 234 wsdlLocation = new File(basedir, wsdlLocation).getAbsolutePath(); 235 } 236 wsdlLocation = ProcessorUtil.absolutize(ProcessorUtil.getFileOrURLName(wsdlLocation)); 237 238 if (!StringUtils.getURL(wsdlURL).equals(StringUtils.getURL(wsdlLocation))) { 239 Message msg = new Message("NOT_POINTTO_URL", LOG, new Object []{wsdlLocation, wsdlURL}); 240 throw new ToolException(msg); 241 } 242 } else { 243 return false; 244 } 245 } catch (MalformedURLException e) { 246 Message msg = new Message("CAN_NOT_GET_WSDL_LOCATION", LOG); 247 throw new ToolException(msg, e); 248 } 249 return true; 250 } 251 252 private Element parse(InputSource source) { 253 try { 254 DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); 255 builderFactory.setNamespaceAware(true); 256 builderFactory.setValidating(false); 257 DocumentBuilder builder = builderFactory.newDocumentBuilder(); 258 builder.setErrorHandler(new ErrorHandler () { 259 public void error(SAXParseException e) 260 throws SAXParseException { 261 throw e; 262 } 263 264 public void fatalError(SAXParseException e) 265 throws SAXParseException { 266 throw e; 267 } 268 269 public void warning(SAXParseException err) 270 throws SAXParseException { 271 } 273 }); 274 275 return builder.parse(source).getDocumentElement(); 277 } catch (ParserConfigurationException e) { 278 throw new ToolException("parsing.parserConfigException", e); 279 } catch (FactoryConfigurationError e) { 280 throw new ToolException("parsing.factoryConfigException", e); 281 } catch (SAXException e) { 282 throw new ToolException("parsing.saxException", e); 283 } catch (IOException e) { 284 throw new ToolException("parsing.saxException", e); 285 } 286 } 287 288 private void evaluateBindingsNode(Element bindings, String expression) throws WSDLException { 289 BindingsNode bindingsNode = evaluateXPathNode(expression); 290 if (bindingsNode == null) { 291 return; 292 } 293 bindingsNode.setElement(bindings); 294 295 JAXWSBindingParser bindingsParser = new JAXWSBindingParser(); 296 JAXWSBinding jaxwsBinding = bindingsParser.parse(bindingsNode, this.definition); 297 298 if (bindingsNode.getParentType().equals(Definition.class)) { 299 definitionExtensions.put(bindingsNode, jaxwsBinding); 300 } 301 302 if (bindingsNode.getParentType().equals(PortType.class)) { 303 portTypeExtensions.put(bindingsNode, jaxwsBinding); 304 } 305 306 if (bindingsNode.getParentType().equals(Operation.class)) { 307 operationExtensions.put(bindingsNode, jaxwsBinding); 308 } 309 310 if (bindingsNode.getParentType().equals(BindingOperation.class)) { 311 operationExtensions.put(bindingsNode, jaxwsBinding); 312 } 313 314 } 315 316 private BindingsNode evaluateXPathNode(String expression) { 317 String [] parts = expression.split("/"); 318 if (parts == null) { 319 return null; 320 } 321 322 BindingsNode node = new BindingsNode(); 323 node.setXPathExpression(expression); 324 for (int i = parts.length - 1; i > 0; i--) { 325 if (parts[i].startsWith("wsdl:definitions")) { 326 node.setParentType(Definition.class); 327 break; 328 } 329 if (parts[i].startsWith("wsdl:binding")) { 330 node.setParentType(Binding.class); 331 node.setNodeName(getNodeName(parts[i])); 332 break; 333 } 334 if (parts[i].startsWith("wsdl:portType")) { 335 node.setParentType(PortType.class); 336 node.setNodeName(getNodeName(parts[i])); 337 break; 338 } 339 if (parts[i].startsWith("wsdl:operation")) { 340 if (i > 1 && parts[i - 1].startsWith("wsdl:binding")) { 341 node.setParentType(BindingOperation.class); 342 } else if (i > 1 && parts[i - 1].startsWith("wsdl:portType")) { 343 node.setParentType(Operation.class); 344 } 345 node.setNodeName(getNodeName(parts[i])); 346 break; 347 } 348 } 349 return node; 350 } 351 352 private String getNodeName(String expression) { 353 return StringUtils.extract(expression, "[@name='", "']"); 354 } 355 356 private String wrapper(String nodeName) { 357 return StringUtils.wrapper(nodeName, "[@name='", "']"); 358 } 359 } 360 | Popular Tags |