1 package org.objectweb.celtix.bus.handlers; 2 3 import java.io.InputStream ; 4 import java.util.ArrayList ; 5 import java.util.List ; 6 7 import javax.xml.namespace.QName ; 8 import javax.xml.parsers.DocumentBuilder ; 9 import javax.xml.parsers.DocumentBuilderFactory ; 10 import javax.xml.ws.WebServiceException; 11 12 import org.w3c.dom.Document ; 13 import org.w3c.dom.Element ; 14 import org.w3c.dom.Node ; 15 import org.w3c.dom.NodeList ; 16 17 import org.objectweb.celtix.bus.jaxws.configuration.types.HandlerChainType; 18 import org.objectweb.celtix.configuration.ConfigurationItemMetadata; 19 import org.objectweb.celtix.configuration.impl.TypeSchema; 20 import org.objectweb.celtix.configuration.impl.TypeSchemaHelper; 21 22 public class HandlerChainDocument { 23 private static final String HANDLER_CHAIN_TYPE_NAME = "handlerChainType"; 24 private static final String HANDLER_CONFIG_ELEM_NAME = "handler-config"; 25 private static final String HANDLER_CHAIN_ELEM_NAME = "handler-chain"; 26 private static final String HANDLER_CHAIN_NAME_ELEM_NAME = "handler-chain-name"; 27 private static final String HANDLER_ELEM_NAME = "handler"; 28 private static final String HANDLER_NAME_ELEM_NAME = "handler-name"; 29 private static final String HANDLER_CLASS_ELEM_NAME = "handler-class"; 30 private static final String INIT_PARAM_ELEM_NAME = "init=param"; 31 private static final String PARAM_NAME_ELEM_NAME = "param-name"; 32 private static final String PARAM_VALUE_ELEM_NAME = "param-value"; 33 private static final String JAXWS_TYPES_URI = "http://celtix.objectweb.org/bus/jaxws/configuration/types"; 34 35 private List <HandlerChainType> chains; 36 37 HandlerChainDocument(InputStream is, boolean doValidate) { 38 chains = new ArrayList <HandlerChainType>(); 39 try { 40 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 41 dbf.setNamespaceAware(false); 42 DocumentBuilder builder = dbf.newDocumentBuilder(); 43 Document srcDoc = builder.parse(is); 44 dbf.setNamespaceAware(true); 45 Document destDoc = builder.newDocument(); 46 transform(srcDoc, destDoc); 47 48 NodeList chainNodes = destDoc.getFirstChild().getChildNodes(); 49 for (int i = 0; i < chainNodes.getLength(); i++) { 50 Node node = chainNodes.item(i); 51 if (Node.ELEMENT_NODE == node.getNodeType() 52 && HANDLER_CHAIN_ELEM_NAME.equals(getNodeName(node))) { 53 54 String location = "schemas/configuration/jaxws-types.xsd"; 55 TypeSchema ts = new TypeSchemaHelper(true).get(JAXWS_TYPES_URI, null, location); 56 57 ConfigurationItemMetadata mdi = new ConfigurationItemMetadata() { 58 public Object getDefaultValue() { 59 return null; 60 } 61 62 public LifecyclePolicy getLifecyclePolicy() { 63 return LifecyclePolicy.STATIC; 64 } 65 66 public String getName() { 67 return "handlerChain"; 68 } 69 70 public QName getType() { 71 return new QName (JAXWS_TYPES_URI, HANDLER_CHAIN_TYPE_NAME); 72 } 73 74 }; 75 76 Object obj = ts.unmarshalDefaultValue(mdi, (Element )node, doValidate); 77 chains.add((HandlerChainType)obj); 78 } 79 } 80 } catch (Exception ex) { 81 if (ex instanceof WebServiceException) { 82 throw (WebServiceException)ex; 83 } 84 throw new WebServiceException(ex); 85 } 86 } 87 88 HandlerChainType getChain(String name) { 89 if (null == name || "".equals(name)) { 90 return chains.size() > 0 ? chains.get(0) : null; 91 } 92 for (HandlerChainType hc : chains) { 93 if (name.equals(hc.getHandlerChainName())) { 94 return hc; 95 } 96 } 97 return null; 98 } 99 100 private String getNodeName(Node node) { 101 String name = node.getNodeName(); 102 if (name.contains(":")) { 103 name = name.substring(name.indexOf(":") + 1); 104 } 105 return name; 106 } 107 108 private void transform(Document src, Document dest) { 109 Node destNode = dest.createElement(HANDLER_CONFIG_ELEM_NAME); 110 dest.appendChild(destNode); 111 Node srcNode = src.getFirstChild(); 112 createChainNodes(src, srcNode, dest, destNode); 113 } 114 115 private void createChainNodes(Document src, Node srcNode, Document dest, Node destNode) { 116 NodeList nodes = srcNode.getChildNodes(); 117 for (int i = 0; i < nodes.getLength(); i++) { 118 Node node = nodes.item(i); 119 if (Node.ELEMENT_NODE == node.getNodeType() 120 && HANDLER_CHAIN_ELEM_NAME.equals(getNodeName(node))) { 121 Element el = dest.createElementNS(JAXWS_TYPES_URI, HANDLER_CHAIN_ELEM_NAME); 122 destNode.appendChild(el); 123 createLeafNode(src, node, dest, el, HANDLER_CHAIN_NAME_ELEM_NAME); 124 createHandlerNodes(src, node, dest, el); 125 } 126 } 127 } 128 129 private void createLeafNode(Document src, Node srcNode, Document dest, Node destNode, String type) { 130 NodeList nodes = srcNode.getChildNodes(); 131 for (int i = 0; i < nodes.getLength(); i++) { 132 Node node = nodes.item(i); 133 if (Node.ELEMENT_NODE == node.getNodeType() && type.equals(getNodeName(node))) { 134 Element el = dest.createElementNS(JAXWS_TYPES_URI, type); 135 el.setTextContent(node.getTextContent()); 136 destNode.appendChild(el); 137 break; 138 } 139 } 140 } 141 142 private void createHandlerNodes(Document src, Node srcNode, Document dest, Node destNode) { 143 NodeList nodes = srcNode.getChildNodes(); 144 for (int i = 0; i < nodes.getLength(); i++) { 145 Node node = nodes.item(i); 146 if (Node.ELEMENT_NODE == node.getNodeType() && HANDLER_ELEM_NAME.equals(getNodeName(node))) { 147 Element el = dest.createElementNS(JAXWS_TYPES_URI, HANDLER_ELEM_NAME); 148 destNode.appendChild(el); 149 createLeafNode(src, node, dest, el, HANDLER_NAME_ELEM_NAME); 150 createLeafNode(src, node, dest, el, HANDLER_CLASS_ELEM_NAME); 151 createInitParamNodes(src, node, dest, el); 152 } 153 } 154 } 155 156 private void createInitParamNodes(Document src, Node srcNode, Document dest, Node destNode) { 157 NodeList nodes = srcNode.getChildNodes(); 158 for (int i = 0; i < nodes.getLength(); i++) { 159 Node node = nodes.item(i); 160 if (Node.ELEMENT_NODE == node.getNodeType() && INIT_PARAM_ELEM_NAME.equals(getNodeName(node))) { 161 Element el = dest.createElementNS(JAXWS_TYPES_URI, INIT_PARAM_ELEM_NAME); 162 destNode.appendChild(el); 163 createLeafNode(src, node, dest, el, PARAM_NAME_ELEM_NAME); 164 createLeafNode(src, node, dest, el, PARAM_VALUE_ELEM_NAME); 165 } 166 } 167 } 168 } 169 | Popular Tags |