You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tuscany.apache.org by an...@apache.org on 2011/04/19 12:46:08 UTC

svn commit: r1095019 - in /tuscany/sca-java-2.x/trunk/samples/getting-started/helloworld-jaxrs: ./ README pom.xml src/main/java/sample/JaxrsHelloworld.java src/main/resources/helloworld.composite src/test/java/sample/HelloworldTestCase.java

Author: antelder
Date: Tue Apr 19 10:46:08 2011
New Revision: 1095019

URL: http://svn.apache.org/viewvc?rev=1095019&view=rev
Log:
Add a helloworld sample using JAX-RS and the REST binding

Added:
    tuscany/sca-java-2.x/trunk/samples/getting-started/helloworld-jaxrs/
      - copied from r1094917, tuscany/sca-java-2.x/trunk/samples/getting-started/helloworld/
    tuscany/sca-java-2.x/trunk/samples/getting-started/helloworld-jaxrs/src/main/java/sample/JaxrsHelloworld.java
Modified:
    tuscany/sca-java-2.x/trunk/samples/getting-started/helloworld-jaxrs/README
    tuscany/sca-java-2.x/trunk/samples/getting-started/helloworld-jaxrs/pom.xml
    tuscany/sca-java-2.x/trunk/samples/getting-started/helloworld-jaxrs/src/main/resources/helloworld.composite
    tuscany/sca-java-2.x/trunk/samples/getting-started/helloworld-jaxrs/src/test/java/sample/HelloworldTestCase.java

Modified: tuscany/sca-java-2.x/trunk/samples/getting-started/helloworld-jaxrs/README
URL: http://svn.apache.org/viewvc/tuscany/sca-java-2.x/trunk/samples/getting-started/helloworld-jaxrs/README?rev=1095019&r1=1094917&r2=1095019&view=diff
==============================================================================
--- tuscany/sca-java-2.x/trunk/samples/getting-started/helloworld-jaxrs/README (original)
+++ tuscany/sca-java-2.x/trunk/samples/getting-started/helloworld-jaxrs/README Tue Apr 19 10:46:08 2011
@@ -1,43 +1,22 @@
-Tuscany - Getting Started - Helloworld Sample
----------------------------------------------
+Tuscany - Getting Started - Helloworld JAX-RS Sample
+----------------------------------------------------
 
-This sample demonstrates a simple helloworld style SCA application and how to run that with Tuscany.
+This sample extends the helloworld sample to make the helloworld service available as an HTTP endpoint by using JAX-RS and the Tuscany REST binding.
 
-See the README in the top-level samples folder for general information on the Tuscany samples.
+The changes to the helloworld sample are:
+- update the pom.xml with the additional dependencies
+- add a new Java interface which uses JAX-RS annotations
+- update the composite to use the new interface and binding 
 
-This project creates an SCA contribution with a deployable composite named helloworld.composite.
-The composite defines an SCA component, HelloworldComponent, which provides a Helloworld service, 
-the component is implemented by a Java class. 
-
-You can use the contribution by starting the composite in the Tuscany Shell. To do that run the 
-following command in the helloworld folder:
+As with all the getting-started samples you can run this sample with:
 
    mvn tuscany:run 
 
-Alternatively, the Tuscany Shell can be started with the scripts in the Tuscany binary distribution 
-bin folder. To do that run the following command at the root of a Tuscany binary distribution:
-
-   bin\tuscany.bat samples\getting-started\helloworld
-
-Once the Shell has been started with one of those methods you can use Shell commands to explore
-the SCA domain, for example, use the "installed" command to get the status of installed contributions, 
-"services" to see the available component services, and you may test calling the helloworld service by 
-using the "invoke" command:
-
-   invoke HelloworldComponent sayHello yourName
-
----
- 
-This sample was created by the Tuscany contribution-jar Maven archetype. You can use that archetype to 
-create your own SCA contribution projects by running the following Maven command:
+Then at a web browser enter the following URL:
 
-   mvn archetype:generate -DarchetypeCatalog=http://tuscany.apache.org
+http://localhost:8080/HelloworldComponent/Helloworld/sayHello?name=world
 
-then at the prompt select 1 to choose the contribution-jar archetype and then answer the questions. 
-This project used the following answers:
+which should return a page saying:
 
-   Define value for property 'groupId': : org.apache.tuscany.sca.samples
-   Define value for property 'artifactId': : helloworld-contribution
-   Define value for property 'version': 1.0-SNAPSHOT: 2.0-SNAPSHOT
-   Define value for property 'package': org.apache.tuscany.sca.samples: sample  
+Hello world
 

Modified: tuscany/sca-java-2.x/trunk/samples/getting-started/helloworld-jaxrs/pom.xml
URL: http://svn.apache.org/viewvc/tuscany/sca-java-2.x/trunk/samples/getting-started/helloworld-jaxrs/pom.xml?rev=1095019&r1=1094917&r2=1095019&view=diff
==============================================================================
--- tuscany/sca-java-2.x/trunk/samples/getting-started/helloworld-jaxrs/pom.xml (original)
+++ tuscany/sca-java-2.x/trunk/samples/getting-started/helloworld-jaxrs/pom.xml Tue Apr 19 10:46:08 2011
@@ -27,10 +27,10 @@
       <version>2.0-SNAPSHOT</version>
       <relativePath>../../pom.xml</relativePath>
    </parent>
-   <artifactId>helloworld</artifactId>
+   <artifactId>helloworld-jaxrs</artifactId>
    <version>2.0-SNAPSHOT</version>
 
-   <name>Apache Tuscany SCA Samples Helloworld</name>
+   <name>Apache Tuscany SCA Samples Helloworld JAX-RS</name>
 
    <properties>
       <tuscany.version>${project.version}</tuscany.version>
@@ -46,6 +46,13 @@
       </dependency>
 
       <dependency>
+         <groupId>javax.ws.rs</groupId>
+         <artifactId>jsr311-api</artifactId>
+         <version>1.1.1</version>
+         <scope>provided</scope>
+      </dependency>
+
+      <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>4.8.1</version>
@@ -59,6 +66,20 @@
          <scope>test</scope>
       </dependency>
 
+      <dependency>
+         <groupId>org.apache.tuscany.sca</groupId>
+         <artifactId>tuscany-binding-rest-runtime</artifactId>
+         <version>${tuscany.version}</version>
+         <scope>test</scope>
+      </dependency>
+
+      <dependency>
+         <groupId>org.mortbay.jetty</groupId>
+         <artifactId>jetty</artifactId>
+         <version>6.1.26</version>
+         <scope>test</scope>
+      </dependency>
+
    </dependencies>
 
    <build>
@@ -70,6 +91,13 @@
             <groupId>org.apache.tuscany.maven.plugins</groupId>
             <artifactId>maven-tuscany-plugin</artifactId>
             <version>${tuscany.version}</version>
+            <dependencies>
+               <dependency>
+                  <groupId>org.apache.tuscany.sca</groupId>
+                  <artifactId>tuscany-binding-rest-runtime</artifactId>
+                  <version>${tuscany.version}</version>
+               </dependency>
+            </dependencies>
          </plugin>
       </plugins>
 

Added: tuscany/sca-java-2.x/trunk/samples/getting-started/helloworld-jaxrs/src/main/java/sample/JaxrsHelloworld.java
URL: http://svn.apache.org/viewvc/tuscany/sca-java-2.x/trunk/samples/getting-started/helloworld-jaxrs/src/main/java/sample/JaxrsHelloworld.java?rev=1095019&view=auto
==============================================================================
--- tuscany/sca-java-2.x/trunk/samples/getting-started/helloworld-jaxrs/src/main/java/sample/JaxrsHelloworld.java (added)
+++ tuscany/sca-java-2.x/trunk/samples/getting-started/helloworld-jaxrs/src/main/java/sample/JaxrsHelloworld.java Tue Apr 19 10:46:08 2011
@@ -0,0 +1,34 @@
+/*
+ * 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.    
+ */
+package sample;
+
+import javax.ws.rs.GET;
+import javax.ws.rs.Path;
+import javax.ws.rs.QueryParam;
+
+import org.oasisopen.sca.annotation.Remotable;
+
+@Remotable
+public interface JaxrsHelloworld {
+
+    @GET
+    @Path("sayHello")
+    String sayHello(@QueryParam("name") String name);
+
+}

Modified: tuscany/sca-java-2.x/trunk/samples/getting-started/helloworld-jaxrs/src/main/resources/helloworld.composite
URL: http://svn.apache.org/viewvc/tuscany/sca-java-2.x/trunk/samples/getting-started/helloworld-jaxrs/src/main/resources/helloworld.composite?rev=1095019&r1=1094917&r2=1095019&view=diff
==============================================================================
--- tuscany/sca-java-2.x/trunk/samples/getting-started/helloworld-jaxrs/src/main/resources/helloworld.composite (original)
+++ tuscany/sca-java-2.x/trunk/samples/getting-started/helloworld-jaxrs/src/main/resources/helloworld.composite Tue Apr 19 10:46:08 2011
@@ -24,6 +24,10 @@
 
     <component name="HelloworldComponent">
         <implementation.java class="sample.HelloworldImpl"/>
+        <service name="Helloworld">
+            <interface.java interface="sample.JaxrsHelloworld"/>
+            <tuscany:binding.rest/>
+        </service>
     </component>
 
 </composite>

Modified: tuscany/sca-java-2.x/trunk/samples/getting-started/helloworld-jaxrs/src/test/java/sample/HelloworldTestCase.java
URL: http://svn.apache.org/viewvc/tuscany/sca-java-2.x/trunk/samples/getting-started/helloworld-jaxrs/src/test/java/sample/HelloworldTestCase.java?rev=1095019&r1=1094917&r2=1095019&view=diff
==============================================================================
--- tuscany/sca-java-2.x/trunk/samples/getting-started/helloworld-jaxrs/src/test/java/sample/HelloworldTestCase.java (original)
+++ tuscany/sca-java-2.x/trunk/samples/getting-started/helloworld-jaxrs/src/test/java/sample/HelloworldTestCase.java Tue Apr 19 10:46:08 2011
@@ -18,31 +18,54 @@
  */
 package sample;
 
-import org.junit.Assert;
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.net.URL;
 
 import org.apache.tuscany.sca.Node;
 import org.apache.tuscany.sca.TuscanyRuntime;
+import org.apache.tuscany.sca.contribution.processor.ContributionReadException;
+import org.apache.tuscany.sca.monitor.ValidationException;
+import org.apache.tuscany.sca.runtime.ActivationException;
+import org.junit.Assert;
 import org.junit.Test;
 import org.oasisopen.sca.NoSuchServiceException;
 
 public class HelloworldTestCase {
 
     @Test
-    public void testSayHello() throws NoSuchServiceException {
+    public void testSayHello() throws NoSuchServiceException, IOException, ContributionReadException, ActivationException, ValidationException {
 
         // Run the SCA composite in a Tuscany runtime
         Node node = TuscanyRuntime.runComposite("helloworld.composite", "target/classes");
         try {
             
-            // Get the Helloworld service proxy
-            Helloworld helloworld = node.getService(Helloworld.class, "HelloworldComponent");
-            
-            // test that it works as expected
-            Assert.assertEquals("Hello Amelia", helloworld.sayHello("Amelia"));
+            // test that has exposed an HTTP endpoint that works as expected
+            URL url = new URL("http://localhost:8080/HelloworldComponent/Helloworld/sayHello?name=Amelia");
+            Assert.assertEquals("Hello Amelia", read(url.openStream()));
             
         } finally {
             // Stop the Tuscany runtime Node
             node.stop();        
         }
     }
+    
+    private static String read(InputStream is) throws IOException {
+        BufferedReader reader = null;
+        try {
+            reader = new BufferedReader(new InputStreamReader(is));
+            StringBuffer sb = new StringBuffer();
+            String str;
+            while ((str = reader.readLine()) != null) {
+                sb.append(str);
+            }
+            return sb.toString();
+        } finally {
+            if (reader != null) {
+                reader.close();
+            }
+        }
+    }
 }