diff options
author | lresende <lresende@13f79535-47bb-0310-9956-ffa450edef68> | 2009-11-11 23:06:42 +0000 |
---|---|---|
committer | lresende <lresende@13f79535-47bb-0310-9956-ffa450edef68> | 2009-11-11 23:06:42 +0000 |
commit | f860c2f35b2f94e379d2ff7d5c13f54cd4a3132a (patch) | |
tree | 5808b087a5eb9f5d251932d6828565f6c5d4ed9e /sca-java-1.x/branches/sca-java-0.90/modules/binding-jsonrpc/src/main/java/org/apache/tuscany/sca | |
parent | 587877fcbd358e233f653e01c4b3ed3354203626 (diff) |
Moving 1.x branches
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@835119 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'sca-java-1.x/branches/sca-java-0.90/modules/binding-jsonrpc/src/main/java/org/apache/tuscany/sca')
13 files changed, 844 insertions, 0 deletions
diff --git a/sca-java-1.x/branches/sca-java-0.90/modules/binding-jsonrpc/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/DefaultJSONRPCBindingFactory.java b/sca-java-1.x/branches/sca-java-0.90/modules/binding-jsonrpc/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/DefaultJSONRPCBindingFactory.java new file mode 100644 index 0000000000..31575518f4 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-0.90/modules/binding-jsonrpc/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/DefaultJSONRPCBindingFactory.java @@ -0,0 +1,35 @@ +/* + * 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.sca.binding.jsonrpc; + +import org.apache.tuscany.sca.binding.jsonrpc.impl.JSONRPCBindingImpl; + +/** + * A factory for the JSONRPC binding model. + * + * @version $Rev$ $Date$ + */ +public class DefaultJSONRPCBindingFactory implements JSONRPCBindingFactory { + + public JSONRPCBinding createJSONRPCBinding() { + return new JSONRPCBindingImpl(); + } + +} diff --git a/sca-java-1.x/branches/sca-java-0.90/modules/binding-jsonrpc/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/JSONRPCBinding.java b/sca-java-1.x/branches/sca-java-0.90/modules/binding-jsonrpc/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/JSONRPCBinding.java new file mode 100644 index 0000000000..5e0e3b5b28 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-0.90/modules/binding-jsonrpc/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/JSONRPCBinding.java @@ -0,0 +1,30 @@ +/* + * 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.sca.binding.jsonrpc; + +import org.apache.tuscany.sca.assembly.Binding; + +/** + * A model for the sample JSONRPC binding. + * + * @version $Rev$ $Date$ + */ +public interface JSONRPCBinding extends Binding { +} diff --git a/sca-java-1.x/branches/sca-java-0.90/modules/binding-jsonrpc/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/JSONRPCBindingFactory.java b/sca-java-1.x/branches/sca-java-0.90/modules/binding-jsonrpc/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/JSONRPCBindingFactory.java new file mode 100644 index 0000000000..7ba422eb85 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-0.90/modules/binding-jsonrpc/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/JSONRPCBindingFactory.java @@ -0,0 +1,35 @@ +/* + * 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.sca.binding.jsonrpc; + +/** + * A factory for the JSONRPC binding model. + * + * @version $Rev$ $Date$ + */ +public interface JSONRPCBindingFactory { + + /** + * Creates a new JSONRPC binding. + * + * @return a new JSONRPC binding + */ + JSONRPCBinding createJSONRPCBinding(); +} diff --git a/sca-java-1.x/branches/sca-java-0.90/modules/binding-jsonrpc/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/impl/JSONRPCBindingImpl.java b/sca-java-1.x/branches/sca-java-0.90/modules/binding-jsonrpc/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/impl/JSONRPCBindingImpl.java new file mode 100644 index 0000000000..66f6dd640d --- /dev/null +++ b/sca-java-1.x/branches/sca-java-0.90/modules/binding-jsonrpc/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/impl/JSONRPCBindingImpl.java @@ -0,0 +1,79 @@ +/* + * 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.sca.binding.jsonrpc.impl; + +import java.util.Collections; +import java.util.List; + +import org.apache.tuscany.sca.binding.jsonrpc.JSONRPCBinding; +import org.apache.tuscany.sca.policy.Intent; +import org.apache.tuscany.sca.policy.PolicySet; + + +/** + * Implementation of the JSONRPC binding model. + * + * @version $Rev$ $Date$ + */ +public class JSONRPCBindingImpl implements JSONRPCBinding { + + private String name; + private String uri; + + public String getName() { + return name; + } + + public String getURI() { + return uri; + } + + public void setName(String name) { + this.name = name; + } + + public void setURI(String uri) { + this.uri = uri; + } + + public List<PolicySet> getPolicySets() { + // The sample binding does not support policies + return Collections.emptyList(); + } + + public List<Intent> getRequiredIntents() { + // The sample binding does not support policies + return Collections.emptyList(); + } + + public List<Object> getExtensions() { + // The sample binding does not support extensions + return Collections.emptyList(); + } + + public boolean isUnresolved() { + return false; + } + + public void setUnresolved(boolean unresolved) { + // The sample binding is always resolved + } + +} diff --git a/sca-java-1.x/branches/sca-java-0.90/modules/binding-jsonrpc/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/impl/JSONRPCBindingProcessor.java b/sca-java-1.x/branches/sca-java-0.90/modules/binding-jsonrpc/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/impl/JSONRPCBindingProcessor.java new file mode 100644 index 0000000000..1ff888ace6 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-0.90/modules/binding-jsonrpc/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/impl/JSONRPCBindingProcessor.java @@ -0,0 +1,75 @@ +/* + * 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.sca.binding.jsonrpc.impl; + +import static org.osoa.sca.Constants.SCA_NS; + +import javax.xml.namespace.QName; +import javax.xml.stream.XMLStreamReader; +import javax.xml.stream.XMLStreamWriter; + +import org.apache.tuscany.sca.binding.jsonrpc.JSONRPCBinding; +import org.apache.tuscany.sca.binding.jsonrpc.JSONRPCBindingFactory; +import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor; +import org.apache.tuscany.sca.contribution.resolver.ModelResolver; +import org.apache.tuscany.sca.contribution.service.ContributionReadException; +import org.apache.tuscany.sca.contribution.service.ContributionResolveException; +import org.apache.tuscany.sca.contribution.service.ContributionWriteException; + + +/** + * A processor for <binding.jsonrpc> elements. + * + * @version $Rev$ $Date$ + */ +public class JSONRPCBindingProcessor implements StAXArtifactProcessor<JSONRPCBinding> { + + private QName BINDING_JSONRPC = new QName(SCA_NS, "binding.jsonrpc"); + + private final JSONRPCBindingFactory factory; + + public JSONRPCBindingProcessor(JSONRPCBindingFactory factory) { + this.factory = factory; + } + + public QName getArtifactType() { + return BINDING_JSONRPC; + } + + public Class<JSONRPCBinding> getModelType() { + return JSONRPCBinding.class; + } + + public JSONRPCBinding read(XMLStreamReader reader) throws ContributionReadException { + String uri = reader.getAttributeValue(null, "uri"); + JSONRPCBinding JSONRPCBinding = factory.createJSONRPCBinding(); + if (uri != null) { + JSONRPCBinding.setURI(uri.trim()); + } + return JSONRPCBinding; + } + + public void write(JSONRPCBinding JSONRPCBinding, XMLStreamWriter writer) throws ContributionWriteException { + } + + public void resolve(JSONRPCBinding JSONRPCBinding, ModelResolver resolver) throws ContributionResolveException { + } + +} diff --git a/sca-java-1.x/branches/sca-java-0.90/modules/binding-jsonrpc/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/module/JSONRPCModuleActivator.java b/sca-java-1.x/branches/sca-java-0.90/modules/binding-jsonrpc/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/module/JSONRPCModuleActivator.java new file mode 100644 index 0000000000..3475a31923 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-0.90/modules/binding-jsonrpc/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/module/JSONRPCModuleActivator.java @@ -0,0 +1,62 @@ +/* + * 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.sca.binding.jsonrpc.module; + +import org.apache.tuscany.sca.binding.jsonrpc.DefaultJSONRPCBindingFactory; +import org.apache.tuscany.sca.binding.jsonrpc.JSONRPCBindingFactory; +import org.apache.tuscany.sca.binding.jsonrpc.impl.JSONRPCBindingProcessor; +import org.apache.tuscany.sca.binding.jsonrpc.provider.JSONRPCBindingProviderFactory; +import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessorExtensionPoint; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.core.ModuleActivator; +import org.apache.tuscany.sca.http.ServletHost; +import org.apache.tuscany.sca.provider.ProviderFactoryExtensionPoint; + +/** + * A module activator for the JSONRPC binding extension. + * + * @version $Rev$ $Date$ + */ +public class JSONRPCModuleActivator implements ModuleActivator { + + public Object[] getExtensionPoints() { + // No extensionPoints being contributed here + return null; + } + + public void start(ExtensionPointRegistry registry) { + + // Create the JSONRPC model factory + JSONRPCBindingFactory JSONRPCFactory = new DefaultJSONRPCBindingFactory(); + + // Add the JSONRPCProcessor extension + StAXArtifactProcessorExtensionPoint processors = registry.getExtensionPoint(StAXArtifactProcessorExtensionPoint.class); + JSONRPCBindingProcessor JSONRPCBindingProcessor = new JSONRPCBindingProcessor(JSONRPCFactory); + processors.addArtifactProcessor(JSONRPCBindingProcessor); + + ProviderFactoryExtensionPoint providerFactories = registry.getExtensionPoint(ProviderFactoryExtensionPoint.class); + ServletHost servletHost = registry.getExtensionPoint(ServletHost.class); + providerFactories.addProviderFactory(new JSONRPCBindingProviderFactory(servletHost)); + } + + public void stop(ExtensionPointRegistry registry) { + } + +} diff --git a/sca-java-1.x/branches/sca-java-0.90/modules/binding-jsonrpc/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/provider/JSONRPCBindingInvoker.java b/sca-java-1.x/branches/sca-java-0.90/modules/binding-jsonrpc/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/provider/JSONRPCBindingInvoker.java new file mode 100644 index 0000000000..ade2603516 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-0.90/modules/binding-jsonrpc/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/provider/JSONRPCBindingInvoker.java @@ -0,0 +1,50 @@ +/* + * 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.sca.binding.jsonrpc.provider; + +import java.lang.reflect.InvocationTargetException; + +import org.apache.tuscany.sca.invocation.Invoker; +import org.apache.tuscany.sca.invocation.Message; + +/** + * Interceptor for the sample JSONRPC binding. + * + * @version $Rev$ $Date$ + */ +public class JSONRPCBindingInvoker implements Invoker { + + private Object echo(Object[] args) throws InvocationTargetException { + // echo back the result, a real binding would invoke some API for flowing the request + return args[0]; + } + + public Message invoke(Message msg) { + try { + Object resp = echo((Object[])msg.getBody()); + msg.setBody(resp); + } catch (InvocationTargetException e) { + msg.setFaultBody(e.getCause()); + } catch (Throwable e) { + msg.setFaultBody(e); + } + return msg; + } + +} diff --git a/sca-java-1.x/branches/sca-java-0.90/modules/binding-jsonrpc/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/provider/JSONRPCBindingProviderFactory.java b/sca-java-1.x/branches/sca-java-0.90/modules/binding-jsonrpc/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/provider/JSONRPCBindingProviderFactory.java new file mode 100644 index 0000000000..4bbf39e02c --- /dev/null +++ b/sca-java-1.x/branches/sca-java-0.90/modules/binding-jsonrpc/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/provider/JSONRPCBindingProviderFactory.java @@ -0,0 +1,57 @@ +/* + * 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.sca.binding.jsonrpc.provider; + +import org.apache.tuscany.sca.binding.jsonrpc.JSONRPCBinding; +import org.apache.tuscany.sca.http.ServletHost; +import org.apache.tuscany.sca.provider.BindingProviderFactory; +import org.apache.tuscany.sca.provider.ReferenceBindingProvider; +import org.apache.tuscany.sca.provider.ServiceBindingProvider; +import org.apache.tuscany.sca.runtime.RuntimeComponent; +import org.apache.tuscany.sca.runtime.RuntimeComponentReference; +import org.apache.tuscany.sca.runtime.RuntimeComponentService; + + + +/** + * Implementation of the JSONRPC binding model. + * + * @version $Rev$ $Date$ + */ +public class JSONRPCBindingProviderFactory implements BindingProviderFactory<JSONRPCBinding> { + + ServletHost servletHost; + + public JSONRPCBindingProviderFactory(ServletHost servletHost) { + this.servletHost = servletHost; + } + + public ReferenceBindingProvider createReferenceBindingProvider(RuntimeComponent component, RuntimeComponentReference reference, JSONRPCBinding binding) { + return new JSONRPCReferenceBindingProvider(component, reference, binding); + } + + public ServiceBindingProvider createServiceBindingProvider(RuntimeComponent component, RuntimeComponentService service, JSONRPCBinding binding) { + return new JSONRPCServiceBindingProvider(component, service, binding, servletHost); + } + + public Class<JSONRPCBinding> getModelType() { + return JSONRPCBinding.class; + } +} diff --git a/sca-java-1.x/branches/sca-java-0.90/modules/binding-jsonrpc/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/provider/JSONRPCReferenceBindingProvider.java b/sca-java-1.x/branches/sca-java-0.90/modules/binding-jsonrpc/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/provider/JSONRPCReferenceBindingProvider.java new file mode 100644 index 0000000000..c8e43d824a --- /dev/null +++ b/sca-java-1.x/branches/sca-java-0.90/modules/binding-jsonrpc/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/provider/JSONRPCReferenceBindingProvider.java @@ -0,0 +1,64 @@ +/* + * 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.sca.binding.jsonrpc.provider; + +import org.apache.tuscany.sca.binding.jsonrpc.JSONRPCBinding; +import org.apache.tuscany.sca.interfacedef.InterfaceContract; +import org.apache.tuscany.sca.interfacedef.Operation; +import org.apache.tuscany.sca.invocation.Invoker; +import org.apache.tuscany.sca.provider.ReferenceBindingProvider; +import org.apache.tuscany.sca.runtime.RuntimeComponent; +import org.apache.tuscany.sca.runtime.RuntimeComponentReference; + + +/** + * Implementation of the JSONRPC binding provider. + * + * @version $Rev$ $Date$ + */ +public class JSONRPCReferenceBindingProvider implements ReferenceBindingProvider { + + private RuntimeComponentReference reference; + + public JSONRPCReferenceBindingProvider(RuntimeComponent component, + RuntimeComponentReference reference, + JSONRPCBinding binding) { + this.reference = reference; + } + + public Invoker createInvoker(Operation operation, boolean isCallback) { + if (isCallback) { + throw new UnsupportedOperationException(); + } else { + return new JSONRPCBindingInvoker(); + } + } + + public InterfaceContract getBindingInterfaceContract() { + return reference.getInterfaceContract(); + } + + public void start() { + } + + public void stop() { + } + +} diff --git a/sca-java-1.x/branches/sca-java-0.90/modules/binding-jsonrpc/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/provider/JSONRPCServiceBindingProvider.java b/sca-java-1.x/branches/sca-java-0.90/modules/binding-jsonrpc/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/provider/JSONRPCServiceBindingProvider.java new file mode 100644 index 0000000000..be593baa97 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-0.90/modules/binding-jsonrpc/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/provider/JSONRPCServiceBindingProvider.java @@ -0,0 +1,106 @@ +/* + * 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.sca.binding.jsonrpc.provider; + +import org.apache.tuscany.sca.binding.jsonrpc.JSONRPCBinding; +import org.apache.tuscany.sca.binding.jsonrpc.server.JSONRPCEntryPointServlet; +import org.apache.tuscany.sca.binding.jsonrpc.server.JSONRPCScriptServlet; +import org.apache.tuscany.sca.http.ServletHost; +import org.apache.tuscany.sca.interfacedef.Interface; +import org.apache.tuscany.sca.interfacedef.InterfaceContract; +import org.apache.tuscany.sca.interfacedef.java.JavaInterface; +import org.apache.tuscany.sca.provider.ServiceBindingProvider; +import org.apache.tuscany.sca.runtime.RuntimeComponent; +import org.apache.tuscany.sca.runtime.RuntimeComponentService; + +/** + * Implementation of the JSONRPC binding provider. + * + * @version $Rev$ $Date$ + */ +public class JSONRPCServiceBindingProvider implements ServiceBindingProvider { + + private static int servletRegistrationCount = 0; + private RuntimeComponent component; + private RuntimeComponentService service; + private JSONRPCBinding binding; + private ServletHost servletHost; + + // path to the JSONRPC javascript servlet + public static final String SCRIPT_GETTER_SERVICE_MAPPING = "/SCA/scripts"; + + public static final String JSONRPC_SERVICE_MAPPING_PREFIX = "/"; + + public JSONRPCServiceBindingProvider(RuntimeComponent component, + RuntimeComponentService service, + JSONRPCBinding binding, + ServletHost servletHost) { + this.component = component; + this.service = service; + this.binding = binding; + this.servletHost = servletHost; + } + + public InterfaceContract getBindingInterfaceContract() { + return service.getInterfaceContract(); + } + + public void start() { + JSONRPCEntryPointServlet servlet; + + Class<?> aClass = getTargetJavaClass(service.getInterfaceContract().getInterface()); + Object instance = component.createSelfReference(aClass).getService(); + + servlet = new JSONRPCEntryPointServlet(binding.getName(), aClass, instance); + + // register the servlet based on the service name + servletHost.addServletMapping(JSONRPC_SERVICE_MAPPING_PREFIX + binding.getName(), servlet); + + // if the script getter servlet is not already registered then register + // it + if (servletRegistrationCount == 0) { + servletHost.addServletMapping(SCRIPT_GETTER_SERVICE_MAPPING, new JSONRPCScriptServlet()); + } + + // increase the registered servlet count + servletRegistrationCount++; + } + + public void stop() { + + // Unregister from the servlet mapping + servletHost.removeServletMapping(JSONRPC_SERVICE_MAPPING_PREFIX + binding.getName()); + servletRegistrationCount--; + // if we unregistered the last JSONRPC servlet, then unreister the + // script servlet + if (servletRegistrationCount == 0) { + servletHost.removeServletMapping(SCRIPT_GETTER_SERVICE_MAPPING); + } + } + + private Class<?> getTargetJavaClass(Interface targetInterface) { + // TODO: right now assume that the target is always a Java + // Implementation. Need to figure out + // how to generate Java Interface in cases where the target is not a + // Java Implementation + return ((JavaInterface)targetInterface).getJavaClass(); + } + +} diff --git a/sca-java-1.x/branches/sca-java-0.90/modules/binding-jsonrpc/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/server/JSONRPCEntryPointServlet.java b/sca-java-1.x/branches/sca-java-0.90/modules/binding-jsonrpc/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/server/JSONRPCEntryPointServlet.java new file mode 100644 index 0000000000..cab8965f90 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-0.90/modules/binding-jsonrpc/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/server/JSONRPCEntryPointServlet.java @@ -0,0 +1,150 @@ +/* + * 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.sca.binding.jsonrpc.server; + +import java.io.BufferedReader; +import java.io.CharArrayWriter; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.text.ParseException; + +import javax.servlet.ServletConfig; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import javax.servlet.http.HttpSession; + +import org.apache.tuscany.sca.binding.jsonrpc.util.JavaToSmd; +import org.json.JSONObject; + +import com.metaparadigm.jsonrpc.JSONRPCBridge; +import com.metaparadigm.jsonrpc.JSONRPCResult; +import com.metaparadigm.jsonrpc.JSONRPCServlet; + +/** + * + * + */ +public class JSONRPCEntryPointServlet extends JSONRPCServlet { + private static final long serialVersionUID = 1L; + + private static final int BUF_SIZE = 4096; + + String serviceName; + + Object serviceInstance; + + Class<?> serviceInterface; + + // default constructor for unit testing + public JSONRPCEntryPointServlet() { + } + + public JSONRPCEntryPointServlet(String serviceName, Class<?> serviceInterface, Object serviceInstance) { + this.serviceName = serviceName; + this.serviceInterface = serviceInterface; + this.serviceInstance = serviceInstance; + } + + /** + * Override to do nothing as the JSONRPCServlet is setup by the + * service method in this class. + */ + @Override + public void init(ServletConfig config) { + } + + @Override + public void service(HttpServletRequest request, HttpServletResponse response) + throws IOException, ClassCastException { + + // if there is a smd parameter, we should retrun the SMD for this service + if (request.getParameter("smd") != null) { + String serviceUrl = request.getRequestURL().toString(); + String smd = JavaToSmd.interfaceToSmd(serviceInterface, serviceUrl); + + response.setContentType("text/plain;charset=utf-8"); + OutputStream out = response.getOutputStream(); + byte[] bout = smd.getBytes("UTF-8"); + + out.write(bout); + out.flush(); + out.close(); + } else { + /* + * Create a new bridge for every request to aviod all the problems with + * JSON-RPC-Java storing the bridge in the session + */ + HttpSession session = request.getSession(); + + JSONRPCBridge jsonrpcBridge = new JSONRPCBridge(); + jsonrpcBridge.registerObject(serviceName, serviceInstance, serviceInterface); + session.setAttribute("JSONRPCBridge", jsonrpcBridge); + + // Encode using UTF-8, although We are actually ASCII clean as + // all unicode data is JSON escaped using backslash u. This is + // less data efficient for foreign character sets but it is + // needed to support naughty browsers such as Konqueror and Safari + // which do not honour the charset set in the response + response.setContentType("text/plain;charset=utf-8"); + OutputStream out = response.getOutputStream(); + + // Decode using the charset in the request if it exists otherwise + // use UTF-8 as this is what all browser implementations use. + // The JSON-RPC-Java JavaScript client is ASCII clean so it + // although here we can correctly handle data from other clients + // that do not escape non ASCII data + String charset = request.getCharacterEncoding(); + if (charset == null) { + charset = "UTF-8"; + } + BufferedReader in = new BufferedReader(new InputStreamReader(request.getInputStream(), charset)); + + // Read the request + CharArrayWriter data = new CharArrayWriter(); + char buf[] = new char[BUF_SIZE]; + int ret; + while ((ret = in.read(buf, 0, BUF_SIZE)) != -1) { + data.write(buf, 0, ret); + } + + // Process the request + JSONObject jsonReq = null; + JSONRPCResult jsonResp = null; + try { + jsonReq = new JSONObject(data.toString()); + String method = jsonReq.getString("method"); + if ((method != null) && (method.indexOf('.') < 0)) { + jsonReq.putOpt("method", serviceName + "." + method); + } + jsonResp = jsonrpcBridge.call(new Object[] {request}, jsonReq); + } catch (ParseException e) { + throw new RuntimeException("Unable to parse request", e); + } + + byte[] bout = jsonResp.toString().getBytes("UTF-8"); + + out.write(bout); + out.flush(); + out.close(); + + } + } + +} diff --git a/sca-java-1.x/branches/sca-java-0.90/modules/binding-jsonrpc/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/server/JSONRPCScriptServlet.java b/sca-java-1.x/branches/sca-java-0.90/modules/binding-jsonrpc/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/server/JSONRPCScriptServlet.java new file mode 100644 index 0000000000..0fff49100e --- /dev/null +++ b/sca-java-1.x/branches/sca-java-0.90/modules/binding-jsonrpc/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/server/JSONRPCScriptServlet.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 org.apache.tuscany.sca.binding.jsonrpc.server; + +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; + +import javax.servlet.ServletOutputStream; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +public class JSONRPCScriptServlet extends HttpServlet { + + private static final long serialVersionUID = 1L; + + // private static final String SCA_INIT_JS = "SCA = new JSONRpcClient(\"serviceBindings/HelloWorldService\");"; + + public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { + URL url = getClass().getResource("jsonrpc.js"); + InputStream is = url.openStream(); + writeToStream(response, is); + + // writeToStream(response, new ByteArrayInputStream(SCA_INIT_JS.getBytes())); + } + + private void writeToStream(HttpServletResponse response, InputStream is) throws IOException { + ServletOutputStream os = response.getOutputStream(); + int i; + while ((i = is.read()) != -1) { // NOPMD + os.write(i); + } + } + +} diff --git a/sca-java-1.x/branches/sca-java-0.90/modules/binding-jsonrpc/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/util/JavaToSmd.java b/sca-java-1.x/branches/sca-java-0.90/modules/binding-jsonrpc/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/util/JavaToSmd.java new file mode 100644 index 0000000000..339aca376c --- /dev/null +++ b/sca-java-1.x/branches/sca-java-0.90/modules/binding-jsonrpc/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/util/JavaToSmd.java @@ -0,0 +1,49 @@ +/*
+ * 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.sca.binding.jsonrpc.util;
+
+import java.lang.reflect.Method;
+
+
+public class JavaToSmd {
+
+ public static String interfaceToSmd(Class klazz, String serviceUrl) {
+ String name = klazz.getSimpleName();
+ Method methods[] = klazz.getMethods();
+
+ StringBuffer smdSb = new StringBuffer();
+ smdSb.append("{\"SMDVersion\":\".1\",\"objectName\":\"" + name + "\",\"serviceType\":\"JSON-RPC\",\"serviceURL\":\""+ serviceUrl + "\",\"methods\":[");
+ for (int i = 0; i < methods.length; i++) {
+ if (i != 0) smdSb.append(",");
+ Class<?> params[] = methods[i].getParameterTypes();
+ smdSb.append("{\"name\":\""+methods[i].getName() + "\",\"parameters\":[");
+ for (int j = 0; j < params.length; j++) {
+ if (j != 0) smdSb.append(",");
+ // right now Dojo doesn't look at the type value, so we'll default it to STRING
+ // also, since we can't introspect the method paramater names we'll just create an incrementing parameter name
+ smdSb.append("{\"name\":\"param" + j + "\",\"type\":\"STRING\"}");
+ }
+ smdSb.append("]}");
+ }
+ smdSb.append("]}");
+
+ return smdSb.toString();
+ }
+
+}
|