This project has retired. For details please refer to its Attic page.
RMITransport 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  import java.util.Properties;
21  
22  import javax.naming.InitialContext;
23  import javax.xml.parsers.DocumentBuilder;
24  import javax.xml.parsers.DocumentBuilderFactory;
25  
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  import org.apache.ws.scout.util.XMLUtils;
29  import org.w3c.dom.Document;
30  import org.w3c.dom.Element;
31  import org.w3c.dom.Node;
32  
33  /**
34   * RMI Message transport class.
35   * 
36   * <p>This transport calls jUDDI using RMI.</p>
37   * 
38   * @author Kurt Stam (kurt.stam@redhat.com)
39   */
40  public class RMITransport implements Transport
41  {
42    // private reference to the jUDDI logger
43    private static Log log = LogFactory.getLog(RMITransport.class);
44  
45    /** 
46     * Sends an element and returns an element.
47     */
48    public Element send(Element request,URI endpointURI)
49      throws TransportException
50    {    
51      Element response = null;
52  
53      if (log.isDebugEnabled()) {
54      	log.debug("\nRequest message:\n" + XMLUtils.convertNodeToXMLString(request));
55      	log.debug("Calling " + endpointURI + " using rmi");
56      }
57      
58      try {
59  		String host       = endpointURI.getHost();
60  		int port          = endpointURI.getPort();
61  		String scheme     = endpointURI.getScheme();
62      	String service    = endpointURI.getPath();
63      	String className  = endpointURI.getQuery();
64      	String methodName = endpointURI.getFragment();
65      	Properties env    = new Properties();
66      	//It be a lot nicer if this is configured through properties, but for now
67      	//I'd like to keep the changes localized, so this seems pretty reasonable.
68      	String factoryInitial = SecurityActions.getProperty("java.naming.factory.initial");
69          if (factoryInitial==null) factoryInitial = "org.jnp.interfaces.NamingContextFactory";
70          String factoryURLPkgs = SecurityActions.getProperty("java.naming.factory.url.pkgs");
71          if (factoryURLPkgs==null) factoryURLPkgs = "org.jboss.naming";
72          env.setProperty("java.naming.factory.initial", factoryInitial);
73          env.setProperty("java.naming.factory.url.pkgs", factoryURLPkgs);
74      	env.setProperty("java.naming.provider.url", scheme + "://" + host + ":" + port);
75      	log.debug("Initial Context using env=" + env.toString());
76      	InitialContext context = new InitialContext(env);
77      	log.debug("Calling service=" + service + ", Class = " + className + ", Method=" + methodName);
78      	//Looking up the object (i.e. Publish)
79      	Object requestHandler = context.lookup(service);
80      	//Loading up the stub
81      	Class<?> c = Class.forName(className);
82      	//Getting a handle to method we want to call (i.e. publish.publish(Element element))
83      	Method method = c.getMethod(methodName, Element.class);
84      	//Calling that method
85      	Node node = (Node) method.invoke(requestHandler, request);
86      	//The result is in the first element
87      	if (node.getFirstChild()!=null) {
88      		response = (Element) node.getFirstChild();
89      	}
90      }
91      catch (Exception ex) {
92        throw new TransportException(ex);
93      }
94      if (log.isDebugEnabled()) {
95      	log.debug("\nResponse message:\n" + XMLUtils.convertNodeToXMLString(response));
96      }
97      return response;
98    }
99    
100   /**
101    * Sends an XML, responds with an XML.
102    */
103   public String send(String request,URI endpointURI)
104     throws TransportException
105   {    
106     String response = null;
107     log.debug("\nRequest message:\n" + request);
108     try {
109     	DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
110         DocumentBuilder parser = factory.newDocumentBuilder();
111         Document document = parser.parse(request);
112         Element element = document.getDocumentElement();
113         response= XMLUtils.convertNodeToXMLString(send(element, endpointURI));
114     } catch (Exception ex) { 
115     	throw new TransportException(ex);
116     }
117     log.debug("\nResponse message:\n" + response);
118     return response;
119   }
120   
121   
122 }