From bb4b895471e3165c71bdfd1fdae5e1ffde8f1696 Mon Sep 17 00:00:00 2001 From: jsdelfino Date: Sun, 7 Feb 2010 00:36:25 +0000 Subject: Moved server configuration to HTTPD postConfig phase, to avoid running configuration commands twice and added a way for runtime modules and components to handle start/restart/stop events. Improved build scripts a little, to not depend on external environment variables. git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@907352 13f79535-47bb-0310-9956-ffa450edef68 --- sca-cpp/trunk/modules/http/Makefile.am | 6 ++ sca-cpp/trunk/modules/http/http-test | 4 +- sca-cpp/trunk/modules/http/httpd-conf | 1 + sca-cpp/trunk/modules/http/httpd-restart | 25 +++++ sca-cpp/trunk/modules/http/httpd-start | 25 +++++ sca-cpp/trunk/modules/http/httpd-stop | 25 +++++ sca-cpp/trunk/modules/http/httpd-test | 4 +- sca-cpp/trunk/modules/http/httpd.hpp | 34 ++++--- sca-cpp/trunk/modules/java/Makefile.am | 9 +- sca-cpp/trunk/modules/java/driver.hpp | 1 + sca-cpp/trunk/modules/java/eval.hpp | 13 ++- sca-cpp/trunk/modules/java/java-test.cpp | 4 + sca-cpp/trunk/modules/java/mod-java.cpp | 29 +++++- sca-cpp/trunk/modules/java/mod-java.hpp | 20 ++-- sca-cpp/trunk/modules/java/server-test | 7 +- sca-cpp/trunk/modules/java/wiring-test | 7 +- sca-cpp/trunk/modules/python/driver.hpp | 1 + sca-cpp/trunk/modules/python/eval.hpp | 24 ++++- sca-cpp/trunk/modules/python/mod-python.cpp | 29 +++++- sca-cpp/trunk/modules/python/mod-python.hpp | 4 +- sca-cpp/trunk/modules/python/python-test.cpp | 2 + sca-cpp/trunk/modules/python/server-test | 7 +- sca-cpp/trunk/modules/python/wiring-test | 7 +- sca-cpp/trunk/modules/scheme/primitive.hpp | 22 ++++- sca-cpp/trunk/modules/server/httpd-test | 7 +- sca-cpp/trunk/modules/server/impl-test.cpp | 2 +- sca-cpp/trunk/modules/server/mod-cpp.hpp | 29 +++++- sca-cpp/trunk/modules/server/mod-eval.cpp | 27 ++++- sca-cpp/trunk/modules/server/mod-eval.hpp | 141 ++++++++++++++++++--------- sca-cpp/trunk/modules/server/mod-scheme.hpp | 4 +- sca-cpp/trunk/modules/server/mod-wiring.cpp | 86 ++++++++-------- sca-cpp/trunk/modules/server/server-test | 7 +- sca-cpp/trunk/modules/server/wiring-test | 7 +- 33 files changed, 449 insertions(+), 171 deletions(-) create mode 100755 sca-cpp/trunk/modules/http/httpd-restart create mode 100755 sca-cpp/trunk/modules/http/httpd-start create mode 100755 sca-cpp/trunk/modules/http/httpd-stop (limited to 'sca-cpp/trunk/modules') diff --git a/sca-cpp/trunk/modules/http/Makefile.am b/sca-cpp/trunk/modules/http/Makefile.am index 0e1364747c..b522113423 100644 --- a/sca-cpp/trunk/modules/http/Makefile.am +++ b/sca-cpp/trunk/modules/http/Makefile.am @@ -19,7 +19,13 @@ noinst_PROGRAMS = curl-test INCLUDES = -I${HTTPD_INCLUDE} +httpdir=$(prefix)/modules/http +http_DATA = httpd.prefix + curl_test_SOURCES = curl-test.cpp curl_test_LDFLAGS = -lxml2 -lcurl -lmozjs +httpd.prefix: $(top_builddir)/config.status + echo ${HTTPD_PREFIX} >httpd.prefix + TESTS = httpd-test http-test diff --git a/sca-cpp/trunk/modules/http/http-test b/sca-cpp/trunk/modules/http/http-test index bf6230aec8..9f65b37eec 100755 --- a/sca-cpp/trunk/modules/http/http-test +++ b/sca-cpp/trunk/modules/http/http-test @@ -19,7 +19,7 @@ # Setup ./httpd-conf tmp 8090 htdocs -apachectl -k start -d `pwd`/tmp +./httpd-start tmp sleep 2 # Test @@ -27,6 +27,6 @@ sleep 2 rc=$? # Cleanup -apachectl -k stop -d `pwd`/tmp +./httpd-stop tmp sleep 2 return $rc diff --git a/sca-cpp/trunk/modules/http/httpd-conf b/sca-cpp/trunk/modules/http/httpd-conf index 686bfbcec7..434040803d 100755 --- a/sca-cpp/trunk/modules/http/httpd-conf +++ b/sca-cpp/trunk/modules/http/httpd-conf @@ -27,6 +27,7 @@ mkdir -p $root mkdir -p $root/logs mkdir -p $root/conf cat >$root/conf/httpd.conf < const C& serverConf(const request_rec* r, const module* mod return *(C*)ap_get_module_config(r->server->module_config, mod); } -template C& serverConf(const cmd_parms *cmd, const module* mod) { - return *(C*)ap_get_module_config(cmd->server->module_config, mod); +template C& serverConf(const server_rec* s, const module* mod) { + return *(C*)ap_get_module_config(s->module_config, mod); } - -/** - * Returns a directory-scoped module configuration. - */ -template void *makeDirConf(apr_pool_t *p, char *dirspec) { - return new (gc_new(p)) C(dirspec); +template C& serverConf(const cmd_parms *cmd, const module* mod) { + return *(C*)ap_get_module_config(cmd->server->module_config, mod); } -template C& dirConf(const request_rec* r, const module* mod) { - return *(C*)ap_get_module_config(r->per_dir_config, mod); -} /** * Convert a path string to a list of values. @@ -404,6 +397,23 @@ const int internalRedirect(const string& uri, request_rec* r) { return httpd::internalRedirect(content(nr)); } +/** + * Put a value in the process user data. + */ +const bool putUserData(const string& k, const int v, const server_rec* s) { + apr_pool_userdata_set((const void *)v, c_str(k), apr_pool_cleanup_null, s->process->pool); + return true; +} + +/** + * Return a user data value. + */ +const int userData(const string& k, const server_rec* s) { + void* v = (int)0; + apr_pool_userdata_get(&v, c_str(k), s->process->pool); + return (int)v; +} + } } diff --git a/sca-cpp/trunk/modules/java/Makefile.am b/sca-cpp/trunk/modules/java/Makefile.am index 5ef145c6e3..5402ab495e 100644 --- a/sca-cpp/trunk/modules/java/Makefile.am +++ b/sca-cpp/trunk/modules/java/Makefile.am @@ -15,9 +15,8 @@ # specific language governing permissions and limitations # under the License. -datadir=$(prefix)/modules/java JAVAROOT = $(top_builddir)/modules/java -libdir=$(prefix)/lib +libdir = $(prefix)/lib if WANT_JAVA @@ -50,6 +49,12 @@ CLEANFILES = ${jarfile} org/apache/tuscany/*.class test/*.class client_test_SOURCES = client-test.cpp client_test_LDFLAGS = -lxml2 -lcurl -lmozjs +prefixdir = $(top_builddir)/modules/java +prefix_DATA = java.prefix + +java.prefix: $(top_builddir)/config.status + echo ${JAVA_PREFIX} >java.prefix + TESTS = java-test server-test endif diff --git a/sca-cpp/trunk/modules/java/driver.hpp b/sca-cpp/trunk/modules/java/driver.hpp index 5f117b8e68..ddfc057940 100644 --- a/sca-cpp/trunk/modules/java/driver.hpp +++ b/sca-cpp/trunk/modules/java/driver.hpp @@ -48,6 +48,7 @@ const value evalDriverLoop(const JavaRuntime& jr, const JavaClass jc, istream& i const bool evalDriverRun(const char* name, istream& in, ostream& out) { scheme::setupDisplay(out); + JavaRuntime javaRuntime; const failable jc = readClass(javaRuntime, ".", name); if (!hasContent(jc)) return true; diff --git a/sca-cpp/trunk/modules/java/eval.hpp b/sca-cpp/trunk/modules/java/eval.hpp index 4272543506..e10ec29a0b 100644 --- a/sca-cpp/trunk/modules/java/eval.hpp +++ b/sca-cpp/trunk/modules/java/eval.hpp @@ -161,7 +161,7 @@ public: jmethodID iterableCdr; jmethodID iterableIsNil; -} javaRuntime; +}; /** * Return the last exception that occurred in a JVM. @@ -210,6 +210,8 @@ public: } const value operator()(const list& expr) const { + if (isNil(expr)) + return func(expr); const value& op(car(expr)); if (op == "equals") return value(cadr(expr) == this); @@ -495,8 +497,15 @@ const failable evalClass(const JavaRuntime& jr, const value& expr, const // Lookup the Java function named as the expression operand const list func = assoc(car(expr), jc.m); - if (isNil(func)) + if (isNil(func)) { + + // The start, stop, and restart functions are optional + const value fn = car(expr); + if (fn == "start" || fn == "stop" || fn == "restart") + return value(false); + return mkfailure(string("Couldn't find function: ") + car(expr) + " : " + lastException(jr)); + } const jmethodID fid = (jmethodID)(long)(double)cadr(func); // Convert args to Java jvalues diff --git a/sca-cpp/trunk/modules/java/java-test.cpp b/sca-cpp/trunk/modules/java/java-test.cpp index d050180be8..f811a4f58d 100644 --- a/sca-cpp/trunk/modules/java/java-test.cpp +++ b/sca-cpp/trunk/modules/java/java-test.cpp @@ -33,6 +33,7 @@ namespace java { bool testEvalExpr() { gc_scoped_pool pool; + JavaRuntime javaRuntime; { const failable obj = readClass(javaRuntime, ".", "test.CalcImpl"); assert(hasContent(obj)); @@ -77,6 +78,7 @@ const value add(const list& args) { bool testEvalLambda() { gc_scoped_pool pool; + JavaRuntime javaRuntime; { const failable obj = readClass(javaRuntime, ".", "test.CalcImpl"); assert(hasContent(obj)); @@ -98,6 +100,7 @@ bool testEvalLambda() { bool testClassLoader() { gc_scoped_pool pool; + JavaRuntime javaRuntime; const failable obj = readClass(javaRuntime, ".", "org.apache.tuscany.ClassLoader$Test"); assert(hasContent(obj)); const value exp = mklist("testClassLoader"); @@ -109,6 +112,7 @@ bool testClassLoader() { bool testIterableUtil() { gc_scoped_pool pool; + JavaRuntime javaRuntime; const failable obj = readClass(javaRuntime, ".", "org.apache.tuscany.IterableUtil$Test"); assert(hasContent(obj)); const value exp = mklist("testList"); diff --git a/sca-cpp/trunk/modules/java/mod-java.cpp b/sca-cpp/trunk/modules/java/mod-java.cpp index a5dc48679d..b8ac642c0e 100644 --- a/sca-cpp/trunk/modules/java/mod-java.cpp +++ b/sca-cpp/trunk/modules/java/mod-java.cpp @@ -36,16 +36,39 @@ namespace tuscany { namespace server { namespace modeval { +/** + * Start the module. + */ +const failable start(unused ServerConf& sc) { + // Start a Java runtime + sc.moduleConf = new (gc_new()) java::JavaRuntime(); + return true; +} + +/** + * Stop the module. + */ +const failable stop(unused ServerConf& sc) { + return true; +} + +/** + * Restart the module. + */ +const failable restart(unused ServerConf& sc) { + return true; +} + /** * Evaluate a Java component implementation and convert it to an applicable * lambda function. */ -const failable&)> > evalImplementation(const string& path, const value& impl, const list& px) { +const failable&)> > evalImplementation(const string& path, const value& impl, const list& px, modeval::ServerConf& sc) { const string itype(elementName(impl)); if (contains(itype, ".java")) - return modjava::evalImplementation(path, impl, px); + return modjava::evalImplementation(path, impl, px, sc); if (contains(itype, ".cpp")) - return modcpp::evalImplementation(path, impl, px); + return modcpp::evalImplementation(path, impl, px, sc); return mkfailure&)> >(string("Unsupported implementation type: ") + itype); } diff --git a/sca-cpp/trunk/modules/java/mod-java.hpp b/sca-cpp/trunk/modules/java/mod-java.hpp index a2235fc17f..ccb6d9262b 100644 --- a/sca-cpp/trunk/modules/java/mod-java.hpp +++ b/sca-cpp/trunk/modules/java/mod-java.hpp @@ -34,24 +34,32 @@ #include "value.hpp" #include "monad.hpp" #include "eval.hpp" -#include "../http/httpd.hpp" +#include "../server/mod-eval.hpp" namespace tuscany { namespace server { namespace modjava { +/** + * Return the Java runtime configured in a server. + */ +java::JavaRuntime& javaRuntime(modeval::ServerConf sc) { + return *(java::JavaRuntime*)sc.moduleConf; +} + /** * Apply a Java component implementation function. */ struct applyImplementation { java::JavaClass impl; const list px; - applyImplementation(const java::JavaClass& impl, const list& px) : impl(impl), px(px) { + java::JavaRuntime& jr; + applyImplementation(const java::JavaClass& impl, const list& px, java::JavaRuntime& jr) : impl(impl), px(px), jr(jr) { } const value operator()(const list& params) const { const value expr = append(params, px); debug(expr, "modeval::java::applyImplementation::input"); - const failable val = java::evalClass(java::javaRuntime, expr, impl); + const failable val = java::evalClass(jr, expr, impl); debug(val, "modeval::java::applyImplementation::result"); if (!hasContent(val)) return mklist(value(), reason(val)); @@ -63,12 +71,12 @@ struct applyImplementation { * Evaluate a Java component implementation and convert it to an applicable * lambda function. */ -const failable&)> > evalImplementation(const string& path, const value& impl, const list& px) { +const failable&)> > evalImplementation(const string& path, const value& impl, const list& px, modeval::ServerConf& sc) { const string cn(attributeValue("class", impl)); - const failable jc = java::readClass(java::javaRuntime, path, cn); + const failable jc = java::readClass(javaRuntime(sc), path, cn); if (!hasContent(jc)) return mkfailure&)> >(reason(jc)); - return lambda&)>(applyImplementation(content(jc), px)); + return lambda&)>(applyImplementation(content(jc), px, javaRuntime(sc))); } } diff --git a/sca-cpp/trunk/modules/java/server-test b/sca-cpp/trunk/modules/java/server-test index 45c5efafb2..fbd12e5542 100755 --- a/sca-cpp/trunk/modules/java/server-test +++ b/sca-cpp/trunk/modules/java/server-test @@ -22,16 +22,13 @@ ../server/server-conf tmp ./java-conf tmp cat >>tmp/conf/httpd.conf < SCAContribution `pwd`/ SCAComposite domain-test.composite - EOF export CLASSPATH="`pwd`/libmod-tuscany-java-1.0.jar:`pwd`" -apachectl -k start -d `pwd`/tmp +../http/httpd-start tmp sleep 2 # Test @@ -39,6 +36,6 @@ sleep 2 rc=$? # Cleanup -apachectl -k stop -d `pwd`/tmp +../http/httpd-stop tmp sleep 2 return $rc diff --git a/sca-cpp/trunk/modules/java/wiring-test b/sca-cpp/trunk/modules/java/wiring-test index 7b47787b28..c0b6cbfd62 100755 --- a/sca-cpp/trunk/modules/java/wiring-test +++ b/sca-cpp/trunk/modules/java/wiring-test @@ -24,16 +24,13 @@ echo "Testing..." ../server/server-conf tmp ./java-conf tmp cat >>tmp/conf/httpd.conf < SCAContribution `pwd`/ SCAComposite domain-test.composite - EOF export CLASSPATH="`pwd`/libmod-tuscany-java-1.0.jar:`pwd`" -apachectl -k start -d `pwd`/tmp +../http/httpd-start tmp sleep 2 # Test HTTP GET @@ -73,7 +70,7 @@ if [ "$rc" = "0" ]; then fi # Cleanup -apachectl -k stop -d `pwd`/tmp +../http/httpd-stop tmp sleep 2 if [ "$rc" = "0" ]; then echo "OK" diff --git a/sca-cpp/trunk/modules/python/driver.hpp b/sca-cpp/trunk/modules/python/driver.hpp index a1467c41a8..2820201057 100644 --- a/sca-cpp/trunk/modules/python/driver.hpp +++ b/sca-cpp/trunk/modules/python/driver.hpp @@ -47,6 +47,7 @@ const value evalDriverLoop(PyObject* script, istream& in, ostream& out) { } const bool evalDriverRun(const char* path, istream& in, ostream& out) { + PythonRuntime py; scheme::setupDisplay(out); ifstream is(path); failable script = readScript(path, is); diff --git a/sca-cpp/trunk/modules/python/eval.hpp b/sca-cpp/trunk/modules/python/eval.hpp index 1b9392aed7..09c2cba92d 100644 --- a/sca-cpp/trunk/modules/python/eval.hpp +++ b/sca-cpp/trunk/modules/python/eval.hpp @@ -39,16 +39,23 @@ namespace python { */ class PythonRuntime { public: - PythonRuntime() { - Py_Initialize(); - + PythonRuntime() : owner(true) { + Py_InitializeEx(0); setupIO(); } + PythonRuntime(unused const PythonRuntime& r) : owner(false) { + } + ~PythonRuntime() { + if (!owner) + return; + //Py_Finalize(); } -} pythonRuntime; +private: + const bool owner; +}; /** * Declare conversion functions. @@ -211,8 +218,15 @@ const failable evalScript(const value& expr, PyObject* script) { // Get the requested function PyObject* func = PyObject_GetAttrString(script, c_str(car(expr))); - if (func == NULL) + if (func == NULL) { + + // The start, stop, and restart functions are optional + const value fn = car(expr); + if (fn == "start" || fn == "stop" || fn == "restart") + return value(false); + return mkfailure(string("Couldn't find function: ") + car(expr) + " : " + lastError()); + } if (!PyCallable_Check(func)) { Py_DECREF(func); return mkfailure(string("Couldn't find callable function: ") + car(expr)); diff --git a/sca-cpp/trunk/modules/python/mod-python.cpp b/sca-cpp/trunk/modules/python/mod-python.cpp index 1c35467b4b..a45b83997f 100644 --- a/sca-cpp/trunk/modules/python/mod-python.cpp +++ b/sca-cpp/trunk/modules/python/mod-python.cpp @@ -36,16 +36,39 @@ namespace tuscany { namespace server { namespace modeval { +/** + * Start the module. + */ +const failable start(unused ServerConf& sc) { + // Start a Python runtime + sc.moduleConf = new (gc_new()) python::PythonRuntime(); + return true; +} + +/** + * Stop the module. + */ +const failable stop(unused ServerConf& sc) { + return true; +} + +/** + * Restart the module. + */ +const failable restart(unused ServerConf& sc) { + return true; +} + /** * Evaluate a Python component implementation and convert it to an applicable * lambda function. */ -const failable&)> > evalImplementation(const string& path, const value& impl, const list& px) { +const failable&)> > evalImplementation(const string& path, const value& impl, const list& px, modeval::ServerConf& sc) { const string itype(elementName(impl)); if (contains(itype, ".python")) - return modpython::evalImplementation(path, impl, px); + return modpython::evalImplementation(path, impl, px, sc); if (contains(itype, ".cpp")) - return modcpp::evalImplementation(path, impl, px); + return modcpp::evalImplementation(path, impl, px, sc); return mkfailure&)> >(string("Unsupported implementation type: ") + itype); } diff --git a/sca-cpp/trunk/modules/python/mod-python.hpp b/sca-cpp/trunk/modules/python/mod-python.hpp index e67b9a4a93..cbe2b6b97c 100644 --- a/sca-cpp/trunk/modules/python/mod-python.hpp +++ b/sca-cpp/trunk/modules/python/mod-python.hpp @@ -34,7 +34,7 @@ #include "value.hpp" #include "monad.hpp" #include "eval.hpp" -#include "../http/httpd.hpp" +#include "../server/mod-eval.hpp" namespace tuscany { namespace server { @@ -63,7 +63,7 @@ struct applyImplementation { * Evaluate a Python component implementation and convert it to an applicable * lambda function. */ -const failable&)> > evalImplementation(const string& path, const value& impl, const list& px) { +const failable&)> > evalImplementation(const string& path, const value& impl, const list& px, unused modeval::ServerConf& sc) { const string fpath(path + attributeValue("script", impl)); ifstream is(fpath); if (fail(is)) diff --git a/sca-cpp/trunk/modules/python/python-test.cpp b/sca-cpp/trunk/modules/python/python-test.cpp index 48687fa0ec..c4ffcee57e 100644 --- a/sca-cpp/trunk/modules/python/python-test.cpp +++ b/sca-cpp/trunk/modules/python/python-test.cpp @@ -37,6 +37,7 @@ const string testPythonAdd = bool testEvalExpr() { gc_scoped_pool pool; + PythonRuntime py; istringstream is(testPythonAdd); failable script = readScript("script", is); @@ -69,6 +70,7 @@ const string testCallLambda( bool testEvalLambda() { gc_scoped_pool pool; + PythonRuntime py; const value trl = mklist("testReturnLambda"); istringstream trlis(testReturnLambda); diff --git a/sca-cpp/trunk/modules/python/server-test b/sca-cpp/trunk/modules/python/server-test index 247734e63d..fe1ff7a486 100755 --- a/sca-cpp/trunk/modules/python/server-test +++ b/sca-cpp/trunk/modules/python/server-test @@ -22,14 +22,11 @@ ../server/server-conf tmp ./python-conf tmp cat >>tmp/conf/httpd.conf < SCAContribution `pwd`/ SCAComposite domain-test.composite - EOF -apachectl -k start -d `pwd`/tmp +../http/httpd-start tmp sleep 2 # Test @@ -37,6 +34,6 @@ sleep 2 rc=$? # Cleanup -apachectl -k stop -d `pwd`/tmp +../http/httpd-stop tmp sleep 2 return $rc diff --git a/sca-cpp/trunk/modules/python/wiring-test b/sca-cpp/trunk/modules/python/wiring-test index 96306ace28..a3c85838fd 100755 --- a/sca-cpp/trunk/modules/python/wiring-test +++ b/sca-cpp/trunk/modules/python/wiring-test @@ -24,14 +24,11 @@ echo "Testing..." ../server/server-conf tmp ./python-conf tmp cat >>tmp/conf/httpd.conf < SCAContribution `pwd`/ SCAComposite domain-test.composite - EOF -apachectl -k start -d `pwd`/tmp +../http/httpd-start tmp sleep 2 # Test HTTP GET @@ -71,7 +68,7 @@ if [ "$rc" = "0" ]; then fi # Cleanup -apachectl -k stop -d `pwd`/tmp +../http/httpd-stop tmp sleep 2 if [ "$rc" = "0" ]; then echo "OK" diff --git a/sca-cpp/trunk/modules/scheme/primitive.hpp b/sca-cpp/trunk/modules/scheme/primitive.hpp index f1396710f5..5a13725ffd 100644 --- a/sca-cpp/trunk/modules/scheme/primitive.hpp +++ b/sca-cpp/trunk/modules/scheme/primitive.hpp @@ -171,6 +171,18 @@ const value cdddrProc(unused const list& args) { return cdddr((list )car(args)); } +const value startProc(unused const list& args) { + return false; +} + +const value stopProc(unused const list& args) { + return false; +} + +const value restartProc(unused const list& args) { + return false; +} + const value applyPrimitiveProcedure(const value& proc, list& args) { const lambda&)> func(cadr((list)proc)); return func(args); @@ -222,7 +234,10 @@ const list primitiveProcedureNames() { + "cdddr" + "display" + "log" - + "uuid"; + + "uuid" + + "start" + + "stop" + + "restart"; } const list primitiveProcedureObjects() { @@ -245,7 +260,10 @@ const list primitiveProcedureObjects() { + primitiveProcedure(cdddrProc) + primitiveProcedure(displayProc) + primitiveProcedure(logProc) - + primitiveProcedure(uuidProc); + + primitiveProcedure(uuidProc) + + primitiveProcedure(startProc) + + primitiveProcedure(stopProc) + + primitiveProcedure(restartProc); } const bool isFalse(const value& exp) { diff --git a/sca-cpp/trunk/modules/server/httpd-test b/sca-cpp/trunk/modules/server/httpd-test index 79651c8351..86718f96c5 100755 --- a/sca-cpp/trunk/modules/server/httpd-test +++ b/sca-cpp/trunk/modules/server/httpd-test @@ -24,14 +24,11 @@ echo "Testing..." ./server-conf tmp ./scheme-conf tmp cat >>tmp/conf/httpd.conf < SCAContribution `pwd`/ SCAComposite domain-test.composite - EOF -apachectl -k start -d `pwd`/tmp +../http/httpd-start tmp sleep 2 # Test HTTP GET @@ -71,7 +68,7 @@ if [ "$rc" = "0" ]; then fi # Cleanup -apachectl -k stop -d `pwd`/tmp +../http/httpd-stop tmp sleep 2 if [ "$rc" = "0" ]; then echo "OK" diff --git a/sca-cpp/trunk/modules/server/impl-test.cpp b/sca-cpp/trunk/modules/server/impl-test.cpp index 748e5f3837..51162d070d 100644 --- a/sca-cpp/trunk/modules/server/impl-test.cpp +++ b/sca-cpp/trunk/modules/server/impl-test.cpp @@ -70,7 +70,7 @@ const tuscany::value apply(const tuscany::list& params) { return tuscany::server::del(cdr(params)); if (func == "echo") return tuscany::server::echo(cdr(params)); - return tuscany::mkfailure(tuscany::string("Function not supported: ") + func); + return tuscany::mkfailure(); } } diff --git a/sca-cpp/trunk/modules/server/mod-cpp.hpp b/sca-cpp/trunk/modules/server/mod-cpp.hpp index f10b91dc28..19ba938034 100644 --- a/sca-cpp/trunk/modules/server/mod-cpp.hpp +++ b/sca-cpp/trunk/modules/server/mod-cpp.hpp @@ -37,7 +37,7 @@ #include "monad.hpp" #include "dynlib.hpp" #include "../scheme/driver.hpp" -#include "../http/httpd.hpp" +#include "mod-eval.hpp" namespace tuscany { namespace server { @@ -46,6 +46,21 @@ namespace modcpp { /** * Apply a C++ component implementation function. */ +const list failableResult(const value& func, const list& v) { + if (isNil(cdr(v))) + return v; + + // Report a failure with an empty reason as 'function not supported' + // Except for the start, stop, and restart functions, which are optional + const value reason = cadr(v); + if (length(reason) == 0) { + if (func == "start" || func == "stop" || func == "restart") + return mklist(false); + return mklist(value(), string("Function not supported: ") + func); + } + return v; +} + struct applyImplementation { const lib ilib; const lambda&)> impl; @@ -54,7 +69,10 @@ struct applyImplementation { } const value operator()(const list& params) const { debug(params, "modeval::cpp::applyImplementation::input"); - const value val = impl(append(params, px)); + + // Apply the component implementation function + const value val = failableResult(car(params), impl(append(params, px))); + debug(val, "modeval::cpp::applyImplementation::result"); return val; } @@ -64,7 +82,9 @@ struct applyImplementation { * Evaluate a C++ component implementation and convert it to * an applicable lambda function. */ -const failable&)> > evalImplementation(const string& path, const value& impl, const list& px) { +const failable&)> > evalImplementation(const string& path, const value& impl, const list& px, unused modeval::ServerConf& sc) { + + // Configure the implementation's lambda function const value ipath(attributeValue("path", impl)); const value iname(attributeValue("library", impl)); const string fpath(isNil(ipath)? path + iname : path + ipath + "/" + iname); @@ -72,7 +92,8 @@ const failable&)> > evalImplementation(const stri const failable&)> > evalf(dynlambda&)>("apply", ilib)); if (!hasContent(evalf)) return evalf; - return lambda&)>(applyImplementation(ilib, content(evalf), px)); + const lambda&)> l(applyImplementation(ilib, content(evalf), px)); + return l; } } diff --git a/sca-cpp/trunk/modules/server/mod-eval.cpp b/sca-cpp/trunk/modules/server/mod-eval.cpp index 54ca171ec4..5a9b87ca2f 100644 --- a/sca-cpp/trunk/modules/server/mod-eval.cpp +++ b/sca-cpp/trunk/modules/server/mod-eval.cpp @@ -36,16 +36,37 @@ namespace tuscany { namespace server { namespace modeval { +/** + * Start the module. + */ +const failable start(unused ServerConf& sc) { + return true; +} + +/** + * Stop the module. + */ +const failable stop(unused ServerConf& sc) { + return true; +} + +/** + * Restart the module. + */ +const failable restart(unused ServerConf& sc) { + return true; +} + /** * Evaluate a Scheme or C++ component implementation and convert it to an * applicable lambda function. */ -const failable&)> > evalImplementation(const string& path, const value& impl, const list& px) { +const failable&)> > evalImplementation(const string& path, const value& impl, const list& px, ServerConf& sc) { const string itype(elementName(impl)); if (contains(itype, ".scheme")) - return modscheme::evalImplementation(path, impl, px); + return modscheme::evalImplementation(path, impl, px, sc); if (contains(itype, ".cpp")) - return modcpp::evalImplementation(path, impl, px); + return modcpp::evalImplementation(path, impl, px, sc); return mkfailure&)> >(string("Unsupported implementation type: ") + itype); } diff --git a/sca-cpp/trunk/modules/server/mod-eval.hpp b/sca-cpp/trunk/modules/server/mod-eval.hpp index b622283fed..0e68e63016 100644 --- a/sca-cpp/trunk/modules/server/mod-eval.hpp +++ b/sca-cpp/trunk/modules/server/mod-eval.hpp @@ -19,6 +19,9 @@ /* $Rev$ $Date$ */ +#ifndef tuscany_modeval_hpp +#define tuscany_modeval_hpp + /** * HTTPD module used to eval component implementations. */ @@ -50,22 +53,13 @@ namespace modeval { */ class ServerConf { public: - ServerConf(server_rec* s) : s(s), home(""), wiringServerName("") { + ServerConf(server_rec* s) : s(s), moduleConf(NULL), home(""), wiringServerName(""), contributionPath(""), compositeName("") { } const server_rec* s; + void* moduleConf; string home; string wiringServerName; -}; - -/** - * Directory configuration. - */ -class DirConf { -public: - DirConf(char* dirspec) : dirspec(dirspec), contributionPath(""), compositeName("") { - } - const char* dirspec; string contributionPath; string compositeName; list implementations; @@ -262,9 +256,9 @@ int handler(request_rec *r) { httpdDebugRequest(r, "modeval::handler::input"); // Get the component implementation lambda - DirConf& dc = httpd::dirConf(r, &mod_tuscany_eval); + const ServerConf& sc = httpd::serverConf(r, &mod_tuscany_eval); const list path(httpd::pathValues(r->uri)); - const list impl(assoctree(cadr(path), dc.implementations)); + const list impl(assoctree(cadr(path), sc.implementations)); if (isNil(impl)) return HTTP_NOT_FOUND; @@ -322,8 +316,8 @@ const list propProxies(const list& props) { /** * Evaluate a component and convert it to an applicable lambda function. */ -const value evalComponent(DirConf& dc, ServerConf& sc, server_rec& server, const value& comp) { - extern const failable&)> > evalImplementation(const string& cpath, const value& impl, const list& px); +const value evalComponent(ServerConf& sc, server_rec& server, const value& comp) { + extern const failable&)> > evalImplementation(const string& cpath, const value& impl, const list& px, ServerConf& sc); const value impl = scdl::implementation(comp); @@ -342,23 +336,19 @@ const value evalComponent(DirConf& dc, ServerConf& sc, server_rec& server, const const list ppx(propProxies(scdl::properties(comp))); // Evaluate the component implementation and convert it to an applicable lambda function - const failable&)> > cimpl(evalImplementation(dc.contributionPath, impl, append(rpx, ppx))); + const failable&)> > cimpl(evalImplementation(sc.contributionPath, impl, append(rpx, ppx), sc)); if (!hasContent(cimpl)) return reason(cimpl); return content(cimpl); } /** - * Return a tree of component-name + configured-implementation pairs. + * Return a list of component-name + configured-implementation pairs. */ -const list componentToImplementationAssoc(DirConf& dc, ServerConf& sc, server_rec& server, const list& c) { +const list componentToImplementationAssoc(ServerConf& sc, server_rec& server, const list& c) { if (isNil(c)) return c; - return cons(mklist(scdl::name(car(c)), evalComponent(dc, sc, server, car(c))), componentToImplementationAssoc(dc, sc, server, cdr(c))); -} - -const list componentToImplementationTree(DirConf& dc, ServerConf& sc, server_rec& server, const list& c) { - return mkbtree(sort(componentToImplementationAssoc(dc, sc, server, c))); + return cons(mklist(scdl::name(car(c)), evalComponent(sc, server, car(c))), componentToImplementationAssoc(sc, server, cdr(c))); } /** @@ -372,48 +362,73 @@ const failable > readComponents(const string& path) { } /** - * Configure the components declared in the deployed composite. + * Apply a list of component implementations to a (start, stop or restart) lifecycle expression. */ -const bool confComponents(DirConf& dc, ServerConf& sc, server_rec& server) { - if (dc.contributionPath == "" || dc.compositeName == "") +const failable applyLifecycleExpr(const list impls, const list& expr) { + if (isNil(impls)) return true; - const failable > comps = readComponents(dc.contributionPath + dc.compositeName); + + // Evaluate lifecycle expression against a component implementation lambda + const lambda&)> l(cadr(car(impls))); + const failable r = failableResult(l(expr)); + if (!hasContent(r)) + return mkfailure(reason(r)); + + return applyLifecycleExpr(cdr(impls), expr); +} + +/** + * Configure the components declared in the deployed composite. + */ +const failable confComponents(const string& lifecycle, ServerConf& sc, server_rec& server) { + if (sc.contributionPath == "" || sc.compositeName == "") + return false; + + // Read the components and get their implementation lambda functions + const failable > comps = readComponents(sc.contributionPath + sc.compositeName); if (!hasContent(comps)) - return true; - dc.implementations = componentToImplementationTree(dc, sc, server, content(comps)); - debug(dc.implementations, "modeval::confComponents::implementations"); - return true; + return mkfailure(reason(comps)); + const list impls = componentToImplementationAssoc(sc, server, content(comps)); + + // Store the implementation lambda functions in a tree for fast retrieval + sc.implementations = mkbtree(sort(impls)); + debug(sc.implementations, "modeval::confComponents::implementations"); + + // Start or restart the component implementations + return applyLifecycleExpr(impls, mklist(lifecycle)); } /** * Configuration commands. */ -const char *confHome(cmd_parms *cmd, unused void *c, const char *arg) { +const char* confHome(cmd_parms *cmd, unused void *c, const char *arg) { gc_scoped_pool pool(cmd->pool); ServerConf& sc = httpd::serverConf(cmd, &mod_tuscany_eval); sc.home = arg; return NULL; } -const char *confWiringServerName(cmd_parms *cmd, unused void *c, const char *arg) { +const char* confWiringServerName(cmd_parms *cmd, unused void *c, const char *arg) { gc_scoped_pool pool(cmd->pool); ServerConf& sc = httpd::serverConf(cmd, &mod_tuscany_eval); sc.wiringServerName = arg; return NULL; } -const char *confContribution(cmd_parms *cmd, void *c, const char *arg) { +const char* confContribution(cmd_parms *cmd, unused void *c, const char *arg) { gc_scoped_pool pool(cmd->pool); ServerConf& sc = httpd::serverConf(cmd, &mod_tuscany_eval); - DirConf& dc = *(DirConf*)c; - dc.contributionPath = arg; - confComponents(dc, sc, *cmd->server); + sc.contributionPath = arg; return NULL; } -const char *confComposite(cmd_parms *cmd, void *c, const char *arg) { +const char* confComposite(cmd_parms *cmd, unused void *c, const char *arg) { gc_scoped_pool pool(cmd->pool); ServerConf& sc = httpd::serverConf(cmd, &mod_tuscany_eval); - DirConf& dc = *(DirConf*)c; - dc.compositeName = arg; - confComponents(dc, sc, *cmd->server); + sc.compositeName = arg; + return NULL; +} + +const char* confEnv(unused cmd_parms *cmd, unused void *c, const char *name, const char *value) { + gc_scoped_pool pool(cmd->pool); + setenv(name, value != NULL? value : "", 1); return NULL; } @@ -423,15 +438,49 @@ const char *confComposite(cmd_parms *cmd, void *c, const char *arg) { const command_rec commands[] = { AP_INIT_TAKE1("TuscanyHome", (const char*(*)())confHome, NULL, RSRC_CONF, "Tuscany home directory"), AP_INIT_TAKE1("SCAWiringServerName", (const char*(*)())confWiringServerName, NULL, RSRC_CONF, "SCA wiring server name"), - AP_INIT_TAKE1("SCAContribution", (const char*(*)())confContribution, NULL, ACCESS_CONF, "SCA contribution location"), - AP_INIT_TAKE1("SCAComposite", (const char*(*)())confComposite, NULL, ACCESS_CONF, "SCA composite location"), + AP_INIT_TAKE1("SCAContribution", (const char*(*)())confContribution, NULL, RSRC_CONF, "SCA contribution location"), + AP_INIT_TAKE1("SCAComposite", (const char*(*)())confComposite, NULL, RSRC_CONF, "SCA composite location"), + AP_INIT_TAKE12("SetEnv", (const char*(*)())confEnv, NULL, OR_FILEINFO, "Environment variable name and optional value"), {NULL, NULL, NULL, 0, NO_ARGS, NULL} }; -int postConfig(unused apr_pool_t *p, unused apr_pool_t *plog, unused apr_pool_t *ptemp, unused server_rec *s) { +int postConfig(apr_pool_t *p, unused apr_pool_t *plog, unused apr_pool_t *ptemp, server_rec *s) { + extern const failable start(ServerConf& sc); + extern const failable restart(ServerConf& sc); + gc_scoped_pool pool(p); + ServerConf& sc = httpd::serverConf(s, &mod_tuscany_eval); + + // Count the calls to post config + const string k("tuscany::modeval::postConfig"); + const int count = httpd::userData(k, s); + httpd::putUserData(k, count +1, s); + + // Count == 0, do nothing as post config is always called twice, + // count == 1 is the first start, count > 1 is a restart + if (count == 0) + return OK; + if (count == 1) { + debug("modeval::postConfig::start"); + start(sc); + } + if (count > 1) { + debug("modeval::postConfig::restart"); + restart(sc); + } + + // Configure the components deployed to the server + debug(sc.wiringServerName, "modeval::postConfig::wiringServerName"); + debug(sc.contributionPath, "modeval::postConfig::contributionPath"); + debug(sc.compositeName, "modeval::postConfig::compositeName"); + const failable res = confComponents(count > 1? "restart" : "start", sc, *s); + if (!hasContent(res)) + return -1; return OK; } +/** + * Child process initialization. + */ void childInit(apr_pool_t* p, server_rec* svr_rec) { gc_scoped_pool pool(p); ServerConf* c = (ServerConf*)ap_get_module_config(svr_rec->module_config, &mod_tuscany_eval); @@ -457,7 +506,7 @@ extern "C" { module AP_MODULE_DECLARE_DATA mod_tuscany_eval = { STANDARD20_MODULE_STUFF, // dir config and merger - tuscany::httpd::makeDirConf, NULL, + NULL, NULL, // server config and merger tuscany::httpd::makeServerConf, NULL, // commands and hooks @@ -465,3 +514,5 @@ module AP_MODULE_DECLARE_DATA mod_tuscany_eval = { }; } + +#endif diff --git a/sca-cpp/trunk/modules/server/mod-scheme.hpp b/sca-cpp/trunk/modules/server/mod-scheme.hpp index fd03ebb43e..e18eececf6 100644 --- a/sca-cpp/trunk/modules/server/mod-scheme.hpp +++ b/sca-cpp/trunk/modules/server/mod-scheme.hpp @@ -34,7 +34,7 @@ #include "value.hpp" #include "monad.hpp" #include "../scheme/eval.hpp" -#include "../http/httpd.hpp" +#include "../server/mod-eval.hpp" namespace tuscany { namespace server { @@ -73,7 +73,7 @@ struct applyImplementation { * Evaluate a Scheme component implementation and convert it to an * applicable lambda function. */ -const failable&)> > evalImplementation(const string& path, const value& impl, const list& px) { +const failable&)> > evalImplementation(const string& path, const value& impl, const list& px, unused modeval::ServerConf& sc) { const string fpath(path + attributeValue("script", impl)); ifstream is(fpath); if (fail(is)) diff --git a/sca-cpp/trunk/modules/server/mod-wiring.cpp b/sca-cpp/trunk/modules/server/mod-wiring.cpp index aef2f00c83..e21f8be773 100644 --- a/sca-cpp/trunk/modules/server/mod-wiring.cpp +++ b/sca-cpp/trunk/modules/server/mod-wiring.cpp @@ -48,11 +48,16 @@ namespace modwiring { */ class ServerConf { public: - ServerConf(server_rec* s) : s(s), home(""), wiringServerName("") { + ServerConf(server_rec* s) : s(s), start(false), home(""), wiringServerName(""), contributionPath(""), compositeName("") { } const server_rec* s; + bool start; string home; string wiringServerName; + string contributionPath; + string compositeName; + list references; + list services; }; /** @@ -60,20 +65,6 @@ public: */ const bool useModProxy = true; -/** - * Directory configuration. - */ -class DirConf { -public: - DirConf(char* dirspec) : dirspec(dirspec), contributionPath(""), compositeName("") { - } - const char* dirspec; - string contributionPath; - string compositeName; - list references; - list services; -}; - /** * Returns true if a URI is absolute. */ @@ -90,9 +81,9 @@ int translateReference(request_rec *r) { debug(r->uri, "modwiring::translateReference::uri"); // Find the requested component - DirConf& dc = httpd::dirConf(r, &mod_tuscany_wiring); + const ServerConf& sc = httpd::serverConf(r, &mod_tuscany_wiring); const list rpath(httpd::pathValues(r->uri)); - const list comp(assoctree(cadr(rpath), dc.references)); + const list comp(assoctree(cadr(rpath), sc.references)); if (isNil(comp)) return HTTP_NOT_FOUND; @@ -155,9 +146,9 @@ int translateService(request_rec *r) { debug(r->uri, "modwiring::translateService::uri"); // Find the requested component - DirConf& dc = httpd::dirConf(r, &mod_tuscany_wiring); + const ServerConf& sc = httpd::serverConf(r, &mod_tuscany_wiring); const list path(httpd::pathValues(r->uri)); - const list svc(assocPath(path, dc.services)); + const list svc(assocPath(path, sc.services)); if (isNil(svc)) return DECLINED; debug(svc, "modwiring::translateService::service"); @@ -282,16 +273,19 @@ const list uriToComponentTree(const list& c) { /** * Configure the components declared in the server's deployment composite. */ -const bool confComponents(DirConf& dc) { - if (dc.contributionPath == "" || dc.compositeName == "") +const bool confComponents(ServerConf& sc) { + if (sc.contributionPath == "" || sc.compositeName == "") return true; - const failable > comps = readComponents(dc.contributionPath + dc.compositeName); + + // Read the component configuration and store the references and service + // URIs in trees for fast retrieval later + const failable > comps = readComponents(sc.contributionPath + sc.compositeName); if (!hasContent(comps)) return true; - dc.references = componentReferenceToTargetTree(content(comps)); - debug(dc.references, "modwiring::confComponents::references"); - dc.services = uriToComponentTree(content(comps)); - debug(dc.services, "modwiring::confComponents::services"); + sc.references = componentReferenceToTargetTree(content(comps)); + debug(sc.references, "modwiring::confComponents::references"); + sc.services = uriToComponentTree(content(comps)); + debug(sc.services, "modwiring::confComponents::services"); return true; } @@ -310,18 +304,16 @@ const char *confWiringServerName(cmd_parms *cmd, unused void *c, const char *arg sc.wiringServerName = arg; return NULL; } -const char *confContribution(cmd_parms *cmd, void *c, const char *arg) { +const char *confContribution(cmd_parms *cmd, unused void *c, const char *arg) { gc_scoped_pool pool(cmd->pool); - DirConf& dc = *(DirConf*)c; - dc.contributionPath = arg; - confComponents(dc); + ServerConf& sc = httpd::serverConf(cmd, &mod_tuscany_wiring); + sc.contributionPath = arg; return NULL; } -const char *confComposite(cmd_parms *cmd, void *c, const char *arg) { +const char *confComposite(cmd_parms *cmd, unused void *c, const char *arg) { gc_scoped_pool pool(cmd->pool); - DirConf& dc = *(DirConf*)c; - dc.compositeName = arg; - confComponents(dc); + ServerConf& sc = httpd::serverConf(cmd, &mod_tuscany_wiring); + sc.compositeName = arg; return NULL; } @@ -330,14 +322,28 @@ const char *confComposite(cmd_parms *cmd, void *c, const char *arg) { */ const command_rec commands[] = { AP_INIT_TAKE1("TuscanyHome", (const char*(*)())confHome, NULL, RSRC_CONF, "Tuscany home directory"), - AP_INIT_TAKE1("SCAWiringServerName", (const char*(*)())confWiringServerName, NULL, ACCESS_CONF, "SCA wiring server name"), - AP_INIT_TAKE1("SCAContribution", (const char*(*)())confContribution, NULL, ACCESS_CONF, "SCA contribution location"), - AP_INIT_TAKE1("SCAComposite", (const char*(*)())confComposite, NULL, ACCESS_CONF, "SCA composite location"), + AP_INIT_TAKE1("SCAWiringServerName", (const char*(*)())confWiringServerName, NULL, RSRC_CONF, "SCA wiring server name"), + AP_INIT_TAKE1("SCAContribution", (const char*(*)())confContribution, NULL, RSRC_CONF, "SCA contribution location"), + AP_INIT_TAKE1("SCAComposite", (const char*(*)())confComposite, NULL, RSRC_CONF, "SCA composite location"), {NULL, NULL, NULL, 0, NO_ARGS, NULL} }; -int postConfig(unused apr_pool_t *p, unused apr_pool_t *plog, unused apr_pool_t *ptemp, unused server_rec *s) { - return OK; +int postConfig(unused apr_pool_t *p, unused apr_pool_t *plog, unused apr_pool_t *ptemp, server_rec *s) { + // Count the calls to post config, skip the first one as + // postConfig is always called twice + const string k("tuscany::modwiring::postConfig"); + const int count = httpd::userData(k, s); + httpd::putUserData(k, count +1, s); + if (count == 0) + return OK; + + // Configure the wiring for the deployed components + ServerConf& sc = httpd::serverConf(s, &mod_tuscany_wiring); + debug(sc.wiringServerName, "modwiring::postConfig::wiringServerName"); + debug(sc.contributionPath, "modwiring::postConfig::contributionPath"); + debug(sc.compositeName, "modwiring::postConfig::compositeName"); + confComponents(sc); + return OK; } void childInit(apr_pool_t* p, server_rec* svr_rec) { @@ -365,7 +371,7 @@ extern "C" { module AP_MODULE_DECLARE_DATA mod_tuscany_wiring = { STANDARD20_MODULE_STUFF, // dir config and merger - tuscany::httpd::makeDirConf, NULL, + NULL, NULL, // server config and merger tuscany::httpd::makeServerConf, NULL, // commands and hooks diff --git a/sca-cpp/trunk/modules/server/server-test b/sca-cpp/trunk/modules/server/server-test index 97b3fd919b..e1d9932a5d 100755 --- a/sca-cpp/trunk/modules/server/server-test +++ b/sca-cpp/trunk/modules/server/server-test @@ -22,14 +22,11 @@ ./server-conf tmp ./scheme-conf tmp cat >>tmp/conf/httpd.conf < SCAContribution `pwd`/ SCAComposite domain-test.composite - EOF -apachectl -k start -d `pwd`/tmp +../http/httpd-start tmp sleep 2 # Test @@ -37,6 +34,6 @@ sleep 2 rc=$? # Cleanup -apachectl -k stop -d `pwd`/tmp +../http/httpd-stop tmp sleep 2 return $rc diff --git a/sca-cpp/trunk/modules/server/wiring-test b/sca-cpp/trunk/modules/server/wiring-test index 29eef996e6..0deab33d68 100755 --- a/sca-cpp/trunk/modules/server/wiring-test +++ b/sca-cpp/trunk/modules/server/wiring-test @@ -24,14 +24,11 @@ echo "Testing..." ./server-conf tmp ./scheme-conf tmp cat >>tmp/conf/httpd.conf < SCAContribution `pwd`/ SCAComposite domain-test.composite - EOF -apachectl -k start -d `pwd`/tmp +../http/httpd-start tmp sleep 2 # Test HTTP GET @@ -71,7 +68,7 @@ if [ "$rc" = "0" ]; then fi # Cleanup -apachectl -k stop -d `pwd`/tmp +../http/httpd-stop tmp sleep 2 if [ "$rc" = "0" ]; then echo "OK" -- cgit v1.2.3