diff options
author | fmoga <fmoga@13f79535-47bb-0310-9956-ffa450edef68> | 2010-09-29 07:07:43 +0000 |
---|---|---|
committer | fmoga <fmoga@13f79535-47bb-0310-9956-ffa450edef68> | 2010-09-29 07:07:43 +0000 |
commit | 4edeed38dc46ff49ba570bc03af5fe990de4bff1 (patch) | |
tree | 59f599ffdaf732fa02884fb2da58385a39c9580e /sca-java-2.x/trunk/samples | |
parent | 5e47a5cd2a6e4e7a12e1ca140677227dfda0070a (diff) |
Added sources for the callback-api sample.
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@1002494 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'sca-java-2.x/trunk/samples')
9 files changed, 468 insertions, 0 deletions
diff --git a/sca-java-2.x/trunk/samples/getting-started/callback-api/pom.xml b/sca-java-2.x/trunk/samples/getting-started/callback-api/pom.xml new file mode 100644 index 0000000000..446c7330d7 --- /dev/null +++ b/sca-java-2.x/trunk/samples/getting-started/callback-api/pom.xml @@ -0,0 +1,51 @@ +<?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. +--> +<project> + <modelVersion>4.0.0</modelVersion> + <parent> + <groupId>org.apache.tuscany.sca</groupId> + <artifactId>tuscany-samples</artifactId> + <version>2.0-SNAPSHOT</version> + <relativePath>../pom.xml</relativePath> + </parent> + <artifactId>sample-callback-api</artifactId> + <name>Apache Tuscany SCA Sample Callback API</name> + + <dependencies> + <dependency> + <groupId>org.apache.tuscany.sca</groupId> + <artifactId>tuscany-node-api</artifactId> + <version>2.0-SNAPSHOT</version> + </dependency> + + <dependency> + <groupId>org.apache.tuscany.sca</groupId> + <artifactId>tuscany-node-impl</artifactId> + <version>2.0-SNAPSHOT</version> + </dependency> + + <dependency> + <groupId>org.apache.tuscany.sca</groupId> + <artifactId>tuscany-implementation-java-runtime</artifactId> + <version>2.0-SNAPSHOT</version> + </dependency> + </dependencies> + +</project> diff --git a/sca-java-2.x/trunk/samples/getting-started/callback-api/src/main/java/sample/CallBack.java b/sca-java-2.x/trunk/samples/getting-started/callback-api/src/main/java/sample/CallBack.java new file mode 100644 index 0000000000..16e4297e9d --- /dev/null +++ b/sca-java-2.x/trunk/samples/getting-started/callback-api/src/main/java/sample/CallBack.java @@ -0,0 +1,33 @@ +/* + * 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; + +import org.oasisopen.sca.annotation.Remotable; + +/** + * The callback interface. + */ +@Remotable +public interface CallBack { + + void callBackMessage(String aString); + + void callBackIncrement(); + +} diff --git a/sca-java-2.x/trunk/samples/getting-started/callback-api/src/main/java/sample/Client.java b/sca-java-2.x/trunk/samples/getting-started/callback-api/src/main/java/sample/Client.java new file mode 100644 index 0000000000..7e0709e660 --- /dev/null +++ b/sca-java-2.x/trunk/samples/getting-started/callback-api/src/main/java/sample/Client.java @@ -0,0 +1,28 @@ +/* + * 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; + +import org.oasisopen.sca.annotation.Remotable; + +@Remotable +public interface Client { + + void run(); + +} diff --git a/sca-java-2.x/trunk/samples/getting-started/callback-api/src/main/java/sample/ClientImpl.java b/sca-java-2.x/trunk/samples/getting-started/callback-api/src/main/java/sample/ClientImpl.java new file mode 100644 index 0000000000..01219aa608 --- /dev/null +++ b/sca-java-2.x/trunk/samples/getting-started/callback-api/src/main/java/sample/ClientImpl.java @@ -0,0 +1,108 @@ +/* + * 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; + +import org.oasisopen.sca.annotation.Reference; +import org.oasisopen.sca.annotation.Service; + +@Service(Client.class) +public class ClientImpl implements Client, CallBack { + + public static final String DELIMITER = "\n----------------------------"; + + @Reference + protected sample.Service service; + + private static int callBackCount = 0; + + /** + * This function prints the message received from the service + * implementation. + * + * @param String the message received from the service + */ + public void callBackMessage(String aString) { + System.out.println("ClientImpl - Received callback message: " + aString); + } + + /** + * This function increments the callBackCount variable when called from the + * service implementation. + */ + public void callBackIncrement() { + System.out.println("ClientImpl - Received increment callback"); + callBackCount++; + System.out.println("ClientImpl - Callback counter incremented to : " + getCallBackCount()); + } + + /** + * This method runs different kinds of service calls implying callbacks. + */ + public void run() { + simpleCallBack(); + simpleCallBackByRef(); + noCallBack(); + multipleCallBack(); + } + + /** + * The basic callback where the target calls back prior to returning to the + * client. + */ + private void simpleCallBack() { + System.out.println(DELIMITER + "\nSimple callback" + DELIMITER); + service.knockKnock("Knock Knock"); + } + + /** + * The basic callback where the target calls back prior to returning to the + * client. + */ + private void simpleCallBackByRef() { + System.out.println(DELIMITER + "\nSimple callback by reference" + DELIMITER); + service.knockKnockByRef("Knock Knock by reference"); + } + + /** + * The basic callback where the target does not call back to the client. + */ + private void noCallBack() { + System.out.println(DELIMITER + "\nNo callback" + DELIMITER); + service.noCallBack("No Reply Desired"); + } + + /** + * The basic callback where the target calls back multiple times to the + * client. + */ + private void multipleCallBack() { + System.out.println(DELIMITER + "\nMultiple callbacks" + DELIMITER); + service.multiCallBack("Call me back 3 times"); + } + + /** + * This function returns the callBackCount variable. + * + * @return Integer the callBackCount variable + */ + public int getCallBackCount() { + return callBackCount; + } + +} diff --git a/sca-java-2.x/trunk/samples/getting-started/callback-api/src/main/java/sample/Launcher.java b/sca-java-2.x/trunk/samples/getting-started/callback-api/src/main/java/sample/Launcher.java new file mode 100644 index 0000000000..fa1a59d726 --- /dev/null +++ b/sca-java-2.x/trunk/samples/getting-started/callback-api/src/main/java/sample/Launcher.java @@ -0,0 +1,60 @@ +/* + * 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; + +import org.apache.tuscany.sca.node.Contribution; +import org.apache.tuscany.sca.node.ContributionLocationHelper; +import org.apache.tuscany.sca.node.Node; +import org.apache.tuscany.sca.node.NodeFactory; + +/** + * This class starts the Tuscany Runtime and runs the client calls to the + * service. + */ +public class Launcher { + + public static void main(String[] args) { + Node node = startRuntime(); + Client client = node.getService(Client.class, "Client"); + client.run(); + stopRuntime(node); + } + + /** + * Starts a Tuscany node with the predefined contribution. + * + * @return the running node + */ + private static Node startRuntime() { + String location = ContributionLocationHelper.getContributionLocation("CallBackApi.composite"); + Node node = NodeFactory.newInstance().createNode("CallBackApi.composite", new Contribution("c1", location)); + node.start(); + return node; + } + + /** + * Stops a Tuscany node. + * + * @param node the node to stop + */ + private static void stopRuntime(Node node) { + node.stop(); + } +} diff --git a/sca-java-2.x/trunk/samples/getting-started/callback-api/src/main/java/sample/Service.java b/sca-java-2.x/trunk/samples/getting-started/callback-api/src/main/java/sample/Service.java new file mode 100644 index 0000000000..6723af2adf --- /dev/null +++ b/sca-java-2.x/trunk/samples/getting-started/callback-api/src/main/java/sample/Service.java @@ -0,0 +1,36 @@ +/* + * 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; + +import org.oasisopen.sca.annotation.Callback; +import org.oasisopen.sca.annotation.Remotable; + +@Remotable +@Callback(CallBack.class) +public interface Service { + + void knockKnock(String aString); + + void knockKnockByRef(String aString); + + void noCallBack(String aString); + + void multiCallBack(String aString); + +} diff --git a/sca-java-2.x/trunk/samples/getting-started/callback-api/src/main/java/sample/ServiceImpl.java b/sca-java-2.x/trunk/samples/getting-started/callback-api/src/main/java/sample/ServiceImpl.java new file mode 100644 index 0000000000..4850f434ff --- /dev/null +++ b/sca-java-2.x/trunk/samples/getting-started/callback-api/src/main/java/sample/ServiceImpl.java @@ -0,0 +1,96 @@ +/* + * 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; + +import org.oasisopen.sca.ComponentContext; +import org.oasisopen.sca.ServiceReference; +import org.oasisopen.sca.annotation.Callback; +import org.oasisopen.sca.annotation.Context; +import org.oasisopen.sca.annotation.Service; + +@Service(sample.Service.class) +public class ServiceImpl implements sample.Service { + + public static final String MESSAGE_RECEIVED = "ServiceImpl - Received message: "; + + @Context + protected ComponentContext componentContext; + + @Callback + protected ServiceReference<CallBack> callbackRef; + + /** + * This function gets an object of ServiceImpl by calling + * getCallBackInterface function and calls the callBackMessage function. + * + * @param aString String passed by a function call + */ + public void knockKnock(String aString) { + System.out.println(MESSAGE_RECEIVED + aString); + CallBack callback = this.getCallBackFromComponentContext(); + callback.callBackMessage("Who's There"); + } + + /** + * This function calls the callBackMessage function. The reference to this + * function is received from the callback reference to the Service class. + * + * @param aString String passed by a function call + */ + public void knockKnockByRef(String aString) { + System.out.println(MESSAGE_RECEIVED + aString); + callbackRef.getService().callBackMessage("Who's There"); + } + + /** + * This function gets an object of ServiceImpl by calling + * getCallBackInterface function. This function then places multiple + * callbacks using the callbackIncrement function defined in the callback + * implementation. + * + * @param aString String passed by a function call + */ + public void multiCallBack(String aString) { + CallBack callback = this.getCallBackFromComponentContext(); + System.out.println(MESSAGE_RECEIVED + aString); + callback.callBackIncrement(); + callback.callBackIncrement(); + callback.callBackIncrement(); + } + + /** + * This function does not callBack any function. + * + * @param aString String passed by a function call + */ + public void noCallBack(String aString) { + System.out.println(MESSAGE_RECEIVED + aString); + } + + /** + * This function gets an object of ServiceImpl from the present + * componentContext. + * + * @return the callback + */ + private CallBack getCallBackFromComponentContext() { + return componentContext.getRequestContext().getCallback(); + } + +} diff --git a/sca-java-2.x/trunk/samples/getting-started/callback-api/src/main/resources/CallBackApi.composite b/sca-java-2.x/trunk/samples/getting-started/callback-api/src/main/resources/CallBackApi.composite new file mode 100644 index 0000000000..ca3a6e5f9d --- /dev/null +++ b/sca-java-2.x/trunk/samples/getting-started/callback-api/src/main/resources/CallBackApi.composite @@ -0,0 +1,33 @@ +<?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/200912" + targetNamespace="http://callback" + name="CallBackApi"> + + <component name="Client"> + <implementation.java class="sample.ClientImpl"/> + <reference name="service" target="Service"/> + </component> + + <component name="Service"> + <implementation.java class="sample.ServiceImpl"/> + </component> + +</composite> diff --git a/sca-java-2.x/trunk/samples/getting-started/callback-api/src/main/resources/META-INF/sca-contribution.xml b/sca-java-2.x/trunk/samples/getting-started/callback-api/src/main/resources/META-INF/sca-contribution.xml new file mode 100644 index 0000000000..f17791205a --- /dev/null +++ b/sca-java-2.x/trunk/samples/getting-started/callback-api/src/main/resources/META-INF/sca-contribution.xml @@ -0,0 +1,23 @@ +<?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. +--> +<contribution xmlns="http://docs.oasis-open.org/ns/opencsa/sca/200912" + xmlns:t="http://callback"> + <deployable composite="t:CallBackApi" /> +</contribution>
\ No newline at end of file |