Difference between revisions of "EclipseLink/Examples/DBWS/AdvancedJavase6Containerless"

From Eclipsepedia

Jump to: navigation, search
(DBWSBuilder javase packager)
Line 24: Line 24:
 
<source lang="text">
 
<source lang="text">
 
wls packager
 
wls packager
  root of war file
+
  root of .war file
 
     \---web-inf
 
     \---web-inf
 
     |  web.xml
 
     |  web.xml
Line 44: Line 44:
  
 
javase packager
 
javase packager
  root of jar file
+
  root of .jar file
 
     \---meta-inf
 
     \---meta-inf
 
     |  eclipselink-dbws.xml
 
     |  eclipselink-dbws.xml
Line 81: Line 81:
 
@WebServiceProvider(
 
@WebServiceProvider(
 
     /* wsdlLocation not present in @WebServiceProvider annotation */
 
     /* wsdlLocation not present in @WebServiceProvider annotation */
     serviceName = "crudService",
+
     serviceName = "testService",
     portName = "crudServicePort",
+
     portName = "testServicePort",
     targetNamespace = "urn:crudService"
+
     targetNamespace = "urn:testService"
 
)
 
)
 
@ServiceMode(MESSAGE)
 
@ServiceMode(MESSAGE)
Line 92: Line 92:
 
</source>
 
</source>
  
 +
With the <tt>.jar</tt> on the classpath, an Endpoint can be created programmatically:
 +
<source lang="java5">
 +
//java imports
 +
...
 +
import static javax.xml.ws.BindingProvider.ENDPOINT_ADDRESS_PROPERTY;
 +
import static javax.xml.ws.soap.SOAPBinding.SOAP11HTTP_BINDING;
 +
...
 +
//domain imports
 +
import _dbws.DBWSProvider;
  
 +
public class ContainerlessTest {
 +
 +
    static final String ENDPOINT_ADDRESS =
 +
        "http://localhost:9999/test";
 +
 +
    public static void main(String[] args) {
 +
 +
        // build Endpoint
 +
        Endpoint endpoint = Endpoint.create(new DBWSProvider());
 +
        endpoint.publish(ENDPOINT_ADDRESS);
 +
 +
        // retrieve info from Provider's annotations
 +
        WebServiceProvider dbwsProvider = DBWSProvider.class.getAnnotation(WebServiceProvider.class);
 +
        String serviceNamspace = dbwsProvider.targetNamespace();
 +
        String serviceName = dbwsProvider.serviceName();
 +
        String portName = dbwsProvider.portName();
 +
 +
        // build service
 +
        QName serviceQName = new QName(serviceNamspace, serviceName);
 +
        QName portQName = new QName(serviceNamspace, portName);
 +
        Service testService = Service.create(serviceQName);
 +
        testService.addPort(portQName, SOAP11HTTP_BINDING, ENDPOINT_ADDRESS);
 +
 +
        // build dispatch to send SOAP msg
 +
        Dispatch<SOAPMessage> dispatch = testService.createDispatch(portQName, SOAPMessage.class,
 +
            Service.Mode.MESSAGE);
 +
        BindingProvider bp = (BindingProvider)dispatch;
 +
        Map<String, Object> rc = bp.getRequestContext();
 +
        rc.put(ENDPOINT_ADDRESS_PROPERTY,ENDPOINT_ADDRESS);
 +
 +
        MessageFactory factory = MessageFactory.newInstance();
 +
        SOAPMessage request = factory.createMessage();
 +
        SOAPPart part = request.getSOAPPart();
 +
        DOMSource domSource = new DOMSource(db.parse(
 +
            new InputSource(new StringReader(FIND_BY_PK_REQUEST))));
 +
        part.setContent(domSource);
 +
        SOAPMessage response = null;
 +
        try {
 +
            response = dispatch.invoke(request);
 +
        }
 +
        catch (Exception e) {
 +
            e.printStackTrace();
 +
        }
 +
        if (response != null) {
 +
            Source src = response.getSOAPPart().getContent();
 +
            TransformerFactory tf = TransformerFactory.newInstance();
 +
            Transformer transformer = tf.newTransformer();
 +
            DOMResult result = new DOMResult();
 +
            transformer.transform(src, result);
 +
            Document resultDoc = (Document)result.getNode();
 +
            System.out.print(documentToString(resultDoc));
 +
        }
 +
 +
        // tearDown service
 +
        endpoint.stop();
 +
    }
 +
 +
    static final String FIND_BY_PK_REQUEST =
 +
        "<env:Envelope xmlns:env=\"http://schemas.xmlsoap.org/soap/envelope/\">" +
 +
          "<env:Body>" +
 +
            "<ns1:findByPrimaryKey_empType xmlns:ns1=\"urn:testService\" xmlns=\"urn:test\">" +
 +
              "<ns1:id>7369</ns1:id>" +
 +
            "</ns1:findByPrimaryKey_empType>" +
 +
          "</env:Body>" +
 +
        "</env:Envelope>";
 +
 +
    static String documentToString(Document doc) {
 +
        DOMSource domSource = new DOMSource(doc);
 +
        StringWriter stringWriter = new StringWriter();
 +
        StreamResult result = new StreamResult(stringWriter);
 +
        try {
 +
            Transformer transformer = TransformerFactory.newInstance().newTransformer();
 +
            transformer.setOutputProperty("indent", "yes");
 +
            transformer.transform(domSource, result);
 +
            return stringWriter.toString();
 +
        } catch (Exception e) {
 +
            // e.printStackTrace();
 +
            return "<empty/>";
 +
        }
 +
    } 
 +
}
 +
</source>
  
 
</onlyinclude>
 
</onlyinclude>

Revision as of 16:42, 20 April 2009


DBWS Services running 'containerless' using the Endpoint API

It is possible to run a Web service using Javase 6's "containerless" javax.xml.ws.Endpoint API.

Javase 6 ships with the JAX-WS 2.x APIx inside the JDK - versions prior to update 4 have JAX-WS 2.0 APIs, which may cause some issues with later API requirements: to fix this, copy jaxws-api.jar and jaxb-api.jar into the endorsed directory (typically $JAVA_HOME/lib/endorsed or $JDK_HOME/jre/lib/endorsed). Please see this link for more details.

DBWSBuilder javase packager

When invoked from the command-line, the DBWSBuilder offers out-of-the-box support for building and packaging DBWS Services for either the WebLogic server or for running 'containerless' using the javax.xml.ws.Endpoint API

prompt > dbwsbuilder.cmd -builderFile {path_to_builder.xml} -stageDir {path_to_stageDir} -packageAs {packager}
Available packagers:
  -packageAs:[default=archive] wls [warFilename]
  -packageAs:[default=archive] javase [jarFilename]
                               ^^^^^^

The content of the .jar file is arranged differently from how the wls packager builds a .war file:

wls packager
 root of .war file
    \---web-inf
    |   web.xml
    |
    +---classes
    |   |
    |   +---META-INF
    |   |       eclipselink-dbws.xml
    |   |       eclipselink-dbws-or.xml
    |   |       eclipselink-dbws-ox.xml
    |   |       eclipselink-dbws-sessions.xml
    |   |
    |   \---_dbws
    |           DBWSProvider.class            -- auto-generated JAX-WS 2.0 Provider
    |
    \---wsdl
            eclipselink-dbws-schema.xsd
            eclipselink-dbws.wsdl
 
javase packager
 root of .jar file
    \---meta-inf
    |   eclipselink-dbws.xml
    |   eclipselink-dbws-or.xml
    |   eclipselink-dbws-ox.xml
    |   eclipselink-dbws-sessions.xml
    |
    +---wsdl
    |   eclipselink-dbws-schema.xsd
    |   eclipselink-dbws.wsdl
    |
    \---_dbws
        DBWSProvider.class

The code-generated _dbws.DBWSProvider class is annotated slightly differently for 'containerless' deployment:

package _dbws;
 
//javase imports
import java.lang.reflect.Method;
 
//Java extension libraries
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.servlet.ServletContext;
import javax.xml.soap.SOAPMessage;
import javax.xml.ws.Provider;
import javax.xml.ws.ServiceMode;
import javax.xml.ws.WebServiceProvider;
import static javax.xml.ws.Service.Mode.MESSAGE;
 
//EclipseLink imports
import org.eclipse.persistence.internal.dbws.ProviderHelper;
 
@WebServiceProvider(
    /* wsdlLocation not present in @WebServiceProvider annotation */
    serviceName = "testService",
    portName = "testServicePort",
    targetNamespace = "urn:testService"
)
@ServiceMode(MESSAGE)
public class DBWSProvider extends ProviderHelper implements Provider<SOAPMessage> {
    public  DBWSProvider() {
        super();
    }

With the .jar on the classpath, an Endpoint can be created programmatically:

//java imports
...
import static javax.xml.ws.BindingProvider.ENDPOINT_ADDRESS_PROPERTY;
import static javax.xml.ws.soap.SOAPBinding.SOAP11HTTP_BINDING;
...
//domain imports
import _dbws.DBWSProvider;
 
public class ContainerlessTest {
 
    static final String ENDPOINT_ADDRESS = 
        "http://localhost:9999/test";
 
    public static void main(String[] args) {
 
        // build Endpoint
        Endpoint endpoint = Endpoint.create(new DBWSProvider());
        endpoint.publish(ENDPOINT_ADDRESS);
 
        // retrieve info from Provider's annotations
        WebServiceProvider dbwsProvider = DBWSProvider.class.getAnnotation(WebServiceProvider.class);
        String serviceNamspace = dbwsProvider.targetNamespace();
        String serviceName = dbwsProvider.serviceName();
        String portName = dbwsProvider.portName();
 
        // build service
        QName serviceQName = new QName(serviceNamspace, serviceName);
        QName portQName = new QName(serviceNamspace, portName);
        Service testService = Service.create(serviceQName);
        testService.addPort(portQName, SOAP11HTTP_BINDING, ENDPOINT_ADDRESS);
 
        // build dispatch to send SOAP msg
        Dispatch<SOAPMessage> dispatch = testService.createDispatch(portQName, SOAPMessage.class,
            Service.Mode.MESSAGE);
        BindingProvider bp = (BindingProvider)dispatch;
        Map<String, Object> rc = bp.getRequestContext();
        rc.put(ENDPOINT_ADDRESS_PROPERTY,ENDPOINT_ADDRESS);
 
        MessageFactory factory = MessageFactory.newInstance();
        SOAPMessage request = factory.createMessage();
        SOAPPart part = request.getSOAPPart();
        DOMSource domSource = new DOMSource(db.parse(
            new InputSource(new StringReader(FIND_BY_PK_REQUEST))));
        part.setContent(domSource);
        SOAPMessage response = null;
        try {
            response = dispatch.invoke(request);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        if (response != null) {
            Source src = response.getSOAPPart().getContent();
            TransformerFactory tf = TransformerFactory.newInstance();
            Transformer transformer = tf.newTransformer();
            DOMResult result = new DOMResult();
            transformer.transform(src, result);
            Document resultDoc = (Document)result.getNode();
            System.out.print(documentToString(resultDoc));
        }
 
        // tearDown service
        endpoint.stop();
    }
 
    static final String FIND_BY_PK_REQUEST =
        "<env:Envelope xmlns:env=\"http://schemas.xmlsoap.org/soap/envelope/\">" +
          "<env:Body>" +
            "<ns1:findByPrimaryKey_empType xmlns:ns1=\"urn:testService\" xmlns=\"urn:test\">" +
               "<ns1:id>7369</ns1:id>" +
            "</ns1:findByPrimaryKey_empType>" +
          "</env:Body>" +
         "</env:Envelope>";
 
    static String documentToString(Document doc) {
        DOMSource domSource = new DOMSource(doc);
        StringWriter stringWriter = new StringWriter();
        StreamResult result = new StreamResult(stringWriter);
        try {
            Transformer transformer = TransformerFactory.newInstance().newTransformer();
            transformer.setOutputProperty("indent", "yes");
            transformer.transform(domSource, result);
            return stringWriter.toString();
        } catch (Exception e) {
            // e.printStackTrace();
            return "<empty/>";
        }
    }   
}