001: /*
002: * Copyright 2001-2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.ws.scout.transport;
017:
018: import java.lang.reflect.Method;
019: import java.net.URI;
020: import java.util.Properties;
021:
022: import javax.naming.InitialContext;
023: import javax.xml.parsers.DocumentBuilder;
024: import javax.xml.parsers.DocumentBuilderFactory;
025:
026: import org.apache.commons.logging.Log;
027: import org.apache.commons.logging.LogFactory;
028: import org.apache.ws.scout.registry.RegistryException;
029: import org.apache.ws.scout.util.XMLUtils;
030: import org.w3c.dom.Document;
031: import org.w3c.dom.Element;
032: import org.w3c.dom.Node;
033:
034: /**
035: * RMI Message transport class.
036: *
037: * <p>This transpor calls jUDDI using RMI.</p>
038: *
039: * @author Kurt Stam (kurt.stam@redhat.com)
040: */
041: public class RMITransport implements Transport {
042: // private reference to the jUDDI logger
043: private static Log log = LogFactory.getLog(RMITransport.class);
044:
045: /**
046: * Sends an element and returns an element.
047: */
048: public Element send(Element request, URI endpointURI)
049: throws RegistryException {
050: Element response = null;
051:
052: if (log.isDebugEnabled()) {
053: log.debug("\nRequest message:\n"
054: + XMLUtils.convertNodeToXMLString(request));
055: log.debug("Calling " + endpointURI + " using rmi");
056: }
057:
058: try {
059: String host = endpointURI.getHost();
060: int port = endpointURI.getPort();
061: String scheme = endpointURI.getScheme();
062: String service = endpointURI.getPath();
063: String className = endpointURI.getQuery();
064: String methodName = endpointURI.getFragment();
065: Properties env = new Properties();
066: //It be a lot nicer if this is configured through properties, but for now
067: //I'd like to keep the changes localized, so this seems pretty reasonable.
068: String factoryInitial = System
069: .getProperty("java.naming.factory.initial");
070: if (factoryInitial == null)
071: factoryInitial = "org.jnp.interfaces.NamingContextFactory";
072: String factoryURLPkgs = System
073: .getProperty("java.naming.factory.url.pkgs");
074: if (factoryURLPkgs == null)
075: factoryURLPkgs = "org.jboss.naming";
076: env.setProperty("java.naming.factory.initial",
077: factoryInitial);
078: env.setProperty("java.naming.factory.url.pkgs",
079: factoryURLPkgs);
080: env.setProperty("java.naming.provider.url", scheme + "://"
081: + host + ":" + port);
082: log.debug("Initial Context using env=" + env.toString());
083: InitialContext context = new InitialContext(env);
084: log.debug("Calling service=" + service + ", Class = "
085: + className + ", Method=" + methodName);
086: //Looking up the object (i.e. Publish)
087: Object requestHandler = context.lookup(service);
088: //Loading up the stub
089: Class<?> c = Class.forName(className);
090: //Getting a handle to method we want to call (i.e. publish.publish(Element element))
091: Method method = c.getMethod(methodName, Element.class);
092: //Calling that method
093: Node node = (Node) method.invoke(requestHandler, request);
094: //The result is in the first element
095: response = (Element) node.getFirstChild();
096: } catch (Exception ex) {
097: throw new RegistryException(ex);
098: }
099: if (log.isDebugEnabled()) {
100: log.debug("\nResponse message:\n"
101: + XMLUtils.convertNodeToXMLString(response));
102: }
103: return response;
104: }
105:
106: /**
107: * Sends an XML, responds with an XML.
108: */
109: public String send(String request, URI endpointURI)
110: throws RegistryException {
111: String response = null;
112: log.debug("\nRequest message:\n" + request);
113: try {
114: DocumentBuilderFactory factory = DocumentBuilderFactory
115: .newInstance();
116: DocumentBuilder parser = factory.newDocumentBuilder();
117: Document document = parser.parse(request);
118: Element element = document.getDocumentElement();
119: response = XMLUtils.convertNodeToXMLString(send(element,
120: endpointURI));
121: } catch (Exception ex) {
122: ex.printStackTrace();
123: throw new RegistryException(ex);
124: }
125: log.debug("\nResponse message:\n" + response);
126: return response;
127: }
128:
129: }
|