PHP Web Services and SOA with the Service Component Architecture

Document Sample
PHP Web Services and SOA with the Service Component Architecture Powered By Docstoc
					                   Service Component
                   Architecture

                          Web services and SOA with the
                          Service Component Architecture

Graham Charters     gcc@php.net (aka charters@uk.ibm.com)

IBM
November 1, 2006
Agenda
  Six slide introduction
     Service-Oriented Architecture vs Service
     Component Architecture
  Using SCA (including demos)
     Writing services
     Consuming services
     Switching bindings
  Summary, Futures and Useful info

November 1, 2006                                2
Problem
     Businesses and individuals suffer:
          Application/solution inflexibility
          Poor asset and skills reuse
          Operational inefficiency
          Partner integration
          Long application lifecycles


     The IT world has seen many “silver bullets”:
          distributed objects, message passing, J2EE, WS-*
     But still the world looks like this …

November 1, 2006                                             3
…the real world




      A combinatorial headache of:
           technologies
                   WS-*, JMS, IIOP, XML-RPC, Atompub, RSS, JSON-
                   RPC, and so on, …
           languages
                   PHP, Perl, Java, C, C++, Ruby, …


November 1, 2006                                                   4
Service-Oriented Architecture
    It’s an idea – a philosophy
    Around long before the term was first coined
         Just like AJAX, and Web 2.0

    The common building block is a service -
    anything which has:
         Interface
              Verbs (or actions, methods)
              Messages (or parameters)
         Protocol (soap/http, json-rpc, local, …)
         Location (target, address, endpoint)
         Ideally stateless and context-free

    Not just WSDL and soap/http – services can
    be provided using many technologies
         Also encompasses REST, J2EE, distributed
         objects, asynchronous messaging, etc. …

    Service Component Architecture brings these
    things together…



November 1, 2006                                    5
Service Component Architecture
    Specifications covering…
         Assembly of SOA solutions from assets in multiple technologies
         Language bindings; Java, C++, COBOL, …
    …and implementations
         SCA for PHP
         See Apache Tuscany Incubator for Java and C++
         implementations
    Focus of SCA for PHP
         Help in making reusable components and exposing them as
         services
         Help in consuming services of various types

    Given some useful business logic, how do you make that
    as useful and reusable as possible?

November 1, 2006                                                          6
Making a component reusable
    It’s all about “separation of concerns”

1. Be flexible about how you are called
         Expose as many ‘bindings’ as needed – make sure your business logic does not
         need to know how it was called
2. Be flexible about your dependencies
         Define your service interface dependencies – make sure your business logic
         does not need to know how to resolve these
         Ideally get something else to “wire up” the components (Inversion of Control;
         Dependency Injection patterns)



                       Accounts                                LogService
                         Service
                         (REST)


           1. Be flexible about          Accounts             2. Be flexible about
           how you are called             Service             your dependencies

                       Accounts                                             Directory
                         Service                                             Service
                         (SOAP)                                               (LDAP)




November 1, 2006                                                                         7
Writing an SCA Component
                         <?php
  It’s largely just a
                         include 'SCA/SCA.php';
  PHP class
                         /**
                          * Service for sending emails
                          *
  ‘includes’ the SCA      * @service
                          * @binding.ws
  runtime (must be        */
                         class EmailService {
  last)                      …
                             /**
  Annotated to                * Send a simple text email
                              *
  declare capabilities        * @param string $to The "to" email address
                              * @param string $from The "from" email address
  and dependencies            * @param string $subject The subject of the email
                              * @param string $message The email message

  MUST assume                 * @return boolean
                              */
  pass-by-value              public function send($to, $from, $subject, $message) {
                                 …
                             }
                         }

                         ?>

November 1, 2006                                                                      8
Demo (Simple Email Form)

                   Write an SCA Component
                   Expose it as a Web service
                   Generate the WSDL


     EmailClient.php

           To:
         From:

       Subject:
                         Email       Email
       Message:
                         (SOAP)     (SOAP)
                                                 Email


                                             EmailService.php




November 1, 2006                                                9
Consuming services
                          /**
                           * Service for sending emails (supports shortnames)
                          * @service

  Two ways:                */
                          class ContactEmailService {

                              /**
                               * @reference
                               * @binding.ws ./EmailService.wsdl
                               */
                              public $email_service;
       From a Component
                              /** … */
                              public function send($to, $from, $subject, $message) {
                                 …
                                // a proxy to the service is ‘injected’ so we
                                // can just use it…
                                 $this->email_service->
                                       send($to, $from, $subject, $message);
                                 ...
                              }
                          }


       From a script      $email_service =
                            SCA::getService('../EmailService.wsdl');
                          $email_service->send($to, $from, $subject, $message);



November 1, 2006                                                                  10
Handling data structures

    Not all services exchange primitives!

    SCA uses Service Data Objects to handle data
    structures
    SDO requires a description of the data
    structures
         Currently XML schema
         Future: annotated PHP classes


November 1, 2006                                   11
                                      <schema xmlns="http://www.w3.org/2001/XMLSchema"
                                      targetNamespace="http://example.org/contacts">
Annotations for                   1       <element name="contact">
                                           <complexType>
data structures                             <sequence>
                                              <element name="shortname" type="string" />
                                              <element name="fullname" type="string" />
                                              <element name="email" type="string" />
                                            </sequence>
     Three steps to                        </complexType>
                                          </element>
     providing a service
                                      </schema>
     with complex types
                                      /**
   1.     Create a schema for          * Service for managing email contacts
                                       * @service
          the data structure      2    * @types http://example.org/contacts contacts.xsd
                                       */
   2.     Annotate class to say       class ContactService {
          which types are used
                                           /**
   3.     Document the class                * Retrieve contact details
                                            *
          methods to show                   * @param string $shortname Short name of the contact
                                  3         * @return contact http://example.org/contacts The contact
          where the types are               */
          used                             public function retrieve($shortname) {
                                               $contact = SCA::createDataObject(
                                                    ‘http://example.org/contacts’, ‘contact’);
                                               …
                                               return $contact;
                                           }                                                   12
November 1, 2006
                                      }
           Demo (Contacts Email Client)
                     Consume PHP service and Web service from Component
                     Consume a Component from a ‘view’ script
                     Adding data structures to a component
                     Using data structures in a component



EmailClient.php
                                                                              Contact          Contact
    To:
  From:

Subject:                                                                  ContactService.php
Message:
                                  ContactEmail


                              ContactEmailService.php   Email    Email
                                                        (SOAP)   (SOAP)
                                                                               Email


                                                                           EmailService.php
           November 1, 2006                                                                      13
Bindings

    Need to be able to choose protocols
         As a provider: different clients (customers) prefer or
         require different protocols
              Java client (soap/http), JavaScript client (json-rpc), …
         As a consumer: no one protocol is supported by all
         service providers
    JSON-RPC binding currently being prototyped
    Intend to provide others



November 1, 2006                                                         14
Demo (DOJO Email client)
         Adding a json-rpc binding
         Calling an SCA service from DOJO via json-rpc


                           Contact
email_client.html        (JSON-RPC)
                                           Contact         Contact

       To:
     From:                            ContactService.php
   Subject:
   Message:




                             Email
                         (JSON-RPC)         Email
   DOJO

                                       EmailService.php

November 1, 2006                                                     15
Summary

    SCA for PHP enables a PHP programmer to write components in
    PHP which are unaware of local/remote and protocol differences
    and can focus on providing reusable business logic.

    Components use PHP annotations both to declare their
    dependencies on other components, and to define the interface
    which they expose as a service. The SCA for PHP runtime resolves
    all of these.

    Deploying a PHP component as a ‘Web service’ can be as simple
    as copying it into a web server’s document root. The SCA for PHP
    runtime automatically generates service descriptions (WSDL, SMD)
    for these when requested.




November 1, 2006                                                     16
Futures

    Annotation overriding
         Externally changing service targets, bindings,
         properties
    PHP classes for data structures
    Simple database services
    Other bindings
         Atompub, REST (XML and JSON), RSS


November 1, 2006                                      17
Useful info
       SCA/SDO for PHP homepage
            http://osoa.org/display/PHP/SOA+PHP+Homepage
       SCA new home on PECL (imminent)
            http://pecl.php.net/sca_sdo
       Discussion Group
            http://groups.google.com/group/phpsoa/
       Blog
            http://www.ibm.com/developerworks/blogs/page/phpblog
            Syndicated on to planet-php
       SDO for PHP documentation
            http://www.php.net/sdo



November 1, 2006                                                   18

				
DOCUMENT INFO
Shared By:
Stats:
views:51
posted:8/9/2010
language:English
pages:18
Description: SCA for PHP enables a PHP programmer to write components in PHP which are unaware of local/remote and protocol differences and can focus on providing reusable business logic. Components use PHP annotations both to declare their dependencies on other components, and to define the interface which they expose as a service. The SCA for PHP runtime resolves all of these. Deploying a PHP component as a ‘Web service’ can be as simple as copying it into a web server’s document root. The SCA for PHP runtime automatically generates service descriptions (WSDL, SMD) for these when requested.