001: package org.objectweb.celtix.bus.handlers;
002:
003: import java.io.InputStream;
004: import java.util.ArrayList;
005: import java.util.List;
006:
007: import javax.xml.namespace.QName;
008: import javax.xml.parsers.DocumentBuilder;
009: import javax.xml.parsers.DocumentBuilderFactory;
010: import javax.xml.ws.WebServiceException;
011:
012: import org.w3c.dom.Document;
013: import org.w3c.dom.Element;
014: import org.w3c.dom.Node;
015: import org.w3c.dom.NodeList;
016:
017: import org.objectweb.celtix.bus.jaxws.configuration.types.HandlerChainType;
018: import org.objectweb.celtix.configuration.ConfigurationItemMetadata;
019: import org.objectweb.celtix.configuration.impl.TypeSchema;
020: import org.objectweb.celtix.configuration.impl.TypeSchemaHelper;
021:
022: public class HandlerChainDocument {
023: private static final String HANDLER_CHAIN_TYPE_NAME = "handlerChainType";
024: private static final String HANDLER_CONFIG_ELEM_NAME = "handler-config";
025: private static final String HANDLER_CHAIN_ELEM_NAME = "handler-chain";
026: private static final String HANDLER_CHAIN_NAME_ELEM_NAME = "handler-chain-name";
027: private static final String HANDLER_ELEM_NAME = "handler";
028: private static final String HANDLER_NAME_ELEM_NAME = "handler-name";
029: private static final String HANDLER_CLASS_ELEM_NAME = "handler-class";
030: private static final String INIT_PARAM_ELEM_NAME = "init=param";
031: private static final String PARAM_NAME_ELEM_NAME = "param-name";
032: private static final String PARAM_VALUE_ELEM_NAME = "param-value";
033: private static final String JAXWS_TYPES_URI = "http://celtix.objectweb.org/bus/jaxws/configuration/types";
034:
035: private List<HandlerChainType> chains;
036:
037: HandlerChainDocument(InputStream is, boolean doValidate) {
038: chains = new ArrayList<HandlerChainType>();
039: try {
040: DocumentBuilderFactory dbf = DocumentBuilderFactory
041: .newInstance();
042: dbf.setNamespaceAware(false);
043: DocumentBuilder builder = dbf.newDocumentBuilder();
044: Document srcDoc = builder.parse(is);
045: dbf.setNamespaceAware(true);
046: Document destDoc = builder.newDocument();
047: transform(srcDoc, destDoc);
048:
049: NodeList chainNodes = destDoc.getFirstChild()
050: .getChildNodes();
051: for (int i = 0; i < chainNodes.getLength(); i++) {
052: Node node = chainNodes.item(i);
053: if (Node.ELEMENT_NODE == node.getNodeType()
054: && HANDLER_CHAIN_ELEM_NAME
055: .equals(getNodeName(node))) {
056:
057: String location = "schemas/configuration/jaxws-types.xsd";
058: TypeSchema ts = new TypeSchemaHelper(true).get(
059: JAXWS_TYPES_URI, null, location);
060:
061: ConfigurationItemMetadata mdi = new ConfigurationItemMetadata() {
062: public Object getDefaultValue() {
063: return null;
064: }
065:
066: public LifecyclePolicy getLifecyclePolicy() {
067: return LifecyclePolicy.STATIC;
068: }
069:
070: public String getName() {
071: return "handlerChain";
072: }
073:
074: public QName getType() {
075: return new QName(JAXWS_TYPES_URI,
076: HANDLER_CHAIN_TYPE_NAME);
077: }
078:
079: };
080:
081: Object obj = ts.unmarshalDefaultValue(mdi,
082: (Element) node, doValidate);
083: chains.add((HandlerChainType) obj);
084: }
085: }
086: } catch (Exception ex) {
087: if (ex instanceof WebServiceException) {
088: throw (WebServiceException) ex;
089: }
090: throw new WebServiceException(ex);
091: }
092: }
093:
094: HandlerChainType getChain(String name) {
095: if (null == name || "".equals(name)) {
096: return chains.size() > 0 ? chains.get(0) : null;
097: }
098: for (HandlerChainType hc : chains) {
099: if (name.equals(hc.getHandlerChainName())) {
100: return hc;
101: }
102: }
103: return null;
104: }
105:
106: private String getNodeName(Node node) {
107: String name = node.getNodeName();
108: if (name.contains(":")) {
109: name = name.substring(name.indexOf(":") + 1);
110: }
111: return name;
112: }
113:
114: private void transform(Document src, Document dest) {
115: Node destNode = dest.createElement(HANDLER_CONFIG_ELEM_NAME);
116: dest.appendChild(destNode);
117: Node srcNode = src.getFirstChild();
118: createChainNodes(src, srcNode, dest, destNode);
119: }
120:
121: private void createChainNodes(Document src, Node srcNode,
122: Document dest, Node destNode) {
123: NodeList nodes = srcNode.getChildNodes();
124: for (int i = 0; i < nodes.getLength(); i++) {
125: Node node = nodes.item(i);
126: if (Node.ELEMENT_NODE == node.getNodeType()
127: && HANDLER_CHAIN_ELEM_NAME
128: .equals(getNodeName(node))) {
129: Element el = dest.createElementNS(JAXWS_TYPES_URI,
130: HANDLER_CHAIN_ELEM_NAME);
131: destNode.appendChild(el);
132: createLeafNode(src, node, dest, el,
133: HANDLER_CHAIN_NAME_ELEM_NAME);
134: createHandlerNodes(src, node, dest, el);
135: }
136: }
137: }
138:
139: private void createLeafNode(Document src, Node srcNode,
140: Document dest, Node destNode, String type) {
141: NodeList nodes = srcNode.getChildNodes();
142: for (int i = 0; i < nodes.getLength(); i++) {
143: Node node = nodes.item(i);
144: if (Node.ELEMENT_NODE == node.getNodeType()
145: && type.equals(getNodeName(node))) {
146: Element el = dest
147: .createElementNS(JAXWS_TYPES_URI, type);
148: el.setTextContent(node.getTextContent());
149: destNode.appendChild(el);
150: break;
151: }
152: }
153: }
154:
155: private void createHandlerNodes(Document src, Node srcNode,
156: 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()
161: && HANDLER_ELEM_NAME.equals(getNodeName(node))) {
162: Element el = dest.createElementNS(JAXWS_TYPES_URI,
163: HANDLER_ELEM_NAME);
164: destNode.appendChild(el);
165: createLeafNode(src, node, dest, el,
166: HANDLER_NAME_ELEM_NAME);
167: createLeafNode(src, node, dest, el,
168: HANDLER_CLASS_ELEM_NAME);
169: createInitParamNodes(src, node, dest, el);
170: }
171: }
172: }
173:
174: private void createInitParamNodes(Document src, Node srcNode,
175: Document dest, Node destNode) {
176: NodeList nodes = srcNode.getChildNodes();
177: for (int i = 0; i < nodes.getLength(); i++) {
178: Node node = nodes.item(i);
179: if (Node.ELEMENT_NODE == node.getNodeType()
180: && INIT_PARAM_ELEM_NAME.equals(getNodeName(node))) {
181: Element el = dest.createElementNS(JAXWS_TYPES_URI,
182: INIT_PARAM_ELEM_NAME);
183: destNode.appendChild(el);
184: createLeafNode(src, node, dest, el,
185: PARAM_NAME_ELEM_NAME);
186: createLeafNode(src, node, dest, el,
187: PARAM_VALUE_ELEM_NAME);
188: }
189: }
190: }
191: }
|