diff options
Diffstat (limited to 'site/trunk/site-publish/getting-started-with-tuscany-using-the-command-line.html')
-rw-r--r-- | site/trunk/site-publish/getting-started-with-tuscany-using-the-command-line.html | 577 |
1 files changed, 577 insertions, 0 deletions
diff --git a/site/trunk/site-publish/getting-started-with-tuscany-using-the-command-line.html b/site/trunk/site-publish/getting-started-with-tuscany-using-the-command-line.html new file mode 100644 index 0000000000..c3067192c9 --- /dev/null +++ b/site/trunk/site-publish/getting-started-with-tuscany-using-the-command-line.html @@ -0,0 +1,577 @@ + +<!-- + + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE- 2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +--> + + +<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> +<HTML> + <HEAD> + <!-- generateKeywords macro --> + <META name="description" content="Apache Tuscany"> + <META name="keywords" content="apache, apache tuscany, tuscany, service, services, fabric, soa, service oriented architecture, sca, service component architecture, das, sdo, csa, ruby, opensource"> + <!-- generateKeywords macro end --> + + <LINK type="text/css" rel="stylesheet" href="http://tuscany.apache.org/stylesheets/default.css"> + <LINK rel="SHORTCUT ICON" href="https://cwiki.apache.org/confluence/display/TUSCANY/$images/favicon.ico"> + <TITLE>Apache Tuscany : Getting Started with Tuscany ( using the Command Line)</TITLE> + <META http-equiv="Content-Type" content="text/html;charset=UTF-8"></HEAD> + + <BODY onload="init()"> + <!-- topNav macro --> + <TABLE valign="top" border="0" cellspacing="0" cellpadding="0" width="100%" background="http://tuscany.apache.org/images/TuscanyLogoNEW_Text_120px_bg.jpg"> + <TR> + <TD valing="top" align="left"> + <A href="https://cwiki.apache.org/confluence/pages/viewpage.action?spaceKey=TUSCANY&title=$siteroot"><IMG src="http://tuscany.apache.org/images/TuscanyLogoNEW_Text_120px_bg.jpg" height="91" width="25" border="0"></A> + </TD> + <TD> + <A href="http://tuscany.apache.org/"><IMG src="http://tuscany.apache.org/images/TuscanyLogo.jpg" border="0"></A> + </TD> + + <TD width="100%"> + + </TD> + + <!-- Adds the edit page link to the top banner--> + <TD valign="bottom"> + <DIV style="padding: 2px 10px; margin: 0px;"> + <A href="https://cwiki.apache.org/confluence/pages/editpage.action?pageId=84815"> + <IMG src="http://tuscany.apache.org/images/notep_16.gif" height="16" width="16" border="0" align="absmiddle" title="Edit Page"></A> + </DIV> + </TD> + + </TR> + </TABLE> + <!-- topNav macro end --> + + <!-- breadCrumbs macro --> + <TABLE border="0" cellpadding="2" cellspacing="0" width="100%"> + <TR class="topBar"> + <TD align="left" valign="middle" class="topBarDiv" nowrap="true" width="100%"> + <A href="home.html" title="Apache Tuscany">Apache Tuscany</A> > <A href="home.html" title="Home">Home</A> > <A href="sca-overview.html" title="SCA Overview">SCA Overview</A> > <A href="sca-java.html" title="SCA Java">SCA Java</A> > <A href="java-sca-documentation-menu.html" title="Java SCA Documentation Menu">Java SCA Documentation Menu</A> > <A href="" title="Getting Started with Tuscany ( using the Command Line)">Getting Started with Tuscany ( using the Command Line)</A> + </TD> + + <TD align="right" valign="middle" class="topBarDiv" align="left" nowrap="true"> + <A href="http://mail-archives.apache.org/mod_mbox/tuscany-user">User List</A> | <A href="http://mail-archives.apache.org/mod_mbox/tuscany-dev">Dev List</A> | <A href="http://issues.apache.org/jira/browse/Tuscany">Issue Tracker</A> + </TD> + </TR> + </TABLE> + <!-- breadCrumbs macro end --> + + + <TABLE border="0" cellpadding="0" width="100%" bgcolor="#FFFFFF"> + <TR> + <TD align="left" valign="top"> + + <!-- pageContent macro --> + <DIV id="PageContent"> + + <DIV class="pagecontent"> + <DIV class="wiki-content"> + <TABLE class="sectionMacroWithBorder" border="0" cellpadding="5" cellspacing="0" width="100%"><TBODY><TR> +<TD class="confluenceTd" valign="top" width="15%"> +<DIV class="panel" style="background-color: #ECF4D1;border-color: #C3CDA1;border-style: solid;border-width: 1px;"><DIV class="panelHeader" style="border-bottom-width: 1px;border-bottom-style: solid;border-bottom-color: #C3CDA1;background-color: #C3CDA1;"><B>General</B></DIV><DIV class="panelContent" style="background-color: #ECF4D1;"> +<P><A href="home.html" title="Home">Home</A><BR> +<A href="http://www.apache.org/licenses/LICENSE-2.0.html" class="external-link" rel="nofollow">License </A><BR> +<A href="sca-java-get-involved.html" title="SCA Java Get Involved">Get Involved</A><BR> +<A href="sca-java-found-a-bug.html" title="SCA Java Found A Bug">Found a Bug?</A></P> +</DIV></DIV> + +<DIV class="panel" style="background-color: #ECF4D1;border-color: #b4b4b5;border-style: solid;border-width: 1px;"><DIV class="panelHeader" style="border-bottom-width: 1px;border-bottom-style: solid;border-bottom-color: #b4b4b5;background-color: #C3CDA1;"><B>SCA Java</B></DIV><DIV class="panelContent" style="background-color: #ECF4D1;"> +<P><A href="sca-java.html" title="SCA Java">SCA Java Home</A><BR> +<A href="tuscany-dashboard.html" title="Tuscany Dashboard">SCA Dashboard</A><BR> +<A href="java-sca-tools.html" title="Java SCA Tools">Tools </A><BR> +<A href="tuscany-sca-java-faq.html" title="Tuscany SCA Java - FAQ">FAQ</A></P> +</DIV></DIV> + +<DIV class="panel" style="background-color: #ECF4D1;border-color: #b4b4b5;border-style: solid;border-width: 1px;"><DIV class="panelHeader" style="border-bottom-width: 1px;border-bottom-style: solid;border-bottom-color: #b4b4b5;background-color: #C3CDA1;"><B>SCA Java 1.x</B></DIV><DIV class="panelContent" style="background-color: #ECF4D1;"> +<P><A href="sca-java-releases.html" title="SCA Java Releases">Downloads</A><BR> +<A href="java-sca-documentation-menu.html" title="Java SCA Documentation Menu">Documentation</A><BR> +<A href="https://svn.apache.org/repos/asf/tuscany/sca-java-1.x/trunk/" class="external-link" rel="nofollow">1.x Source Code</A><BR> +<A href="http://tinyurl.com/8rlxd3" class="external-link" rel="nofollow">Roadmap</A></P> +</DIV></DIV> + +<DIV class="panel" style="background-color: #ECF4D1;border-color: #b4b4b5;border-style: solid;border-width: 1px;"><DIV class="panelHeader" style="border-bottom-width: 1px;border-bottom-style: solid;border-bottom-color: #b4b4b5;background-color: #C3CDA1;"><B>SCA Java 2.x</B></DIV><DIV class="panelContent" style="background-color: #ECF4D1;"> +<P><A href="sca-java-2x-releases.html" title="SCA Java 2.x Releases">Downloads</A><BR> +<A href="http://tuscany.apache.org/documentation-2x/" class="external-link" rel="nofollow">Documentation</A><BR> +<A href="https://svn.apache.org/repos/asf/tuscany/sca-java-2.x/trunk/" class="external-link" rel="nofollow">2.x Source Code</A><BR> +<A href="http://tinyurl.com/8rlxd3" class="external-link" rel="nofollow">Roadmap</A></P> +</DIV></DIV> + + +<DIV class="panel" style="background-color: #ECF4D1;border-color: #b4b4b5;border-style: solid;border-width: 1px;"><DIV class="panelHeader" style="border-bottom-width: 1px;border-bottom-style: solid;border-bottom-color: #b4b4b5;background-color: #C3CDA1;"><B>Documentation</B></DIV><DIV class="panelContent" style="background-color: #ECF4D1;"> +<P><A href="sca-java-user-guide.html" title="SCA Java User Guide">User Guide</A><BR> +<A href="sca-java-architecture-guide.html" title="SCA Java Architecture Guide">Architecture Guide</A><BR> +<A href="sca-java-development-guide.html" title="SCA Java Development Guide">Developer Guide</A><BR> +<A href="sca-java-extension-development-guide.html" title="SCA Java Extension Development Guide">Extension Developer Guide</A></P> +</DIV></DIV> + +<DIV class="panel" style="background-color: #ECF4D1;border-color: #b4b4b5;border-style: solid;border-width: 1px;"><DIV class="panelHeader" style="border-bottom-width: 1px;border-bottom-style: solid;border-bottom-color: #b4b4b5;background-color: #C3CDA1;"><B>Resources</B></DIV><DIV class="panelContent" style="background-color: #ECF4D1;"> +<P><A href="tuscany-sca-java-faq.html" title="Tuscany SCA Java - FAQ">FAQ</A><BR> +<A href="https://svn.apache.org/repos/asf/incubator/tuscany/java/sca" class="external-link" rel="nofollow">Source Code</A></P> +</DIV></DIV> +</TD> +<TD class="confluenceTd" valign="top" width="85%"> + +<P><BR class="atl-forced-newline"></P> +<DIV class="panel" style="background-color: #ECF4D1;border-color: #C3CDA1;border-style: solid;border-width: 1px;"><DIV class="panelHeader" style="border-bottom-width: 1px;border-bottom-style: solid;border-bottom-color: #C3CDA1;background-color: #C3CDA1;"><B>Build Calculator Using Command Line</B></DIV><DIV class="panelContent" style="background-color: #ECF4D1;"> +<UL> + <LI><A href="#GettingStartedwithTuscany%2528usingtheCommandLine%2529-intro"><B>What this is about</B></A></LI> + <LI><A href="#GettingStartedwithTuscany%2528usingtheCommandLine%2529-setup"><B>Setup Environment</B></A></LI> + <LI><A href="#GettingStartedwithTuscany%2528usingtheCommandLine%2529-runcalculator"><B>Run An Existing Calculator Appliction</B></A></LI> + <LI><A href="#GettingStartedwithTuscany%2528usingtheCommandLine%2529-develop"><B>Build The Calculator Sample</B></A></LI> + <LI><A href="#GettingStartedwithTuscany%2528usingtheCommandLine%2529-assemble"><B>Assemble The Calculator Application</B></A></LI> + <LI><A href="#GettingStartedwithTuscany%2528usingtheCommandLine%2529-Deploy"><B>Deploy The Calculator Application</B></A></LI> + <LI><A href="#GettingStartedwithTuscany%2528usingtheCommandLine%2529-Bindings"><B>Reconfigure The Calculator Application</B></A></LI> + <LI><A href="#GettingStartedwithTuscany%2528usingtheCommandLine%2529-Implementation"><B>Use Alternative Implementation Types</B></A><BR> + </LI> +</UL> +</DIV></DIV> + +<H2><A name="GettingStartedwithTuscany%28usingtheCommandLine%29-"></A><A name="GettingStartedwithTuscany%28usingtheCommandLine%29-intro"></A><DIV class="" style="background-color: #C3CDA1;">What this is about</DIV> +</H2> + +<P>This guide takes a command line based approach to getting started with Apache Tuscany Java SCA. For the Eclipse users amongst us there are other <A href="java-sca-documentation-menu.html" title="Java SCA Documentation Menu">guides</A> that show how to install the distribution into Eclipse or how to exploit Tuscany SCA Java using the Eclipse plugin that is has been built as part of the project. This article however just uses the downloaded Tuscany SCA Java distribution, Java, Maven or Ant and the command line. So if you want to feel close to the action this is where to start!</P> + +<P>Tuscany ships with a number of samples. One of the most straightforward of these is the calculator sample. We will use this to make sure that you installation is working properly. As the name indicates, this example performs typical calculator operations. It is given two numbers and asked to perform an operation on them. Our calculator will handle add, subtract, multiply and divide. </P> + +<P>By the end of this exercise you know how to develop, deploy and run the calculator application and how to reconfigure it to make it work for different environments.</P> + +<H2><A name="GettingStartedwithTuscany%28usingtheCommandLine%29-"></A><A name="GettingStartedwithTuscany%28usingtheCommandLine%29-setup"></A><DIV class="" style="background-color: #C3CDA1;">Setup Environment</DIV> +</H2> + +<P>First of all we will walk through running the calculator sample to make sure you installation is working correctly. Then we will walk through the steps taken to implement the calculator sample in the first place. Following this you should be familiar with how to build a basic SCA application that runs in a single, stand-alone, SCA runtime. The next step is to update the sample to use some of the other Tuscany SCA Java extensions. Finally we will use the calculator sample to show you how you can build a distributed Service Oriented Architecture using Tuscany SCA Java. </P> + +<UL> + <LI>Download the <A href="sca-java-releases.html" title="SCA Java Releases">Tuscany Java SCA release</A>.<BR> +From this page you can get binary and source releases for both windows and linux. For this exercise you will need the binary release for your preferred platform. You can use the source code release but you will have to use Maven at all stages.</LI> + <LI>Download java<BR> +<A href="http://java.sun.com/javase/downloads/index.jsp" class="external-link" rel="nofollow">Java 5</A></LI> + <LI>Download a build tool<BR> +<A href="http://maven.apache.org/download.html" class="external-link" rel="nofollow">Maven 2.0.7+</A><BR> +OR<BR> +<A href="http://ant.apache.org/bindownload.cgi" class="external-link" rel="nofollow">Ant 1.7.0</A></LI> +</UL> + + +<H2><A name="GettingStartedwithTuscany%28usingtheCommandLine%29-"></A><A name="GettingStartedwithTuscany%28usingtheCommandLine%29-runcalculator"></A><DIV class="" style="background-color: #C3CDA1;">Run An Existing Calculator Appliction</DIV> +</H2> + +<P>Tuscan SCA provides a calculator sample with its binary distribution. You can find it in the samples/calculator directory. As shown below, this sample and every sample in Tuscany has a "readme" that explains how to run the sample, a *.png file that has shows how the SCA application looks like.</P> + +<P><SPAN class="image-wrap" style="display: block; text-align: center"><IMG src="getting-started-with-tuscany-using-the-command-line.data/calculator1.png" style="border: 0px solid black"></SPAN></P> + +<P>Let's first run the sample before we go about building it. It is easy! Go to the directory ..\samples\calculator</P> +<DIV class="code panel" style="border-width: 1px;"><DIV class="codeContent panelContent"> +<PRE class="code-java"> +ant run +</PRE> +</DIV></DIV> + +<P>Alternatively if you want to run the sample directly from the command line try the following.</P> + +<UL> + <LI>if you are using Windows issue the command: +<DIV class="code panel" style="border-width: 1px;"><DIV class="codeContent panelContent"> +<PRE class="code-java"> +java -cp ..\..\lib\tuscany-sca-manifest.jar;target\sample-calculator.jar calculator.CalculatorClient +</PRE> +</DIV></DIV></LI> + <LI>if you are using *nix issue the command: +<DIV class="code panel" style="border-width: 1px;"><DIV class="codeContent panelContent"> +<PRE class="code-java"> +java -cp ../../lib/tuscany-sca-manifest.jar:target/sample-calculator.jar calculator.CalculatorClient +</PRE> +</DIV></DIV></LI> +</UL> + + +<P>You should see the following result:</P> + +<P>3 + 2=5.0<BR> +3 - 2=1.0<BR> +3 * 2=6.0<BR> +3 / 2=1.5</P> + +<P><SPAN class="image-wrap" style="display: block; text-align: center"><IMG src="getting-started-with-tuscany-using-the-command-line.data/calculator2.png" style="border: 0px solid black"></SPAN></P> + +<P>If you are using the source distribution then we suggest you use Maven to build and run the calculator sample because the tuscany-sca-manifest.jar is not provided with the source distribution. This jar is part of the binary distribution and collects together all of the tuscany jars in one place so that the java command line is nice and short when running samples.</P> + +<H2><A name="GettingStartedwithTuscany%28usingtheCommandLine%29-"></A><A name="GettingStartedwithTuscany%28usingtheCommandLine%29-build"></A><DIV class="" style="background-color: #C3CDA1;">Build The Calculator Sample In Java</DIV> +</H2> + +<P>This example illustrates how to define your application while staying focused on the business logic. It walks you through the steps of building the calculator sample. All connections between the components within the composite are local and described using Java interfaces.</P> + +<P><B>Step 1 - Define what building blocks are needed:</B> Think about how your application can be broken down into smaller functions/services. Each block is a logical unit of operation that can be used in the overall application. In this case, calculator application can be divided into five blocks: AddService block, SubstractService block, MultiplyService block and DivideService block and a main block that takes a request and routes it to the right operation. We have called this main block the CalculatorService </P> + +<P><SPAN class="image-wrap" style="display: block; text-align: center"><IMG src="getting-started-with-tuscany-using-the-command-line.data/calculatorblocks.png" style="border: 0px solid black"></SPAN></P> + +<P><B>Step 2 - Implement each block:</B> Now that you have identified the blocks of functionality in your application, you are ready to create each block. In SCA the blocks of functionality are referred to as components so let's look at how we implement a component. We'll take the AddService component as our first example. </P> + +<P>The AddService component will provide a service that adds two numbers together. The CalculatorService component uses the AddService component whenever it is asked to perform additions. If we were writing the AddService component in plain old Java we would start by describing a (Java) interface.</P> + +<DIV class="code panel" style="border-width: 1px;"><DIV class="codeContent panelContent"> +<PRE class="code-java"> +<SPAN class="code-keyword">public</SPAN> <SPAN class="code-keyword">interface</SPAN> AddService { + + <SPAN class="code-object">double</SPAN> add(<SPAN class="code-object">double</SPAN> n1, <SPAN class="code-object">double</SPAN> n2); +} +</PRE> +</DIV></DIV> + +<P>Now, we provide an implementation of this interface.</P> + +<DIV class="code panel" style="border-width: 1px;"><DIV class="codeContent panelContent"> +<PRE class="code-java"> +<SPAN class="code-keyword">public</SPAN> class AddServiceImpl <SPAN class="code-keyword">implements</SPAN> AddService { + + <SPAN class="code-keyword">public</SPAN> <SPAN class="code-object">double</SPAN> add(<SPAN class="code-object">double</SPAN> n1, <SPAN class="code-object">double</SPAN> n2) { + <SPAN class="code-keyword">return</SPAN> n1 + n2; + } +} +</PRE> +</DIV></DIV> + +<P>But wait! Aren't we writing an SCA component? It must be more complicated than plain old Java interface and implementation, right? Well, actually, an SCA component can just be plain old Java so we have just done all the coding we needed to implement the SCA AddService component. We can use SCA to expose the service that the AddService component provides over any of the supported bindings, for example, WebServices, JMS or RMI, without changing the AddService implementation.</P> + +<P>Let's take a look at the CalculatorService component. This is interesting because it's going to call the AddService component. In the full application it will call the SubtractService, MultiplyService and DivideService components as well, but we will ignore these for the time being as they follow the same pattern as we will implement for the AddService component. </P> + +<P>Again we will start by defining an interface because CalcultorService is itself providing an interface that others will call.</P> + +<DIV class="code panel" style="border-width: 1px;"><DIV class="codeContent panelContent"> +<PRE class="code-java"> +<SPAN class="code-keyword">public</SPAN> <SPAN class="code-keyword">interface</SPAN> CalculatorService { + + <SPAN class="code-object">double</SPAN> add(<SPAN class="code-object">double</SPAN> n1, <SPAN class="code-object">double</SPAN> n2); + <SPAN class="code-object">double</SPAN> subtract(<SPAN class="code-object">double</SPAN> n1, <SPAN class="code-object">double</SPAN> n2); + <SPAN class="code-object">double</SPAN> multiply(<SPAN class="code-object">double</SPAN> n1, <SPAN class="code-object">double</SPAN> n2); + <SPAN class="code-object">double</SPAN> divide(<SPAN class="code-object">double</SPAN> n1, <SPAN class="code-object">double</SPAN> n2); +} +</PRE> +</DIV></DIV> + +<P>Now we implement this interface. </P> + +<DIV class="code panel" style="border-width: 1px;"><DIV class="codeContent panelContent"> +<PRE class="code-java"> +<SPAN class="code-keyword">public</SPAN> class CalculatorServiceImpl <SPAN class="code-keyword">implements</SPAN> CalculatorService { + + <SPAN class="code-keyword">private</SPAN> AddService addService; + <SPAN class="code-keyword">private</SPAN> SubtractService subtractService; + <SPAN class="code-keyword">private</SPAN> MultiplyService multiplyService; + <SPAN class="code-keyword">private</SPAN> DivideService divideService; + + @Reference + <SPAN class="code-keyword">public</SPAN> void setAddService(AddService addService) { + <SPAN class="code-keyword">this</SPAN>.addService = addService; + } + + ...set methods <SPAN class="code-keyword">for</SPAN> the other attributes would go here + + <SPAN class="code-keyword">public</SPAN> <SPAN class="code-object">double</SPAN> add(<SPAN class="code-object">double</SPAN> n1, <SPAN class="code-object">double</SPAN> n2) { + <SPAN class="code-keyword">return</SPAN> addService.add(n1, n2); + } + + ...implementations of the other methods would go here +} +</PRE> +</DIV></DIV> + +<P>So we now have some components implemented using Java. Each component has a well defined interface, using Java interfaces in this case. Notice that these are just plain Java services with business logic and nothing else in them.</P> + +<P><SPAN class="image-wrap" style="display: block; text-align: center"><IMG src="getting-started-with-tuscany-using-the-command-line.data/calculator3.png" style="border: 0px solid black"></SPAN></P> + + +<H2><A name="GettingStartedwithTuscany%28usingtheCommandLine%29-"></A><A name="GettingStartedwithTuscany%28usingtheCommandLine%29-assemble"></A><DIV class="" style="background-color: #C3CDA1;">Assemble the Calculator Application</DIV> +</H2> +<P>So far we have created the components. How do we actually run the calculator application? Well of course the java programmer in us wants to get down to it, write a mainline to connect our two components together and run then. We could still do that easily in this case.</P> + +<DIV class="code panel" style="border-width: 1px;"><DIV class="codeContent panelContent"> +<PRE class="code-java"> +<SPAN class="code-keyword">public</SPAN> class CalculatorClient { + <SPAN class="code-keyword">public</SPAN> <SPAN class="code-keyword">static</SPAN> void main(<SPAN class="code-object">String</SPAN>[] args) <SPAN class="code-keyword">throws</SPAN> Exception { + + CalculatorServiceImpl calculatorService = <SPAN class="code-keyword">new</SPAN> CalculatorServiceImpl(); + AddService addService = <SPAN class="code-keyword">new</SPAN> AddServiceImpl(); + calculatorService.setAddService(addService); + + <SPAN class="code-object">System</SPAN>.out.println(<SPAN class="code-quote">"3 + 2="</SPAN> + calculatorService.add(3, 2)); + <SPAN class="code-comment">// calls to other methods go here <SPAN class="code-keyword">if</SPAN> we have implemented SubtractService, MultiplyService, DivideService +</SPAN> } +} +</PRE> +</DIV></DIV> + +<P>But this doesn't run using the Tuscany SCA runtime and extending this code to provide web services interfaces, for example, would be a little more complicated. What do we have to do to make it run in Tuscany where we get all things like web service support for free? Well, not much actually. First let's change the client to fire up the Tuscany SCA runtime before calling our components. </P> + +<DIV class="code panel" style="border-width: 1px;"><DIV class="codeContent panelContent"> +<PRE class="code-java"> +<SPAN class="code-keyword">public</SPAN> class CalculatorClient { + <SPAN class="code-keyword">public</SPAN> <SPAN class="code-keyword">static</SPAN> void main(<SPAN class="code-object">String</SPAN>[] args) <SPAN class="code-keyword">throws</SPAN> Exception { + + SCADomain scaDomain = SCADomain.newInstance(<SPAN class="code-quote">"Calculator.composite"</SPAN>); + CalculatorService calculatorService = scaDomain.getService(CalculatorService.class, <SPAN class="code-quote">"CalculatorServiceComponent"</SPAN>); + + <SPAN class="code-object">System</SPAN>.out.println(<SPAN class="code-quote">"3 + 2="</SPAN> + calculatorService.add(3, 2)); + <SPAN class="code-comment">// calls to other methods go here <SPAN class="code-keyword">if</SPAN> we have implemented SubtractService, MultiplyService, DivideService +</SPAN> + scaDomain.close(); + } +} +</PRE> +</DIV></DIV> +<P>You can see that we start by using a static method on SCADomain to create a new instance of itself. The SCADomain is a concept in SCA that represents the boundary of an SCA system. This could be distributed across many processors. For now, lets concentrate on getting this working inside a single Java VM. </P> + +<P>The parameter "Calculator.composite" refers to an XML file that tells SCA how the components in our calculator application are assembled into a working application. Here is the XML that's inside Calculator.composite. </P> + +<DIV class="code panel" style="border-width: 1px;"><DIV class="codeContent panelContent"> +<PRE class="code-java"> +<composite xmlns=<SPAN class="code-quote">"http:<SPAN class="code-comment">//www.osoa.org/xmlns/sca/1.0"</SPAN> +</SPAN> name=<SPAN class="code-quote">"Calculator"</SPAN>> + + <component name=<SPAN class="code-quote">"CalculatorServiceComponent"</SPAN>> + <implementation.java class=<SPAN class="code-quote">"calculator.CalculatorServiceImpl"</SPAN>/> + <reference name=<SPAN class="code-quote">"addService"</SPAN> target=<SPAN class="code-quote">"AddServiceComponent"</SPAN> /> + <!-- You can add references to SubtractComponent, MultiplyComponent and DivideComponent --> + </component> + + <component name=<SPAN class="code-quote">"AddServiceComponent"</SPAN>> + <implementation.java class=<SPAN class="code-quote">"calculator.AddServiceImpl"</SPAN>/> + </component> + + <!-- definitions of SubtractComponent, MultiplyComponent and DivideComponent --> + +</composite> +</PRE> +</DIV></DIV> + +<P>In our case so far, we defined two components and we specify the Java implementation classes that Tuscany SCA needs to load to make them work in the .composite file. These are the classes we have just implemented.</P> + +<P>Also note that the CalculatorServiceComponent has a reference named "addService". In the XML, this reference targets the AddServiceComponent. It is no coincidence that the reference name, "addService", matches the name of the addService field we created when we implemented CalculatorServiceImpl. The Tuscany SCA runtime parses the information from the XML composite file and uses it to build the objects and relationships that represent our calculator application. It first creates instances of AddServiceImpl and CalcualtorSreviceImpl. It then injects a reference to the AddServiceImpl object into the addService field in the CalculatorServiceImpl object. If you look back at how we implemented the CalculatorService you will see an @Reference annotation that tells SCA which fields are expecting to be set automatically by SCA. This is equivalent to this piece of code from our normal Java client.</P> + +<DIV class="code panel" style="border-width: 1px;"><DIV class="codeContent panelContent"> +<PRE class="code-java"> + CalculatorServiceImpl calculatorService = <SPAN class="code-keyword">new</SPAN> CalculatorServiceImpl(); + AddService addService = <SPAN class="code-keyword">new</SPAN> AddServiceImpl(); + calculatorService.setAddService(addService); +</PRE> +</DIV></DIV> + +<P>Once the composite file is loaded into the SCADomain our client code asks the SCADomain to give us a reference to the component called "CalculatorServiceComponent". </P> + +<DIV class="code panel" style="border-width: 1px;"><DIV class="codeContent panelContent"> +<PRE class="code-java"> + CalculatorService calculatorService = scaDomain.getService(CalculatorService.class, <SPAN class="code-quote">"CalculatorServiceComponent"</SPAN>); +</PRE> +</DIV></DIV> + +<P>We can now use this reference as though we had created it ourselves, for example, from the CalculatorServiceImpl.add() method implementation.</P> + +<DIV class="code panel" style="border-width: 1px;"><DIV class="codeContent panelContent"> +<PRE class="code-java"> +<SPAN class="code-keyword">return</SPAN> addService.add(n1, n2); +</PRE> +</DIV></DIV> + +<P>The SCA specifications often describe SCA applications is diagrammatic form. This often helps give a quick overview of what components are part of an application and how they are wired together. If we draw a diagram of what we have build in the calculator sample we come up with something like.</P> + +<P><SPAN class="image-wrap" style="display: block; text-align: center"><IMG src="getting-started-with-tuscany-using-the-command-line.data/calculator4.png" style="border: 0px solid black"></SPAN></P> + +<P>You will notice that diagrams are provided with all of our samples. If you like to take a visual approach to things this may help you become quickly familiar with the components in the samples. Take a look at the ".png" files in the top level directory of each sample.</P> + + +<H2><A name="GettingStartedwithTuscany%28usingtheCommandLine%29-"></A><A name="GettingStartedwithTuscany%28usingtheCommandLine%29-deploy"></A><DIV class="" style="background-color: #C3CDA1;">Deploy the Calculator Application</DIV> +</H2> +<P>The composite describes how SCA components are implemented and how they are assembled by wiring references to targets. This composite file has some dependencies, in this case the Java class and interface files that are used to implement the SCA components that it defines. The collection of composite files and other artifacts that are required to run an SCA application are collected together into one or more SCA contributions. A contribution can be a simple as a directory in a file system or could be packaged in, for example, a Jar file. SCA does not mandate any particular packing scheme. For our calculator sample you can imagine the contribution holding the calculator composite and all of its dependencies. </P> + +<P>In fact if you look inside the jar file that the calculator sample produces, you will find the following</P> +<DIV class="code panel" style="border-width: 1px;"><DIV class="codeContent panelContent"> +<PRE class="code-java"> +calculator/AddService.class +calculator/AddServiceImpl.class +calculator/CalculatorClient.class +calculator/CalculatorService.class +calculator/CalculatorServiceImpl.class +calculator/DivideService.class +calculator/DivideServiceImpl.class +calculator/MultiplyService.class +calculator/MultiplyServiceImpl.class +calculator/SubtractService.class +calculator/SubtractServiceImpl.class +Calculator.composite +</PRE> +</DIV></DIV> +<P>Which are all the artifacts that are required to run the calculator sample. We just need to add this contribution to the Tuscany SCA java runtime and then call the services that will be enabled.</P> + +<P>The samples in Tuscany come with an Ant build.xml file that allows the sample files to be rebuilt so if you want to experiment with the sample code you can do so and then recompile it. </P> + +<DIV class="code panel" style="border-width: 1px;"><DIV class="codeContent panelContent"> +<PRE class="code-java"> +ant compile +</PRE> +</DIV></DIV> + +<P>Once recompiled you can run it as before in the <A href="http://cwiki.apache.org/confluence/display/TUSCANY/Getting%20Started%20with%20Tuscany%20(%20using%20the%20Command%20Line)#GettingStartedwithTuscany%2528usingtheCommandLine%2529-runcalculator" class="external-link" rel="nofollow">Running The Calculator Sample</A> section, for example, we provide a run target in the Ant build.xml file so the calculator sample can also be run using. </P> + +<DIV class="preformatted panel" style="border-width: 1px;"><DIV class="preformattedContent panelContent"> +<PRE>ant run +</PRE> +</DIV></DIV> + +<H2><A name="GettingStartedwithTuscany%28usingtheCommandLine%29-"></A><A name="GettingStartedwithTuscany%28usingtheCommandLine%29-bindings"></A><DIV class="" style="background-color: #C3CDA1;">Reconfigure The Calculator Application - Change Bindings</DIV> +</H2> + +<P>Looking back, the client code we have written to start the calculator application using the Tuscany SCA runtime is no longer than a normal Java client for the application. However we do now have the XML composite file that describes how our application is assembled. </P> + +<P>This concept of assembly is a great advantage as our applications become more complex and we want to change them, reuse them, integrate them with other applications or just further develop them using a programming model consistent with all our other SCA applications. Regardless of what language is used to implement each of them. </P> + +<P>For example, lets say our calculator sample is so poweful and popular that we want to put it on the company intranet and let other people access it as a service directly from their browser based Web2.0 applications. It's at this point we would normally start reaching for the text books to work out how to make this happen. As we have an XML file that describes our application it's easy in Tuscany SCA. The following should do the trick. </P> + + +<DIV class="code panel" style="border-width: 1px;"><DIV class="codeContent panelContent"> +<PRE class="code-java"> +<composite xmlns=<SPAN class="code-quote">"http:<SPAN class="code-comment">//www.osoa.org/xmlns/sca/1.0"</SPAN> +</SPAN> name=<SPAN class="code-quote">"Calculator"</SPAN>> + + <service name=<SPAN class="code-quote">"CalculatorService"</SPAN> promote=<SPAN class="code-quote">"CalculatorServiceComponent/CalculatorService"</SPAN>> + <<SPAN class="code-keyword">interface</SPAN>.java <SPAN class="code-keyword">interface</SPAN>=<SPAN class="code-quote">"calculator.CalculatorService"</SPAN>/> + <binding.jsonrpc/> + </service> + + <component name=<SPAN class="code-quote">"CalculatorServiceComponent"</SPAN>> + <implementation.java class=<SPAN class="code-quote">"calculator.CalculatorServiceImpl"</SPAN>/> + <reference name=<SPAN class="code-quote">"addService"</SPAN> target=<SPAN class="code-quote">"AddServiceComponent"</SPAN> /> + <!-- references to SubtractComponent, MultiplyComponent and DivideComponent --> + </component> + + <component name=<SPAN class="code-quote">"AddServiceComponent"</SPAN>> + <implementation.java class=<SPAN class="code-quote">"calculator.AddServiceImpl"</SPAN>/> + </component> + + <!-- definitions of SubtractComponent, MultiplyComponent and DivideComponent --> + +</composite> +</PRE> +</DIV></DIV> + +<P>All we have done is added the <service> element which tells Tuscany SCA how to expose our CalculatorServiceComponent as a JSONRPC service. Note that we didn't have to change the Java code of our components. This is just a configuration change. The helloworld-jsonrpc sample shows a working example of the jsonrpc binding that you can use as an example to change the calculator application to use JSONRPC.</P> + +<P>If we really wanted a SOAP/HTTP web service we can do that easily too. </P> + +<DIV class="code panel" style="border-width: 1px;"><DIV class="codeContent panelContent"> +<PRE class="code-java"> +<composite xmlns=<SPAN class="code-quote">"http:<SPAN class="code-comment">//www.osoa.org/xmlns/sca/1.0"</SPAN> +</SPAN> name=<SPAN class="code-quote">"Calculator"</SPAN>> + + <service name=<SPAN class="code-quote">"CalculatorService"</SPAN> promote=<SPAN class="code-quote">"CalculatorServiceComponent/CalculatorService"</SPAN>> + <<SPAN class="code-keyword">interface</SPAN>.java <SPAN class="code-keyword">interface</SPAN>=<SPAN class="code-quote">"calculator.CalculatorService"</SPAN>/> + <!-- ** Below we added info <SPAN class="code-keyword">for</SPAN> json binding ** --> + <binding.jsonrpc/> + <binding.ws/> + </service> + + <component name=<SPAN class="code-quote">"CalculatorServiceComponent"</SPAN>> + <implementation.java class=<SPAN class="code-quote">"calculator.CalculatorServiceImpl"</SPAN>/> + <reference name=<SPAN class="code-quote">"addService"</SPAN> target=<SPAN class="code-quote">"AddServiceComponent"</SPAN> /> + <!-- references to SubtractComponent, MultiplyComponent and DivideComponent --> + </component> + + <component name=<SPAN class="code-quote">"AddServiceComponent"</SPAN>> + <implementation.java class=<SPAN class="code-quote">"calculator.AddServiceImpl"</SPAN>/> + </component> + + <!-- definitions of SubtractComponent, MultiplyComponent and DivideComponent --> + +</composite> +</PRE> +</DIV></DIV> + +<P>We have added binding.ws here so that your calculator service is available over JSONRPC <B>and</B> WebService bindings <B>at the same time</B>. The helloworld-ws-service and helloworld-ws-reference samples show you how to work with web services. </P> + +<P>SCA allows other kinds of flexibility. We can rewire our components, for example, using a one of the remote bindings, like RMI, we could have the CalculatorServiceComponent running on one machine wired to a remote version of the application running on another machine. The calculator-rmi-service and calculator-rmi-reference samples show the RMI binding at work. </P> + +<H2><A name="GettingStartedwithTuscany%28usingtheCommandLine%29-"></A><A name="GettingStartedwithTuscany%28usingtheCommandLine%29-implementation"></A><DIV class="" style="background-color: #C3CDA1;">Use Alternative Implementation Types </DIV> +</H2> +<P>We could also introduce components implemented in different languages, for example, let's add the SubtractServiceComponent implemented in Ruby. </P> + +<DIV class="code panel" style="border-width: 1px;"><DIV class="codeContent panelContent"> +<PRE class="code-java"> +<composite xmlns=<SPAN class="code-quote">"http:<SPAN class="code-comment">//www.osoa.org/xmlns/sca/1.0"</SPAN> +</SPAN> name=<SPAN class="code-quote">"Calculator"</SPAN>> + + <component name=<SPAN class="code-quote">"CalculatorServiceComponent"</SPAN>> + <implementation.java class=<SPAN class="code-quote">"calculator.CalculatorServiceImpl"</SPAN>/> + <reference name=<SPAN class="code-quote">"addService"</SPAN> target=<SPAN class="code-quote">"AddServiceComponent"</SPAN> /> + <reference name=<SPAN class="code-quote">"subtractService"</SPAN> target=<SPAN class="code-quote">"SubtractServiceComponent"</SPAN> /> + <!-- references to MultiplyComponent and DivideComponent --> + </component> + + <component name=<SPAN class="code-quote">"AddServiceComponent"</SPAN>> + <implementation.java class=<SPAN class="code-quote">"calculator.AddServiceImpl"</SPAN>/> + </component> + + <component name=<SPAN class="code-quote">"SubtractServiceComponent"</SPAN>> + <implementation.script script=<SPAN class="code-quote">"calculator/SubtractServiceImpl.rb"</SPAN>/> + </component> + + <!-- definitions of MultiplyComponent and DivideComponent --> + +</composite> +</PRE> +</DIV></DIV> + +<P>Of course we need the Ruby code that implements the component. </P> + +<DIV class="code panel" style="border-width: 1px;"><DIV class="codeContent panelContent"> +<PRE class="code-java"> +def subtract(n1, n2) + <SPAN class="code-keyword">return</SPAN> n1 - n2 +end +</PRE> +</DIV></DIV> + +<P>The Tuscany SCA runtime handles wiring Java components to Ruby components and performs any required data transformations. The calculator-script sample shows different script languages in use. </P> + +<P>So, now that our application is described as an SCA assembly there are lots of possibilities as we further develop it and integrate it with other applications. </P> +</TD></TR></TBODY></TABLE> + </DIV> + </DIV> + </DIV> + <!-- pageContent macro end --> + + </TD> + </TR> + </TABLE> + + <!-- footer macro --> + <SCRIPT src="http://www.google-analytics.com/urchin.js" type="text/javascript"> + </SCRIPT> + <SCRIPT type="text/javascript"> + _uacct = "UA-1174707-5"; + urchinTracker(); + </SCRIPT> + + <A href="http://www.statcounter.com/" target="_blank"><IMG src="http://c26.statcounter.com/counter.php?sc_project=2619156&java=0&security=94bd7e7d&invisible=0" alt="website stats" border="0"></A> + + <DIV class="footer"> + Copyright � 2003-2012, The Apache Software Foundation </BR> + Apache Tuscany and the Apache Tuscany project logo are trademarks of The Apache Software Foundation. + </DIV> + <!-- footer macro end --> + + </BODY> +</HTML> |