This project has retired. For details please refer to its Attic page.
LocalTransport xref
View Javadoc
1   /*
2    * Copyright 2001-2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
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.util.XMLUtils;
27  import org.w3c.dom.Document;
28  import org.w3c.dom.Element;
29  import org.w3c.dom.Node;
30  
31  /**
32   * Local Message transport class.
33   * 
34   * <p>This transport calls jUDDI directly.</p>
35   * 
36   * @author Kurt Stam (kurt.stam@redhat.com)
37   */
38  public class LocalTransport implements Transport
39  {
40    private static Log log = LogFactory.getLog(LocalTransport.class);
41    private String nodeName;
42    private String managerName;
43  
44    public LocalTransport(){};
45    
46    public LocalTransport(String nodeName, String managerName) {
47      super();
48      this.nodeName = nodeName;
49      this.managerName = managerName;
50  }
51  
52  /** 
53     * Sends an element and returns an element.
54     */
55    public Element send(Element request,URI endpointURI)
56      throws TransportException
57    {    
58      Element response = null;
59  
60      if (log.isDebugEnabled()) {
61      	log.debug("\nRequest message:\n" + XMLUtils.convertNodeToXMLString(request));
62      	log.debug("Calling " + endpointURI + " locally");
63      }
64      try {
65      	String className = endpointURI.getPath();
66      	String methodName = endpointURI.getFragment();
67      	log.debug("Calling class=" + className);
68      	log.debug("Method=" + methodName);
69      	Class<?> c = Class.forName(className);
70      	Object requestHandler = c.newInstance();
71      	Node node = null;
72      	if (managerName!=null) {
73      	    Method method = c.getMethod(methodName, Element.class, String.class, String.class);
74      	    node = (Node) method.invoke(requestHandler, request, nodeName, managerName);
75      	} else {
76      	    Method method = c.getMethod(methodName, Element.class);
77      	    node = (Node) method.invoke(requestHandler, request);
78      	}
79      	if (node!=null && node.getFirstChild()!=null) {
80      		response = (Element) node.getFirstChild();
81      	}
82      } catch (Exception ex) {
83        throw new TransportException(ex);
84      }
85      if (log.isDebugEnabled()) {
86      	log.debug("\nResponse message:\n" + XMLUtils.convertNodeToXMLString(response));
87      }
88      return response;
89    }
90    
91    /**
92     * Sends an XML, responds with an XML.
93     */
94    public String send(String request,URI endpointURI)
95      throws TransportException
96    {    
97      String response = null;
98      log.debug("\nRequest message:\n" + request);
99      try {
100     	DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
101         DocumentBuilder parser = factory.newDocumentBuilder();
102         Document document = parser.parse(request);
103         Element element = document.getDocumentElement();
104         response= XMLUtils.convertNodeToXMLString(send(element, endpointURI));
105     } catch (Exception ex) { 
106     	throw new TransportException(ex);
107     }
108     log.debug("\nResponse message:\n" + response);
109     return response;
110   }
111   
112   
113 }