PHP Web Services and SOA with the Service Component Architecture by shared123


More Info
									                   Service Component

                          Web services and SOA with the
                          Service Component Architecture

Graham Charters (aka

November 1, 2006
  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
     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:
                   WS-*, JMS, IIOP, XML-RPC, Atompub, RSS, JSON-
                   RPC, and so on, …
                   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:
              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++
    Focus of SCA for PHP
         Help in making reusable components and exposing them as
         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

           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
  It’s largely just a
                         include 'SCA/SCA.php';
  PHP class
                          * Service for sending emails
  ‘includes’ the SCA      * @service
  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



                         Email       Email
                         (SOAP)     (SOAP)


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

  Two ways:                */
                          class ContactEmailService {

                               * @reference
                               * ./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…
                                       send($to, $from, $subject, $message);

       From a script      $email_service =
                          $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
    SDO requires a description of the data
         Currently XML schema
         Future: annotated PHP classes

November 1, 2006                                   11
                                      <schema xmlns=""
Annotations for                   1       <element name="contact">
data structures                             <sequence>
                                              <element name="shortname" type="string" />
                                              <element name="fullname" type="string" />
                                              <element name="email" type="string" />
     Three steps to                        </complexType>
     providing a service
     with complex types
   1.     Create a schema for          * Service for managing email contacts
                                       * @service
          the data structure      2    * @types 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 The contact
          where the types are               */
          used                             public function retrieve($shortname) {
                                               $contact = SCA::createDataObject(
                                                    ‘’, ‘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

                                                                              Contact          Contact

Subject:                                                                  ContactService.php

                              ContactEmailService.php   Email    Email
                                                        (SOAP)   (SOAP)

           November 1, 2006                                                                      13

    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

email_client.html        (JSON-RPC)
                                           Contact         Contact

     From:                            ContactService.php

                         (JSON-RPC)         Email


November 1, 2006                                                     15

    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

    Annotation overriding
         Externally changing service targets, bindings,
    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
       SCA new home on PECL (imminent)
       Discussion Group
            Syndicated on to planet-php
       SDO for PHP documentation

November 1, 2006                                                   18

To top