SCA Java implementation.web (TUSCANY) edited by ant
      Page: 
http://cwiki.apache.org/confluence/display/TUSCANY/SCA+Java+implementation.web
   Changes: 
http://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=92446&originalVersion=4&revisedVersion=5






Content:
---------------------------------------------------------------------

{section:border=false}
{column:width=15%}
{include: SCA Java Subproject Menu}
{include: Java SCA Menu New}
{column}
{column:width=85%}

h3. <implementation.web>

(!) This module is under development. What is described here reflects 
unreleased trunk code and is subject to change. Your input and comments are 
welcome

The Tuscany Java SCA runtime supports components implemented as JEE web modules 
by using the <implementation.web> SCDL extension. Implementation.web is one of 
the SCA extensions being formalized in the OASIS Open Composite Services 
Architecture with a published [specification 
|http://www.oasis-open.org/committees/download.php/28798/SCA_JAVAEE_Integration_V100.pdf]
 document.

The web component implementation SCDL has the following format:

{code}
   <implementation.web web-uri="<module name>" />
{code}

Implementation.web can be used by components such as Servlets, JSPs, and Web 
2.0 style html pages. How it works depends on the way the Tuscany runtime is 
being used.

h4. Using implementation.web with Servlets, Filters, and Event listeners

JEE Servlets, Filters, and Event listeners may acquire references to the 
services wired to a component which use implementation.web as shown in the 
following code snippets.

{code}
<component name="WebClient">
   <implementation.web web-uri=""/>
   <reference name="service" target="HelloworldService"/>
</component>
{code}

Then when using a JEE container with SCA integration (currently only a 
prototype for Tomcat, see below) Servlets, Filters, and Event listeners can use 
annotations to acquire references to the services wired to the component as 
shown in the following Servlet code snippet (note the Servlet field name 
'service' matches the reference name in the SCDL):

{code}
public class HelloworldServlet extends HttpServlet {

    @Reference
    protected HelloworldService service;

  . . .
}
{code}

When the container does not have SCA integration then injection via annotations 
does not work, so to work around this the services need to be manually got from 
the ComponentContext which may be obtained from the application context. This 
is shown in the following code snippet:

{code}
public class HelloworldServlet extends HttpServlet {

    public void init(ServletConfig config) {
       ComponentContext context = 
(ComponentContext)config.getServletContext().getAttribute("org.osoa.sca.ComponentContext");
       HelloworldService service = context .getService(HelloworldService.class, 
"service");
       . . .
    }

  . . .
}
{code}

See the 
[helloworld-servlet|https://svn.apache.org/repos/asf/tuscany/java/sca/samples/helloworld-servlet/]
 sample for the complete code of the above Servlet example.

h4. Using implementation.web with JSPs

A JSP tag library is available to expose SCA components in JSP pages. To use 
SCA references within a JSP use a taglib to include the SCA tags and define the 
reference field with the reference tag.  

{code}
<component name="WebClient">
   <implementation.web web-uri=""/>
   <reference name="service" target="HelloworldService"/>
</component>
{code}

In the .jsp file:
{code}
<%@ taglib uri="http://www.osoa.org/sca/sca_jsp.tld"; prefix="sca" %>
<sca:reference name="service" type="sample.HelloworldService" />

<html>
  <body >
    <%= service.sayHello("world") %>
  </body>
</html>
{code}

See the 
[helloworld-jsp|https://svn.apache.org/repos/asf/tuscany/java/sca/samples/helloworld-jsp/]
 sample for the complete code of the above JSP example.

h4. Using implementation.web with Web 2.0 style browser clients

Web 2.0 style pages can invoke SCA services by making RPC calls from the remote 
browser client to the service-side SCA service. To do this the HTML page 
includes Javascript code to import the SCA ComponentContext and then gets 
service proxys from that ComponentContext. The org.osoa.sca.componentContext.js 
script is generated by Tuscany when a component uses implementation.web.

In the SCDL the reference needs to specify one of the Tuscany Web2.0 bindings 
such as binding.jsonrpc or binding.dwr. 
(TODO: could this be avoided - something like make binding.jsonrpc the default 
sca binding for web2.0 clients?) 

{code}
<component name="WebClient">
   <implementation.web web-uri=""/>
   <reference name="service" target="HelloworldService">
      <tuscany:binding.jsonrpc />
   </reference>
</component>
{code}

In the html page:
{code}
<html>
  <head>

    <script type="text/javascript" 
src="org.osoa.sca.componentContext.js"></script>

    <script language="JavaScript">
    
       function callSayHello() {
          componentContext.getService("service").sayHello(
             document.getElementById('name').value, 
             function(reply) {
                document.getElementById('result').innerHTML=reply;
             });
       }

    </script>

  </head>
  <body >

    . . . 

     <input type="text" id="name" width="10">

     <button name="submit" onclick="callSayHello()">Say hello</button>

     <div id='result'></div>

    . . . 

  </body>
</html>
{code}

See the 
[helloworld-web|https://svn.apache.org/repos/asf/tuscany/java/sca/samples/helloworld-web/]
 sample for the complete code of the above example.

(!) The following on Web 2.0 callbacks is not yet fully implemented, describing 
here to encourage feedback 

Tuscany Web 2.0 clients using implementation.web support SCA asynchronous 
callbacks using what is known as [Comet or Reverse 
Ajax|http://en.wikipedia.org/wiki/Comet_(programming)]. The only Tuscany 
binding that currently supports this in binding.dwr.

The following shows the previous sample changed to use callbacks to receive the 
replies. A callback binding is added to the reference in the SCDL and the 
callback function is defined as a seperate Javascript function defined by the 
client and attached to the service proxy object. 

{code}
<component name="WebClient">
    <implementation.web web-uri=""/>
    <reference name="service" target="HelloworldService">
        <tuscany:binding.dwr />
        <callback>
            <tuscany:binding.dwr />
        </callback>
    </reference>
</component>
{code}

In the html page:
{code}
<html>
  <head>

    <script type="text/javascript" 
src="org.osoa.sca.componentContext.js"></script>

    <script language="JavaScript">

       componentContext.getService("service").sayHelloCallback = 
          function(reply) {
             document.getElementById('result').innerHTML=reply;
          };
    
       function callSayHello() {
          
componentContext.getService("service").sayHello(document.getElementById('name').value);
       }

    </script>

  </head>
  <body >

    . . . 

     <input type="text" id="name" width="10">

     <button name="submit" onclick="callSayHello()">Say hello</button>

     <div id='result'></div>

    . . . 

  </body>
</html>
{code}

See the 
[helloworld-web-callback|https://svn.apache.org/repos/asf/tuscany/java/sca/samples/helloworld-web-callback/]
 sample for the complete code of the above example.

h4. Using implementation.web with the Tuscany runtime embedded within a webapp
 
This is the style all the current Tuscany webapp samples use. When using 
implementation.web with the Tuscany runtime embedded within a webapp the 
implementation.web web-uri attribute is ignored.

h4. Using implementation.web with the Tuscany runtime integrated into a JEE 
server such as Tomcat or Geronimo

The only code that is currently working is some prototype code in the Tuscany 
runtime-tomcat module that supports injecting references into Servlets, Filters 
and Event listeners using the @Reference annotation.

h4. Using implementation.web with the Tuscany standalone runtime

Nothing implemented yet

h4. Using implementation.web with the Tuscany distributed domain runtime

Nothing implemented yet

{column}
{section}

---------------------------------------------------------------------
CONFLUENCE INFORMATION
This message is automatically generated by Confluence

Unsubscribe or edit your notifications preferences
   http://cwiki.apache.org/confluence/users/viewnotifications.action

If you think it was sent incorrectly contact one of the administrators
   http://cwiki.apache.org/confluence/administrators.action

If you want more information on Confluence, or have a bug to report see
   http://www.atlassian.com/software/confluence


Reply via email to