From e34c4a9109bbbb28d7ecef7cccccdc537008f8a5 Mon Sep 17 00:00:00 2001 From: jsdelfino Date: Sun, 17 Jan 2010 09:02:39 +0000 Subject: Integrated Java component support as an HTTPD module. Added test case and store scenario skeleton. git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@900072 13f79535-47bb-0310-9956-ffa450edef68 --- sca-cpp/trunk/modules/java/client-test.cpp | 46 ++ sca-cpp/trunk/modules/java/domain-test.composite | 42 ++ sca-cpp/trunk/modules/java/eval.hpp | 132 ++-- sca-cpp/trunk/modules/java/java-conf | 26 + sca-cpp/trunk/modules/java/java-test.cpp | 3 - sca-cpp/trunk/modules/java/mod-java.cpp | 54 ++ sca-cpp/trunk/modules/java/mod-java.hpp | 78 +++ .../java/org/apache/tuscany/InvocationHandler.java | 4 +- .../modules/java/org/apache/tuscany/Service.java | 38 ++ sca-cpp/trunk/modules/java/test/Client.java | 38 ++ sca-cpp/trunk/modules/java/test/ClientImpl.java | 52 ++ sca-cpp/trunk/modules/java/test/Server.java | 38 ++ sca-cpp/trunk/modules/java/test/ServerImpl.java | 59 ++ sca-cpp/trunk/test/store-java/htdocs/.htaccess | 19 + sca-cpp/trunk/test/store-java/htdocs/store.html | 169 ++++++ sca-cpp/trunk/test/store-java/htdocs/store.js | 661 +++++++++++++++++++++ sca-cpp/trunk/test/store-java/store.composite | 69 +++ 17 files changed, 1475 insertions(+), 53 deletions(-) create mode 100644 sca-cpp/trunk/modules/java/client-test.cpp create mode 100644 sca-cpp/trunk/modules/java/domain-test.composite create mode 100755 sca-cpp/trunk/modules/java/java-conf create mode 100644 sca-cpp/trunk/modules/java/mod-java.cpp create mode 100644 sca-cpp/trunk/modules/java/mod-java.hpp create mode 100644 sca-cpp/trunk/modules/java/org/apache/tuscany/Service.java create mode 100644 sca-cpp/trunk/modules/java/test/Client.java create mode 100644 sca-cpp/trunk/modules/java/test/ClientImpl.java create mode 100644 sca-cpp/trunk/modules/java/test/Server.java create mode 100644 sca-cpp/trunk/modules/java/test/ServerImpl.java create mode 100644 sca-cpp/trunk/test/store-java/htdocs/.htaccess create mode 100644 sca-cpp/trunk/test/store-java/htdocs/store.html create mode 100644 sca-cpp/trunk/test/store-java/htdocs/store.js create mode 100644 sca-cpp/trunk/test/store-java/store.composite (limited to 'sca-cpp') diff --git a/sca-cpp/trunk/modules/java/client-test.cpp b/sca-cpp/trunk/modules/java/client-test.cpp new file mode 100644 index 0000000000..d4a4d65ab4 --- /dev/null +++ b/sca-cpp/trunk/modules/java/client-test.cpp @@ -0,0 +1,46 @@ +/* + * 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 HTTP client functions. + */ + +#include "stream.hpp" +#include "string.hpp" +#include "../server/client-test.hpp" + +namespace tuscany { +namespace server { + +string testURI = "http://localhost:8090/java"; + +} +} + +int main() { + tuscany::cout << "Testing..." << tuscany::endl; + + tuscany::server::testServer(); + + tuscany::cout << "OK" << tuscany::endl; + + return 0; +} diff --git a/sca-cpp/trunk/modules/java/domain-test.composite b/sca-cpp/trunk/modules/java/domain-test.composite new file mode 100644 index 0000000000..e35945f314 --- /dev/null +++ b/sca-cpp/trunk/modules/java/domain-test.composite @@ -0,0 +1,42 @@ + + + + + + + + + + + + + + + + + + + + + + diff --git a/sca-cpp/trunk/modules/java/eval.hpp b/sca-cpp/trunk/modules/java/eval.hpp index b51a57337e..7641c5de79 100644 --- a/sca-cpp/trunk/modules/java/eval.hpp +++ b/sca-cpp/trunk/modules/java/eval.hpp @@ -43,20 +43,40 @@ class JavaRuntime { public: JavaRuntime() { - // Create a JVM - JavaVMInitArgs args; - args.version = JNI_VERSION_1_6; - args.ignoreUnrecognized = JNI_FALSE; - JavaVMOption options[1]; - options[0].optionString = const_cast("-Djava.class.path=."); - args.options = options; - args.nOptions = 1; - JNI_CreateJavaVM(&jvm, (void**)&env, &args); + // Get existing JVM + jsize nvms = 0; + JNI_GetCreatedJavaVMs(&jvm, 1, &nvms); + if (nvms == 0) { + + // Create a new JVM + JavaVMInitArgs args; + args.version = JNI_VERSION_1_6; + args.ignoreUnrecognized = JNI_FALSE; + JavaVMOption options[1]; + options[0].optionString = const_cast("-Djava.class.path=."); + args.options = options; + args.nOptions = 1; + JNI_CreateJavaVM(&jvm, (void**)&env, &args); + + // Register our native invocation handler function + invokerClass = env->FindClass("org/apache/tuscany/InvocationHandler"); + 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); + + } else { + + // Just hook to existing JVM + jvm->GetEnv((void**)&env, JNI_VERSION_1_6); + invokerClass = env->FindClass("org/apache/tuscany/InvocationHandler"); + } // Capture JVM standard IO setupIO(); - // Lookup the system classes and methods we need + // Lookup the classes and methods we need classClass = env->FindClass("java/lang/Class"); methodClass = env->FindClass("java/lang/reflect/Method"); objectClass = env->FindClass("java/lang/Object"); @@ -70,22 +90,9 @@ public: booleanValue = env->GetMethodID(booleanClass, "booleanValue", "()Z"); declaredMethods = env->GetMethodID(classClass, "getDeclaredMethods", "()[Ljava/lang/reflect/Method;"); methodName = env->GetMethodID(methodClass, "getName", "()Ljava/lang/String;"); - - // Register our native invocation handler native - invokerClass = env->FindClass("org/apache/tuscany/InvocationHandler"); + parameterTypes = env->GetMethodID(methodClass, "getParameterTypes", "()[Ljava/lang/Class;"); invokerValueOf = env->GetStaticMethodID(invokerClass, "valueOf", "(Ljava/lang/Class;J)Ljava/lang/Object;"); invokerLambda = env->GetFieldID(invokerClass, "lambda", "J"); - 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); - } - - ~JavaRuntime() { - if (jvm == NULL) - return; - jvm->DestroyJavaVM(); } JavaVM* jvm; @@ -104,6 +111,7 @@ public: jmethodID booleanValue; jmethodID declaredMethods; jmethodID methodName; + jmethodID parameterTypes; jclass invokerClass; jmethodID invokerValueOf; @@ -125,7 +133,7 @@ string lastException(const JavaRuntime& jr) { /** * Declare conversion functions. */ -const jobject valueToJobject(const JavaRuntime& jr, const value& v); +const jobject valueToJobject(const JavaRuntime& jr, const value& jtype, const value& v); const value jobjectToValue(const JavaRuntime& jr, const jobject o); const jobjectArray valuesToJarray(const JavaRuntime& jr, const list& v); const list jarrayToValues(const JavaRuntime& jr, const jobjectArray o); @@ -156,6 +164,22 @@ public: javaLambda(const JavaRuntime& jr, const value& iface, const lambda&)>& func) : jr(jr), iface(iface), func(func) { } + const value operator()(const list& expr) const { + const value& op(car(expr)); + if (op == "toString") { + ostringstream os; + os << this; + return value(string("org.apache.tuscany.InvocationHandler@") + (c_str(str(os)) + 2)); + } + if (op == "hashCode") { + return value((double)(long)this); + } + if (op == "equals") { + return value(cadr(expr) == this); + } + return func(expr); + } + const JavaRuntime& jr; const value iface; const lambda&)> func; @@ -182,23 +206,18 @@ jobject JNICALL nativeInvoke(JNIEnv* env, jobject self, unused jobject proxy, jo const list expr = cons(func, jarrayToValues(jl.jr, args)); // Invoke the lambda function - value result = jl.func(expr); + value result = jl(expr); // Convert result to a jobject - return valueToJobject(jl.jr, result); + return valueToJobject(jl.jr, value(), result); } -const jobject mkJavaLambda(const JavaRuntime& jr, const lambda&)>& l) { - - // The lambda function is given the opportunity to give us a - // Java interface name that it implements, and which will be - // used as the type of the Java proxy representing it. If the - // lambda function doesn't specify an interface, then the - // proxy implements javax.script.Invocable. - const value iface = l(mklist("interface")); +/** + * Convert a lambda function to Java proxy. + */ +const jobject mkJavaLambda(const JavaRuntime& jr, unused const value& iface, const lambda&)>& l) { const gc_ptr jl = new (gc_new()) javaLambda(jr, iface, l); - - jclass jc = jr.env->FindClass(c_str(jniClassName(string(iface)))); + jclass jc = (jclass)(long)(double)iface; const jobject obj = jr.env->CallStaticObjectMethod(jr.invokerClass, jr.invokerValueOf, jc, (long)(javaLambda*)jl); return obj; } @@ -209,7 +228,7 @@ const jobject mkJavaLambda(const JavaRuntime& jr, const lambda& v, const int i) { if (isNil(v)) return a; - jr.env->SetObjectArrayElement(a, i, valueToJobject(jr, car(v))); + jr.env->SetObjectArrayElement(a, i, valueToJobject(jr, value(), car(v))); return valuesToJarrayHelper(jr, a, cdr(v), i + 1); } @@ -221,12 +240,12 @@ const jobjectArray valuesToJarray(const JavaRuntime& jr, const list& v) { /** * Convert a value to a Java jobject. */ -const jobject valueToJobject(const JavaRuntime& jr, const value& v) { +const jobject valueToJobject(const JavaRuntime& jr, const value& jtype, const value& v) { switch (type(v)) { case value::List: return valuesToJarray(jr, v); case value::Lambda: - return mkJavaLambda(jr, v); + return mkJavaLambda(jr, jtype, v); case value::Symbol: return jr.env->NewStringUTF(c_str(string("'") + v)); case value::String: @@ -243,17 +262,17 @@ const jobject valueToJobject(const JavaRuntime& jr, const value& v) { /** * Convert a list of values to an array of jvalues. */ -const jvalue* valuesToJvaluesHelper(const JavaRuntime& jr, jvalue* a, const list& v) { +const jvalue* valuesToJvaluesHelper(const JavaRuntime& jr, jvalue* a, const list& types, const list& v) { if (isNil(v)) return a; - a->l = valueToJobject(jr, car(v)); - return valuesToJvaluesHelper(jr, a + 1, cdr(v)); + a->l = valueToJobject(jr, car(types), car(v)); + return valuesToJvaluesHelper(jr, a + 1, cdr(types), cdr(v)); } -const jvalue* valuesToJvalues(const JavaRuntime& jr, const list& v) { +const jvalue* valuesToJvalues(const JavaRuntime& jr, const list& types, const list& v) { const int n = length(v); jvalue* a = new (gc_anew(n)) jvalue[n]; - valuesToJvaluesHelper(jr, a, v); + valuesToJvaluesHelper(jr, a, types, v); return a; } @@ -267,6 +286,8 @@ const list jarrayToValuesHelper(const JavaRuntime& jr, jobjectArray a, co } const list jarrayToValues(const JavaRuntime& jr, jobjectArray o) { + if (o == NULL) + return list(); return jarrayToValuesHelper(jr, o, 0, jr.env->GetArrayLength(o)); } @@ -315,15 +336,30 @@ const value jobjectToValue(const JavaRuntime& jr, const jobject o) { } /** - * Returns a balanced tree of a class' methods. + * Returns a balanced tree of the methods of a class. */ +const value parameterTypeToValue(const jobject t) { + return value((double)(long)t); +} + +const list parameterTypesToValues(const JavaRuntime& jr, const jobjectArray t, const int i) { + if (i == 0) + return list(); + return cons(parameterTypeToValue(jr.env->GetObjectArrayElement(t, i - 1)), parameterTypesToValues(jr, t, i - 1)); +} + const value methodToValue(const JavaRuntime& jr, const jobject m) { const jobject s = jr.env->CallObjectMethod(m, jr.methodName); const char* c = jr.env->GetStringUTFChars((jstring)s, NULL); const string& name = string(c); jr.env->ReleaseStringUTFChars((jstring)s, c); + const jmethodID mid = jr.env->FromReflectedMethod(m); - return mklist(c_str(name), (double)(long)mid); + + const jobjectArray t = (jobjectArray)jr.env->CallObjectMethod(m, jr.parameterTypes); + const list types = reverse(parameterTypesToValues(jr, t, jr.env->GetArrayLength(t))); + + return cons(c_str(name), cons((double)(long)mid, types)); } const list methodsToValues(const JavaRuntime& jr, const jobjectArray m, const int i) { @@ -382,7 +418,7 @@ const failable evalClass(const JavaRuntime& jr, const value& expr, const const jmethodID fid = (jmethodID)(long)(double)cadr(func); // Convert args to Java jvalues - const jvalue *args = valuesToJvalues(jr, cdr(expr)); + const jvalue *args = valuesToJvalues(jr, cddr(func), cdr(expr)); // Call the Java function const jobject result = jr.env->CallObjectMethodA(jc.obj, fid, args); diff --git a/sca-cpp/trunk/modules/java/java-conf b/sca-cpp/trunk/modules/java/java-conf new file mode 100755 index 0000000000..d81a14a6c1 --- /dev/null +++ b/sca-cpp/trunk/modules/java/java-conf @@ -0,0 +1,26 @@ +#!/bin/sh + +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +# Generate a Java server conf +here=`readlink -f $0`; here=`dirname $here` +root=`readlink -f $1` + +cat >>$root/conf/httpd.conf <& args) { - if (car(args) == "interface") - return "test.Adder"; - assert(car(args) == "add"); const double x = cadr(args); const double y = caddr(args); diff --git a/sca-cpp/trunk/modules/java/mod-java.cpp b/sca-cpp/trunk/modules/java/mod-java.cpp new file mode 100644 index 0000000000..a5dc48679d --- /dev/null +++ b/sca-cpp/trunk/modules/java/mod-java.cpp @@ -0,0 +1,54 @@ +/* + * 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$ */ + +/** + * HTTPD module used to eval Java component implementations. + */ + +#include "string.hpp" +#include "function.hpp" +#include "list.hpp" +#include "value.hpp" +#include "monad.hpp" +#include "../server/mod-cpp.hpp" +#include "../server/mod-eval.hpp" +#include "mod-java.hpp" + +namespace tuscany { +namespace server { +namespace modeval { + +/** + * 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 string itype(elementName(impl)); + if (contains(itype, ".java")) + return modjava::evalImplementation(path, impl, px); + if (contains(itype, ".cpp")) + return modcpp::evalImplementation(path, impl, px); + 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 new file mode 100644 index 0000000000..933550b7c3 --- /dev/null +++ b/sca-cpp/trunk/modules/java/mod-java.hpp @@ -0,0 +1,78 @@ +/* + * 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_modjava_hpp +#define tuscany_modjava_hpp + +/** + * Evaluation functions used by mod-eval to evaluate Java + * component implementations. + */ + +#include "string.hpp" +#include "stream.hpp" +#include "function.hpp" +#include "list.hpp" +#include "value.hpp" +#include "monad.hpp" +#include "eval.hpp" +#include "../http/httpd.hpp" + +namespace tuscany { +namespace server { +namespace modjava { + +/** + * 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) { + } + 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); + debug(val, "modeval::java::applyImplementation::result"); + if (!hasContent(val)) + return mklist(value(), reason(val)); + return mklist(content(val)); + } +}; + +/** + * Evaluate a Java component implementation and convert it to an applicable + * lambda function. + */ +const failable&)> > evalImplementation(unused const string& path, const value& impl, const list& px) { + const string cn(attributeValue("class", impl)); + const failable jc = java::readClass(java::javaRuntime, cn); + if (!hasContent(jc)) + return mkfailure&)> >(reason(jc)); + return lambda&)>(applyImplementation(content(jc), px)); +} + +} +} +} + +#endif /* tuscany_modjava_hpp */ diff --git a/sca-cpp/trunk/modules/java/org/apache/tuscany/InvocationHandler.java b/sca-cpp/trunk/modules/java/org/apache/tuscany/InvocationHandler.java index 423fe6248d..1c159a99d5 100644 --- a/sca-cpp/trunk/modules/java/org/apache/tuscany/InvocationHandler.java +++ b/sca-cpp/trunk/modules/java/org/apache/tuscany/InvocationHandler.java @@ -29,8 +29,8 @@ public class InvocationHandler implements java.lang.reflect.InvocationHandler { this.lambda = lambda; } - public static Object valueOf(final Class clazz, final long lambda) { - return Proxy.newProxyInstance(clazz.getClassLoader(), new Class[]{clazz}, new InvocationHandler(lambda)); + public static Object valueOf(final Class iface, final long lambda) { + return Proxy.newProxyInstance(iface.getClassLoader(), new Class[]{iface}, new InvocationHandler(lambda)); } @Override diff --git a/sca-cpp/trunk/modules/java/org/apache/tuscany/Service.java b/sca-cpp/trunk/modules/java/org/apache/tuscany/Service.java new file mode 100644 index 0000000000..c8152463b4 --- /dev/null +++ b/sca-cpp/trunk/modules/java/org/apache/tuscany/Service.java @@ -0,0 +1,38 @@ +/* + * 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; + +public interface Service { + + String post(Object[] item); + + Object[] get(String id); + + Object[] getall(); + + boolean put(String id, Object[] item); + + boolean delete(String id); + + boolean deleteall(); + + T apply(Object... params); + +} diff --git a/sca-cpp/trunk/modules/java/test/Client.java b/sca-cpp/trunk/modules/java/test/Client.java new file mode 100644 index 0000000000..447b408cd4 --- /dev/null +++ b/sca-cpp/trunk/modules/java/test/Client.java @@ -0,0 +1,38 @@ +/* + * 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 test; + +public interface Client { + + String echo(String x); + + Object[] getall(); + + Object[] get(String id); + + String post(Object[] item); + + Boolean put(String id, Object[] entry); + + Boolean deleteall(); + + Boolean delete(String id); + +} diff --git a/sca-cpp/trunk/modules/java/test/ClientImpl.java b/sca-cpp/trunk/modules/java/test/ClientImpl.java new file mode 100644 index 0000000000..3d3063b608 --- /dev/null +++ b/sca-cpp/trunk/modules/java/test/ClientImpl.java @@ -0,0 +1,52 @@ +/* + * 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 test; + +public class ClientImpl { + + public String echo(String x, Server server) { + return server.echo(x); + } + + public Object[] getall(Server server) { + return server.getall(); + } + + public Object[] get(String id, Server server) { + return server.get(id); + } + + public String post(Object[] item, Server server) { + return server.post(item); + } + + public Boolean put(String id, Object[] item, Server server) { + return server.put(id, item); + } + + public Boolean deleteall(Server server) { + return server.deleteall(); + } + + public Boolean delete(String id, Server server) { + return server.delete(id); + } + +} diff --git a/sca-cpp/trunk/modules/java/test/Server.java b/sca-cpp/trunk/modules/java/test/Server.java new file mode 100644 index 0000000000..a165988550 --- /dev/null +++ b/sca-cpp/trunk/modules/java/test/Server.java @@ -0,0 +1,38 @@ +/* + * 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 test; + +public interface Server { + + String echo(String x); + + Object[] getall(); + + Object[] get(String id); + + String post(Object[] item); + + Boolean put(String id, Object[] entry); + + Boolean deleteall(); + + Boolean delete(String id); + +} diff --git a/sca-cpp/trunk/modules/java/test/ServerImpl.java b/sca-cpp/trunk/modules/java/test/ServerImpl.java new file mode 100644 index 0000000000..f82a8a24b5 --- /dev/null +++ b/sca-cpp/trunk/modules/java/test/ServerImpl.java @@ -0,0 +1,59 @@ +/* + * 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 test; + +public class ServerImpl { + + Object[] list(Object... o) { + return o; + } + + public String echo(String x) { + return x; + } + + public Object[] getall() { + return list("Sample Feed", "123456789", + list("Item", "111", list(list("'javaClass", "services.Item"), list("'name", "Apple"), list("'currencyCode", "USD"), list("'currencySymbol", "$"), list("'price", 2.99))), + list("Item", "222", list(list("'javaClass", "services.Item"), list("'name", "Orange"), list("'currencyCode", "USD"), list("'currencySymbol", "$"), list("'price", 3.55))), + list("Item", "333", list(list("'javaClass", "services.Item"), list("name", "Pear"), list("'currencyCode", "USD"), list("'currencySymbol", "$"), list("'price", 1.55)))); + } + + public Object[] get(String id) { + Object[] entry = list(list("'javaClass", "services.Item"), list("'name", "Apple"), list("'currencyCode", "USD"), list("'currencySymbol", "$"), list("'price", 2.99)); + return list("Item", id, entry); + } + + public String post(Object[] item) { + return "123456789"; + } + + public Boolean put(String id, Object[] entry) { + return true; + } + + public Boolean deleteall() { + return true; + } + + public Boolean delete(String id) { + return true; + } +} diff --git a/sca-cpp/trunk/test/store-java/htdocs/.htaccess b/sca-cpp/trunk/test/store-java/htdocs/.htaccess new file mode 100644 index 0000000000..e2e343b6b2 --- /dev/null +++ b/sca-cpp/trunk/test/store-java/htdocs/.htaccess @@ -0,0 +1,19 @@ +# +# 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. + +DirectoryIndex store.html diff --git a/sca-cpp/trunk/test/store-java/htdocs/store.html b/sca-cpp/trunk/test/store-java/htdocs/store.html new file mode 100644 index 0000000000..21eabca7a7 --- /dev/null +++ b/sca-cpp/trunk/test/store-java/htdocs/store.html @@ -0,0 +1,169 @@ + + + +Store + + + + + + + + +

Store

+
+

Catalog

+
+
+
+ +
+ +
+ +

Your Shopping Cart

+
+
+
+
+
+ + + (feed) +
+
+ + diff --git a/sca-cpp/trunk/test/store-java/htdocs/store.js b/sca-cpp/trunk/test/store-java/htdocs/store.js new file mode 100644 index 0000000000..9cd8eb526d --- /dev/null +++ b/sca-cpp/trunk/test/store-java/htdocs/store.js @@ -0,0 +1,661 @@ + +/* Apache Tuscany SCA Widget header */ + +/* + * JSON-RPC JavaScript client + * + * $Id: jsonrpc.js,v 1.36.2.3 2006/03/08 15:09:37 mclark Exp $ + * + * Copyright (c) 2003-2004 Jan-Klaas Kollhof + * Copyright (c) 2005 Michael Clark, Metaparadigm Pte Ltd + * + * This code is based on Jan-Klaas' JavaScript o lait library (jsolait). + * + * Licensed 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. + * + */ + +/* + * Modifications for Apache Tuscany: + * - JSONRpcClient_createMethod changed so callback is last arg + */ + +/* escape a character */ + +escapeJSONChar = +function escapeJSONChar(c) +{ + if(c == "\"" || c == "\\") return "\\" + c; + else if (c == "\b") return "\\b"; + else if (c == "\f") return "\\f"; + else if (c == "\n") return "\\n"; + else if (c == "\r") return "\\r"; + else if (c == "\t") return "\\t"; + var hex = c.charCodeAt(0).toString(16); + if(hex.length == 1) return "\\u000" + hex; + else if(hex.length == 2) return "\\u00" + hex; + else if(hex.length == 3) return "\\u0" + hex; + else return "\\u" + hex; +}; + + +/* encode a string into JSON format */ + +escapeJSONString = +function escapeJSONString(s) +{ + /* The following should suffice but Safari's regex is b0rken + (doesn't support callback substitutions) + return "\"" + s.replace(/([^\u0020-\u007f]|[\\\"])/g, + escapeJSONChar) + "\""; + */ + + /* Rather inefficient way to do it */ + var parts = s.split(""); + for(var i=0; i < parts.length; i++) { + var c =parts[i]; + if(c == '"' || + c == '\\' || + c.charCodeAt(0) < 32 || + c.charCodeAt(0) >= 128) + parts[i] = escapeJSONChar(parts[i]); + } + return "\"" + parts.join("") + "\""; +}; + + +/* Marshall objects to JSON format */ + +toJSON = function toJSON(o) +{ + if(o == null) { + return "null"; + } else if(o.constructor == String) { + return escapeJSONString(o); + } else if(o.constructor == Number) { + return o.toString(); + } else if(o.constructor == Boolean) { + return o.toString(); + } else if(o.constructor == Date) { + return '{javaClass: "java.util.Date", time: ' + o.valueOf() +'}'; + } else if(o.constructor == Array) { + var v = []; + for(var i = 0; i < o.length; i++) v.push(toJSON(o[i])); + return "[" + v.join(", ") + "]"; + } else { + var v = []; + for(attr in o) { + if(o[attr] == null) v.push("\"" + attr + "\": null"); + else if(typeof o[attr] == "function"); /* skip */ + else v.push(escapeJSONString(attr) + ": " + toJSON(o[attr])); + } + return "{" + v.join(", ") + "}"; + } +}; + + +/* JSONRpcClient constructor */ + +JSONRpcClient = +function JSONRpcClient_ctor(serverURL, user, pass, objectID) +{ + this.serverURL = serverURL; + this.user = user; + this.pass = pass; + this.objectID = objectID; + + /* Add standard methods */ + if(this.objectID) { + this._addMethods(["listMethods"]); + var req = this._makeRequest("listMethods", []); + } else { + this._addMethods(["system.listMethods"]); + var req = this._makeRequest("system.listMethods", []); + } + var m = this._sendRequest(req); + this._addMethods(m); +}; + + +/* JSONRpcCLient.Exception */ + +JSONRpcClient.Exception = +function JSONRpcClient_Exception_ctor(code, message, javaStack) +{ + this.code = code; + var name; + if(javaStack) { + this.javaStack = javaStack; + var m = javaStack.match(/^([^:]*)/); + if(m) name = m[0]; + } + if(name) this.name = name; + else this.name = "JSONRpcClientException"; + this.message = message; +}; + +JSONRpcClient.Exception.CODE_REMOTE_EXCEPTION = 490; +JSONRpcClient.Exception.CODE_ERR_CLIENT = 550; +JSONRpcClient.Exception.CODE_ERR_PARSE = 590; +JSONRpcClient.Exception.CODE_ERR_NOMETHOD = 591; +JSONRpcClient.Exception.CODE_ERR_UNMARSHALL = 592; +JSONRpcClient.Exception.CODE_ERR_MARSHALL = 593; + +JSONRpcClient.Exception.prototype = new Error(); + +JSONRpcClient.Exception.prototype.toString = +function JSONRpcClient_Exception_toString(code, msg) +{ + return this.name + ": " + this.message; +}; + + +/* Default top level exception handler */ + +JSONRpcClient.default_ex_handler = +function JSONRpcClient_default_ex_handler(e) { alert(e); }; + + +/* Client settable variables */ + +JSONRpcClient.toplevel_ex_handler = JSONRpcClient.default_ex_handler; +JSONRpcClient.profile_async = false; +JSONRpcClient.max_req_active = 1; +JSONRpcClient.requestId = 1; + + +/* JSONRpcClient implementation */ + +JSONRpcClient.prototype._createMethod = +function JSONRpcClient_createMethod(methodName) +{ + var fn=function() + { + var args = []; + var callback = null; + for(var i=0;i 0) { + var res = JSONRpcClient.async_responses.shift(); + if(res.canceled) continue; + if(res.profile) res.profile.dispatch = new Date(); + try { + res.cb(res.result, res.ex, res.profile); + } catch(e) { + JSONRpcClient.toplevel_ex_handler(e); + } + } + + while(JSONRpcClient.async_requests.length > 0 && + JSONRpcClient.num_req_active < JSONRpcClient.max_req_active) { + var req = JSONRpcClient.async_requests.shift(); + if(req.canceled) continue; + req.client._sendRequest.call(req.client, req); + } +}; + +JSONRpcClient.kick_async = +function JSONRpcClient_kick_async() +{ + if(JSONRpcClient.async_timeout == null) + JSONRpcClient.async_timeout = + setTimeout(JSONRpcClient._async_handler, 0); +}; + +JSONRpcClient.cancelRequest = +function JSONRpcClient_cancelRequest(requestId) +{ + /* If it is in flight then mark it as canceled in the inflight map + and the XMLHttpRequest callback will discard the reply. */ + if(JSONRpcClient.async_inflight[requestId]) { + JSONRpcClient.async_inflight[requestId].canceled = true; + return true; + } + + /* If its not in flight yet then we can just mark it as canceled in + the the request queue and it will get discarded before being sent. */ + for(var i in JSONRpcClient.async_requests) { + if(JSONRpcClient.async_requests[i].requestId == requestId) { + JSONRpcClient.async_requests[i].canceled = true; + return true; + } + } + + /* It may have returned from the network and be waiting for its callback + to be dispatched, so mark it as canceled in the response queue + and the response will get discarded before calling the callback. */ + for(var i in JSONRpcClient.async_responses) { + if(JSONRpcClient.async_responses[i].requestId == requestId) { + JSONRpcClient.async_responses[i].canceled = true; + return true; + } + } + + return false; +}; + +JSONRpcClient.prototype._makeRequest = +function JSONRpcClient_makeRequest(methodName, args, cb) +{ + var req = {}; + req.client = this; + req.requestId = JSONRpcClient.requestId++; + + var obj = {}; + obj.id = req.requestId; + if (this.objectID) + obj.method = ".obj#" + this.objectID + "." + methodName; + else + obj.method = methodName; + obj.params = args; + + if (cb) req.cb = cb; + if (JSONRpcClient.profile_async) + req.profile = { "submit": new Date() }; + req.data = toJSON(obj); + + return req; +}; + +JSONRpcClient.prototype._sendRequest = +function JSONRpcClient_sendRequest(req) +{ + if(req.profile) req.profile.start = new Date(); + + /* Get free http object from the pool */ + var http = JSONRpcClient.poolGetHTTPRequest(); + JSONRpcClient.num_req_active++; + + /* Send the request */ + if (typeof(this.user) == "undefined") { + http.open("POST", this.serverURL, (req.cb != null)); + } else { + http.open("POST", this.serverURL, (req.cb != null), this.user, this.pass); + } + + /* setRequestHeader is missing in Opera 8 Beta */ + try { http.setRequestHeader("Content-type", "text/plain"); } catch(e) {} + + /* Construct call back if we have one */ + if(req.cb) { + var self = this; + http.onreadystatechange = function() { + if(http.readyState == 4) { + http.onreadystatechange = function () {}; + var res = { "cb": req.cb, "result": null, "ex": null}; + if (req.profile) { + res.profile = req.profile; + res.profile.end = new Date(); + } + try { res.result = self._handleResponse(http); } + catch(e) { res.ex = e; } + if(!JSONRpcClient.async_inflight[req.requestId].canceled) + JSONRpcClient.async_responses.push(res); + delete JSONRpcClient.async_inflight[req.requestId]; + JSONRpcClient.kick_async(); + } + }; + } else { + http.onreadystatechange = function() {}; + } + + JSONRpcClient.async_inflight[req.requestId] = req; + + try { + http.send(req.data); + } catch(e) { + JSONRpcClient.poolReturnHTTPRequest(http); + JSONRpcClient.num_req_active--; + throw new JSONRpcClient.Exception + (JSONRpcClient.Exception.CODE_ERR_CLIENT, "Connection failed"); + } + + if(!req.cb) return this._handleResponse(http); +}; + +JSONRpcClient.prototype._handleResponse = +function JSONRpcClient_handleResponse(http) +{ + /* Get the charset */ + if(!this.charset) { + this.charset = JSONRpcClient._getCharsetFromHeaders(http); + } + + /* Get request results */ + var status, statusText, data; + try { + status = http.status; + statusText = http.statusText; + data = http.responseText; + } catch(e) { + JSONRpcClient.poolReturnHTTPRequest(http); + JSONRpcClient.num_req_active--; + JSONRpcClient.kick_async(); + throw new JSONRpcClient.Exception + (JSONRpcClient.Exception.CODE_ERR_CLIENT, "Connection failed"); + } + + /* Return http object to the pool; */ + JSONRpcClient.poolReturnHTTPRequest(http); + JSONRpcClient.num_req_active--; + + /* Unmarshall the response */ + if(status != 200) { + throw new JSONRpcClient.Exception(status, statusText); + } + var obj; + try { + eval("obj = " + data); + } catch(e) { + throw new JSONRpcClient.Exception(550, "error parsing result"); + } + if(obj.error) + throw new JSONRpcClient.Exception(obj.error.code, obj.error.msg, + obj.error.trace); + var res = obj.result; + + /* Handle CallableProxy */ + if(res && res.objectID && res.JSONRPCType == "CallableReference") + return new JSONRpcClient(this.serverURL, this.user, + this.pass, res.objectID); + + return res; +}; + + +/* XMLHttpRequest wrapper code */ + +/* XMLHttpRequest pool globals */ +JSONRpcClient.http_spare = []; +JSONRpcClient.http_max_spare = 8; + +JSONRpcClient.poolGetHTTPRequest = +function JSONRpcClient_pool_getHTTPRequest() +{ + if(JSONRpcClient.http_spare.length > 0) { + return JSONRpcClient.http_spare.pop(); + } + return JSONRpcClient.getHTTPRequest(); +}; + +JSONRpcClient.poolReturnHTTPRequest = +function JSONRpcClient_poolReturnHTTPRequest(http) +{ + if(JSONRpcClient.http_spare.length >= JSONRpcClient.http_max_spare) + delete http; + else + JSONRpcClient.http_spare.push(http); +}; + +JSONRpcClient.msxmlNames = [ "MSXML2.XMLHTTP.5.0", + "MSXML2.XMLHTTP.4.0", + "MSXML2.XMLHTTP.3.0", + "MSXML2.XMLHTTP", + "Microsoft.XMLHTTP" ]; + +JSONRpcClient.getHTTPRequest = +function JSONRpcClient_getHTTPRequest() +{ + /* Mozilla XMLHttpRequest */ + try { + JSONRpcClient.httpObjectName = "XMLHttpRequest"; + return new XMLHttpRequest(); + } catch(e) {} + + /* Microsoft MSXML ActiveX */ + for (var i=0;i < JSONRpcClient.msxmlNames.length; i++) { + try { + JSONRpcClient.httpObjectName = JSONRpcClient.msxmlNames[i]; + return new ActiveXObject(JSONRpcClient.msxmlNames[i]); + } catch (e) {} + } + + /* None found */ + JSONRpcClient.httpObjectName = null; + throw new JSONRpcClient.Exception(0, "Can't create XMLHttpRequest object"); +}; + + +/* + * 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. + */ + +function AtomClient(uri) { + + this.msxmlNames = [ "MSXML2.XMLHTTP.5.0", + "MSXML2.XMLHTTP.4.0", + "MSXML2.XMLHTTP.3.0", + "MSXML2.XMLHTTP", + "Microsoft.XMLHTTP" ]; + + this.uri=uri; + + this.get = function(id, responseFunction) { + var xhr = this.createXMLHttpRequest(); + xhr.onreadystatechange = function() { + if (xhr.readyState == 4) { + if (xhr.status == 200) { + var strDocument = xhr.responseText; + var xmlDocument = xhr.responseXML; + if(!xmlDocument || xmlDocument.childNodes.length==0){ + xmlDocument = (new DOMParser()).parseFromString(strDocument, "text/xml"); + } + if (responseFunction != null) responseFunction(xmlDocument); + } else { + alert("get - Error getting data from the server"); + } + } + } + xhr.open("GET", uri + '/' + id, true); + xhr.send(null); + } + + this.post = function (entry, responseFunction) { + var xhr = this.createXMLHttpRequest(); + xhr.onreadystatechange = function() { + if (xhr.readyState == 4) { + if (xhr.status == 201) { + var strDocument = xhr.responseText; + var xmlDocument = xhr.responseXML; + if(!xmlDocument || xmlDocument.childNodes.length==0){ + xmlDocument = (new DOMParser()).parseFromString(strDocument, "text/xml"); + } + if (responseFunction != null) responseFunction(xmlDocument); + } else { + alert("post - Error getting data from the server"); + } + } + } + xhr.open("POST", uri, true); + xhr.setRequestHeader("Content-Type", "application/atom+xml"); + xhr.send(entry); + } + + this.put = function (id, entry, responseFunction) { + var xhr = this.createXMLHttpRequest(); + xhr.onreadystatechange = function() { + if (xhr.readyState == 4) { + if (xhr.status == 200) { + var strDocument = xhr.responseText; + var xmlDocument = xhr.responseXML; + if(!xmlDocument || xmlDocument.childNodes.length==0){ + xmlDocument = (new DOMParser()).parseFromString(strDocument, "text/xml"); + } + if (responseFunction != null) responseFunction(xmlDocument); + } else { + alert("put - Error getting data from the server"); + } + } + } + xhr.open("PUT", uri + '/' + id, true); + xhr.setRequestHeader("Content-Type", "application/atom+xml"); + xhr.send(entry); + } + + this.del = function (id, responseFunction) { + var xhr = this.createXMLHttpRequest(); + xhr.onreadystatechange = function() { + if (xhr.readyState == 4) { + if (xhr.status == 200) { + if (responseFunction != null) responseFunction(); + } else { + alert("delete - Error getting data from the server"); + } + } + } + xhr.open("DELETE", uri + '/' + id, true); + xhr.send(null); + } + this.createXMLHttpRequest = function () { + /* Mozilla XMLHttpRequest */ + try {return new XMLHttpRequest();} catch(e) {} + + /* Microsoft MSXML ActiveX */ + for (var i=0;i < this.msxmlNames.length; i++) { + try {return new ActiveXObject(this.msxmlNames[i]);} catch (e) {} + } + alert("XML http request not supported"); + return null; + } + if (typeof DOMParser == "undefined") { + DOMParser = function () {} + + DOMParser.prototype.parseFromString = function (str, contentType) { + if (typeof ActiveXObject != "undefined") { + var d = new ActiveXObject("MSXML.DomDocument"); + d.loadXML(str); + return d; + } else if (typeof XMLHttpRequest != "undefined") { + var req = new XMLHttpRequest; + req.open("GET", "data:" + (contentType || "application/xml") + + ";charset=utf-8," + encodeURIComponent(str), false); + if (req.overrideMimeType) { + req.overrideMimeType(contentType); + } + req.send(null); + return req.responseXML; + } + } + } +} + + + +/* Tuscany Reference/Property injection code */ + +if (!tuscany) { +var tuscany = {}; +} +if (!tuscany.sca) { +tuscany.sca = {}; +} + +tuscany.sca.propertyMap = new String(); +tuscany.sca.Property = function (name) { + return tuscany.sca.propertyMap[name]; +} + +tuscany.sca.referenceMap = new Object(); +tuscany.sca.referenceMap.catalog = new JSONRpcClient("/catalog").Service; +tuscany.sca.referenceMap.shoppingCart = new AtomClient("/shoppingCart"); +tuscany.sca.referenceMap.shoppingTotal = new JSONRpcClient("/total").Service; +tuscany.sca.Reference = function (name) { + return tuscany.sca.referenceMap[name]; +} + +/** End of Apache Tuscany SCA Widget */ + diff --git a/sca-cpp/trunk/test/store-java/store.composite b/sca-cpp/trunk/test/store-java/store.composite new file mode 100644 index 0000000000..aecd1e8805 --- /dev/null +++ b/sca-cpp/trunk/test/store-java/store.composite @@ -0,0 +1,69 @@ + + + + + + + + + + + + + + + + + USD + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + -- cgit v1.2.3