From 5a390791d3049d415d98507be8b8ed29f523ecf0 Mon Sep 17 00:00:00 2001 From: jsdelfino Date: Wed, 10 Mar 2010 09:34:20 +0000 Subject: Minor cleanup, renamed mcache to memcache, added a uuid util function to get consistent and unique uuids across languages. git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@921270 13f79535-47bb-0310-9956-ffa450edef68 --- sca-cpp/trunk/components/cache/Makefile.am | 10 +- sca-cpp/trunk/components/cache/client-test.cpp | 2 +- sca-cpp/trunk/components/cache/mcache-test.cpp | 82 ---------- sca-cpp/trunk/components/cache/mcache.composite | 32 ---- sca-cpp/trunk/components/cache/mcache.cpp | 133 ---------------- sca-cpp/trunk/components/cache/mcache.hpp | 175 --------------------- sca-cpp/trunk/components/cache/memcache-test.cpp | 82 ++++++++++ sca-cpp/trunk/components/cache/memcache.composite | 32 ++++ sca-cpp/trunk/components/cache/memcache.cpp | 133 ++++++++++++++++ sca-cpp/trunk/components/cache/memcache.hpp | 175 +++++++++++++++++++++ sca-cpp/trunk/components/cache/memcached-stop | 2 +- sca-cpp/trunk/components/cache/memcached-test | 2 +- sca-cpp/trunk/components/cache/server-test | 2 +- sca-cpp/trunk/components/queue/qpidd-stop | 2 +- sca-cpp/trunk/etc/git-exclude | 2 +- sca-cpp/trunk/modules/java/eval.hpp | 38 ++++- .../modules/java/org/apache/tuscany/UUIDUtil.java | 32 ++++ sca-cpp/trunk/modules/wsgi/wsgi-stop | 2 +- sca-cpp/trunk/test/store-cpp/store.composite | 2 +- sca-cpp/trunk/test/store-java/store.composite | 2 +- .../test/store-java/store/ShoppingCartImpl.java | 16 +- sca-cpp/trunk/test/store-python/store.composite | 2 +- sca-cpp/trunk/test/store-scheme/store.composite | 2 +- 23 files changed, 505 insertions(+), 457 deletions(-) delete mode 100644 sca-cpp/trunk/components/cache/mcache-test.cpp delete mode 100644 sca-cpp/trunk/components/cache/mcache.composite delete mode 100644 sca-cpp/trunk/components/cache/mcache.cpp delete mode 100644 sca-cpp/trunk/components/cache/mcache.hpp create mode 100644 sca-cpp/trunk/components/cache/memcache-test.cpp create mode 100644 sca-cpp/trunk/components/cache/memcache.composite create mode 100644 sca-cpp/trunk/components/cache/memcache.cpp create mode 100644 sca-cpp/trunk/components/cache/memcache.hpp create mode 100644 sca-cpp/trunk/modules/java/org/apache/tuscany/UUIDUtil.java (limited to 'sca-cpp') diff --git a/sca-cpp/trunk/components/cache/Makefile.am b/sca-cpp/trunk/components/cache/Makefile.am index d9ab3f805b..b7397c64da 100644 --- a/sca-cpp/trunk/components/cache/Makefile.am +++ b/sca-cpp/trunk/components/cache/Makefile.am @@ -18,14 +18,14 @@ comp_SCRIPTS = memcached-start memcached-stop compdir=$(prefix)/components/cache -comp_LTLIBRARIES = libmcache.la -libmcache_la_SOURCES = mcache.cpp +comp_LTLIBRARIES = libmemcache.la +libmemcache_la_SOURCES = memcache.cpp -mcache_test_SOURCES = mcache-test.cpp -mcache_test_LDFLAGS = -lxml2 +memcache_test_SOURCES = memcache-test.cpp +memcache_test_LDFLAGS = -lxml2 client_test_SOURCES = client-test.cpp client_test_LDFLAGS = -lxml2 -lcurl -lmozjs -noinst_PROGRAMS = mcache-test client-test +noinst_PROGRAMS = memcache-test client-test TESTS = memcached-test server-test diff --git a/sca-cpp/trunk/components/cache/client-test.cpp b/sca-cpp/trunk/components/cache/client-test.cpp index ddf093a6dc..e3ec449e69 100644 --- a/sca-cpp/trunk/components/cache/client-test.cpp +++ b/sca-cpp/trunk/components/cache/client-test.cpp @@ -36,7 +36,7 @@ namespace tuscany { namespace cache { -const string uri("http://localhost:8090/mcache"); +const string uri("http://localhost:8090/memcache"); bool testCache() { http::CURLSession cs; diff --git a/sca-cpp/trunk/components/cache/mcache-test.cpp b/sca-cpp/trunk/components/cache/mcache-test.cpp deleted file mode 100644 index 316372c5be..0000000000 --- a/sca-cpp/trunk/components/cache/mcache-test.cpp +++ /dev/null @@ -1,82 +0,0 @@ -/* - * 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. - */ - -/* $Rev$ $Date$ */ - -/** - * Test Memcached access functions. - */ - -#include -#include "stream.hpp" -#include "string.hpp" -#include "perf.hpp" -#include "mcache.hpp" - -namespace tuscany { -namespace cache { - -bool testMemCached() { - MemCached ch("127.0.0.1", 11211); - const value k = mklist("a"); - - assert(hasContent(post(k, string("AAA"), ch))); - assert((get(k, ch)) == value(string("AAA"))); - assert(hasContent(put(k, string("aaa"), ch))); - assert((get(k, ch)) == value(string("aaa"))); - assert(hasContent(del(k, ch))); - assert(!hasContent(get(k, ch))); - - return true; -} - -struct getLoop { - const value k; - MemCached& ch; - getLoop(const value& k, MemCached& ch) : k(k), ch(ch) { - } - const bool operator()() const { - assert((get(k, ch)) == value(string("CCC"))); - return true; - } -}; - -bool testGetPerf() { - const value k = mklist("c"); - MemCached ch("127.0.0.1", 11211); - assert(hasContent(post(k, string("CCC"), ch))); - - const lambda gl = getLoop(k, ch); - cout << "Memcached get test " << time(gl, 5, 200) << " ms" << endl; - return true; -} - -} -} - -int main() { - tuscany::cout << "Testing..." << tuscany::endl; - - tuscany::cache::testMemCached(); - tuscany::cache::testGetPerf(); - - tuscany::cout << "OK" << tuscany::endl; - - return 0; -} diff --git a/sca-cpp/trunk/components/cache/mcache.composite b/sca-cpp/trunk/components/cache/mcache.composite deleted file mode 100644 index 15411dd702..0000000000 --- a/sca-cpp/trunk/components/cache/mcache.composite +++ /dev/null @@ -1,32 +0,0 @@ - - - - - - - - - - - - diff --git a/sca-cpp/trunk/components/cache/mcache.cpp b/sca-cpp/trunk/components/cache/mcache.cpp deleted file mode 100644 index 782de605c6..0000000000 --- a/sca-cpp/trunk/components/cache/mcache.cpp +++ /dev/null @@ -1,133 +0,0 @@ -/* - * 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. - */ - -/* $Rev$ $Date$ */ - -/** - * Memcached-based cache component implementation. - */ - -#include - -#include "string.hpp" - -#include "function.hpp" -#include "list.hpp" -#include "value.hpp" -#include "monad.hpp" -#include "mcache.hpp" - -namespace tuscany { -namespace mcache { - -/** - * Get an item from the cache. - */ -const failable get(const list& params, cache::MemCached& ch) { - return cache::get(car(params), ch); -} - -/** - * Post an item to the cache. - */ -const value uuidValue() { - apr_uuid_t uuid; - apr_uuid_get(&uuid); - char buf[APR_UUID_FORMATTED_LENGTH]; - apr_uuid_format(buf, &uuid); - return value(string(buf, APR_UUID_FORMATTED_LENGTH)); -} - -const failable post(const list& params, cache::MemCached& ch) { - const value id = append(car(params), mklist(uuidValue())); - const failable val = cache::post(id, cadr(params), ch); - if (!hasContent(val)) - return mkfailure(reason(val)); - return id; -} - -/** - * Put an item into the cache. - */ -const failable put(const list& params, cache::MemCached& ch) { - const failable val = cache::put(car(params), cadr(params), ch); - if (!hasContent(val)) - return mkfailure(reason(val)); - return value(content(val)); -} - -/** - * Delete an item from the cache. - */ -const failable del(const list& params, cache::MemCached& ch) { - const failable val = cache::del(car(params), ch); - if (!hasContent(val)) - return mkfailure(reason(val)); - return value(content(val)); -} - -/** - * Component implementation lambda function. - */ -class applyCache { -public: - applyCache(cache::MemCached& ch) : ch(ch) { - } - - const value operator()(const list& params) const { - const value func(car(params)); - if (func == "get") - return get(cdr(params), ch); - if (func == "post") - return post(cdr(params), ch); - if (func == "put") - return put(cdr(params), ch); - if (func == "delete") - return del(cdr(params), ch); - return tuscany::mkfailure(); - } - -private: - cache::MemCached& ch; -}; - -/** - * Start the component. - */ -const failable start(unused const list& params) { - // Connect to memcached - cache::MemCached& ch = *(new (gc_new()) cache::MemCached("127.0.0.1", 11211)); - - // Return the component implementation lambda function - return value(lambda&)>(applyCache(ch))); -} - -} -} - -extern "C" { - -const tuscany::value apply(const tuscany::list& params) { - const tuscany::value func(car(params)); - if (func == "start") - return tuscany::mcache::start(cdr(params)); - return tuscany::mkfailure(); -} - -} diff --git a/sca-cpp/trunk/components/cache/mcache.hpp b/sca-cpp/trunk/components/cache/mcache.hpp deleted file mode 100644 index 4751975099..0000000000 --- a/sca-cpp/trunk/components/cache/mcache.hpp +++ /dev/null @@ -1,175 +0,0 @@ -/* - * 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. - */ - -/* $Rev$ $Date$ */ - -#ifndef tuscany_mcache_hpp -#define tuscany_mcache_hpp - -/** - * Memcached access functions. - */ - -#include "apr.h" -#include "apu.h" -#include "apr_general.h" -#include "apr_strings.h" -#include "apr_hash.h" -#include "apr_memcache.h" -#include "apr_network_io.h" - -#include "string.hpp" -#include "list.hpp" -#include "value.hpp" -#include "monad.hpp" -#include "../../modules/scheme/eval.hpp" - -namespace tuscany { -namespace cache { - -/** - * Represents a memcached context. - */ -class MemCached { -public: - MemCached() : owner(false) { - } - - MemCached(const string host, const int port) : owner(true) { - apr_pool_create(&pool, NULL); - apr_memcache_create(pool, 1, 0, &mc); - init(host, port); - } - - MemCached(const MemCached& c) : owner(false) { - pool = c.pool; - mc = c.mc; - } - - ~MemCached() { - if (!owner) - return; - apr_pool_destroy(pool); - } - -private: - bool owner; - apr_pool_t* pool; - apr_memcache_t* mc; - - friend const failable post(const value& key, const value& val, const MemCached& cache); - friend const failable put(const value& key, const value& val, const MemCached& cache); - friend const failable get(const value& key, const MemCached& cache); - friend const failable del(const value& key, const MemCached& cache); - - /** - * Initialize the memcached context. - */ - const failable init(const string& host, const int port) { - apr_memcache_server_t *server; - const apr_status_t sc = apr_memcache_server_create(pool, c_str(host), (apr_port_t)port, 0, 1, 1, 60, &server); - if (sc != APR_SUCCESS) - return mkfailure("Could not create server"); - const apr_status_t as = apr_memcache_add_server(mc, server); - if (as != APR_SUCCESS) - return mkfailure("Could not add server"); - return true; - } -}; - -/** - * Post a new item to the cache. - */ -const failable post(const value& key, const value& val, const MemCached& cache) { - debug(key, "cache::post::key"); - debug(val, "cache::post::value"); - - const string ks(scheme::writeValue(key)); - const string vs(scheme::writeValue(val)); - const apr_status_t rc = apr_memcache_add(cache.mc, c_str(ks), const_cast(c_str(vs)), length(vs), 0, 27); - if (rc != APR_SUCCESS) - return mkfailure("Could not add entry"); - - debug(true, "cache::post::result"); - return true; -} - -/** - * Update an item in the cache. If the item doesn't exist it is added. - */ -const failable put(const value& key, const value& val, const MemCached& cache) { - debug(key, "cache::put::key"); - debug(val, "cache::put::value"); - - const string ks(scheme::writeValue(key)); - const string vs(scheme::writeValue(val)); - const apr_status_t rc = apr_memcache_set(cache.mc, c_str(ks), const_cast(c_str(vs)), length(vs), 0, 27); - if (rc != APR_SUCCESS) - return mkfailure("Could not add entry"); - - debug(true, "cache::put::result"); - return true; -} - -/** - * Get an item from the cache. - */ -const failable get(const value& key, const MemCached& cache) { - debug(key, "cache::get::key"); - - const string ks(scheme::writeValue(key)); - apr_pool_t* vpool; - const apr_status_t pc = apr_pool_create(&vpool, cache.pool); - if (pc != APR_SUCCESS) - return mkfailure("Could not allocate memory"); - - char *data; - apr_size_t size; - const apr_status_t rc = apr_memcache_getp(cache.mc, cache.pool, c_str(ks), &data, &size, NULL); - if (rc != APR_SUCCESS) { - apr_pool_destroy(vpool); - return mkfailure("Could not get entry"); - } - - const value val(scheme::readValue(string(data, size))); - apr_pool_destroy(vpool); - - debug(val, "cache::get::result"); - return val; -} - -/** - * Delete an item from the cache - */ -const failable del(const value& key, const MemCached& cache) { - debug(key, "cache::delete::key"); - - const string ks(scheme::writeValue(key)); - const apr_status_t rc = apr_memcache_delete(cache.mc, c_str(ks), 0); - if (rc != APR_SUCCESS) - return mkfailure("Could not delete entry"); - - debug(true, "cache::delete::result"); - return true; -} - -} -} - -#endif /* tuscany_mcache_hpp */ diff --git a/sca-cpp/trunk/components/cache/memcache-test.cpp b/sca-cpp/trunk/components/cache/memcache-test.cpp new file mode 100644 index 0000000000..bc590fa047 --- /dev/null +++ b/sca-cpp/trunk/components/cache/memcache-test.cpp @@ -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. + */ + +/* $Rev$ $Date$ */ + +/** + * Test Memcached access functions. + */ + +#include +#include "stream.hpp" +#include "string.hpp" +#include "perf.hpp" +#include "memcache.hpp" + +namespace tuscany { +namespace cache { + +bool testMemCached() { + MemCached ch("127.0.0.1", 11211); + const value k = mklist("a"); + + assert(hasContent(post(k, string("AAA"), ch))); + assert((get(k, ch)) == value(string("AAA"))); + assert(hasContent(put(k, string("aaa"), ch))); + assert((get(k, ch)) == value(string("aaa"))); + assert(hasContent(del(k, ch))); + assert(!hasContent(get(k, ch))); + + return true; +} + +struct getLoop { + const value k; + MemCached& ch; + getLoop(const value& k, MemCached& ch) : k(k), ch(ch) { + } + const bool operator()() const { + assert((get(k, ch)) == value(string("CCC"))); + return true; + } +}; + +bool testGetPerf() { + const value k = mklist("c"); + MemCached ch("127.0.0.1", 11211); + assert(hasContent(post(k, string("CCC"), ch))); + + const lambda gl = getLoop(k, ch); + cout << "Memcached get test " << time(gl, 5, 200) << " ms" << endl; + return true; +} + +} +} + +int main() { + tuscany::cout << "Testing..." << tuscany::endl; + + tuscany::cache::testMemCached(); + tuscany::cache::testGetPerf(); + + tuscany::cout << "OK" << tuscany::endl; + + return 0; +} diff --git a/sca-cpp/trunk/components/cache/memcache.composite b/sca-cpp/trunk/components/cache/memcache.composite new file mode 100644 index 0000000000..c9a147960e --- /dev/null +++ b/sca-cpp/trunk/components/cache/memcache.composite @@ -0,0 +1,32 @@ + + + + + + + + + + + + diff --git a/sca-cpp/trunk/components/cache/memcache.cpp b/sca-cpp/trunk/components/cache/memcache.cpp new file mode 100644 index 0000000000..304975614e --- /dev/null +++ b/sca-cpp/trunk/components/cache/memcache.cpp @@ -0,0 +1,133 @@ +/* + * 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. + */ + +/* $Rev$ $Date$ */ + +/** + * Memcached-based cache component implementation. + */ + +#include + +#include "string.hpp" + +#include "function.hpp" +#include "list.hpp" +#include "value.hpp" +#include "monad.hpp" +#include "memcache.hpp" + +namespace tuscany { +namespace memcache { + +/** + * Get an item from the cache. + */ +const failable get(const list& params, cache::MemCached& ch) { + return cache::get(car(params), ch); +} + +/** + * Post an item to the cache. + */ +const value uuidValue() { + apr_uuid_t uuid; + apr_uuid_get(&uuid); + char buf[APR_UUID_FORMATTED_LENGTH]; + apr_uuid_format(buf, &uuid); + return value(string(buf, APR_UUID_FORMATTED_LENGTH)); +} + +const failable post(const list& params, cache::MemCached& ch) { + const value id = append(car(params), mklist(uuidValue())); + const failable val = cache::post(id, cadr(params), ch); + if (!hasContent(val)) + return mkfailure(reason(val)); + return id; +} + +/** + * Put an item into the cache. + */ +const failable put(const list& params, cache::MemCached& ch) { + const failable val = cache::put(car(params), cadr(params), ch); + if (!hasContent(val)) + return mkfailure(reason(val)); + return value(content(val)); +} + +/** + * Delete an item from the cache. + */ +const failable del(const list& params, cache::MemCached& ch) { + const failable val = cache::del(car(params), ch); + if (!hasContent(val)) + return mkfailure(reason(val)); + return value(content(val)); +} + +/** + * Component implementation lambda function. + */ +class applyCache { +public: + applyCache(cache::MemCached& ch) : ch(ch) { + } + + const value operator()(const list& params) const { + const value func(car(params)); + if (func == "get") + return get(cdr(params), ch); + if (func == "post") + return post(cdr(params), ch); + if (func == "put") + return put(cdr(params), ch); + if (func == "delete") + return del(cdr(params), ch); + return tuscany::mkfailure(); + } + +private: + cache::MemCached& ch; +}; + +/** + * Start the component. + */ +const failable start(unused const list& params) { + // Connect to memcached + cache::MemCached& ch = *(new (gc_new()) cache::MemCached("127.0.0.1", 11211)); + + // Return the component implementation lambda function + return value(lambda&)>(applyCache(ch))); +} + +} +} + +extern "C" { + +const tuscany::value apply(const tuscany::list& params) { + const tuscany::value func(car(params)); + if (func == "start") + return tuscany::memcache::start(cdr(params)); + return tuscany::mkfailure(); +} + +} diff --git a/sca-cpp/trunk/components/cache/memcache.hpp b/sca-cpp/trunk/components/cache/memcache.hpp new file mode 100644 index 0000000000..c630f6fd55 --- /dev/null +++ b/sca-cpp/trunk/components/cache/memcache.hpp @@ -0,0 +1,175 @@ +/* + * 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. + */ + +/* $Rev$ $Date$ */ + +#ifndef tuscany_memcache_hpp +#define tuscany_memcache_hpp + +/** + * Memcached access functions. + */ + +#include "apr.h" +#include "apu.h" +#include "apr_general.h" +#include "apr_strings.h" +#include "apr_hash.h" +#include "apr_memcache.h" +#include "apr_network_io.h" + +#include "string.hpp" +#include "list.hpp" +#include "value.hpp" +#include "monad.hpp" +#include "../../modules/scheme/eval.hpp" + +namespace tuscany { +namespace cache { + +/** + * Represents a memcached context. + */ +class MemCached { +public: + MemCached() : owner(false) { + } + + MemCached(const string host, const int port) : owner(true) { + apr_pool_create(&pool, NULL); + apr_memcache_create(pool, 1, 0, &mc); + init(host, port); + } + + MemCached(const MemCached& c) : owner(false) { + pool = c.pool; + mc = c.mc; + } + + ~MemCached() { + if (!owner) + return; + apr_pool_destroy(pool); + } + +private: + bool owner; + apr_pool_t* pool; + apr_memcache_t* mc; + + friend const failable post(const value& key, const value& val, const MemCached& cache); + friend const failable put(const value& key, const value& val, const MemCached& cache); + friend const failable get(const value& key, const MemCached& cache); + friend const failable del(const value& key, const MemCached& cache); + + /** + * Initialize the memcached context. + */ + const failable init(const string& host, const int port) { + apr_memcache_server_t *server; + const apr_status_t sc = apr_memcache_server_create(pool, c_str(host), (apr_port_t)port, 0, 1, 1, 60, &server); + if (sc != APR_SUCCESS) + return mkfailure("Could not create server"); + const apr_status_t as = apr_memcache_add_server(mc, server); + if (as != APR_SUCCESS) + return mkfailure("Could not add server"); + return true; + } +}; + +/** + * Post a new item to the cache. + */ +const failable post(const value& key, const value& val, const MemCached& cache) { + debug(key, "cache::post::key"); + debug(val, "cache::post::value"); + + const string ks(scheme::writeValue(key)); + const string vs(scheme::writeValue(val)); + const apr_status_t rc = apr_memcache_add(cache.mc, c_str(ks), const_cast(c_str(vs)), length(vs), 0, 27); + if (rc != APR_SUCCESS) + return mkfailure("Could not add entry"); + + debug(true, "cache::post::result"); + return true; +} + +/** + * Update an item in the cache. If the item doesn't exist it is added. + */ +const failable put(const value& key, const value& val, const MemCached& cache) { + debug(key, "cache::put::key"); + debug(val, "cache::put::value"); + + const string ks(scheme::writeValue(key)); + const string vs(scheme::writeValue(val)); + const apr_status_t rc = apr_memcache_set(cache.mc, c_str(ks), const_cast(c_str(vs)), length(vs), 0, 27); + if (rc != APR_SUCCESS) + return mkfailure("Could not set entry"); + + debug(true, "cache::put::result"); + return true; +} + +/** + * Get an item from the cache. + */ +const failable get(const value& key, const MemCached& cache) { + debug(key, "cache::get::key"); + + const string ks(scheme::writeValue(key)); + apr_pool_t* vpool; + const apr_status_t pc = apr_pool_create(&vpool, cache.pool); + if (pc != APR_SUCCESS) + return mkfailure("Could not allocate memory"); + + char *data; + apr_size_t size; + const apr_status_t rc = apr_memcache_getp(cache.mc, cache.pool, c_str(ks), &data, &size, NULL); + if (rc != APR_SUCCESS) { + apr_pool_destroy(vpool); + return mkfailure("Could not get entry"); + } + + const value val(scheme::readValue(string(data, size))); + apr_pool_destroy(vpool); + + debug(val, "cache::get::result"); + return val; +} + +/** + * Delete an item from the cache + */ +const failable del(const value& key, const MemCached& cache) { + debug(key, "cache::delete::key"); + + const string ks(scheme::writeValue(key)); + const apr_status_t rc = apr_memcache_delete(cache.mc, c_str(ks), 0); + if (rc != APR_SUCCESS) + return mkfailure("Could not delete entry"); + + debug(true, "cache::delete::result"); + return true; +} + +} +} + +#endif /* tuscany_memcache_hpp */ diff --git a/sca-cpp/trunk/components/cache/memcached-stop b/sca-cpp/trunk/components/cache/memcached-stop index b999228b46..bd2c208f46 100755 --- a/sca-cpp/trunk/components/cache/memcached-stop +++ b/sca-cpp/trunk/components/cache/memcached-stop @@ -20,4 +20,4 @@ # Stop memcached mc="memcached -l 127.0.0.1 -m 4 -p 11211" -kill `ps -f | grep -v grep | grep "${mc}" | awk '{ print $2 }'` +kill `ps -ef | grep -v grep | grep "${mc}" | awk '{ print $2 }'` diff --git a/sca-cpp/trunk/components/cache/memcached-test b/sca-cpp/trunk/components/cache/memcached-test index d4b9c04eda..842e8d2030 100755 --- a/sca-cpp/trunk/components/cache/memcached-test +++ b/sca-cpp/trunk/components/cache/memcached-test @@ -22,7 +22,7 @@ sleep 1 # Test -./mcache-test 2>/dev/null +./memcache-test 2>/dev/null rc=$? # Cleanup diff --git a/sca-cpp/trunk/components/cache/server-test b/sca-cpp/trunk/components/cache/server-test index 4942f547bc..a77736f33c 100755 --- a/sca-cpp/trunk/components/cache/server-test +++ b/sca-cpp/trunk/components/cache/server-test @@ -23,7 +23,7 @@ ../../modules/server/scheme-conf tmp cat >>tmp/conf/httpd.conf < +#include #include "list.hpp" #include "value.hpp" @@ -46,6 +47,7 @@ namespace java { * Represent a Java VM runtime. */ jobject JNICALL nativeInvoke(JNIEnv *env, jobject self, jobject proxy, jobject method, jobjectArray args); +jobject JNICALL nativeUUID(JNIEnv *env); class JavaRuntime { public: @@ -133,13 +135,21 @@ public: iterableIsNil = env->GetStaticMethodID(iterableUtilClass, "isNil", "(Ljava/lang/Object;)Z"); iterableCar = env->GetStaticMethodID(iterableUtilClass, "car", "(Ljava/lang/Object;)Ljava/lang/Object;"); iterableCdr = env->GetStaticMethodID(iterableUtilClass, "cdr", "(Ljava/lang/Object;)Ljava/lang/Iterable;"); + uuidClass = env->FindClass("org/apache/tuscany/UUIDUtil"); // Register our native invocation handler function - JNINativeMethod nm; - nm.name = const_cast("invoke"); - nm.signature = const_cast("(Ljava/lang/Object;Ljava/lang/reflect/Method;[Ljava/lang/Object;)Ljava/lang/Object;"); - nm.fnPtr = (void*)nativeInvoke; - env->RegisterNatives(invokerClass, &nm, 1); + JNINativeMethod invokenm; + invokenm.name = const_cast("invoke"); + invokenm.signature = const_cast("(Ljava/lang/Object;Ljava/lang/reflect/Method;[Ljava/lang/Object;)Ljava/lang/Object;"); + invokenm.fnPtr = (void*)nativeInvoke; + env->RegisterNatives(invokerClass, &invokenm, 1); + + // Register our native UUID function + JNINativeMethod uuidnm; + uuidnm.name = const_cast("uuid"); + uuidnm.signature = const_cast("()Ljava/lang/String;"); + uuidnm.fnPtr = (void*)nativeUUID; + env->RegisterNatives(uuidClass, &uuidnm, 1); } JavaVM* jvm; @@ -173,6 +183,7 @@ public: jmethodID iterableCar; jmethodID iterableCdr; jmethodID iterableIsNil; + jclass uuidClass; }; /** @@ -247,8 +258,8 @@ public: }; /** - * Invocation handler invoke method, dispatches to the lambda function wrapped - * in the invocation handler. + * Native implementation of the InvocationHandler.invoke Java method. + * Dispatches the call to the lambda function wrapped in the invocation handler. */ jobject JNICALL nativeInvoke(JNIEnv* env, jobject self, unused jobject proxy, jobject method, jobjectArray args) { @@ -276,6 +287,19 @@ jobject JNICALL nativeInvoke(JNIEnv* env, jobject self, unused jobject proxy, jo return valueToJobject(jl.jr, value(), result); } +/** + * Native implementation of IterableUtil.uuid. We are providing a native implementation + * of this function as java.util.UUID seems to behave differently with different JDKs. + */ +jobject JNICALL nativeUUID(JNIEnv* env) { + apr_uuid_t uuid; + apr_uuid_get(&uuid); + char buf[APR_UUID_FORMATTED_LENGTH]; + apr_uuid_format(buf, &uuid); + string s(buf, APR_UUID_FORMATTED_LENGTH); + return env->NewStringUTF(c_str(s)); +} + /** * Convert a lambda function to Java proxy. */ diff --git a/sca-cpp/trunk/modules/java/org/apache/tuscany/UUIDUtil.java b/sca-cpp/trunk/modules/java/org/apache/tuscany/UUIDUtil.java new file mode 100644 index 0000000000..60076c62ca --- /dev/null +++ b/sca-cpp/trunk/modules/java/org/apache/tuscany/UUIDUtil.java @@ -0,0 +1,32 @@ +/* + * 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 org.apache.tuscany; + +/** + * A fast and portable UUID generator function. + */ +public class UUIDUtil { + + /** + * Return a UUID. + */ + public static native String uuid(); + +} diff --git a/sca-cpp/trunk/modules/wsgi/wsgi-stop b/sca-cpp/trunk/modules/wsgi/wsgi-stop index fa6f42d643..d1e09185f4 100755 --- a/sca-cpp/trunk/modules/wsgi/wsgi-stop +++ b/sca-cpp/trunk/modules/wsgi/wsgi-stop @@ -24,4 +24,4 @@ port=$1 python_prefix=`cat $here/../python/python.prefix` py="$python_prefix/bin/python composite.py $port" -kill `ps -f | grep -v grep | grep "${py}" | awk '{ print $2 }'` +kill `ps -ef | grep -v grep | grep "${py}" | awk '{ print $2 }'` diff --git a/sca-cpp/trunk/test/store-cpp/store.composite b/sca-cpp/trunk/test/store-cpp/store.composite index 3839a8b816..056f7b4c68 100644 --- a/sca-cpp/trunk/test/store-cpp/store.composite +++ b/sca-cpp/trunk/test/store-cpp/store.composite @@ -50,7 +50,7 @@ - + diff --git a/sca-cpp/trunk/test/store-java/store.composite b/sca-cpp/trunk/test/store-java/store.composite index 5024d79ee6..8d733db134 100644 --- a/sca-cpp/trunk/test/store-java/store.composite +++ b/sca-cpp/trunk/test/store-java/store.composite @@ -50,7 +50,7 @@ - + diff --git a/sca-cpp/trunk/test/store-java/store/ShoppingCartImpl.java b/sca-cpp/trunk/test/store-java/store/ShoppingCartImpl.java index 6620cbbbb0..878e0cff49 100644 --- a/sca-cpp/trunk/test/store-java/store/ShoppingCartImpl.java +++ b/sca-cpp/trunk/test/store-java/store/ShoppingCartImpl.java @@ -14,14 +14,13 @@ * "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. + * under the License. */ package store; import static org.apache.tuscany.IterableUtil.*; - -import java.util.UUID; +import static org.apache.tuscany.UUIDUtil.*; import org.apache.tuscany.Service; @@ -43,18 +42,11 @@ public class ShoppingCartImpl { return cart; } - /** - * Returns a UUID. - */ - String uuid() { - return UUID.randomUUID().toString(); - } - /** * Post a new item to the cart. Create a new cart if necessary. */ public Iterable post(final Iterable collection, final Iterable item, final Service cache) { - final String id = this.uuid(); + final String id = uuid(); final Iterable newItem = list(car(item), id, caddr(item)); final Iterable cart = cons(newItem, this.getcart(cartId, cache)); final Iterable iid = list(cartId); @@ -65,7 +57,7 @@ public class ShoppingCartImpl { /** * Find an item in the cart. */ - public Iterable find(final String id, final Iterable cart) { + Iterable find(final String id, final Iterable cart) { if(isNil(cart)) return cons("Item", list("0", list())); if(id.equals(cadr(car(cart)))) diff --git a/sca-cpp/trunk/test/store-python/store.composite b/sca-cpp/trunk/test/store-python/store.composite index 8dc9950204..889f514624 100644 --- a/sca-cpp/trunk/test/store-python/store.composite +++ b/sca-cpp/trunk/test/store-python/store.composite @@ -60,7 +60,7 @@ - + diff --git a/sca-cpp/trunk/test/store-scheme/store.composite b/sca-cpp/trunk/test/store-scheme/store.composite index 94e066acad..36b155b595 100644 --- a/sca-cpp/trunk/test/store-scheme/store.composite +++ b/sca-cpp/trunk/test/store-scheme/store.composite @@ -60,7 +60,7 @@ - + -- cgit v1.2.3