diff options
author | fmoga <fmoga@13f79535-47bb-0310-9956-ffa450edef68> | 2010-08-14 00:42:53 +0000 |
---|---|---|
committer | fmoga <fmoga@13f79535-47bb-0310-9956-ffa450edef68> | 2010-08-14 00:42:53 +0000 |
commit | 24bb8d65b5ad54121ce666ee1d805f0f6ec3d713 (patch) | |
tree | 916871d9cedcbe799938509341153dd4b40d1be3 /sca-java-2.x/contrib/samples/stock-comet | |
parent | eff7bb9dd439b9d9d74cb54b4cf0733e6b3ca0cf (diff) |
Modified the sample in order to illustrate how the binding handles multiple comet services through the same persisted http connection.
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@985415 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'sca-java-2.x/contrib/samples/stock-comet')
12 files changed, 222 insertions, 128 deletions
diff --git a/sca-java-2.x/contrib/samples/stock-comet/pom.xml b/sca-java-2.x/contrib/samples/stock-comet/pom.xml index f6f68d1e66..a329c2bdaf 100644 --- a/sca-java-2.x/contrib/samples/stock-comet/pom.xml +++ b/sca-java-2.x/contrib/samples/stock-comet/pom.xml @@ -23,10 +23,10 @@ </parent> <groupId>org.apache.tuscany.sca</groupId> - <artifactId>sample-stock-comet-webapp</artifactId> + <artifactId>sample-comet-weather-monitor-webapp</artifactId> <packaging>war</packaging> <version>1.0</version> - <name>stock-comet</name> + <name>weather-monitor</name> <dependencies> <dependency> @@ -37,12 +37,12 @@ <dependency> <groupId>org.atmosphere</groupId> <artifactId>atmosphere-commons</artifactId> - <version>0.7-SNAPSHOT</version> + <version>0.6.1</version> </dependency> <dependency> <groupId>org.atmosphere</groupId> <artifactId>atmosphere-runtime</artifactId> - <version>0.7-SNAPSHOT</version> + <version>0.6.1</version> </dependency> <dependency> <groupId>javax</groupId> @@ -67,7 +67,7 @@ </dependencies> <build> - <finalName>stock-comet</finalName> + <finalName>weather-monitor</finalName> </build> </project> diff --git a/sca-java-2.x/contrib/samples/stock-comet/src/main/java/org/apache/tuscany/sca/sample/comet/HumidityService.java b/sca-java-2.x/contrib/samples/stock-comet/src/main/java/org/apache/tuscany/sca/sample/comet/HumidityService.java new file mode 100644 index 0000000000..faf80f02e0 --- /dev/null +++ b/sca-java-2.x/contrib/samples/stock-comet/src/main/java/org/apache/tuscany/sca/sample/comet/HumidityService.java @@ -0,0 +1,10 @@ +package org.apache.tuscany.sca.sample.comet; + +import org.oasisopen.sca.annotation.Remotable; + +@Remotable +public interface HumidityService { + + String getHumidity(); + +} diff --git a/sca-java-2.x/contrib/samples/stock-comet/src/main/java/org/apache/tuscany/sca/sample/comet/PrecipitationService.java b/sca-java-2.x/contrib/samples/stock-comet/src/main/java/org/apache/tuscany/sca/sample/comet/PrecipitationService.java new file mode 100644 index 0000000000..f40b2eb772 --- /dev/null +++ b/sca-java-2.x/contrib/samples/stock-comet/src/main/java/org/apache/tuscany/sca/sample/comet/PrecipitationService.java @@ -0,0 +1,10 @@ +package org.apache.tuscany.sca.sample.comet; + +import org.oasisopen.sca.annotation.Remotable; + +@Remotable +public interface PrecipitationService { + + String getPrecipitation(); + +} diff --git a/sca-java-2.x/contrib/samples/stock-comet/src/main/java/org/apache/tuscany/sca/sample/comet/PrecipitationServiceImpl.java b/sca-java-2.x/contrib/samples/stock-comet/src/main/java/org/apache/tuscany/sca/sample/comet/PrecipitationServiceImpl.java new file mode 100644 index 0000000000..df24a74080 --- /dev/null +++ b/sca-java-2.x/contrib/samples/stock-comet/src/main/java/org/apache/tuscany/sca/sample/comet/PrecipitationServiceImpl.java @@ -0,0 +1,16 @@ +package org.apache.tuscany.sca.sample.comet; + +import java.util.Date; +import java.util.Random; + +import org.oasisopen.sca.annotation.Service; + +@Service(PrecipitationService.class) +public class PrecipitationServiceImpl implements PrecipitationService { + + @Override + public String getPrecipitation() { + return (new Random(new Date().getTime()).nextInt(100)) + "%"; + } + +} diff --git a/sca-java-2.x/contrib/samples/stock-comet/src/main/java/org/apache/tuscany/sca/sample/comet/StockService.java b/sca-java-2.x/contrib/samples/stock-comet/src/main/java/org/apache/tuscany/sca/sample/comet/StockService.java deleted file mode 100644 index 767e8f5666..0000000000 --- a/sca-java-2.x/contrib/samples/stock-comet/src/main/java/org/apache/tuscany/sca/sample/comet/StockService.java +++ /dev/null @@ -1,28 +0,0 @@ -/* - * 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.sample.comet; - -import org.oasisopen.sca.annotation.Remotable; - -@Remotable -public interface StockService { - - String getQuotes(); - -} diff --git a/sca-java-2.x/contrib/samples/stock-comet/src/main/java/org/apache/tuscany/sca/sample/comet/StockServiceImpl.java b/sca-java-2.x/contrib/samples/stock-comet/src/main/java/org/apache/tuscany/sca/sample/comet/StockServiceImpl.java deleted file mode 100644 index d70724e90a..0000000000 --- a/sca-java-2.x/contrib/samples/stock-comet/src/main/java/org/apache/tuscany/sca/sample/comet/StockServiceImpl.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - * 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.sample.comet; - -import java.text.DecimalFormat; -import java.util.Date; -import java.util.Random; - -import org.oasisopen.sca.annotation.Service; - -@Service(StockService.class) -public class StockServiceImpl implements StockService { - - public static final int MAX_VALUE = 1000; - private Random random = new Random(new Date().getTime()); - - @Override - public String getQuotes() { - Double value = Math.abs(random.nextDouble() * random.nextInt(MAX_VALUE)); - return "ASF" + "#" + Double.valueOf(new DecimalFormat("#.##").format(value)); - } - -} diff --git a/sca-java-2.x/contrib/samples/stock-comet/src/main/java/org/apache/tuscany/sca/sample/comet/TemperatureHumidityServiceImpl.java b/sca-java-2.x/contrib/samples/stock-comet/src/main/java/org/apache/tuscany/sca/sample/comet/TemperatureHumidityServiceImpl.java new file mode 100644 index 0000000000..1fff4b1dea --- /dev/null +++ b/sca-java-2.x/contrib/samples/stock-comet/src/main/java/org/apache/tuscany/sca/sample/comet/TemperatureHumidityServiceImpl.java @@ -0,0 +1,26 @@ +package org.apache.tuscany.sca.sample.comet; + +import java.util.Date; +import java.util.Random; + +import org.oasisopen.sca.annotation.Service; + +@Service({TemperatureService.class, HumidityService.class}) +public class TemperatureHumidityServiceImpl implements TemperatureService, HumidityService { + + @Override + public String getHumidity() { + return (new Random(new Date().getTime()).nextInt(100)) + "%"; + } + + @Override + public String getTemperatureCelsius() { + return "" + (new Random(new Date().getTime()).nextInt(40)); + } + + @Override + public String getTemperatureFahrenheit() { + return "" + (new Random(new Date().getTime()).nextInt(200)); + } + +} diff --git a/sca-java-2.x/contrib/samples/stock-comet/src/main/java/org/apache/tuscany/sca/sample/comet/TemperatureService.java b/sca-java-2.x/contrib/samples/stock-comet/src/main/java/org/apache/tuscany/sca/sample/comet/TemperatureService.java new file mode 100644 index 0000000000..6a9e30b051 --- /dev/null +++ b/sca-java-2.x/contrib/samples/stock-comet/src/main/java/org/apache/tuscany/sca/sample/comet/TemperatureService.java @@ -0,0 +1,12 @@ +package org.apache.tuscany.sca.sample.comet; + +import org.oasisopen.sca.annotation.Remotable; + +@Remotable +public interface TemperatureService { + + String getTemperatureCelsius(); + + String getTemperatureFahrenheit(); + +} diff --git a/sca-java-2.x/contrib/samples/stock-comet/src/main/webapp/WEB-INF/web.composite b/sca-java-2.x/contrib/samples/stock-comet/src/main/webapp/WEB-INF/web.composite index 5a1d2bb21b..05b2ddd0d7 100644 --- a/sca-java-2.x/contrib/samples/stock-comet/src/main/webapp/WEB-INF/web.composite +++ b/sca-java-2.x/contrib/samples/stock-comet/src/main/webapp/WEB-INF/web.composite @@ -22,10 +22,22 @@ targetNamespace="http://samples" name="Stock"> - <component name="test"> - <implementation.java class="org.apache.tuscany.sca.sample.comet.StockServiceImpl"/> - <service name="StockService"> - <interface.java interface="org.apache.tuscany.sca.sample.comet.StockService"/> + <component name="c1"> + <implementation.java class="org.apache.tuscany.sca.sample.comet.TemperatureHumidityServiceImpl"/> + <service name="TemperatureService"> + <interface.java interface="org.apache.tuscany.sca.sample.comet.TemperatureService"/> + <tuscany:binding.comet/> + </service> + <service name="HumidityService"> + <interface.java interface="org.apache.tuscany.sca.sample.comet.HumidityService"/> + <tuscany:binding.comet/> + </service> + </component> + + <component name="c2"> + <implementation.java class="org.apache.tuscany.sca.sample.comet.PrecipitationServiceImpl"/> + <service name="PrecipitationService"> + <interface.java interface="org.apache.tuscany.sca.sample.comet.PrecipitationService"/> <tuscany:binding.comet/> </service> </component> diff --git a/sca-java-2.x/contrib/samples/stock-comet/src/main/webapp/WEB-INF/web.xml b/sca-java-2.x/contrib/samples/stock-comet/src/main/webapp/WEB-INF/web.xml index cf792265da..047fd5e21d 100644 --- a/sca-java-2.x/contrib/samples/stock-comet/src/main/webapp/WEB-INF/web.xml +++ b/sca-java-2.x/contrib/samples/stock-comet/src/main/webapp/WEB-INF/web.xml @@ -10,26 +10,18 @@ WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. --> -<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" - xmlns:j2ee="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" - xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee - http://java.sun.com/xml/ns/j2ee/web-app_3.0.xsd"> - - <display-name>Apache Tuscany Stock Comet Sample</display-name> - - <filter> - <filter-name>tuscany</filter-name> - <filter-class>org.apache.tuscany.sca.host.webapp.TuscanyServletFilter</filter-class> - <async-supported>true</async-supported> - </filter> - - <filter-mapping> - <filter-name>tuscany</filter-name> - <url-pattern>/*</url-pattern> - </filter-mapping> - - <welcome-file-list> - <welcome-file>index.html</welcome-file> - </welcome-file-list> -</web-app> - +<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:j2ee="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_3.0.xsd" version="3.0"> + <display-name>Apache Tuscany Stock Comet Sample</display-name> + <filter> + <filter-name>tuscany</filter-name> + <filter-class>org.apache.tuscany.sca.host.webapp.TuscanyServletFilter</filter-class> + <async-supported>true</async-supported> + </filter> + <filter-mapping> + <filter-name>tuscany</filter-name> + <url-pattern>/*</url-pattern> + </filter-mapping> + <welcome-file-list> + <welcome-file>index.html</welcome-file> + </welcome-file-list> +</web-app>
\ No newline at end of file diff --git a/sca-java-2.x/contrib/samples/stock-comet/src/main/webapp/index.html b/sca-java-2.x/contrib/samples/stock-comet/src/main/webapp/index.html index 816038d0e9..71c9b03960 100644 --- a/sca-java-2.x/contrib/samples/stock-comet/src/main/webapp/index.html +++ b/sca-java-2.x/contrib/samples/stock-comet/src/main/webapp/index.html @@ -20,38 +20,51 @@ <head> <title>Apache Tuscany Comet Sample</title> <script type="text/javascript" src="js/jquery-1.4.2.js"></script> - <script type="text/javascript" src="js/jquery.atmosphere.js"></script> + <script type="text/javascript" src="js/jquery.atmosphere.js"></script> + <script type="text/javascript" src="js/org.apache.tuscany.sca.cometComponentContext.js"></script> <script type="text/javascript"> $(document).ready(function() { - var connectedEndpoint; - - function update(response) { - var data = response.responseBody; - var aux = data.toString().split('#'); - document.getElementById('symbol').textContent = aux[0]; - document.getElementById('price').textContent = aux[1]; - } - document.getElementById('connect').onclick = function(event) { /* transport can be : long-polling, streaming or websocket */ - $.atmosphere.subscribe(document.location.toString() + 'test/StockService/getQuotes', - update, - $.atmosphere.request = {transport: document.getElementById('transport').value}); - connectedEndpoint = $.atmosphere.response; - + tuscanyComet.connect(document.getElementById('transport').value); document.getElementById('connect').disabled = true; document.getElementById('transport').disabled = true; - document.getElementById('get-values').disabled = false; + } + + + document.getElementById('tempCButton').onclick = function(event) { + cometComponentContext.c1.TemperatureService.getTemperatureCelsius(updateTempC); + } + + document.getElementById('tempFButton').onclick = function(event) { + cometComponentContext.c1.TemperatureService.getTemperatureFahrenheit(updateTempF); } - document.getElementById('get-values').disabled = true; - document.getElementById('get-values').onclick = function(event) { - connectedEndpoint.push(document.location.toString() + 'test/StockService/getQuotes', - null, - $.atmosphere.request = {method: 'POST'}); + document.getElementById('humButton').onclick = function(event) { + cometComponentContext.c1.HumidityService.getHumidity(updateHum); + } + + document.getElementById('precipButton').onclick = function(event) { + cometComponentContext.c2.PrecipitationService.getPrecipitation(updatePrecip); } }); + + function updateTempC(response) { + document.getElementById('tempCText').textContent = response; + } + + function updateTempF(response) { + document.getElementById('tempFText').textContent = response; + } + + function updateHum(response) { + document.getElementById('humText').textContent = response; + } + + function updatePrecip(response) { + document.getElementById('precipText').textContent = response; + } </script> </head> <body> @@ -63,14 +76,29 @@ <option id="websocket" value="websocket">websocket</option> </select> <input id='connect' type='submit' value='Connect'/> - <h3>Stock Monitor</h3> - <input id='get-values' type="submit" value='Get values'/> + <h3>Weather Monitor</h3> <p/> - <div id='text'> - <b><label>Company Symbol: </label></b> - <span id="symbol">N/A</span> - <b><label>Price: </label></b> - <span id="price">N/A</span> - </div> + <table> + <tr> + <th align="left">Temperature (Celsius):</th> + <td><span id='tempCText'>N/A</span></td> + <td><input type='button' id='tempCButton' value="Update"/></td> + </tr> + <tr> + <th align="left">Temperature (Fahrenheit):</th> + <td><span id='tempFText'>N/A</span></td> + <td><input type='button' id='tempFButton' value="Update"/></td> + </tr> + <tr> + <th align="left">Humidity:</th> + <td><span id='humText'>N/A</span></td> + <td><input type='button' id='humButton' value="Update"/></td> + </tr> + <tr> + <th align="left">Precipitation probability:</th> + <td><span id='precipText'>N/A</span></td> + <td><input type='button' id='precipButton' value="Update"/></td> + </tr> + </table> </body> </html> diff --git a/sca-java-2.x/contrib/samples/stock-comet/src/main/webapp/js/org.apache.tuscany.sca.cometComponentContext.js b/sca-java-2.x/contrib/samples/stock-comet/src/main/webapp/js/org.apache.tuscany.sca.cometComponentContext.js new file mode 100644 index 0000000000..c8dc19d15a --- /dev/null +++ b/sca-java-2.x/contrib/samples/stock-comet/src/main/webapp/js/org.apache.tuscany.sca.cometComponentContext.js @@ -0,0 +1,55 @@ +// TODO: Try isolating functional code one piece at a time by using it like in the inital version +var tuscanyComet = { + connectedEndpoint : null, + connect : function(transport) { + $.atmosphere.subscribe(document.location.toString() + 'tuscany-comet', + this.callback, + $.atmosphere.request = { + transport : transport + }); + this.connectedEndpoint = $.atmosphere.response; + }, + callAsync : function(url, callbackMethod) { + this.connectedEndpoint.push(document.location.toString() + + 'tuscany-comet/' + url, + null, + $.atmosphere.request = { + method : 'POST', + data : 'callback=' + callbackMethod.name + }); + }, + callback : function(response) { + var data = response.responseBody; + if (data.indexOf('#') != -1) { + var callbackMethod = data.substring(0, data.indexOf('#')); + data = data.substring(data.indexOf('#') + 1); + eval(callbackMethod + '("' + data + '");'); + } + } +}; + +// To be generated +var cometComponentContext = { + c1 : { + TemperatureService : { + getTemperatureCelsius : function(callbackMethod) { + tuscanyComet.callAsync('c1/TemperatureService/getTemperatureCelsius', callbackMethod); + }, + getTemperatureFahrenheit : function(callbackMethod) { + tuscanyComet.callAsync('c1/TemperatureService/getTemperatureFahrenheit', callbackMethod); + } + }, + HumidityService : { + getHumidity : function(callbackMethod) { + tuscanyComet.callAsync('c1/HumidityService/getHumidity', callbackMethod); + } + } + }, + c2 : { + PrecipitationService : { + getPrecipitation : function(callbackMethod) { + tuscanyComet.callAsync('c2/PrecipitationService/getPrecipitation', callbackMethod); + } + } + } +}
\ No newline at end of file |