A Guide To Creating C Web Services - ATOMKI

Transcription

A Guide to Creating C Web ServicesWHITE PAPER

AbstractThis whitepaper provides an introduction to creating C Web services and focuses on:» Challenges involved in integrating C applications with Web services» Benefits of using C Web services» An example showing step by step how to create and implement a C Web service» Advanced Web services featuresIntroductionThis paper describes how to develop and host high performance C Web services, including using advancedXML, SOAP, and WSDL productivity tools for transforming new and existing C applications for use inService Oriented Architectures (SOA).The focus is on HydraExpress, part of the Rogue Wave Hydra Suite, which also includes the HydraSDO data components that implement the Service Data Object (SDO) specification, and HydraSCA, the first productavailable for deploying high-performance SOA applications based on the Service Component Architecture(SCA) specification. The SDO and SCA specifications are multi-language industry SOA specifications availablein both Java and C .Challenges of Integrating C with Web ServicesOver the years, many organizations have made significant investments in C and now need to leveragethe business logic locked in these applications. However, web service technologies are focused in Java andMicrosoft .NET, making it difficult to address systems created outside of these environments.Integrating C applications using Web services is a formidable challenge because:» Web services require foundation pieces not supplied by C platform/libraries, including critical protocolimplementations such as HTTP requests and responses.» Since C is a “compile time” language it is difficult to map it with dynamic systems like Web services.» C is a very high-performance language, but writing scalable, threaded infrastructure code can be verycomplicated.Often, new “message bus” middleware is purchased to address this challenge. Message bus products providea C API and may require significant changes to the existing application. The middleware then uses costly“bridges” to communicate with the rest of the enterprise, thereby providing access to the C application. TheWHITE PAPERPAGE 2

use of middleware creates a tightly coupled system by tying the C application to only that message bus andits ability to provide compatibility with the rest of the enterprise. This is not only time consuming and errorprone, but reduces flexibility compared to Web services which, once exposed from the C application, can beeasily linked to the rest of the enterprise.Building C Web ServicesUsing a lightweight framework such as HydraExpress provides an alternative to heavyweight middleware likeCORBA. By providing an intuitive and powerful framework based on standards such as HTTP, SOAP, WSDL,and XML, development costs are reduced and it is possible to rapidly modify applications as business needschange. Developers can quickly transform a monolithic application into C Web Services that can nativelyinteract with all other parts of the technology infrastructure.Code generation technology provides a customized, service-specific programmatic interface built upon a robustframework for communicating with other processes. The high-level interface generated by HydraExpressinteracts with the underlying framework, which in turn handles the details of various networking, XML, andWeb Services standards. This approach separates the business logic code from the underlying communicationframework, ensuring long-term flexibility and compatibility.Web services fabrication products like HydraExpress support the SOA approach by making it easy to createWeb services from both new and existing business processes. Once exposed through a Web service, theseprocesses can interact with other WebsServices anywhere on the network, regardless of the technology thatunderlies these services.Message formats in HydraExpress are based on the SOAP protocol, a widely accepted, easy-to-use mechanismfor transferring messages over a network. As shown below, to use HydraExpress, developers supply a standardWeb Services Description Language (WSDL) file. HydraExpress automatically generates a skeleton service thathandles the details of protocols, standards and networking to ensure interoperability. Developers then drop innew or existing C business logic to create a ready-to-deploy service that can be used in many types of SOAapplications.SOA ApplicationHydraSCAWSDLFEEDS INTOdHyraExspreS TOOYsPRODUCESLC DEPService/Web Service DEPLOYSTOSOA ApplicationCustom or 3rdpartyWHITE PAPERPAGE 3

Accelerating C Web Services DevelopmentThere are several advantages to using a framework to develop C Web services. These include:» Application quality. The reliable technology of a well-established framework such as HydraExpressdelivers proven scalability and performance, freeing you to focus on your business logic rather than theinfrastructure of your service-oriented application.» Efficient evolution. The modular design of HydraExpress enables you to quickly adapt the application asyour business needs change, allowing you to modify only the areas that require adjustment.» Faster time-to-market. HydraExpress improves developer productivity so you can deliver effectiveservice-oriented applications on schedule and on budget.» Cost savings. With HydraExpress, you can achieve the flexibility of service-oriented applications whileleveraging your existing investments where it makes sense, drastically reducing the need to rewrite, testand optimize proven application logic.HydraExpress features:» A high-performance XML parser» A robust container for hosting C Web Services and Servlet servers» Automatic generation of C server skeletons built to run inside the HydraExpress container» Automatic generation of Web service clients from WSDL files» Easy-to-use XML-to-C data binding utility» Extensive native-code cross-platform networking foundation» Pluggable transport that supports Websphere MQ and Tibco Rendezvous » Seamless upgrade to HydraSCAA Simple Step-by-Step ExampleThis example takes you through the process of creating a C Web service, a WSDL file that defines the Webservice, and a SOAP-based client.The basic steps involved in creating and deploying a C Web service and client are as follows:1. Install the HydraExpress softwareWHITE PAPERPAGE 4

2. Set up the environment3. Invoke the generator by passing it the WSDL file4. Implement the service and client5. Compile the service and client6. Deploy the service7. Run the service and clientThe WSDL file used in the example describes a Greeting Service that has a single operation (sayHello) whichtakes a string as input and returns a string as output. The WSDL file can be found in the HelloWorld tutorial thatis available with the HydraExpress evaluation.Now to look at the steps in more detail:1. Install the HydraExpress evaluation.Download the HydraExpress evaluation for Windows or Linux fromhttp://www.roguewave.com/download-center/ and follow the setup instructions.2. Set up the environmenti.Ensure that your C compiler is set up in your environment and available in the PATHenvironment variableii. Set the JAVA HOME environment variable to your JDK installationiii. On UNIX/Linux set the RWSF HOME environment variable to the HydraExpress root directoryiv. Run the script rwsfvars located in the HydraExpress root directoryFurther information on configuring the environment is available in Section 2.3 of the HydraExpress User’sGuide which is provided with the HydraExpress evaluation.3. Invoke the HydraExpress code generator by passing it the WSDL filei.WHITE PAPERUsing a command prompt setup as described above, invoke rwsfgen passing it the name of theproject to be created and the WSDL file name:PAGE 5

Figure1Alternatively you can save the project settings in an xml project file, and pass this as the sole argument torwsfgen. A sample project file is provided with the HelloWorld tutorial.Figure2WHITE PAPERPAGE 6

4. Implement the service and clienta)Implement the serviceOpen the file pp.Edit the sayHello method so that it returns “Hello” the string passed from the sf::CallInfo& callInfo, const std::string& hellorequest in){return std::string(“Hello “ hellorequest in);}b) Implement the clientOpen the file p.Edit the invoke sayHello method so that it calls the proxy’s sayHello method with a fixed input string of“World !”void invoke sayHello(GreetingBindingProxy& proxy){std::string hellorequest in(“World!”);std::string return ret;rwsf::CallInfo callInfo;try {return ret proxy.sayHello(callInfo, hellorequest in);std::cout “Server Response: “ return ret std::endl;} catch(const rwsf::SoapFaultException& e) {std::cout “Fault Code: “ e.getFault().getFaultcode().asString() std::endl;std::cout “Fault String: “ e.getFault().getFaultstring() std::endl;}}5. Compile the service and clientChange to the HelloWorldExample directory and run nmake (Windows) or make (Unix/Linux) at thecommand prompt. This builds the HelloWorld client and service.6. Deploy the serviceRun nmake deploy (Windows) or make deploy (Unix/Linux) to deploy the service.WHITE PAPERPAGE 7

7. Run the service and the clienta)Start the HydraExpress agent using rwsfserver startFigure3A new command window appears showing the helloworld service has been loaded and is running (Loadingcontext: /helloworld/)Figure 4WHITE PAPERPAGE 8

b) Start the client GreetingPortClient from the HelloWorldExample\bin directory. The server responds.Figure 5More Advanced ExamplesThis paper provides a very simple example involving creating a C Web service, a WSDL file that defines theWeb Service, and a SOAP-based client.The evaluation version of HydraExpress available for download (http://www.roguewave.com/downloadcenter/) contains a wide range of tutorials that cover advanced functionality such as asynchronous Web services,WSDL defined fault handling, one-way and notification message patterns, message handlers for processingSOAP messages, the use of SOAP headers, MIME attachments, and XML binding for complex types.Advanced C Web Services FeaturesThe example shown in this whitepaper is intentionally basic in terms of functionality in order to easilydemonstrate the underlying concepts without adding unnecessary complexity. HydraExpress has many featuresthat allow developers to create enterprise ready Web Services. Some of these advanced features are summarizedbelow:Support for message patterns. HydraExpress supports the four basic message patterns defined in the WSDLspecification: request-response, the pattern most often used by Web services, one-way, notification, and solicitresponse. The message patterns can be combined to create anything from applications based on well-definedmodels such as publish/subscribe to arbitrarily complex, choreographed SOA applications.WHITE PAPERPAGE 9

Asynchronous messages. Any message pattern can be used either synchronously or asynchronously. Thischoice can be made at runtime by selecting either the synchronous or asynchronous service operation method.Support for sessions and other state-dependent processes. HydraExpress has built-in support for sessionmanagement. In addition, the generalized support for metadata in headers can be adapted to support sessions,transactions, and other processes that depend on state.Support for XML binding. The HydraExpress code generator automatically compiles XML Schemas into C datatypes, whether the schema is embedded in the WSDL file, or the WSDL references an outside schema.Support for SOAP attachments. HydraExpress creates code to support MIME bindings that are defined inWSDL.Support for WSDL-defined faults. HydraExpress generates classes that encapsulate fault messages defined inthe WSDL, and server code for capturing and returning fault messages to the client.An HTTPS transport. Accessible from the command line or built into the client, this transport provides for asecure client that uses HTTPS.Generated documentation. The code generator creates full, detailed documentation for all generated datatypes.Evolution: From Web Services to SOABuilding C Web Services is the first and most fundamental step toward moving C applications towarda SOA. The next and larger challenge is to deliver SOA applications that are technologically agnostic andlanguage independent. To forward this effort, the OASIS Open CSA finalized the first full version of the SCAspecification in C and Java in 2007, with the support of dozens of application development tool vendorsincluding Rogue Wave Software, IBM, BEA Systems, Sun Microsystems Inc., SAP, and Oracle.The key elements of the SCA specification offer the possibility of creating composite SOA applications usingreusable SOA components in any technology:» Decoupling of application logic from the details of its invoked service calls» Multiple language support, including C and Java, as well as related programming standards such asBPEL, XLST» Support for One-Way, Asynchronous, Call-Return, and Notification communications» Binding services via SOAP-based Web Services, EJB, JMS, JCA, RMI, and more» Data defined via Service Data Objects (SDO). The SDO API is available in both C and JavaWHITE PAPERPAGE 10

For more information, visit s paper details the benefits of C Web Services development and takes the reader step by step through asimple example. It also highlights the challenges faced when developing Web Services in C generally andillustrates how developers can save considerable development time and effort, while producing code that isrobust and easily maintainable. Lastly, some advanced C Web Services features are discussed.PAGE 11WHITE PAPERUSA 1-800-487-3217www.roguewave.comFRANCE 33 (0)1 30 09 78 78GERMANY 49 (0)6103 59340UK 44 (0)118 9360710Copyright 2008, Rogue Wave Software, Inc. All Rights Reserved. Rogue Wave is a registered trademark of Rogue Wave Software, Inc. All othertrademarks are the property of their respective owners.

» Pluggable transport that supports Websphere MQ and Tibco Rendezvous » Seamless upgrade to HydraSCA A Simple Step-by-Step Example This example takes you through the process of creating a C Web service, a WSDL f