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
This commit is contained in:
jsdelfino 2010-03-10 09:34:20 +00:00
commit 5a390791d3
19 changed files with 96 additions and 48 deletions

View file

@ -18,14 +18,14 @@
comp_SCRIPTS = memcached-start memcached-stop comp_SCRIPTS = memcached-start memcached-stop
compdir=$(prefix)/components/cache compdir=$(prefix)/components/cache
comp_LTLIBRARIES = libmcache.la comp_LTLIBRARIES = libmemcache.la
libmcache_la_SOURCES = mcache.cpp libmemcache_la_SOURCES = memcache.cpp
mcache_test_SOURCES = mcache-test.cpp memcache_test_SOURCES = memcache-test.cpp
mcache_test_LDFLAGS = -lxml2 memcache_test_LDFLAGS = -lxml2
client_test_SOURCES = client-test.cpp client_test_SOURCES = client-test.cpp
client_test_LDFLAGS = -lxml2 -lcurl -lmozjs client_test_LDFLAGS = -lxml2 -lcurl -lmozjs
noinst_PROGRAMS = mcache-test client-test noinst_PROGRAMS = memcache-test client-test
TESTS = memcached-test server-test TESTS = memcached-test server-test

View file

@ -36,7 +36,7 @@
namespace tuscany { namespace tuscany {
namespace cache { namespace cache {
const string uri("http://localhost:8090/mcache"); const string uri("http://localhost:8090/memcache");
bool testCache() { bool testCache() {
http::CURLSession cs; http::CURLSession cs;

View file

@ -27,7 +27,7 @@
#include "stream.hpp" #include "stream.hpp"
#include "string.hpp" #include "string.hpp"
#include "perf.hpp" #include "perf.hpp"
#include "mcache.hpp" #include "memcache.hpp"
namespace tuscany { namespace tuscany {
namespace cache { namespace cache {

View file

@ -20,12 +20,12 @@
<composite xmlns="http://docs.oasis-open.org/ns/opencsa/sca/200912" <composite xmlns="http://docs.oasis-open.org/ns/opencsa/sca/200912"
xmlns:t="http://tuscany.apache.org/xmlns/sca/1.1" xmlns:t="http://tuscany.apache.org/xmlns/sca/1.1"
targetNamespace="http://tuscany.apache.org/xmlns/sca/components" targetNamespace="http://tuscany.apache.org/xmlns/sca/components"
name="mcache"> name="memcache">
<component name="mcache"> <component name="memcache">
<implementation.cpp path=".libs" library="libmcache"/> <implementation.cpp path=".libs" library="libmemcache"/>
<service name="mcache"> <service name="memcache">
<t:binding.http uri="mcache"/> <t:binding.http uri="memcache"/>
</service> </service>
</component> </component>

View file

@ -31,10 +31,10 @@
#include "list.hpp" #include "list.hpp"
#include "value.hpp" #include "value.hpp"
#include "monad.hpp" #include "monad.hpp"
#include "mcache.hpp" #include "memcache.hpp"
namespace tuscany { namespace tuscany {
namespace mcache { namespace memcache {
/** /**
* Get an item from the cache. * Get an item from the cache.
@ -126,7 +126,7 @@ extern "C" {
const tuscany::value apply(const tuscany::list<tuscany::value>& params) { const tuscany::value apply(const tuscany::list<tuscany::value>& params) {
const tuscany::value func(car(params)); const tuscany::value func(car(params));
if (func == "start") if (func == "start")
return tuscany::mcache::start(cdr(params)); return tuscany::memcache::start(cdr(params));
return tuscany::mkfailure<tuscany::value>(); return tuscany::mkfailure<tuscany::value>();
} }

View file

@ -19,8 +19,8 @@
/* $Rev$ $Date$ */ /* $Rev$ $Date$ */
#ifndef tuscany_mcache_hpp #ifndef tuscany_memcache_hpp
#define tuscany_mcache_hpp #define tuscany_memcache_hpp
/** /**
* Memcached access functions. * Memcached access functions.
@ -121,7 +121,7 @@ const failable<bool> put(const value& key, const value& val, const MemCached& ca
const string vs(scheme::writeValue(val)); const string vs(scheme::writeValue(val));
const apr_status_t rc = apr_memcache_set(cache.mc, c_str(ks), const_cast<char*>(c_str(vs)), length(vs), 0, 27); const apr_status_t rc = apr_memcache_set(cache.mc, c_str(ks), const_cast<char*>(c_str(vs)), length(vs), 0, 27);
if (rc != APR_SUCCESS) if (rc != APR_SUCCESS)
return mkfailure<bool>("Could not add entry"); return mkfailure<bool>("Could not set entry");
debug(true, "cache::put::result"); debug(true, "cache::put::result");
return true; return true;
@ -172,4 +172,4 @@ const failable<bool> del(const value& key, const MemCached& cache) {
} }
} }
#endif /* tuscany_mcache_hpp */ #endif /* tuscany_memcache_hpp */

View file

@ -20,4 +20,4 @@
# Stop memcached # Stop memcached
mc="memcached -l 127.0.0.1 -m 4 -p 11211" 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 }'`

View file

@ -22,7 +22,7 @@
sleep 1 sleep 1
# Test # Test
./mcache-test 2>/dev/null ./memcache-test 2>/dev/null
rc=$? rc=$?
# Cleanup # Cleanup

View file

@ -23,7 +23,7 @@
../../modules/server/scheme-conf tmp ../../modules/server/scheme-conf tmp
cat >>tmp/conf/httpd.conf <<EOF cat >>tmp/conf/httpd.conf <<EOF
SCAContribution `pwd`/ SCAContribution `pwd`/
SCAComposite mcache.composite SCAComposite memcache.composite
EOF EOF
./memcached-start ./memcached-start

View file

@ -23,4 +23,4 @@ here=`readlink -f $0`; here=`dirname $here`
qpid_prefix=`cat $here/qpidc.prefix` qpid_prefix=`cat $here/qpidc.prefix`
qpidd="$qpid_prefix/sbin/qpidd" qpidd="$qpid_prefix/sbin/qpidd"
kill `ps -f | grep -v grep | grep "${qpidd}" | awk '{ print $2 }'` kill `ps -ef | grep -v grep | grep "${qpidd}" | awk '{ print $2 }'`

View file

@ -80,7 +80,7 @@ eval-shell
json-test json-test
cache-test cache-test
client-test client-test
mcache-test memcache-test
curl-test curl-test
scdl-test scdl-test
jni-test jni-test

View file

@ -26,6 +26,7 @@
* Java component implementation evaluation logic. * Java component implementation evaluation logic.
*/ */
#include <jni.h> #include <jni.h>
#include <apr_uuid.h>
#include "list.hpp" #include "list.hpp"
#include "value.hpp" #include "value.hpp"
@ -46,6 +47,7 @@ namespace java {
* Represent a Java VM runtime. * Represent a Java VM runtime.
*/ */
jobject JNICALL nativeInvoke(JNIEnv *env, jobject self, jobject proxy, jobject method, jobjectArray args); jobject JNICALL nativeInvoke(JNIEnv *env, jobject self, jobject proxy, jobject method, jobjectArray args);
jobject JNICALL nativeUUID(JNIEnv *env);
class JavaRuntime { class JavaRuntime {
public: public:
@ -133,13 +135,21 @@ public:
iterableIsNil = env->GetStaticMethodID(iterableUtilClass, "isNil", "(Ljava/lang/Object;)Z"); iterableIsNil = env->GetStaticMethodID(iterableUtilClass, "isNil", "(Ljava/lang/Object;)Z");
iterableCar = env->GetStaticMethodID(iterableUtilClass, "car", "(Ljava/lang/Object;)Ljava/lang/Object;"); iterableCar = env->GetStaticMethodID(iterableUtilClass, "car", "(Ljava/lang/Object;)Ljava/lang/Object;");
iterableCdr = env->GetStaticMethodID(iterableUtilClass, "cdr", "(Ljava/lang/Object;)Ljava/lang/Iterable;"); iterableCdr = env->GetStaticMethodID(iterableUtilClass, "cdr", "(Ljava/lang/Object;)Ljava/lang/Iterable;");
uuidClass = env->FindClass("org/apache/tuscany/UUIDUtil");
// Register our native invocation handler function // Register our native invocation handler function
JNINativeMethod nm; JNINativeMethod invokenm;
nm.name = const_cast<char*>("invoke"); invokenm.name = const_cast<char*>("invoke");
nm.signature = const_cast<char*>("(Ljava/lang/Object;Ljava/lang/reflect/Method;[Ljava/lang/Object;)Ljava/lang/Object;"); invokenm.signature = const_cast<char*>("(Ljava/lang/Object;Ljava/lang/reflect/Method;[Ljava/lang/Object;)Ljava/lang/Object;");
nm.fnPtr = (void*)nativeInvoke; invokenm.fnPtr = (void*)nativeInvoke;
env->RegisterNatives(invokerClass, &nm, 1); env->RegisterNatives(invokerClass, &invokenm, 1);
// Register our native UUID function
JNINativeMethod uuidnm;
uuidnm.name = const_cast<char*>("uuid");
uuidnm.signature = const_cast<char*>("()Ljava/lang/String;");
uuidnm.fnPtr = (void*)nativeUUID;
env->RegisterNatives(uuidClass, &uuidnm, 1);
} }
JavaVM* jvm; JavaVM* jvm;
@ -173,6 +183,7 @@ public:
jmethodID iterableCar; jmethodID iterableCar;
jmethodID iterableCdr; jmethodID iterableCdr;
jmethodID iterableIsNil; jmethodID iterableIsNil;
jclass uuidClass;
}; };
/** /**
@ -247,8 +258,8 @@ public:
}; };
/** /**
* Invocation handler invoke method, dispatches to the lambda function wrapped * Native implementation of the InvocationHandler.invoke Java method.
* in the invocation handler. * 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) { 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); 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. * Convert a lambda function to Java proxy.
*/ */

View file

@ -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();
}

View file

@ -24,4 +24,4 @@ port=$1
python_prefix=`cat $here/../python/python.prefix` python_prefix=`cat $here/../python/python.prefix`
py="$python_prefix/bin/python composite.py $port" 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 }'`

View file

@ -50,7 +50,7 @@
</component> </component>
<component name="Cache"> <component name="Cache">
<implementation.cpp path="../../components/cache/.libs" library="libmcache"/> <implementation.cpp path="../../components/cache/.libs" library="libmemcache"/>
<service name="Cache"> <service name="Cache">
<t:binding.atom uri="cache"/> <t:binding.atom uri="cache"/>
</service> </service>

View file

@ -50,7 +50,7 @@
</component> </component>
<component name="Cache"> <component name="Cache">
<implementation.cpp path="../../components/cache/.libs" library="libmcache"/> <implementation.cpp path="../../components/cache/.libs" library="libmemcache"/>
<service name="Cache"> <service name="Cache">
<t:binding.atom uri="cache"/> <t:binding.atom uri="cache"/>
</service> </service>

View file

@ -14,14 +14,13 @@
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the * KIND, either express or implied. See the License for the
* specific language governing permissions and limitations * specific language governing permissions and limitations
* under the License. * under the License.
*/ */
package store; package store;
import static org.apache.tuscany.IterableUtil.*; import static org.apache.tuscany.IterableUtil.*;
import static org.apache.tuscany.UUIDUtil.*;
import java.util.UUID;
import org.apache.tuscany.Service; import org.apache.tuscany.Service;
@ -43,18 +42,11 @@ public class ShoppingCartImpl {
return cart; return cart;
} }
/**
* Returns a UUID.
*/
String uuid() {
return UUID.randomUUID().toString();
}
/** /**
* Post a new item to the cart. Create a new cart if necessary. * Post a new item to the cart. Create a new cart if necessary.
*/ */
public Iterable<String> post(final Iterable<String> collection, final Iterable<?> item, final Service cache) { public Iterable<String> post(final Iterable<String> 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<?> newItem = list(car(item), id, caddr(item));
final Iterable<?> cart = cons(newItem, this.getcart(cartId, cache)); final Iterable<?> cart = cons(newItem, this.getcart(cartId, cache));
final Iterable<String> iid = list(cartId); final Iterable<String> iid = list(cartId);
@ -65,7 +57,7 @@ public class ShoppingCartImpl {
/** /**
* Find an item in the cart. * 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)) if(isNil(cart))
return cons("Item", list("0", list())); return cons("Item", list("0", list()));
if(id.equals(cadr(car(cart)))) if(id.equals(cadr(car(cart))))

View file

@ -60,7 +60,7 @@
</component> </component>
<component name="Cache"> <component name="Cache">
<implementation.cpp path="../../components/cache/.libs" library="libmcache"/> <implementation.cpp path="../../components/cache/.libs" library="libmemcache"/>
<service name="Cache"> <service name="Cache">
<t:binding.atom uri="cache"/> <t:binding.atom uri="cache"/>
</service> </service>

View file

@ -60,7 +60,7 @@
</component> </component>
<component name="Cache"> <component name="Cache">
<implementation.cpp path="../../components/cache/.libs" library="libmcache"/> <implementation.cpp path="../../components/cache/.libs" library="libmemcache"/>
<service name="Cache"> <service name="Cache">
<t:binding.atom uri="cache"/> <t:binding.atom uri="cache"/>
</service> </service>