Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

The first thing you do is to create a folder on you disk into which you will download the TUSCANY distribution.
Next you download the latest release distribution. Launch your browser and enter one of the
following URL's.
Latest Release - http://cwiki.apache.org/TUSCANY/sca-java-releases.html

...

Next you enter "services" as the package Name, and press the Finish button to complete the
dialog.

 
Repeat the previous step to create another package named "ufservices". The store project now
should look as follows.
 

 
In the following you will place in the "services" package the regular services, and in the "ufservices"
package the user facing services of the composite service application you create.

Catalog

In this step you create the Catalog service interface and implementation.
Select the "services" package. Next you click on the dropdown arrow next to the New Java Class
button    and select the New Java Interface   
option from the dropdown list. In the dialog
enter "Catalog" as the Name of the interface and select the Finish button to complete the dialog Wiki Markupenter "Catalog" as the *{_}Name{_}* of the interface and select the Finish button to complete the dialog. The Java editor will open on the new created Java interface. Replace the content of the editor by *{_}copy-paste{_}* of the following Java interface code snippet.   package services; import org.osoa.sca.annotations.Remotable; @Remotable public interface Catalog { String\[\] get(); } .
The Java editor will open on the new created Java interface. Replace the content of the editor by
copy-paste of the following Java interface code snippet.

Code Block

package services;
import org.osoa.sca.annotations.Remotable;
@Remotable
public interface Catalog {
    String[] get();
}

Select the "services" package again. Select the New Java Class button Image Added . In the dialog enter
"CatalogImpl" as the Name of the class, add "Catalog" as the interface this class implements, and
then select Finish to complete the dialog.

The Java editor will open on the new created Java class. Replace the content of the editor by
copy-paste of the following Java class code snippet.

Code Block

package services;
import java.util.ArrayList;
import java.util.List;
import org.osoa.sca.annotations.Init;
import org.osoa.sca.annotations.Property;
import org.osoa.sca.annotations.Reference;
public class CatalogImpl implements Catalog {
	@Property
	public String currencyCode = "USD";
	@Reference
	public CurrencyConverter currencyConverter;
	private List<String> catalog = new ArrayList<String>();
	@Init
	public void init() {
		String currencySymbol = currencyConverter.getCurrencySymbol(currencyCode);
		catalog.add("Apple - " + currencySymbol +
		currencyConverter.getConversion("USD", currencyCode, 2.99f));
		catalog.add("Orange - " + currencySymbol +
		currencyConverter.getConversion("USD", currencyCode, 3.55f));
		catalog.add("Pear - " + currencySymbol +
		currencyConverter.getConversion("USD", currencyCode, 1.55f));
	}
	public String[] get() {
		String[] catalogArray = new String[catalog.size()];
		catalog.toArray(catalogArray);
		return catalogArray;
	}
}

After completing these steps the content of the "store" project will look as follows.
Image Added
 
Note: CatalogImpl is red x'ed because it makes use of the CurrencyConverter interface that we
have not implemented yet.
 

CurrencyConverter

In this step you create the CurrencyConverter service interface and implementation.
You follow the same steps that you learned previously to create the interface and implementation.
First create a Java interface in the "services" package named "CurrencyConverter" and copy-paste
the following Java interface code snippet into it.
  

Code Block

package services;
import org.osoa.sca.annotations.Remotable;
@Remotable
public interface CurrencyConverter {
	public float getConversion(String fromCurrenycCode,
	String toCurrencyCode, float amount);
	public String getCurrencySymbol(String currencyCode);
}

Next create a Java class in the "services" package named "CurrencyConverterImpl" and copy-paste
the following Java class code snippet into it.

Code Block

package services;
public class CurrencyConverterImpl implements CurrencyConverter {
	public float getConversion(String fromCurrencyCode,
		String toCurrencyCode, float amount) {
		if (toCurrencyCode.equals("USD"))
			return amount;
		else 			if (toCurrencyCode.equals("EUR"))
				return amount*0.7256f;
		return 0;
	}
	public String getCurrencySymbol(String currencyCode) {
		if (currencyCode.equals("USD"))
			return "$";
		else
			if (currencyCode.equals("EUR"))
				return "€";
		return "?";
	}
}

After completing these steps the content of the "store" project will look as follows.Image Added

ShoppingCart

In this step you create the ShoppingCart service implementation.

You follow the same steps that you learned previously to create the implementation.

Create a Java class in the "services" package named "ShoppingCartImpl" and copy-paste the
following Java class code snippet into it.

Code Block

package services;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import org.apache.tuscany.sca.binding.feed.collection.Collection;
import org.apache.tuscany.sca.binding.feed.collection.NotFoundException;
import com.sun.syndication.feed.atom.Content;
import com.sun.syndication.feed.atom.Entry;
import com.sun.syndication.feed.atom.Feed;
import com.sun.syndication.feed.atom.Link;
public class ShoppingCartImpl implements Collection {
	// needs to change to instance var once conversation scope works
	private static Map<String, Entry> cart = new HashMap<String, Entry>();
	public Feed getFeed() {
		Feed feed = new Feed();
		feed.setTitle("shopping cart");
		Content subtitle = new Content();
		subtitle.setValue("Total : " + getTotal());
		feed.setSubtitle(subtitle);
		feed.getEntries().addAll(cart.values());
		return feed;
	}
	public Entry get(String id) throws NotFoundException {
		return cart.get(id);
	}
	public Entry post(Entry entry) {
		String id = "cart-" + UUID.randomUUID().toString();
		entry.setId(id);
		Link link = new Link();
		link.setRel("edit");
		link.setHref("" + id);
		entry.getOtherLinks().add(link);
		link = new Link();
		link.setRel("alternate");
		link.setHref("" + id);
		entry.getAlternateLinks().add(link);
		entry.setCreated(new Date());
		cart.put(id, entry);
		return entry;
	}
		public Entry put(String id, Entry entry) throws NotFoundException {
		entry.setUpdated(new Date());
		cart.put(id, entry);
		return entry;
	}
	public void delete(String id) throws NotFoundException {
		if (id.equals(""))
			cart.clear();
		else
			cart.remove(id);
	} 	private String getTotal() {
		float total = 0;
		String symbol = "";
		if (!cart.isEmpty()) {
			Entry entry = cart.values().iterator().next();
			String item = ((Content)entry.getContents().get(0)).getValue();
			symbol = item.substring(item.indexOf("-")+2, item.indexOf("-")+3);
		}
		for (Entry entry : cart.values()) {
			String item = ((Content)entry.getContents().get(0)).getValue();
			total += Float.valueOf(item.substring(item.indexOf("-")+3));
		}
		return symbol + String.valueOf(total);
	}
}