01: /*
02: * Copyright 2001-2004 The Apache Software Foundation.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16: package org.apache.ws.scout.transport;
17:
18: import java.lang.reflect.Method;
19: import java.net.URI;
20:
21: import javax.xml.parsers.DocumentBuilder;
22: import javax.xml.parsers.DocumentBuilderFactory;
23:
24: import org.apache.commons.logging.Log;
25: import org.apache.commons.logging.LogFactory;
26: import org.apache.ws.scout.registry.RegistryException;
27: import org.apache.ws.scout.util.XMLUtils;
28: import org.w3c.dom.Document;
29: import org.w3c.dom.Element;
30: import org.w3c.dom.Node;
31:
32: /**
33: * Local Message transport class.
34: *
35: * <p>This transpor calls jUDDI directly.</p>
36: *
37: * @author Kurt Stam (kurt.stam@redhat.com)
38: */
39: public class LocalTransport implements Transport {
40: private static Log log = LogFactory.getLog(LocalTransport.class);
41:
42: /**
43: * Sends an element and returns an element.
44: */
45: public Element send(Element request, URI endpointURI)
46: throws RegistryException {
47: Element response = null;
48:
49: if (log.isDebugEnabled()) {
50: log.debug("\nRequest message:\n"
51: + XMLUtils.convertNodeToXMLString(request));
52: log.debug("Calling " + endpointURI + " locally");
53: }
54: try {
55: String className = endpointURI.getPath();
56: String methodName = endpointURI.getFragment();
57: log.debug("Calling class=" + className);
58: log.debug("Method=" + methodName);
59: Class<?> c = Class.forName(className);
60: Object requestHandler = c.newInstance();
61: Method method = c.getMethod(methodName, Element.class);
62: Node node = (Node) method.invoke(requestHandler, request);
63: response = (Element) node.getFirstChild();
64: } catch (Exception ex) {
65: throw new RegistryException(ex);
66: }
67: if (log.isDebugEnabled()) {
68: log.debug("\nResponse message:\n"
69: + XMLUtils.convertNodeToXMLString(response));
70: }
71: return response;
72: }
73:
74: /**
75: * Sends an XML, responds with an XML.
76: */
77: public String send(String request, URI endpointURI)
78: throws RegistryException {
79: String response = null;
80: log.debug("\nRequest message:\n" + request);
81: try {
82: DocumentBuilderFactory factory = DocumentBuilderFactory
83: .newInstance();
84: DocumentBuilder parser = factory.newDocumentBuilder();
85: Document document = parser.parse(request);
86: Element element = document.getDocumentElement();
87: response = XMLUtils.convertNodeToXMLString(send(element,
88: endpointURI));
89: } catch (Exception ex) {
90: ex.printStackTrace();
91: throw new RegistryException(ex);
92: }
93: log.debug("\nResponse message:\n" + response);
94: return response;
95: }
96:
97: }
|