diff options
author | jsdelfino <jsdelfino@13f79535-47bb-0310-9956-ffa450edef68> | 2010-01-17 09:02:39 +0000 |
---|---|---|
committer | jsdelfino <jsdelfino@13f79535-47bb-0310-9956-ffa450edef68> | 2010-01-17 09:02:39 +0000 |
commit | e34c4a9109bbbb28d7ecef7cccccdc537008f8a5 (patch) | |
tree | f1d946b5401763a306068cc7bbfad4ac7d489d8e /sca-cpp | |
parent | b00fe2747627bc95ac8d1d548e0621930a1775a2 (diff) |
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
Diffstat (limited to 'sca-cpp')
-rw-r--r-- | sca-cpp/trunk/modules/java/client-test.cpp | 46 | ||||
-rw-r--r-- | sca-cpp/trunk/modules/java/domain-test.composite | 42 | ||||
-rw-r--r-- | sca-cpp/trunk/modules/java/eval.hpp | 132 | ||||
-rwxr-xr-x | sca-cpp/trunk/modules/java/java-conf | 26 | ||||
-rw-r--r-- | sca-cpp/trunk/modules/java/java-test.cpp | 3 | ||||
-rw-r--r-- | sca-cpp/trunk/modules/java/mod-java.cpp | 54 | ||||
-rw-r--r-- | sca-cpp/trunk/modules/java/mod-java.hpp | 78 | ||||
-rw-r--r-- | sca-cpp/trunk/modules/java/org/apache/tuscany/InvocationHandler.java | 4 | ||||
-rw-r--r-- | sca-cpp/trunk/modules/java/org/apache/tuscany/Service.java | 38 | ||||
-rw-r--r-- | sca-cpp/trunk/modules/java/test/Client.java | 38 | ||||
-rw-r--r-- | sca-cpp/trunk/modules/java/test/ClientImpl.java | 52 | ||||
-rw-r--r-- | sca-cpp/trunk/modules/java/test/Server.java | 38 | ||||
-rw-r--r-- | sca-cpp/trunk/modules/java/test/ServerImpl.java | 59 | ||||
-rw-r--r-- | sca-cpp/trunk/test/store-java/htdocs/.htaccess | 19 | ||||
-rw-r--r-- | sca-cpp/trunk/test/store-java/htdocs/store.html | 169 | ||||
-rw-r--r-- | sca-cpp/trunk/test/store-java/htdocs/store.js | 661 | ||||
-rw-r--r-- | sca-cpp/trunk/test/store-java/store.composite | 69 |
17 files changed, 1475 insertions, 53 deletions
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 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. +--> +<composite xmlns="http://docs.oasis-open.org/ns/opencsa/sca/200903" + xmlns:t="http://tuscany.apache.org/xmlns/sca/1.1" + targetNamespace="http://domain/test" + name="domain-test"> + + <component name="java-test"> + <implementation.java class="test.ServerImpl"/> + <service name="test"> + <t:binding.http uri="java"/> + </service> + </component> + + <component name="client-test"> + <implementation.java class="test.ClientImpl"/> + <service name="client"> + <t:binding.http uri="client"/> + </service> + <reference name="ref" target="java-test"> + <t:binding.http/> + </reference> + </component> + +</composite> 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<char*>("-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<char*>("-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<char*>("invoke"); + nm.signature = const_cast<char*>("(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<char*>("invoke"); - nm.signature = const_cast<char*>("(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<value>& v); const list<value> jarrayToValues(const JavaRuntime& jr, const jobjectArray o); @@ -156,6 +164,22 @@ public: javaLambda(const JavaRuntime& jr, const value& iface, const lambda<value(const list<value>&)>& func) : jr(jr), iface(iface), func(func) { } + const value operator()(const list<value>& 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<value(const list<value>&)> func; @@ -182,23 +206,18 @@ jobject JNICALL nativeInvoke(JNIEnv* env, jobject self, unused jobject proxy, jo const list<value> expr = cons<value>(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<value(const list<value>&)>& 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<value>("interface")); +/** + * Convert a lambda function to Java proxy. + */ +const jobject mkJavaLambda(const JavaRuntime& jr, unused const value& iface, const lambda<value(const list<value>&)>& l) { const gc_ptr<javaLambda> jl = new (gc_new<javaLambda>()) 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<value(const list< const jobjectArray valuesToJarrayHelper(const JavaRuntime& jr, jobjectArray a, const list<value>& 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<value>& 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<value>& v) { +const jvalue* valuesToJvaluesHelper(const JavaRuntime& jr, jvalue* a, const list<value>& types, const list<value>& 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<value>& v) { +const jvalue* valuesToJvalues(const JavaRuntime& jr, const list<value>& types, const list<value>& v) { const int n = length(v); jvalue* a = new (gc_anew<jvalue>(n)) jvalue[n]; - valuesToJvaluesHelper(jr, a, v); + valuesToJvaluesHelper(jr, a, types, v); return a; } @@ -267,6 +286,8 @@ const list<value> jarrayToValuesHelper(const JavaRuntime& jr, jobjectArray a, co } const list<value> jarrayToValues(const JavaRuntime& jr, jobjectArray o) { + if (o == NULL) + return list<value>(); 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<value> parameterTypesToValues(const JavaRuntime& jr, const jobjectArray t, const int i) { + if (i == 0) + return list<value>(); + return cons<value>(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<value>(c_str(name), (double)(long)mid); + + const jobjectArray t = (jobjectArray)jr.env->CallObjectMethod(m, jr.parameterTypes); + const list<value> types = reverse(parameterTypesToValues(jr, t, jr.env->GetArrayLength(t))); + + return cons<value>(c_str(name), cons<value>((double)(long)mid, types)); } const list<value> methodsToValues(const JavaRuntime& jr, const jobjectArray m, const int i) { @@ -382,7 +418,7 @@ const failable<value> 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<value>(expr)); + const jvalue *args = valuesToJvalues(jr, cddr(func), cdr<value>(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 <<EOF +LoadModule mod_tuscany_eval $here/.libs/libmod_tuscany_java.so +EOF diff --git a/sca-cpp/trunk/modules/java/java-test.cpp b/sca-cpp/trunk/modules/java/java-test.cpp index e50cfe5671..7ece2dfc68 100644 --- a/sca-cpp/trunk/modules/java/java-test.cpp +++ b/sca-cpp/trunk/modules/java/java-test.cpp @@ -53,9 +53,6 @@ bool testEvalExpr() { } const value add(const list<value>& 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<lambda<value(const list<value>&)> > evalImplementation(const string& path, const value& impl, const list<value>& 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<lambda<value(const list<value>&)> >(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<value> px; + applyImplementation(const java::JavaClass& impl, const list<value>& px) : impl(impl), px(px) { + } + const value operator()(const list<value>& params) const { + const value expr = append<value>(params, px); + debug(expr, "modeval::java::applyImplementation::input"); + const failable<value> val = java::evalClass(java::javaRuntime, expr, impl); + debug(val, "modeval::java::applyImplementation::result"); + if (!hasContent(val)) + return mklist<value>(value(), reason(val)); + return mklist<value>(content(val)); + } +}; + +/** + * Evaluate a Java component implementation and convert it to an applicable + * lambda function. + */ +const failable<lambda<value(const list<value>&)> > evalImplementation(unused const string& path, const value& impl, const list<value>& px) { + const string cn(attributeValue("class", impl)); + const failable<java::JavaClass> jc = java::readClass(java::javaRuntime, cn); + if (!hasContent(jc)) + return mkfailure<lambda<value(const list<value>&)> >(reason(jc)); + return lambda<value(const list<value>&)>(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> 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 @@ +<!--
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+-->
+<html>
+<head>
+<title>Store</title>
+
+<script type="text/javascript" src="store.js"></script>
+
+<script language="JavaScript">
+
+ //@Reference
+ var catalog = new tuscany.sca.Reference("catalog");
+
+ //@Reference
+ var shoppingCart = new tuscany.sca.Reference("shoppingCart");
+
+ //@Reference
+ var shoppingTotal = new tuscany.sca.Reference("shoppingTotal");
+
+ var catalogItems;
+
+ function catalog_getResponse(items,exception) {
+ if(exception){
+ alert(exception.message);
+ return;
+ }
+ var catalog = "";
+
+ for (var i=0; i<items.length; i++) {
+ var item = items[i].name + ' - ' + items[i].price;
+ catalog += '<input name="items" type="checkbox" value="' +
+ item + '">' + item + ' <br>';
+ }
+ document.getElementById('catalog').innerHTML=catalog;
+ catalogItems = items;
+
+ // TEMP
+ shoppingTotal.gettotal(shoppingTotal_getTotalResponse);
+ }
+
+ function shoppingCart_getResponse(feed) {
+ if (feed != null) {
+ var entries = feed.getElementsByTagName("entry");
+ var list = "";
+ for (var i=0; i<entries.length; i++) {
+ var content = entries[i].getElementsByTagName("content")[0];
+ var name = content.getElementsByTagName("name")[0].firstChild.nodeValue;
+ var price = content.getElementsByTagName("price")[0].firstChild.nodeValue;
+ list += name + ' - ' + price + ' <br>';
+ }
+ document.getElementById("shoppingCart").innerHTML = list;
+
+ if (entries.length != 0) {
+ try {
+ shoppingTotal.gettotal(shoppingTotal_getTotalResponse);
+ }
+ catch(e){
+ alert(e);
+ }
+ }
+ }
+ }
+
+ function shoppingTotal_getTotalResponse(total,exception) {
+ if(exception) {
+ alert(exception.message);
+ return;
+ }
+ document.getElementById('total').innerHTML = total;
+ }
+
+ function shoppingCart_postResponse(entry) {
+ shoppingCart.get("", shoppingCart_getResponse);
+ }
+
+ function addToCart() {
+ var items = document.catalogForm.items;
+ var j = 0;
+ for (var i=0; i<items.length; i++)
+ if (items[i].checked) {
+ var entry = '<entry xmlns="http://www.w3.org/2005/Atom"><title type="text">Item</title><content type="application/xml">' +
+ '<item>' +
+ '<javaClass>' + catalogItems[i].javaClass + '</javaClass>' +
+ '<name>' + catalogItems[i].name + '</name>' +
+ '<currencyCode>' + catalogItems[i].currencyCode + '</currencyCode>' +
+ '<currencySymbol>' + catalogItems[i].currencySymbol + '</currencySymbol>' +
+ '<price>' + catalogItems[i].price + '</price>' +
+ '</item>' +
+ '</content></entry>';
+ shoppingCart.post(entry, shoppingCart_postResponse);
+ items[i].checked = false;
+ }
+ }
+ function checkoutCart() {
+ document.getElementById('store').innerHTML='<h2>' +
+ 'Thanks for Shopping With Us!</h2>'+
+ '<h2>Your Order</h2>'+
+ '<form name="orderForm">'+
+ document.getElementById('shoppingCart').innerHTML+
+ '<br>'+
+ document.getElementById('total').innerHTML+
+ '<br>'+
+ '<br>'+
+ '<input type="submit" value="Continue Shopping">'+
+ '</form>';
+ shoppingCart.del("", null);
+ }
+ function deleteCart() {
+ shoppingCart.del("", null);
+ document.getElementById('shoppingCart').innerHTML = "";
+ document.getElementById('total').innerHTML = "";
+ }
+
+ function init() {
+
+ try {
+ catalog.get(catalog_getResponse);
+ shoppingCart.get("", shoppingCart_getResponse);
+ }
+ catch(e){
+ alert(e);
+ }
+ }
+
+</script>
+
+</head>
+
+<body onload="init()">
+<h1>Store</h1>
+ <div id="store">
+ <h2>Catalog</h2>
+ <form name="catalogForm">
+ <div id="catalog" ></div>
+ <br>
+ <input type="button" onClick="addToCart()" value="Add to Cart">
+ </form>
+
+ <br>
+
+ <h2>Your Shopping Cart</h2>
+ <form name="shoppingCartForm">
+ <div id="shoppingCart"></div>
+ <br>
+ <div id="total"></div>
+ <br>
+ <input type="button" onClick="checkoutCart()" value="Checkout">
+ <input type="button" onClick="deleteCart()" value="Empty">
+ <a href="../shoppingCart/">(feed)</a>
+ </form>
+ </div>
+</body>
+</html>
diff --git a/sca-cpp/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<arguments.length;i++) args.push(arguments[i]); + +/* TUSCANY change callback to be last arg instead of first to match binding.ajax + if(typeof args[0] == "function") callback = args.shift(); +*/ + if(typeof args[arguments.length-1] == "function") callback = args.pop(); + + var req = fn.client._makeRequest.call(fn.client, fn.methodName, + args, callback); + if(callback == null) { + return fn.client._sendRequest.call(fn.client, req); + } else { + JSONRpcClient.async_requests.push(req); + JSONRpcClient.kick_async(); + return req.requestId; + } + }; + fn.client = this; + fn.methodName = methodName; + return fn; +}; + +JSONRpcClient.prototype._addMethods = +function JSONRpcClient_addMethods(methodNames) +{ + for(var i=0; i<methodNames.length; i++) { + var obj = this; + var names = methodNames[i].split("."); + for(var n=0; n<names.length-1; n++) { + var name = names[n]; + if(obj[name]) { + obj = obj[name]; + } else { + obj[name] = new Object(); + obj = obj[name]; + } + } + var name = names[names.length-1]; + if(!obj[name]) { + var method = this._createMethod(methodNames[i]); + obj[name] = method; + } + } +}; + +JSONRpcClient._getCharsetFromHeaders = +function JSONRpcClient_getCharsetFromHeaders(http) +{ + try { + var contentType = http.getResponseHeader("Content-type"); + var parts = contentType.split(/\s*;\s*/); + for(var i =0; i < parts.length; i++) { + if(parts[i].substring(0, 8) == "charset=") + return parts[i].substring(8, parts[i].length); + } + } catch (e) {} + return "UTF-8"; /* default */ +}; + +/* Async queue globals */ +JSONRpcClient.async_requests = []; +JSONRpcClient.async_inflight = {}; +JSONRpcClient.async_responses = []; +JSONRpcClient.async_timeout = null; +JSONRpcClient.num_req_active = 0; + +JSONRpcClient._async_handler = +function JSONRpcClient_async_handler() +{ + JSONRpcClient.async_timeout = null; + + while(JSONRpcClient.async_responses.length > 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 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. +--> +<composite xmlns="http://docs.oasis-open.org/ns/opencsa/sca/200903" + xmlns:t="http://tuscany.apache.org/xmlns/sca/1.1" + targetNamespace="http://store" + name="store"> + + <component name="Store"> + <implementation.java class="store.StoreImpl"/> + <service name="Widget"> + <t:binding.http uri="store"/> + </service> + <reference name="catalog" target="Catalog"/> + <reference name="shoppingCart" target="ShoppingCart/Cart"/> + <reference name="shoppingTotal" target="ShoppingCart/Total"/> + </component> + + <component name="Catalog"> + <implementation.java class="store.FruitsCatalogImpl"/> + <property name="currencyCode">USD</property> + <service name="Catalog"> + <t:binding.jsonrpc uri="catalog"/> + </service> + <reference name="currencyConverter" target="CurrencyConverter"/> + </component> + + <component name="ShoppingCart"> + <implementation.java class="store.ShoppingCartImpl"/> + <service name="ShoppingCart"> + <t:binding.atom uri="shoppingCart"/> + </service> + <service name="Total"> + <t:binding.jsonrpc uri="total"/> + </service> + <reference name="cache" target="Cache"/> + </component> + + <component name="CurrencyConverter"> + <implementation.java class="store.CurrencyConverterImpl"/> + <service name="CurrencyConverter"> + <t:binding.jsonrpc uri="currencyConverter"/> + </service> + </component> + + <component name="Cache"> + <implementation.cpp path="../../components/cache/.libs" library="libmcache"/> + <service name="Cache"> + <t:binding.atom uri="cache"/> + </service> + </component> + +</composite> |