<?xml version='1.0'?>
<!DOCTYPE chapter PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN"
               "http://docbook.org/xml/4.2/docbookx.dtd"
               [
               <!ENTITY copyright SYSTEM "copyright.xml">
               ]>

<chapter label="Web Services and Jini" id ="28">
  <title>
    Web Services and Jini
  </title>
  

  <tocchap>
    <tocentry>
      
    </tocentry>
    <toclevel1>
      <tocentry linkend="Introduction">
	<ulink url="#Introduction">Introduction</ulink>
      </tocentry>
    </toclevel1>
    <toclevel1>
      <tocentry>
	<ulink url="#Simple Web Service">Simple Web Service</ulink>
      </tocentry>
    </toclevel1>
    <toclevel1>
      <tocentry>
	<ulink url="#Bridging between Jini Client and Web Service (1)">
	  Bridging between Jini Client and Web Service (1)</ulink>
      </tocentry>
    </toclevel1>
    <toclevel1>
      <tocentry>
	<ulink url="#Bridging between Jini Client and Web Service (2)">
	Bridging between Jini Client and Web Service (2)</ulink>
      </tocentry>
    </toclevel1>
    <toclevel1>
      <tocentry>
	<ulink url="#Web Service Client to Jini Service">Web Service Client to Jini Service</ulink>
      </tocentry>
    </toclevel1>
    <toclevel1>
      <tocentry>
	<ulink url="#Summary">Summary</ulink>
      </tocentry>
    </toclevel1>
  </tocchap>
 


  <abstract>
    <para>
      One of the middleware frameworks being heavily promoted ay present is that
      of Web Services. These are built upon the invocation protocol SOAP,
      the specification language WSDL and the discovery system UDDI. In this chapter
      we look how clients and services from different frameworks can interoperate,
      with particular reference to Web Services and Jini     
    </para>
  </abstract>
  
  <sect1>
    <title id="Introduction">
      Introduction
    </title>
    
    <para>
      While this book has been about Jini, there are many other middleware systems
      in use, such as CORBA, Web Services, UPnP, Salutation and many others.
      While it would be very convenient to a software developer if all but their
      favourite middleware were to disappear, this is unlikely to happen. There
      are technical and political reasons for many of these frameworks to survive,
      and so the software developer will just have to live in a world of multiple
      middleware systems. 
    </para>

    <para>
      Users on the other hand just want their different pieces of software to work
      together no matter what framework is used. It is upto the software developer
      to figure out how to get a web Service client to work with a mixture of Jini 
      and UPnP services, say.
    </para>

    <para>
      The most common way of getting such mixtures to work is by a <emphasis>bridge</emphasis>.
      That is, to get a Web Service <emphasis>client</emphasis>
      to talk to a Jini <emphasis>service</emphasis>, typically a bridge
      will be an application that will sit between these and act as a 
      Web Service <emphasis>service</emphasis> and a Jini <emphasis>client</emphasis>.
      In the middle, the bridge translates from one framework to the other, in both
      directions. This is shown in the figure.
      <graphic fileref="images/bridge.png" align="center"></graphic>
    </para>
    
    <para>
      There are actually two aspects to a bridge: one is concerned with
      <emphasis>discovery</emphasis> and the other with <emphasis>invocation</emphasis>.
      <orderedlist>
	<listitem>
	  Discovery allows one middleware client to discover a different
	  middleware service e.g. a CORBA client discovering a Jini service.
	  Typically this is done by the client discovering the bridge, and
	  the bridge discovering the service. This may involve two
	  service cache managers (lookup services, name services, etc).
	</listitem>
	<listitem>
	  The bridge allows one middleware client to make calls on another
	  middleware service e.g. a CORBA client making calls on a Jini service.
	  It will convert calls received under one protocol into calls in the other.
	  This will often involve conversion of data from one format to another.
	</listitem>
      </orderedlist>
    </para>

    <para>
      Web services and Jini have special features that make this a simpler task than in
      general
      <orderedlist>
	<listitem>
	  Web Services are supposed to use the discovery system UDDI. However, UDDI was
	  designed independently as a global white/yellow/green/blue pages directory
	  and turns out to be poorly suited to Web Service discovery. In practise, it
	  seems that most Web Service developers rely on the URL's hard-coded into
	  WSDL files, and don't do any discovery at all.
	</listitem>
	<listitem>
	  In Chapter One was a section "The end of protocols" which talked about how
	  Jini doesn't really care about invocation protocols but only about discovery.
	  Of course, a lot of this book has been about how invocation of a service is
	  done, but much of that is about the choices that a service developer has.
	  The client doesn't care. 
	</listitem>
	<listitem>
	  The XML data types don't map directly onto Java data types and vice versa.
	  However, there are now standardised mappings  with implementations from
	  several vendors.
	</listitem>
      </orderedlist>
      This simplifies things in the following way:
      <orderedlist>
	<listitem>
	  Web services don't need to be discovered, just looked up. Web clients
	  don't need to do discovery since they have the service URL hard-coded
	  in the WSDL document.
	</listitem>
	<listitem>
	  Jini clients and services can talk SOAP (the Web Service protocol) just as
	  easily as they can talk any other invocation protocol. The client doesn't
	  even know what invocation protocol is used, 
	  while the service programmer just has to "SOAP-enable" the services.
	</listitem>
	<listitem>
	  Jini clients and services can make use of existing libraries to handle
	  SOAP queries.
	</listitem>
      </orderedlist>
    </para>

    <para>
      Just looking at the case of a Jini client talking to a Web Service,
      this can lead to several models, as pictured in the figures.
      In the first, the proxy can be an ordinary (e.g. Jeri) proxy talking
      back to its service. This service also acts as a Web Service client.
      <graphic fileref="images/jini_ws_client1.png" align="center"></graphic>
      In the second, the proxy is a "smart" proxy which talks directly to the
      Web Service.
      <graphic fileref="images/jini_ws_client2.png" align="center"></graphic>
    </para>

  </sect1>
  
  <sect1>
    <title id="Simple Web Service">
      Simple Web Service
    </title>
     
    <para>
      We illustrate this with a simple web service, for file classification again.
      To avoid the complexities of Web Service types and with deployment of such
      services, we simplify
      the service to one that takes a string as filename and returns the MIME type
      as a string <code>major/minor</code>. The class is not in a package,
      allowing simple deployment under Apache Axis. 
      The implementation of this service
      is then straightforward
      <programlisting>
	<?program "src/ws/FileClassifierService.java"?>
      </programlisting>
    </para>

    <para>
      The Apache Axis server run under Apache Tomcat is a popular means of delivering
      Web Services written in Java. It includes libraries for both client- and service-side.
      The simplest wasy of deploying the service under Axis is to copy the
      implementation source code to the <code>axis/webapps</code> directory,
      renaming the extension as <code>.jws</code> instead of <code>.java</code>
    </para>
 
    <para>
      The service can of course be written in many different languages.
      This is usually done by a horrible practice which has become common
      with Web Services: reverse engineer the implementation given above
      to a WSDL specification and then forward engineer this to your favourite
      language. We shall ignore all such issues here.
    </para>

    <para>
     On the client-side, a consumer of this service can then be written most simply as
      <programlisting>
	<?program "src/ws/TestWSClient.java"?>
      </programlisting>
      There are other ways, but this is good enough for the rest of this chapter
      which is intended to show how Jini and Web Services can interoperate rather
      than delving into the arcanities of Web Services.
    </para>
    
  </sect1>
  
  <sect1>
    <title id="Bridging between Jini Client and Web Service (1)">
      Bridging between Jini Client and Web Service (1)
    </title>
     
    <para>
      We can write a bridge that acts as a Jini service for the 
      <code>common.FileClassifier</code> specification used throughout this
      book. It will also act as a Web Service client by essentially taking
      all of the client code above and using this in the Jini service
      implementation.
      The bridge is a normal Jini server advertising the following Jini service
      implementation:
      <programlisting>
	<?program "src/ws/FileClassifierImpl.java"?>
      </programlisting>
    </para>
        
    <para>
      This service can export a Jeri or RMI proxy to a Jini client as we have seen
      before. Client calls on the proxy are sent to this service which acts as
      a Web Service client using the model of figure 2.
      When this implementation is built and run, it will need the Axis libraries 
      on the Jini service side.
    </para>
    
  </sect1>
 
 <sect1>
    <title id="Bridging between Jini Client and Web Service (2)">
      Bridging between Jini Client and Web Service (2)
    </title>
     
    <para>
      A service can be written that follows the second pattern in figure 3, simply
      by changing the inheritance from <code>RemoteFileClassifier</code>
      to <code>FileClassifier</code> and <code>Serializable</code>.
      A client then gets a copy of this service and all calls are made locally
      in the client.
      <programlisting>
	<?program "src/ws/FileClassifierSerializableImpl.java"?>
      </programlisting>
    </para>
        
    <para>
      <emphasis>This has major implications for the classes downloaded to the client!</emphasis>
      In order to run this service on the client side, it need access to the class files
      for the Axis classes <code>Call</code>, <code>Service</code> and <code>QName</code>.
      The client cannot be expected to have these, since it doesn't need to know any details
      of the implementation. So the Axis libraries have to be placed on an HTTP server
      and listed in the Jini server's codebase.
      The libraries are over one megabyte in size, and so this can result in a substantial
      download to the Jini client.
    </para>
  </sect1>
      
  <sect1>
    <title id="Web Service Client to Jini Service">
      Web Service Client to Jini Service
    </title>
     
    <para>
      Forming bridge between Web Service clients and Jini services follows the same
      general structure, as
      <graphic fileref="images/ws_jini.png" align="center"></graphic>
      In this case we put the Jini client code into the Web Service implementation
    </para>
    
    <para>
      There are a couple of wrinkles in getting this to work properly.
      With Apache Axis and the Tomcat server, these are
      <orderedlist>
	<listitem>
	  Jini class files
	</listitem>
	<listitem>
	  Security policy
	</listitem>
      </orderedlist>
    </para>

    <para>
      The Jini class files are not in the classpath for Tomcat. However, Tomcat
      and Apache allow any extra jar files required by a Web Service to be placed
      in special <code>lib</code> directories under the service's
      <code>WEB-INF</code> directory. Copying the Jini files
      <code>jsk-lib.jar</code> and <code>jsk-platform.jar</code> to this
      directory will make them available to the Web Service. This will be part of the
      deployment mechanisms for the Web Service
    </para>

    <para>
      The issues of a security policy is potentially more difficult. A server such
      as Tomcat can be started either with or without a security manager. If it uses a
       security manager, then the default security policy does not allow a new
      security manager to be put in place. This blocks a Jini client from
      installing an <code>RMISecurityManager</code> and so it cannot download
      a Jini registrar and find Jini services. Negotiation would then be required
      with the Tomcat administrator to add in sufficient permissions to the
      security policy to allow a Jini client to run.
    </para>

    <para>
      If Tomcat is run without a security manager then it is possible for the Web
      Service to install one. But it will then need to use a security policy.
      Up to now we have specified such a policy as a command line argument,
      but the command line is not accessible to an Axis Web Service. The workaround
      is to use <code>System.setProperty()</code> to set the security policy file
      before installing a security manager.
    </para>

    <para>
      All I/O to the console has to be cleaned up and put into remote exceptions.
      With these factors,
      the Web Service to bridge to a Jini service looks like
      <programlisting>
	<?program "src/ws/FileClassifierJiniService.java"?>
      </programlisting>
    </para>

    <para>
      The steps to get all this running are
      <orderedlist>
	<listitem>
	  Download and install Apache Tomcat and Axis
	</listitem>
	<listitem>
	  Edit the file  <code>FileClassifierJiniService.java</code> to point to
	  a valid security policy file on your system
	</listitem>
	<listitem>
	  Copy the file <code>FileClassifierJiniService.java</code> to the Tomcat
	  <code>webapps/axis</code> directory as <code>FileClassifierJiniService.jws</code>,
	  changing the file extension
	</listitem>
	<listitem>
	  Copy the Jini libraries <code>jsk-lib.jar</code> and <code>jsk-platform.jar</code>
	  to the Tomcat
	  <code>webapps/axis/WEB-INF/lib</code> directory 
	</listitem>
	<listitem>
	  Start Tomcat <emphasis>without</emphasis> a security manager (by default it
	    starts without one)
	</listitem>
	<listitem>
	  Start a Jini lookup service and any Jini implementation of the
	  <code>FileClassifier</code> interface that has been given in this
	  tutorial
	</listitem>
	<listitem>
	  Run the Web Service client <code>ws.TestWS2JiniClient</code>
	</listitem>
      </orderedlist>
      That should run the Web Service which finds the Jini service, makes a call
      on it and returns the result to the Web Service client.
    </para>
  </sect1>
  
  <sect1>
    <title id="Summary">
      Summary
    </title>
        
    <para>
      Bridging between different types of services is not an easy matter as there
      are many complex issues to be considered. Nevertheless, it is possible to
      build upon work performed to build Java Web Services and their Java clients.
      This allows us to use Jini services and clients in a relatively simple
      manner.
    </para>
 
  </sect1>
  
  &copyright;
</chapter>
