diff options
author | giorgio <giorgio@13f79535-47bb-0310-9956-ffa450edef68> | 2012-09-05 08:31:30 +0000 |
---|---|---|
committer | giorgio <giorgio@13f79535-47bb-0310-9956-ffa450edef68> | 2012-09-05 08:31:30 +0000 |
commit | c9bfccc35345ce58fb5774d4b0b6a9868b262c0a (patch) | |
tree | fe84dd4b90f2acd0b933550b6978094926c1d733 /sca-cpp/branches/lightweight-sca/samples/store-constdb | |
parent | 5ddabdaf1ff856aae79dadc045ef2aeff08c7887 (diff) |
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@1381061 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'sca-cpp/branches/lightweight-sca/samples/store-constdb')
11 files changed, 583 insertions, 0 deletions
diff --git a/sca-cpp/branches/lightweight-sca/samples/store-constdb/Makefile.am b/sca-cpp/branches/lightweight-sca/samples/store-constdb/Makefile.am new file mode 100644 index 0000000000..d2783f3f3b --- /dev/null +++ b/sca-cpp/branches/lightweight-sca/samples/store-constdb/Makefile.am @@ -0,0 +1,25 @@ +# 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. + +dist_sample_SCRIPTS = start stop ssl-start +sampledir = $(prefix)/samples/store-constdb + +nobase_dist_sample_DATA = currency-converter.scm fruits-catalog.scm shopping-cart.scm store.scm store.composite htdocs/*.html + +dist_noinst_SCRIPTS = server-test +TESTS = server-test + diff --git a/sca-cpp/branches/lightweight-sca/samples/store-constdb/currency-converter.scm b/sca-cpp/branches/lightweight-sca/samples/store-constdb/currency-converter.scm new file mode 100644 index 0000000000..fc506c3d73 --- /dev/null +++ b/sca-cpp/branches/lightweight-sca/samples/store-constdb/currency-converter.scm @@ -0,0 +1,27 @@ +; 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. + +; Currency converter implementation + +(define (convert from to amount) + (if (equal? to "EUR") (* amount 0.70) amount) +) + +(define (symbol currency) + (if (equal? currency "EUR") "E" "$") +) + diff --git a/sca-cpp/branches/lightweight-sca/samples/store-constdb/fruits-catalog.scm b/sca-cpp/branches/lightweight-sca/samples/store-constdb/fruits-catalog.scm new file mode 100644 index 0000000000..d55394b96a --- /dev/null +++ b/sca-cpp/branches/lightweight-sca/samples/store-constdb/fruits-catalog.scm @@ -0,0 +1,30 @@ +; 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. + +; Catalog implementation + +(define (items converter currencyCode) + (define code (currencyCode)) + (define (convert price) (converter "convert" "USD" code price)) + (define symbol (converter "symbol" code)) + (list + (list (list 'name "Apple") (list 'currencyCode code) (list 'currencySymbol symbol) (list 'price (convert 2.99))) + (list (list 'name "Orange") (list 'currencyCode code) (list 'currencySymbol symbol) (list 'price (convert 3.55))) + (list (list 'name "Pear") (list 'currencyCode code) (list 'currencySymbol symbol) (list 'price (convert 1.55))) + ) +) + diff --git a/sca-cpp/branches/lightweight-sca/samples/store-constdb/htdocs/index.html b/sca-cpp/branches/lightweight-sca/samples/store-constdb/htdocs/index.html new file mode 100644 index 0000000000..0698a32cc5 --- /dev/null +++ b/sca-cpp/branches/lightweight-sca/samples/store-constdb/htdocs/index.html @@ -0,0 +1,156 @@ +<!-- + * 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. +--> +<html> +<head> +<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0"/> +<meta name="apple-mobile-web-app-capable" content="yes"/> +<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent"/> +<link rel="stylesheet" type="text/css" href="/ui-min.css"/> +<title>Store</title> + +<script type="text/javascript" src="/all-min.js"></script> + +<script type="text/javascript"> +var store = sca.component("Store"); +var catalog = sca.defun(sca.reference(store, "catalog"), "items"); +var shoppingCart = sca.reference(store, "shoppingCart"); +var shoppingTotal = sca.defun(sca.reference(store, "shoppingTotal"), "total"); + +var catalogItems; + +function catalog_itemsResponse(items, exception) { + if (exception){ + alert(exception.message); + return; + } + var catalog = ""; + for (var i=0; i<items.length; i++) { + var item = items[i].name + ' - ' + items[i].price; + catalog += '<input name="items" type="checkbox" value="' + + item + '">' + item + ' <br>'; + } + document.getElementById('catalog').innerHTML=catalog; + catalogItems = items; + +} + +function shoppingCart_getResponse(doc) { + if (doc != null) { + var feed = parseXML([doc]); + var entries = feed.getElementsByTagName("entry"); + var list = ""; + for (var i=0; i<entries.length; i++) { + var content = entries[i].getElementsByTagName("content")[0]; + var name = content.getElementsByTagName("name")[0].firstChild.nodeValue; + var price = content.getElementsByTagName("price")[0].firstChild.nodeValue; + list += name + ' - ' + price + ' <br>'; + } + document.getElementById("shoppingCart").innerHTML = list; + + shoppingTotal.total(shoppingTotal_totalResponse); + } +} + +function shoppingTotal_totalResponse(total, exception) { + if (exception) { + alert(exception.message); + return; + } + document.getElementById('total').innerHTML = total; +} + +function shoppingCart_postResponse(entry) { + shoppingCart.get("", shoppingCart_getResponse); +} + +function addToCart() { + var items = document.catalogForm.items; + var j = 0; + for (var i=0; i<items.length; i++) + if (items[i].checked) { + var entry = '<?xml version="1.0" encoding="UTF-8"?>\n' + + '<entry xmlns="http://www.w3.org/2005/Atom"><title type="text">Item</title><content type="application/xml">' + + '<item>' + + '<name>' + catalogItems[i].name + '</name>' + + '<currencyCode>' + catalogItems[i].currencyCode + '</currencyCode>' + + '<currencySymbol>' + catalogItems[i].currencySymbol + '</currencySymbol>' + + '<price>' + catalogItems[i].price + '</price>' + + '</item>' + + '</content></entry>'; + shoppingCart.post(entry, shoppingCart_postResponse); + items[i].checked = false; + } +} +function checkoutCart() { + document.getElementById('store').innerHTML='<h2>' + + 'Thanks for Shopping With Us!</h2>'+ + '<h2>Your Order</h2>'+ + '<form name="orderForm">'+ + document.getElementById('shoppingCart').innerHTML+ + '<br>'+ + document.getElementById('total').innerHTML+ + '<br>'+ + '<br>'+ + '<input type="submit" value="Continue Shopping">'+ + '</form>'; + shoppingCart.del("", null); +} +function deleteCart() { + shoppingCart.del("", null); + document.getElementById('shoppingCart').innerHTML = ""; + document.getElementById('total').innerHTML = ""; +} + +function init() { + try { + catalog.items(catalog_itemsResponse); + shoppingCart.get("", shoppingCart_getResponse); + } catch(e){ + alert(e); + } +} +</script> +</head> + +<body onload="init()"> +<h1>Store</h1> +<br/> +<div id="store"> +<h2>Catalog</h2> +<form name="catalogForm"> +<div id="catalog" ></div> +<br> +<input type="button" onClick="addToCart()" value="Add to Cart"> +</form> +<br> + +<h2>Your Shopping Cart</h2> +<form name="shoppingCartForm"> +<div id="shoppingCart"></div> +<br> +<div id="total"></div> +<br> +<input type="button" onClick="checkoutCart()" value="Checkout"> +<input type="button" onClick="deleteCart()" value="Empty"> +<a href="shoppingCart/">(feed)</a> +</form> +</div> + +</body> +</html> diff --git a/sca-cpp/branches/lightweight-sca/samples/store-constdb/server-test b/sca-cpp/branches/lightweight-sca/samples/store-constdb/server-test new file mode 100755 index 0000000000..fb629a6814 --- /dev/null +++ b/sca-cpp/branches/lightweight-sca/samples/store-constdb/server-test @@ -0,0 +1,58 @@ +#!/bin/sh + +# 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. + +echo "Testing..." +here=`echo "import os; print os.path.realpath('$0')" | python`; here=`dirname $here` +curl_prefix=`cat $here/../../modules/http/curl.prefix` + +# Setup +./start +sleep 2 + +# Test HTTP GET +$curl_prefix/bin/curl http://localhost:8090/ 2>/dev/null >tmp/index.html +diff tmp/index.html htdocs/index.html +rc=$? + +# Test Catalog +if [ "$rc" = "0" ]; then + $curl_prefix/bin/curl http://localhost:8090/references/Store/catalog -X POST -H "Content-type: application/json-rpc" --data @../store-cpp/htdocs/test/items-request.txt >tmp/items-result.txt 2>/dev/null + diff tmp/items-result.txt ../store-cpp/htdocs/test/items-result.txt + rc=$? +fi + +# Test Shopping Cart +if [ "$rc" = "0" ]; then + $curl_prefix/bin/curl http://localhost:8090/references/Store/shoppingCart -X POST -H "Content-type: application/atom+xml" --data @../store-cpp/htdocs/test/shopping-cart-entry.xml 2>/dev/null + rc=$? +fi +if [ "$rc" = "0" ]; then + $curl_prefix/bin/curl http://localhost:8090/references/Store/shoppingCart >tmp/shopping-cart-feed.xml 2>/dev/null + grep "3.55" tmp/shopping-cart-feed.xml >/dev/null + rc=$? +fi + +# Cleanup +./stop +sleep 2 + +if [ "$rc" = "0" ]; then + echo "OK" +fi +exit $rc diff --git a/sca-cpp/branches/lightweight-sca/samples/store-constdb/shopping-cart.scm b/sca-cpp/branches/lightweight-sca/samples/store-constdb/shopping-cart.scm new file mode 100644 index 0000000000..e653f1e33c --- /dev/null +++ b/sca-cpp/branches/lightweight-sca/samples/store-constdb/shopping-cart.scm @@ -0,0 +1,82 @@ +; 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. + +; Shopping cart implementation + +(define cartId "1234") + +; Get the shopping cart from the cache +; Return an empty cart if not found +(define (getcart id cache) + (define cart (cache "get" (list id))) + (if (nul cart) + (list) + cart) +) + +; Post a new item to the cart, create a new cart if necessary +(define (post collection item cache) + (define id (uuid)) + (define newItem (list 'entry (cadr (car item)) (list 'id id) (cadddr (car item)))) + (define cart (cons newItem (getcart cartId cache))) + (cache "put" (list cartId) cart) + (list id) +) + +; Find an item in the cart +(define (find id cart) + (if (nul cart) + (list (list 'entry (list 'title "Item") (list 'id "0"))) + (if (= id (cadr (caddr (car cart)))) + (list (car cart)) + (find id (cdr cart)))) +) + +; Get items from the cart +(define (get id cache) + (if (nul id) + (list (append (list 'feed (list 'title "Your Cart") (list 'id cartId)) (getcart cartId cache))) + (find (car id) (getcart cartId cache)) + ) +) + +; Delete items from the cart +(define (delete id cache) + (if (nul id) + (cache "delete" (list cartId)) + true + ) +) + +; Return the price of an item +(define (price item) + (cadr (assoc 'price (cadr (cadddr item)))) +) + +; Sum the prices of a list of items +(define (sum items) + (if (nul items) + 0 + (+ (price (car items)) (sum (cdr items)))) +) + +; Return the total price of the items in the cart +(define (total cache) + (define cart (getcart cartId cache)) + (sum cart) +) + diff --git a/sca-cpp/branches/lightweight-sca/samples/store-constdb/ssl-start b/sca-cpp/branches/lightweight-sca/samples/store-constdb/ssl-start new file mode 100755 index 0000000000..047ec974cb --- /dev/null +++ b/sca-cpp/branches/lightweight-sca/samples/store-constdb/ssl-start @@ -0,0 +1,36 @@ +#!/bin/sh + +# 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. + +../../modules/http/ssl-ca-conf tmp localhost +../../modules/http/ssl-cert-conf tmp localhost +../../modules/http/httpd-conf tmp localhost 8090 htdocs +../../modules/http/httpd-ssl-conf tmp 8453 +../../modules/http/basic-auth-conf tmp +../../modules/http/passwd-auth-conf tmp foo foo +../../modules/server/server-conf tmp +../../modules/server/scheme-conf tmp +cat >>tmp/conf/httpd.conf <<EOF +# Configure SCA Composite +SCAContribution `pwd`/ +SCAComposite store.composite + +EOF + +../../components/constdb/tinycdb -c -m tmp/store.cdb </dev/null +../../modules/http/httpd-start tmp diff --git a/sca-cpp/branches/lightweight-sca/samples/store-constdb/start b/sca-cpp/branches/lightweight-sca/samples/store-constdb/start new file mode 100755 index 0000000000..58b15c0ec3 --- /dev/null +++ b/sca-cpp/branches/lightweight-sca/samples/store-constdb/start @@ -0,0 +1,33 @@ +#!/bin/sh + +# 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. + +rm -rf tmp +../../modules/http/httpd-conf tmp localhost 8090 htdocs +../../modules/http/httpd-event-conf tmp +../../modules/server/server-conf tmp +../../modules/server/scheme-conf tmp +cat >>tmp/conf/httpd.conf <<EOF +# Configure SCA Composite +SCAContribution `pwd`/ +SCAComposite store.composite + +EOF + +../../components/constdb/tinycdb -c -m tmp/store.cdb </dev/null +../../modules/http/httpd-start tmp diff --git a/sca-cpp/branches/lightweight-sca/samples/store-constdb/stop b/sca-cpp/branches/lightweight-sca/samples/store-constdb/stop new file mode 100755 index 0000000000..3b4f46694d --- /dev/null +++ b/sca-cpp/branches/lightweight-sca/samples/store-constdb/stop @@ -0,0 +1,20 @@ +#!/bin/sh + +# 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. + +../../modules/http/httpd-stop tmp diff --git a/sca-cpp/branches/lightweight-sca/samples/store-constdb/store.composite b/sca-cpp/branches/lightweight-sca/samples/store-constdb/store.composite new file mode 100644 index 0000000000..168be26ab6 --- /dev/null +++ b/sca-cpp/branches/lightweight-sca/samples/store-constdb/store.composite @@ -0,0 +1,69 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + * 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. +--> +<composite xmlns="http://docs.oasis-open.org/ns/opencsa/sca/200912" + targetNamespace="http://store" + name="store"> + + <component name="Store"> + <implementation.scheme script="store.scm"/> + <service name="Widget"> + <binding.http uri="store"/> + </service> + <reference name="catalog" target="Catalog"/> + <reference name="shoppingCart" target="ShoppingCart/Cart"/> + <reference name="shoppingTotal" target="ShoppingCart/Total"/> + </component> + + <component name="Catalog"> + <implementation.scheme script="fruits-catalog.scm"/> + <property name="currencyCode">USD</property> + <service name="Catalog"> + <binding.jsonrpc uri="catalog"/> + </service> + <reference name="currencyConverter" target="CurrencyConverter"/> + </component> + + <component name="ShoppingCart"> + <implementation.scheme script="shopping-cart.scm"/> + <service name="Cart"> + <binding.atom uri="shoppingCart"/> + </service> + <service name="Total"> + <binding.jsonrpc uri="total"/> + </service> + <reference name="cache" target="ConstDb"/> + </component> + + <component name="CurrencyConverter"> + <implementation.scheme script="currency-converter.scm"/> + <service name="CurrencyConverter"> + <binding.jsonrpc uri="currencyConverter"/> + </service> + </component> + + <component name="ConstDb"> + <implementation.cpp path="../../components/constdb" library="libconstdb"/> + <property name="dbname">tmp/store.cdb</property> + <service name="ConstDb"> + <binding.atom uri="constdb"/> + </service> + </component> + +</composite> diff --git a/sca-cpp/branches/lightweight-sca/samples/store-constdb/store.scm b/sca-cpp/branches/lightweight-sca/samples/store-constdb/store.scm new file mode 100644 index 0000000000..f54257343e --- /dev/null +++ b/sca-cpp/branches/lightweight-sca/samples/store-constdb/store.scm @@ -0,0 +1,47 @@ +; 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. + +; Store implementation + +(define (post item catalog shoppingCart shoppingTotal) + (shoppingCart "post" item) +) + +(define (getall catalog shoppingCart shoppingTotal) + (shoppingCart "getall") +) + +(define (get id catalog shoppingCart shoppingTotal) + (shoppingCart "get" id) +) + +(define (items catalog shoppingCart shoppingTotal) + (catalog "items") +) + +(define (total catalog shoppingCart shoppingTotal) + (shoppingCart "total") +) + +(define (deleteall catalog shoppingCart shoppingTotal) + (shoppingCart "deleteall") +) + +(define (delete id catalog shoppingCart shoppingTotal) + (shoppingCart "delete" id) +) + |