From 4557455428b08ff38811bd2cbb4595580430956a Mon Sep 17 00:00:00 2001 From: antelder Date: Fri, 1 Oct 2010 07:56:18 +0000 Subject: Add a binding extension sample git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@1003421 13f79535-47bb-0310-9956-ffa450edef68 --- .../main/java/sample/binding/SampleBinding.java | 49 +++++++++++++++++++ .../java/sample/binding/SampleBindingFactory.java | 32 ++++++++++++ .../runtime/SampleBindingProviderFactory.java | 47 ++++++++++++++++++ .../runtime/SampleReferenceBindingProvider.java | 55 +++++++++++++++++++++ .../binding/runtime/SampleReferenceInvoker.java | 53 ++++++++++++++++++++ .../runtime/SampleServiceBindingProvider.java | 57 ++++++++++++++++++++++ .../binding/runtime/SampleServiceInvoker.java | 40 +++++++++++++++ .../java/sample/binding/runtime/SampleStash.java | 44 +++++++++++++++++ 8 files changed, 377 insertions(+) create mode 100644 sca-java-2.x/trunk/samples/extending-tuscany/binding-sample/src/main/java/sample/binding/SampleBinding.java create mode 100644 sca-java-2.x/trunk/samples/extending-tuscany/binding-sample/src/main/java/sample/binding/SampleBindingFactory.java create mode 100644 sca-java-2.x/trunk/samples/extending-tuscany/binding-sample/src/main/java/sample/binding/runtime/SampleBindingProviderFactory.java create mode 100644 sca-java-2.x/trunk/samples/extending-tuscany/binding-sample/src/main/java/sample/binding/runtime/SampleReferenceBindingProvider.java create mode 100644 sca-java-2.x/trunk/samples/extending-tuscany/binding-sample/src/main/java/sample/binding/runtime/SampleReferenceInvoker.java create mode 100644 sca-java-2.x/trunk/samples/extending-tuscany/binding-sample/src/main/java/sample/binding/runtime/SampleServiceBindingProvider.java create mode 100644 sca-java-2.x/trunk/samples/extending-tuscany/binding-sample/src/main/java/sample/binding/runtime/SampleServiceInvoker.java create mode 100644 sca-java-2.x/trunk/samples/extending-tuscany/binding-sample/src/main/java/sample/binding/runtime/SampleStash.java (limited to 'sca-java-2.x/trunk/samples/extending-tuscany/binding-sample/src/main/java/sample/binding') diff --git a/sca-java-2.x/trunk/samples/extending-tuscany/binding-sample/src/main/java/sample/binding/SampleBinding.java b/sca-java-2.x/trunk/samples/extending-tuscany/binding-sample/src/main/java/sample/binding/SampleBinding.java new file mode 100644 index 0000000000..174a32fce4 --- /dev/null +++ b/sca-java-2.x/trunk/samples/extending-tuscany/binding-sample/src/main/java/sample/binding/SampleBinding.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 sample.binding; + +import javax.xml.namespace.QName; + +import org.apache.tuscany.sca.provider.BaseBindingImpl; + +/** + * Represents a binding to a Sample service. + */ +public class SampleBinding extends BaseBindingImpl { + + public static final QName TYPE = new QName(SCA11_TUSCANY_NS, "binding.sample"); + + private String someAttr; + + public SampleBinding() { + } + + public QName getType() { + return TYPE; + } + + public String getSomeAttr() { + return someAttr; + } + + public void setSomeAttr(String someAttr) { + this.someAttr = someAttr; + } + +} diff --git a/sca-java-2.x/trunk/samples/extending-tuscany/binding-sample/src/main/java/sample/binding/SampleBindingFactory.java b/sca-java-2.x/trunk/samples/extending-tuscany/binding-sample/src/main/java/sample/binding/SampleBindingFactory.java new file mode 100644 index 0000000000..6f4920448c --- /dev/null +++ b/sca-java-2.x/trunk/samples/extending-tuscany/binding-sample/src/main/java/sample/binding/SampleBindingFactory.java @@ -0,0 +1,32 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package sample.binding; + + +/** + * Factory implementation to create Sample Models + */ +public class SampleBindingFactory { + + public SampleBinding createSampleBinding() { + return new SampleBinding(); + } + +} diff --git a/sca-java-2.x/trunk/samples/extending-tuscany/binding-sample/src/main/java/sample/binding/runtime/SampleBindingProviderFactory.java b/sca-java-2.x/trunk/samples/extending-tuscany/binding-sample/src/main/java/sample/binding/runtime/SampleBindingProviderFactory.java new file mode 100644 index 0000000000..4fb966138d --- /dev/null +++ b/sca-java-2.x/trunk/samples/extending-tuscany/binding-sample/src/main/java/sample/binding/runtime/SampleBindingProviderFactory.java @@ -0,0 +1,47 @@ +/* + * 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 sample.binding.runtime; + +import sample.binding.SampleBinding; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +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.RuntimeEndpoint; +import org.apache.tuscany.sca.runtime.RuntimeEndpointReference; + +public class SampleBindingProviderFactory implements BindingProviderFactory { + + public SampleBindingProviderFactory(ExtensionPointRegistry extensionPoints) { + } + + public Class getModelType() { + return SampleBinding.class; + } + + public ReferenceBindingProvider createReferenceBindingProvider(RuntimeEndpointReference endpoint) { + return new SampleReferenceBindingProvider(endpoint); + } + + public ServiceBindingProvider createServiceBindingProvider(RuntimeEndpoint endpoint) { + return new SampleServiceBindingProvider(endpoint); + } + +} diff --git a/sca-java-2.x/trunk/samples/extending-tuscany/binding-sample/src/main/java/sample/binding/runtime/SampleReferenceBindingProvider.java b/sca-java-2.x/trunk/samples/extending-tuscany/binding-sample/src/main/java/sample/binding/runtime/SampleReferenceBindingProvider.java new file mode 100644 index 0000000000..b1edf6c729 --- /dev/null +++ b/sca-java-2.x/trunk/samples/extending-tuscany/binding-sample/src/main/java/sample/binding/runtime/SampleReferenceBindingProvider.java @@ -0,0 +1,55 @@ +/* + * 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 sample.binding.runtime; + +import org.apache.tuscany.sca.assembly.EndpointReference; +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; + +public class SampleReferenceBindingProvider implements ReferenceBindingProvider { + + private EndpointReference endpoint; + private InterfaceContract contract; + + public SampleReferenceBindingProvider(EndpointReference endpoint) { + this.endpoint = endpoint; + } + + public Invoker createInvoker(Operation operation) { + return new SampleReferenceInvoker(operation, endpoint); + } + + public void start() { + } + + public void stop() { + } + + public InterfaceContract getBindingInterfaceContract() { + return contract; + } + + public boolean supportsOneWayInvocation() { + return false; + } + +} diff --git a/sca-java-2.x/trunk/samples/extending-tuscany/binding-sample/src/main/java/sample/binding/runtime/SampleReferenceInvoker.java b/sca-java-2.x/trunk/samples/extending-tuscany/binding-sample/src/main/java/sample/binding/runtime/SampleReferenceInvoker.java new file mode 100644 index 0000000000..31cb339818 --- /dev/null +++ b/sca-java-2.x/trunk/samples/extending-tuscany/binding-sample/src/main/java/sample/binding/runtime/SampleReferenceInvoker.java @@ -0,0 +1,53 @@ +/* + * 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 sample.binding.runtime; + +import org.apache.tuscany.sca.assembly.EndpointReference; +import org.apache.tuscany.sca.interfacedef.Operation; +import org.apache.tuscany.sca.invocation.Invoker; +import org.apache.tuscany.sca.invocation.Message; + +public class SampleReferenceInvoker implements Invoker { + + protected Operation operation; + protected EndpointReference endpoint; + + public SampleReferenceInvoker(Operation operation, EndpointReference endpoint) { + this.operation = operation; + this.endpoint = endpoint; + } + + public Message invoke(Message msg) { + try { + + return doInvoke(msg); + + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + public Message doInvoke(Message msg) { + // Add some code here to make an invocation over the Sample binding protocol + // For this sample we'll just get it from the static stash + SampleServiceInvoker fi = SampleStash.getService(endpoint.getBinding().getURI()); + return fi.invokeService(msg); + } +} diff --git a/sca-java-2.x/trunk/samples/extending-tuscany/binding-sample/src/main/java/sample/binding/runtime/SampleServiceBindingProvider.java b/sca-java-2.x/trunk/samples/extending-tuscany/binding-sample/src/main/java/sample/binding/runtime/SampleServiceBindingProvider.java new file mode 100644 index 0000000000..f8aab77fde --- /dev/null +++ b/sca-java-2.x/trunk/samples/extending-tuscany/binding-sample/src/main/java/sample/binding/runtime/SampleServiceBindingProvider.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 sample.binding.runtime; + +import sample.binding.SampleBinding; +import org.apache.tuscany.sca.interfacedef.InterfaceContract; +import org.apache.tuscany.sca.provider.ServiceBindingProvider; +import org.apache.tuscany.sca.runtime.RuntimeEndpoint; + +public class SampleServiceBindingProvider implements ServiceBindingProvider { + + private RuntimeEndpoint endpoint; + private InterfaceContract contract; + + public SampleServiceBindingProvider(RuntimeEndpoint endpoint) { + this.endpoint = endpoint; + } + + public void start() { + // add some code here to start the service + + // For this sample we'll just share it in a static + SampleStash.addService(endpoint.getBinding().getURI(), new SampleServiceInvoker(endpoint)); + + System.out.println("someAttr=" + ((SampleBinding)endpoint.getBinding()).getSomeAttr()); + } + + public void stop() { + SampleStash.removeService(endpoint.getBinding().getURI()); + } + + public InterfaceContract getBindingInterfaceContract() { + return contract; + } + + public boolean supportsOneWayInvocation() { + return false; + } + +} diff --git a/sca-java-2.x/trunk/samples/extending-tuscany/binding-sample/src/main/java/sample/binding/runtime/SampleServiceInvoker.java b/sca-java-2.x/trunk/samples/extending-tuscany/binding-sample/src/main/java/sample/binding/runtime/SampleServiceInvoker.java new file mode 100644 index 0000000000..bfbfe80966 --- /dev/null +++ b/sca-java-2.x/trunk/samples/extending-tuscany/binding-sample/src/main/java/sample/binding/runtime/SampleServiceInvoker.java @@ -0,0 +1,40 @@ +/* + * 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 sample.binding.runtime; + +import org.apache.tuscany.sca.invocation.Message; +import org.apache.tuscany.sca.runtime.RuntimeEndpoint; + +public class SampleServiceInvoker { + + private RuntimeEndpoint wire; + + public SampleServiceInvoker(RuntimeEndpoint wire) { + this.wire = wire; + } + + /** + * Send the request down the wire to invoke the service + */ + public Message invokeService(Message msg) { + return wire.invoke(msg); + } + +} diff --git a/sca-java-2.x/trunk/samples/extending-tuscany/binding-sample/src/main/java/sample/binding/runtime/SampleStash.java b/sca-java-2.x/trunk/samples/extending-tuscany/binding-sample/src/main/java/sample/binding/runtime/SampleStash.java new file mode 100644 index 0000000000..cbebe77cf6 --- /dev/null +++ b/sca-java-2.x/trunk/samples/extending-tuscany/binding-sample/src/main/java/sample/binding/runtime/SampleStash.java @@ -0,0 +1,44 @@ +/* + * 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 sample.binding.runtime; + +import java.util.HashMap; +import java.util.Map; + +/** + * Simplistic static Map to share service endpoints with references + */ +public class SampleStash { + + private static Map services = new HashMap(); + + public static void addService(String uri, SampleServiceInvoker SampleServiceInvoker) { + services.put(uri, SampleServiceInvoker); + } + + public static SampleServiceInvoker getService(String uri) { + return services.get(uri); + } + + public static void removeService(String uri) { + services.remove(uri); + } + +} -- cgit v1.2.3