Coverage Report - org.apache.ws.scout.transport.RMITransport
 
Classes in this File Line Coverage Branch Coverage Complexity
RMITransport
0%
0/47
0%
0/10
0
 
 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  0
 public class RMITransport implements Transport
 41  
 {
 42  
   // private reference to the jUDDI logger
 43  0
   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  0
     Element response = null;
 52  
 
 53  0
     if (log.isDebugEnabled()) {
 54  0
             log.debug("\nRequest message:\n" + XMLUtils.convertNodeToXMLString(request));
 55  0
             log.debug("Calling " + endpointURI + " using rmi");
 56  
     }
 57  
     
 58  
     try {
 59  0
                 String host       = endpointURI.getHost();
 60  0
                 int port          = endpointURI.getPort();
 61  0
                 String scheme     = endpointURI.getScheme();
 62  0
             String service    = endpointURI.getPath();
 63  0
             String className  = endpointURI.getQuery();
 64  0
             String methodName = endpointURI.getFragment();
 65  0
             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  0
             String factoryInitial = SecurityActions.getProperty("java.naming.factory.initial");
 69  0
         if (factoryInitial==null) factoryInitial = "org.jnp.interfaces.NamingContextFactory";
 70  0
         String factoryURLPkgs = SecurityActions.getProperty("java.naming.factory.url.pkgs");
 71  0
         if (factoryURLPkgs==null) factoryURLPkgs = "org.jboss.naming";
 72  0
         env.setProperty("java.naming.factory.initial", factoryInitial);
 73  0
         env.setProperty("java.naming.factory.url.pkgs", factoryURLPkgs);
 74  0
             env.setProperty("java.naming.provider.url", scheme + "://" + host + ":" + port);
 75  0
             log.debug("Initial Context using env=" + env.toString());
 76  0
             InitialContext context = new InitialContext(env);
 77  0
             log.debug("Calling service=" + service + ", Class = " + className + ", Method=" + methodName);
 78  
             //Looking up the object (i.e. Publish)
 79  0
             Object requestHandler = context.lookup(service);
 80  
             //Loading up the stub
 81  0
             Class<?> c = Class.forName(className);
 82  
             //Getting a handle to method we want to call (i.e. publish.publish(Element element))
 83  0
             Method method = c.getMethod(methodName, Element.class);
 84  
             //Calling that method
 85  0
             Node node = (Node) method.invoke(requestHandler, request);
 86  
             //The result is in the first element
 87  0
             if (node.getFirstChild()!=null) {
 88  0
                     response = (Element) node.getFirstChild();
 89  
             }
 90  
     }
 91  0
     catch (Exception ex) {
 92  0
       throw new TransportException(ex);
 93  0
     }
 94  0
     if (log.isDebugEnabled()) {
 95  0
             log.debug("\nResponse message:\n" + XMLUtils.convertNodeToXMLString(response));
 96  
     }
 97  0
     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  0
     String response = null;
 107  0
     log.debug("\nRequest message:\n" + request);
 108  
     try {
 109  0
             DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
 110  0
         DocumentBuilder parser = factory.newDocumentBuilder();
 111  0
         Document document = parser.parse(request);
 112  0
         Element element = document.getDocumentElement();
 113  0
         response= XMLUtils.convertNodeToXMLString(send(element, endpointURI));
 114  0
     } catch (Exception ex) { 
 115  0
             throw new TransportException(ex);
 116  0
     }
 117  0
     log.debug("\nResponse message:\n" + response);
 118  0
     return response;
 119  
   }
 120  
   
 121  
   
 122  
 }