Step 6: Changing implementations and bindings
You can use alternative implementations and bindings in your SCA application. For example, you can replace the Java implementation with JavaScript and you can add an RMI binding to the composite service.
Using JavaScript for your implementation
To replace the Java implementation with JavaScript:
Remove the Java implementation of the component VatServiceComponent.
-
Add, as a new component implementation, a script implementation (use the ScriptImplementation creation tool from the Palette or right-click on VatServiceComponent).

-
In the Properties view, set the Script property to the value restaurant/lib/VatServiceImpl.js
.

-
In directory src/restaurant/lib add a new File named VatServiceImpl.js. Open the newly created file. Set the following code as implementation:
var vatRate=10
function getPriceWithVat(price) {
return price * vatRate/100 + price;
}
|
-
Launch the Client. You should see:

Use an RMI Binding
You can add an RMIBinding to the RestaurantService composite service as follows:

-
Click on the RestaurantService composite.
-
In the Properties view set the following properites:
- ServiceName: RestaurantServiceRMI,
- Port: 8099, and
- Host: localhost.

-
In the package test add a new Java Class named RestaurantServiceServer.
package test;
import org.apache.tuscany.sca.host.embedded.SCADomain;
/**
* A restaurant service server. Starts up the SCA runtime which will start
* listening for RMI service requests.
*/
public class RestaurantServiceServer {
public static void main(String[] args) throws Exception {
System.out
.println("Starting of the SCA Restaurant Application
exposed as RMI Services...");
SCADomain scaDomain = SCADomain
.newInstance("Restaurant.composite");
System.out.println("... Press Enter to Exit...");
System.in.read();
scaDomain.close();
System.out.println("Exited...");
System.exit(0);
}
}
|
-
Create a new Java Class named Client2 in the package test.
package test;
import java.rmi.Naming;
import restaurant.api.Menu;
import restaurant.api.RestaurantService;
public class Client2 {
public static void main(String[] args) throws Exception {
RestaurantService restaurantService = (RestaurantService) Naming
.lookup("//localhost:8099/RestaurantServiceRMI");
Menu[] menus = restaurantService.getMenus();
System.out.println("--- Menu ---");
for (Menu m : menus) {
System.out.println("- " + m.printMenu());
}
System.out.println();
Menu menu = menus[3];
System.out.println("My choice: " + menu.printMenu());
System.out.println();
double price = restaurantService.getBill(menu);
System.out.println("Price (" + menu.printMenu() + "): " + price);
}
}
|
-
Launch the RestaurantServiceServer application.

-
Launch Client2. You should see:
