From 76e64f8719a90867fa0f8157c4e66e75ccfc8481 Mon Sep 17 00:00:00 2001 From: fmoga Date: Mon, 2 Aug 2010 20:27:46 +0000 Subject: Updated the stock sample to use the comet binding implementation. git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@981678 13f79535-47bb-0310-9956-ffa450edef68 --- .../tuscany/sca/sample/comet/StockService.java | 28 +++++ .../tuscany/sca/sample/comet/StockServiceImpl.java | 39 +++++++ .../src/main/java/sample/CometServlet.java | 116 --------------------- .../src/main/java/sample/ScriptFilter.java | 20 ---- .../src/main/java/sample/StockService.java | 27 ----- .../src/main/java/sample/StockServiceImpl.java | 41 -------- .../src/main/webapp/WEB-INF/context.xml | 4 - .../src/main/webapp/WEB-INF/web.composite | 18 ++-- .../stock-comet/src/main/webapp/WEB-INF/web.xml | 32 +----- .../samples/stock-comet/src/main/webapp/index.html | 19 +++- 10 files changed, 91 insertions(+), 253 deletions(-) create mode 100644 sca-java-2.x/contrib/samples/stock-comet/src/main/java/org/apache/tuscany/sca/sample/comet/StockService.java create mode 100644 sca-java-2.x/contrib/samples/stock-comet/src/main/java/org/apache/tuscany/sca/sample/comet/StockServiceImpl.java delete mode 100644 sca-java-2.x/contrib/samples/stock-comet/src/main/java/sample/CometServlet.java delete mode 100644 sca-java-2.x/contrib/samples/stock-comet/src/main/java/sample/ScriptFilter.java delete mode 100644 sca-java-2.x/contrib/samples/stock-comet/src/main/java/sample/StockService.java delete mode 100644 sca-java-2.x/contrib/samples/stock-comet/src/main/java/sample/StockServiceImpl.java delete mode 100755 sca-java-2.x/contrib/samples/stock-comet/src/main/webapp/WEB-INF/context.xml (limited to 'sca-java-2.x/contrib/samples/stock-comet/src') 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 new file mode 100644 index 0000000000..767e8f5666 --- /dev/null +++ b/sca-java-2.x/contrib/samples/stock-comet/src/main/java/org/apache/tuscany/sca/sample/comet/StockService.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 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 new file mode 100644 index 0000000000..d70724e90a --- /dev/null +++ b/sca-java-2.x/contrib/samples/stock-comet/src/main/java/org/apache/tuscany/sca/sample/comet/StockServiceImpl.java @@ -0,0 +1,39 @@ +/* + * 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/sample/CometServlet.java b/sca-java-2.x/contrib/samples/stock-comet/src/main/java/sample/CometServlet.java deleted file mode 100644 index c93dc144dc..0000000000 --- a/sca-java-2.x/contrib/samples/stock-comet/src/main/java/sample/CometServlet.java +++ /dev/null @@ -1,116 +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 sample; - -import java.io.IOException; -import java.util.LinkedList; -import java.util.List; - -import javax.servlet.ServletConfig; -import javax.servlet.ServletException; -import javax.servlet.http.HttpServlet; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - -import org.atmosphere.cpr.AtmosphereResourceEvent; -import org.atmosphere.cpr.AtmosphereResourceEventListener; -import org.atmosphere.cpr.BroadcastFilter; -import org.atmosphere.cpr.Broadcaster; -import org.atmosphere.cpr.Meteor; -import org.atmosphere.util.XSSHtmlFilter; -import org.oasisopen.sca.ComponentContext; - -@SuppressWarnings("serial") -public class CometServlet extends HttpServlet { - - private static final String COMPONENT_CONTEXT_KEY = "org.oasisopen.sca.ComponentContext"; - private static final String COMET_SCOPE_KEY = "org.apache.tuscany.comet.scope"; - private static final String METEOR_KEY = "org.apache.tuscany.comet.meteor"; - - // TODO: check if static variables are a good choice here - private static List filters; - private static Broadcaster.SCOPE scope; - - - @Override - public void init(ServletConfig config) throws ServletException { - super.init(config); - filters = new LinkedList(); - filters.add(new XSSHtmlFilter()); - filters.add(new ScriptFilter()); - String cometScope = getInitParameter(COMET_SCOPE_KEY).trim().toUpperCase(); - scope = Broadcaster.SCOPE.valueOf(cometScope); - } - - @Override - protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - Meteor meteor = Meteor.build(request, scope, filters, null); - meteor.addListener(new CometEventListener()); - request.getSession().setAttribute(METEOR_KEY, meteor); - response.setContentType("text/html"); - meteor.suspend(-1); // http streaming - } - - @Override - protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, - IOException { - Meteor meteor = (Meteor)request.getSession().getAttribute(METEOR_KEY); - - // TODO: add dynamic call and cast - // String component = request.getParameter("component"); - // String method = request.getParameter("method"); - // String params = request.getParameter("params"); - // String componentClass = request.getParameter("component.class"); - - // TODO: check if cc has all the information when servlet is not in - // web.composite - ComponentContext cc = (ComponentContext)getServletContext().getAttribute(COMPONENT_CONTEXT_KEY); - StockService service = cc.getService(StockService.class, "service"); - - // TODO: add JSON serialization - meteor.broadcast(service.getSymbol() + "#" + service.getValue()); - } - - // ---------------------------------------------- - - public class CometEventListener implements AtmosphereResourceEventListener { - - @Override - public void onSuspend(AtmosphereResourceEvent event) { - System.out.println("onSuspend: " + event); - } - - @Override - public void onResume(AtmosphereResourceEvent event) { - System.out.println("onResume: " + event); - } - - @Override - public void onDisconnect(AtmosphereResourceEvent event) { - System.out.println("onDisconnect: " + event); - } - - @Override - public void onBroadcast(AtmosphereResourceEvent event) { - System.out.println("onBroadcast: " + event); - } - - } - -} diff --git a/sca-java-2.x/contrib/samples/stock-comet/src/main/java/sample/ScriptFilter.java b/sca-java-2.x/contrib/samples/stock-comet/src/main/java/sample/ScriptFilter.java deleted file mode 100644 index 61cf1b79df..0000000000 --- a/sca-java-2.x/contrib/samples/stock-comet/src/main/java/sample/ScriptFilter.java +++ /dev/null @@ -1,20 +0,0 @@ -package sample; - -import org.atmosphere.cpr.BroadcastFilter; - -public class ScriptFilter implements BroadcastFilter { - - private static final String BEGIN_SCRIPT_TAG = "\n"; - // TODO: add dynamic function configuration - private static final String DEFAULT_FUNCTION = "window.parent.update"; - - public BroadcastAction filter(Object o) { - if (o instanceof String) { - String message = (String)o; - return new BroadcastAction(BEGIN_SCRIPT_TAG + DEFAULT_FUNCTION + "('" + message + "');\n" + END_SCRIPT_TAG); - } else { - return new BroadcastAction(o); - } - } -} diff --git a/sca-java-2.x/contrib/samples/stock-comet/src/main/java/sample/StockService.java b/sca-java-2.x/contrib/samples/stock-comet/src/main/java/sample/StockService.java deleted file mode 100644 index 46f69834ae..0000000000 --- a/sca-java-2.x/contrib/samples/stock-comet/src/main/java/sample/StockService.java +++ /dev/null @@ -1,27 +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 sample; - -public interface StockService { - - String getSymbol(); - - Double getValue(); - -} diff --git a/sca-java-2.x/contrib/samples/stock-comet/src/main/java/sample/StockServiceImpl.java b/sca-java-2.x/contrib/samples/stock-comet/src/main/java/sample/StockServiceImpl.java deleted file mode 100644 index f6a4d743a1..0000000000 --- a/sca-java-2.x/contrib/samples/stock-comet/src/main/java/sample/StockServiceImpl.java +++ /dev/null @@ -1,41 +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 sample; - -import java.text.DecimalFormat; -import java.util.Date; -import java.util.Random; - -public class StockServiceImpl implements StockService { - - public static final int MAX_VALUE = 1000; - private Random random = new Random(new Date().getTime()); - - @Override - public String getSymbol() { - return "ASF"; - } - - @Override - public Double getValue() { - Double value = Math.abs(random.nextDouble() * random.nextInt(MAX_VALUE)); - return Double.valueOf(new DecimalFormat("#.##").format(value)); - } - -} diff --git a/sca-java-2.x/contrib/samples/stock-comet/src/main/webapp/WEB-INF/context.xml b/sca-java-2.x/contrib/samples/stock-comet/src/main/webapp/WEB-INF/context.xml deleted file mode 100755 index 80763de382..0000000000 --- a/sca-java-2.x/contrib/samples/stock-comet/src/main/webapp/WEB-INF/context.xml +++ /dev/null @@ -1,4 +0,0 @@ - - - - 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 47ab8557ea..5a1d2bb21b 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,12 +22,12 @@ targetNamespace="http://samples" name="Stock"> - - - - - - - - - + + + + + + + + + \ No newline at end of file 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 4a534c043d..66c2ee4480 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 @@ -28,38 +28,8 @@ org.apache.tuscany.sca.host.webapp.TuscanyContextListener - - - CometServlet - CometServlet - org.atmosphere.cpr.MeteorServlet - true - - org.atmosphere.servlet - sample.CometServlet - - - org.apache.tuscany.comet.scope - request - - - - - 0 - - - CometServlet - /comet - - + index.html 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 3ae0ac1ae8..e03a08ccd2 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 @@ -27,24 +27,33 @@ document.getElementById('price').textContent = aux[1]; } - function startMonitoring() { - new Ajax.Request('/stock-comet/comet', + function getQuotes() { + new Ajax.Request('test/StockService/getQuotes', { - method: 'post', + onSuccess: function(response) { + alert("onSuccess"); + }, + onError: function(response) { + alert("onError"); + } }); } + + Event.observe(document, 'DOMContentLoaded', function(e) { + $('comet').src='test/StockService/getQuotes'; + });

Apache Tuscany Asynchronous Servlet Sample

Stock Monitor

- +
N/A N/A
- + -- cgit v1.2.3