Wednesday, 12 September 2012

Forget about WSIF: welcome Spring

A few years ago I was very proud that I managed to call java from BPEL through WSIF. I think that the method pretty much should work in 11g. But my method back than used the deprecated tool Schema Compile. So I would need to figure out how to do that with JAXB or something. But now in the later patchsets of Weblogic/SOASuite there is a far more nicer method: Spring. Now, my experience with Spring starts since yesterday... Spring is a method to embed simple java objects (Plain Old Java Objects: POJO's) into an application server context. In SOASuite it is implemented in a real cool way, so that you can embed them in a real declarative way. If you google your way around you can find several examples on Spring and SOASuite. Documentation you can find here. And Edwin Biemond created an article about it. But unfortunately, it did not become clear to me how I had to implement my setup.

In my previous article I explained how to call Oracle Reports from java. I want to call that from BPEL or Mediator. Earlier I thought about the Socket adapter or the HTTP service-reference. But those options failed because the examples I found expect XML as an input (REST), where I needed to call an URL with parameters. Or they prevented me to call a url on port 80 (on which our reportservers listen...).
In that article you read that I had a service, and the input is a bean and the response is a bean. The reason of that was that I needed to pass multiple attributes as parameter and receive multiple attributes as a response. In reallife you might have a service with a bean structure as a request and response that reflect a complete hierarchical XML structure. The examples I found just talked about classes with elementary datatypes as  parameters and results. So how to set up your spring context to be able to do your call? At first I thought I needed just one context for the service. But it turns out that you have to create a context for every bean that plays a part in the interface (basically your service and every bean in the call parameter and response class structure).

To be able to expose the java classes to a WSDL (that is generated for you when you wire the service to BPEL) you have to generate Interfaces on your classes. So building further on my previous story, that would be on the classes ReportRequest, ReportResponse and CallReportsService.
This can be generated for you as follows. Open the ReportRequest class and right click in the source, choose Refactor and then Extract Interface:
Then you can select all the methods you want to expose:


In my previous article I put a method in my request bean to construct the Reports Server url (getReportsURL()). This method I don't want to have exposed in my WSDL. So I did not select that in the list. You can do the same for the other two classes.

Now it's time to declare the Spring-bean-structure. Open the composite editor and right-click:
When you choose Insert SpringContext, you might get a "Spring Not Available" error:
Go to Help->Check for Updates and search for the Spring & Weblogic SCA Addon:
Add it and restart JDeveloper.

Now you can add a SpringContext to your composite:
 Edit the Spring Context (CallReportsServer.xml). Then in the component palette you can look for the Weblogic SCA tab, and drag a service into the composite:

Name it CallReportsService, and as a target the CallReportsServiceBean. As a type you can browse to your classes, choose here the interface ICallReportsService.
Now you can add a bean to the context. The tag for this can be found on the "Spring 2.5 Core" page. But you can also type it in of course:
 Then you have to define it. Convenient is the Inspector palette:

 The Id of the bean should be the same as the target of the service: CallReportsServiceBean.
The class can be browsed the same way as the type in the Service. Choose here the implementation class "CallReportsService".

Now we can do the same for the other two beans. For the two input/output beans (ReportRequest and ReportResponse) I choose the bean implementation itself as the type. In my first setup I encountered that at Runtime SOASuite tries to call the public no-parameter-constructor of the type. Since an interface does not have a constructor, it failed. So I replaced that with the implementation bean.

If you have the three beans you can tie them together:
This gets you the following context definitions.
CallReportsServer.xml:
<?xml version="1.0" encoding="windows-1252" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:util="http://www.springframework.org/schema/util"
       xmlns:jee="http://www.springframework.org/schema/jee"
       xmlns:lang="http://www.springframework.org/schema/lang"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:sca="http://xmlns.oracle.com/weblogic/weblogic-sca"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/tool http://www.springframework.org/schema/tool/spring-tool-2.5.xsd http://xmlns.oracle.com/weblogic/weblogic-sca META-INF/weblogic-sca.xsd">
  <!--Spring Bean definitions go here-->
  <sca:service name="CallReportsService" target="CallReportsServiceBean"
               type="nl.darwinit.soa.callreportsservice.ICallReportsService"/>
  <bean id="CallReportsServiceBean"
        class="nl.darwinit.soa.callreportsservice.CallReportsService"/>
  <sca:reference type="nl.darwinit.soa.callreportsservice.ReportRequest"
                 name="ReportRequest.ReportRequest"/>
  <sca:reference type="nl.darwinit.soa.callreportsservice.ReportResponse"
                 name="ReportResponse.ReportResponse"/>
</beans>

ReportRequest.xml:
<?xml version="1.0" encoding="windows-1252" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:util="http://www.springframework.org/schema/util"
       xmlns:jee="http://www.springframework.org/schema/jee"
       xmlns:lang="http://www.springframework.org/schema/lang"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:sca="http://xmlns.oracle.com/weblogic/weblogic-sca"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/tool http://www.springframework.org/schema/tool/spring-tool-2.5.xsd http://xmlns.oracle.com/weblogic/weblogic-sca META-INF/weblogic-sca.xsd">
  <!--Spring Bean definitions go here-->
  <sca:service name="ReportRequest" target="ReportRequestBean"
               type="nl.darwinit.soa.callreportsservice.ReportRequest"/>
  <bean id="ReportRequestBean"
        class="nl.darwinit.soa.callreportsservice.ReportRequest"/>
</beans>

ReportResponse.xml:
<?xml version="1.0" encoding="windows-1252" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:util="http://www.springframework.org/schema/util"
       xmlns:jee="http://www.springframework.org/schema/jee"
       xmlns:lang="http://www.springframework.org/schema/lang"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:sca="http://xmlns.oracle.com/weblogic/weblogic-sca"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/tool http://www.springframework.org/schema/tool/spring-tool-2.5.xsd http://xmlns.oracle.com/weblogic/weblogic-sca META-INF/weblogic-sca.xsd">
  <!--Spring Bean definitions go here-->
  <sca:service name="ReportResponse" target="ReportResponseBean"
               type="nl.darwinit.soa.callreportsservice.ReportResponse"/>
  <bean id="ReportResponseBean"
        class="nl.darwinit.soa.callreportsservice.ReportResponse"/>
</beans>

Well, actually that is about it. Now lets create a BPEL process.


I changed the xsd to:
<?xml version="1.0" encoding="UTF-8"?> 
<schema attributeFormDefault="unqualified"
 elementFormDefault="qualified"
 targetNamespace="http://xmlns.darwin-it.nl/CallReportsService/CallReportsService/CallReportsService"
 xmlns="http://www.w3.org/2001/XMLSchema" xmlns:xsd="http://www.w3.org/2001/XMLSchema" >
 <element name="process">
  <complexType>
   <sequence>
    <xsd:element name="aanvraagID" type="xsd:string" minOccurs="0"/>
    <xsd:element name="destination" type="xsd:string" minOccurs="0"/>
    <xsd:element name="httpTimeOut" type="xsd:int" minOccurs="0"/>
    <xsd:element name="jobName" type="xsd:string" minOccurs="0"/>
    <xsd:element name="previewJN" type="xsd:string" minOccurs="0"/>
    <xsd:element name="reportName" type="xsd:string" minOccurs="0"/>
    <xsd:element name="reportsBaseUrl" type="xsd:string" minOccurs="0"/>
    <xsd:element name="userId" type="xsd:string" minOccurs="0"/>
   </sequence>
  </complexType>
 </element>
 <element name="processResponse">
  <complexType>
   <sequence>
    <xsd:element name="errorCode" type="xsd:string" minOccurs="0"/>
    <xsd:element name="errorMessage" type="xsd:string" minOccurs="0"/>
    <xsd:element name="jobId" type="xsd:string" minOccurs="0"/>
    <xsd:element name="jobStatus" type="xsd:string" minOccurs="0"/>
    <xsd:element name="jobStatusCode" type="xsd:string" minOccurs="0"/>
    <xsd:element name="responseXml" type="xsd:string" minOccurs="0"/>
   </sequence>
  </complexType>
 </element>
</schema>


Now we can tie the CallReportServer component to the BPEL Component:

Trying this will get a message that asks you to compile the classes, click yes:
 
 After the compilation, the wiring has to be redone, which will result in the message that the wsdl is created:
Now you can implement the BPEL Process.
Since I choose the ReportRequest implementation bean as a type in the ReportRequest Spring Context, it turns out that it generated an element for the reportURL getter. You could remove that from the wsdl, but since it is not exposed as a service, you can also leave it that way. Just don't map it to any input.

Now, you would think you're done. But there is one little caveat in the deployment of the spring contexts. We tied the beans neatly together. But the Spring Context of the CallReportServer.xml expects in the references Interfaces as a type. So change those to the interfaces of the beans:

<?xml version="1.0" encoding="windows-1252" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:util="http://www.springframework.org/schema/util"
       xmlns:jee="http://www.springframework.org/schema/jee"
       xmlns:lang="http://www.springframework.org/schema/lang"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:sca="http://xmlns.oracle.com/weblogic/weblogic-sca"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/tool http://www.springframework.org/schema/tool/spring-tool-2.5.xsd http://xmlns.oracle.com/weblogic/weblogic-sca META-INF/weblogic-sca.xsd">
  <!--Spring Bean definitions go here-->
  <sca:service name="CallReportsService" target="CallReportsServiceBean"
               type="nl.darwinit.soa.callreportsservice.ICallReportsService"/>
  <bean id="CallReportsServiceBean"
        class="nl.darwinit.soa.callreportsservice.CallReportsService"/>
  <sca:reference type="nl.darwinit.soa.callreportsservice.IReportRequest"
                 name="ReportRequest.ReportRequest"/>
  <sca:reference type="nl.darwinit.soa.callreportsservice.IReportResponse"
                 name="ReportResponse.ReportResponse"/>
</beans>
(nl.darwinit.soa.callreportsservice.ReportRequest changed to nl.darwinit.soa.callreportsservice.IReportRequest, same for IReportResponse)

Now you can deploy and test.

As you can see, it's a very declarative way to create a webservice on a java class.

I wrapped up my complete project here.





No comments:

Post a Comment