summaryrefslogtreecommitdiffstats
path: root/sca-java-2.x/contrib/samples/async-servlet-sca
diff options
context:
space:
mode:
authorantelder <antelder@13f79535-47bb-0310-9956-ffa450edef68>2010-06-22 11:30:14 +0000
committerantelder <antelder@13f79535-47bb-0310-9956-ffa450edef68>2010-06-22 11:30:14 +0000
commit650382e2bb8b650ab99fd16bdda7c97b565a58d3 (patch)
treed89f2169341592ef80b62b1a848c54da7f5e62dd /sca-java-2.x/contrib/samples/async-servlet-sca
parentec29a8d7084b842c37943e8e036ef981ac7631ff (diff)
TUSCANY-3517: Apply latest GSoC project zip from Florian
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@956851 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to '')
-rw-r--r--sca-java-2.x/contrib/samples/async-servlet-sca/pom.xml50
-rw-r--r--sca-java-2.x/contrib/samples/async-servlet-sca/src/main/java/sample/StockService.java3
-rw-r--r--sca-java-2.x/contrib/samples/async-servlet-sca/src/main/java/sample/StockServiceImpl.java35
-rw-r--r--sca-java-2.x/contrib/samples/async-servlet-sca/src/main/java/sample/StockServlet.java62
-rw-r--r--sca-java-2.x/contrib/samples/async-servlet-sca/src/main/java/sample/TestServlet.java71
-rw-r--r--sca-java-2.x/contrib/samples/async-servlet-sca/src/main/resources/log4j.properties5
-rw-r--r--sca-java-2.x/contrib/samples/async-servlet-sca/src/main/resources/proposals.txt52
-rw-r--r--sca-java-2.x/contrib/samples/async-servlet-sca/src/main/webapp/WEB-INF/web.composite6
-rw-r--r--sca-java-2.x/contrib/samples/async-servlet-sca/src/main/webapp/WEB-INF/web.xml37
-rw-r--r--sca-java-2.x/contrib/samples/async-servlet-sca/src/main/webapp/stock.html7
10 files changed, 224 insertions, 104 deletions
diff --git a/sca-java-2.x/contrib/samples/async-servlet-sca/pom.xml b/sca-java-2.x/contrib/samples/async-servlet-sca/pom.xml
index f70044f97b..f4200c7f93 100644
--- a/sca-java-2.x/contrib/samples/async-servlet-sca/pom.xml
+++ b/sca-java-2.x/contrib/samples/async-servlet-sca/pom.xml
@@ -42,6 +42,18 @@
</dependency>
<dependency>
+ <groupId>org.springframework</groupId>
+ <artifactId>spring</artifactId>
+ <version>2.5.5</version>
+ </dependency>
+
+ <dependency>
+ <groupId>log4j</groupId>
+ <artifactId>log4j</artifactId>
+ <version>1.2.12</version>
+ </dependency>
+
+ <dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.1</version>
@@ -52,43 +64,7 @@
<build>
<finalName>async-servlet</finalName>
- <plugins>
- <plugin>
- <groupId>org.mortbay.jetty</groupId>
- <artifactId>maven-jetty-plugin</artifactId>
- <version>6.1.18</version>
- <configuration>
- <contextPath>helloworld-servlet</contextPath>
- <stopKey>foo</stopKey>
- <stopPort>9999</stopPort>
- </configuration>
- <executions>
- <execution>
- <id>start-jetty</id>
- <phase>process-test-classes</phase>
- <goals>
- <goal>run</goal>
- </goals>
- <configuration>
- <scanIntervalSeconds>0</scanIntervalSeconds>
- <daemon>true</daemon>
- <connectors>
- <connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">
- <port>8085</port>
- </connector>
- </connectors>
- </configuration>
- </execution>
- <execution>
- <id>stop-jetty</id>
- <phase>prepare-package</phase>
- <goals>
- <goal>stop</goal>
- </goals>
- </execution>
- </executions>
- </plugin>
- </plugins>
</build>
+
</project>
diff --git a/sca-java-2.x/contrib/samples/async-servlet-sca/src/main/java/sample/StockService.java b/sca-java-2.x/contrib/samples/async-servlet-sca/src/main/java/sample/StockService.java
index 84d6d90480..d0c387a5f1 100644
--- a/sca-java-2.x/contrib/samples/async-servlet-sca/src/main/java/sample/StockService.java
+++ b/sca-java-2.x/contrib/samples/async-servlet-sca/src/main/java/sample/StockService.java
@@ -18,12 +18,9 @@
*/
package sample;
-import java.util.Observer;
-
public interface StockService {
String getSymbol();
Double getValue();
- void register(Observer observer);
}
diff --git a/sca-java-2.x/contrib/samples/async-servlet-sca/src/main/java/sample/StockServiceImpl.java b/sca-java-2.x/contrib/samples/async-servlet-sca/src/main/java/sample/StockServiceImpl.java
index ebbc7519ff..e49a68120e 100644
--- a/sca-java-2.x/contrib/samples/async-servlet-sca/src/main/java/sample/StockServiceImpl.java
+++ b/sca-java-2.x/contrib/samples/async-servlet-sca/src/main/java/sample/StockServiceImpl.java
@@ -18,46 +18,29 @@
*/
package sample;
+import java.text.DecimalFormat;
import java.util.Date;
-import java.util.Observable;
-import java.util.Observer;
import java.util.Random;
-public class StockServiceImpl extends Observable implements StockService,
- Runnable {
+import org.apache.log4j.Logger;
- public static final int MAX_VALUE = 1000;
- public static final int MAX_SECONDS = 10;
- public static final int MILLIS_PER_SECOND = 1000;
+public class StockServiceImpl implements StockService {
+ public static final int MAX_VALUE = 1000;
private Random random = new Random(new Date().getTime());
+ private Logger logger = Logger.getLogger(StockServiceImpl.class);
@Override
public String getSymbol() {
+ logger.debug("Getting symbol...");
return "ASF";
}
@Override
public Double getValue() {
- return Math.abs(random.nextDouble() * random.nextInt(MAX_VALUE));
- }
-
- @Override
- public void run() {
- try {
- while (true) {
- setChanged();
- notifyObservers();
- Thread.sleep((random.nextInt(MAX_SECONDS - 1) + 1) * MILLIS_PER_SECOND);
- }
- } catch (InterruptedException e) {
- // ignore
- }
- }
-
- @Override
- public void register(Observer observer) {
- this.addObserver(observer);
+ logger.debug("Getting value...");
+ 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/async-servlet-sca/src/main/java/sample/StockServlet.java b/sca-java-2.x/contrib/samples/async-servlet-sca/src/main/java/sample/StockServlet.java
index a705b45f4e..0c0cc6fb2d 100644
--- a/sca-java-2.x/contrib/samples/async-servlet-sca/src/main/java/sample/StockServlet.java
+++ b/sca-java-2.x/contrib/samples/async-servlet-sca/src/main/java/sample/StockServlet.java
@@ -20,42 +20,70 @@ package sample;
import java.io.IOException;
import java.io.Writer;
-import java.util.Observable;
-import java.util.Observer;
+import java.util.Timer;
+import java.util.TimerTask;
import javax.servlet.AsyncContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
+import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
+import org.apache.log4j.Logger;
import org.oasisopen.sca.annotation.Reference;
-@WebServlet(asyncSupported = true, urlPatterns = "stock/")
-public class StockServlet implements Observer {
-
- @Reference
- private StockService service;
+@WebServlet(asyncSupported = true, urlPatterns = "/stock")
+public class StockServlet extends HttpServlet {
+ private static final long serialVersionUID = 1L;
+ private static final long MILLIS_PER_SECOND = 1000L;
+ // @Reference
+ private StockService service = new StockServiceImpl();
private AsyncContext asyncContext;
+ private Timer timer;
+ private long updatePeriod = 1L; // default 1 sec
+ private Logger logger = Logger.getLogger(StockServlet.class);
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
- throws ServletException, IOException {
- asyncContext = req.startAsync(req, resp);
- service.register(this);
+ throws ServletException {
+ logger.debug("Received request.");
+ logger.debug("Starting async context...");
+ asyncContext = req.startAsync();
+ logger.debug("Service reference: " + service);
+ logger.debug("Setting timer...");
+ timer = new Timer();
+ logger.debug("Setting update period...");
+ this.updatePeriod = Long.parseLong(req.getParameter("period"));
+ timer.scheduleAtFixedRate(new StockTask(), 0, updatePeriod * MILLIS_PER_SECOND);
+ logger.debug("Exiting doGet method...");
}
-
- @Override
- public void update(Observable observable, Object arg) {
+
+ public void updateClient() {
try {
- asyncContext.getResponse().setContentType("text/html");
+ logger.debug("Updating response...");
+ asyncContext.getResponse().setContentType("text/plain");
Writer writer = asyncContext.getResponse().getWriter();
- writer.write("<span>Symbol: " + service.getSymbol() + ", Price "
- + service.getValue() + "</span>");
+ writer.write("Symbol: " + service.getSymbol() + ", Price "
+ + service.getValue() + "\n");
writer.flush();
+ asyncContext.getResponse().flushBuffer();
+ logger.debug("Flushed response.");
} catch (IOException e) {
- // ignore
+ logger.debug(e.getMessage(), e);
+ asyncContext.complete();
}
}
+ //-----------------------------------------------------------
+
+ public class StockTask extends TimerTask {
+
+ @Override
+ public void run() {
+ updateClient();
+ }
+
+ }
+
}
diff --git a/sca-java-2.x/contrib/samples/async-servlet-sca/src/main/java/sample/TestServlet.java b/sca-java-2.x/contrib/samples/async-servlet-sca/src/main/java/sample/TestServlet.java
new file mode 100644
index 0000000000..25857dac58
--- /dev/null
+++ b/sca-java-2.x/contrib/samples/async-servlet-sca/src/main/java/sample/TestServlet.java
@@ -0,0 +1,71 @@
+/*
+ * 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.io.Writer;
+
+import javax.servlet.AsyncContext;
+import javax.servlet.ServletException;
+import javax.servlet.annotation.WebServlet;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.log4j.Logger;
+
+@WebServlet(asyncSupported = true, urlPatterns = "/test")
+public class TestServlet extends HttpServlet {
+ private static final long serialVersionUID = 1L;
+
+ private AsyncContext asyncContext;
+ private Logger logger = Logger.getLogger(TestServlet.class);
+
+ protected void doGet(HttpServletRequest req, HttpServletResponse resp)
+ throws ServletException {
+ logger.debug("Received test request.");
+ logger.debug("Gathering async context...");
+ asyncContext = req.startAsync();
+ resp.setContentType("text/plain");
+
+ logger.debug("Gathering writer...");
+ Writer writer;
+ try {
+ writer = asyncContext.getResponse().getWriter();
+ for (int i = 0; i < 10; i++) {
+ Thread.sleep(1000);
+ logger.debug("Sending " + i + "...");
+ writer.write("" + i + "\n");
+ writer.flush();
+ resp.flushBuffer();
+ }
+
+ logger.debug("Completing response...");
+ asyncContext.complete();
+ writer.close();
+ } catch (IOException e1) {
+ asyncContext.complete();
+ e1.printStackTrace();
+ } catch (InterruptedException e) {
+ asyncContext.complete();
+ e.printStackTrace();
+ }
+ }
+
+} \ No newline at end of file
diff --git a/sca-java-2.x/contrib/samples/async-servlet-sca/src/main/resources/log4j.properties b/sca-java-2.x/contrib/samples/async-servlet-sca/src/main/resources/log4j.properties
new file mode 100644
index 0000000000..aa524fcfa8
--- /dev/null
+++ b/sca-java-2.x/contrib/samples/async-servlet-sca/src/main/resources/log4j.properties
@@ -0,0 +1,5 @@
+log4j.rootLogger=DEBUG, CA
+#Console Appender
+log4j.appender.CA=org.apache.log4j.ConsoleAppender
+log4j.appender.CA.layout=org.apache.log4j.PatternLayout
+log4j.appender.CA.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
diff --git a/sca-java-2.x/contrib/samples/async-servlet-sca/src/main/resources/proposals.txt b/sca-java-2.x/contrib/samples/async-servlet-sca/src/main/resources/proposals.txt
new file mode 100644
index 0000000000..de0d1bb26d
--- /dev/null
+++ b/sca-java-2.x/contrib/samples/async-servlet-sca/src/main/resources/proposals.txt
@@ -0,0 +1,52 @@
+web.composite
+~~~~~~~~~~~~~
+
+<composite xmlns="http://docs.oasis-open.org/ns/opencsa/sca/200912"
+ xmlns:tuscany="http://tuscany.apache.org/xmlns/sca/1.1"
+ targetNamespace="http://samples"
+ name="Stock">
+
+ <component name="WebPage">
+ <implementation.?/>
+ <callback>
+ <binding.? url="/stock/**" method="callbackJsMethod"/>
+ </callback>
+ </component>
+
+ <component name="Servlet">
+ <implementation.web web-uri=""/>
+ <reference name="service" target="StockComponent"/>
+ </component>
+
+ <component name="StockComponent">
+ <implementation.java class="sample.StockServiceImpl"/>
+ </component>
+
+</composite>
+
+
+stock.html
+~~~~~~~~~~
+
+<html>
+
+<head>
+ <title>Apache Tuscany Asynchronous Servlet Sample</title>
+ <script type="text/javascript" src="org.apache.tuscany.callbackListener.js"></script>
+
+ <script language="JavaScript">
+ function callbackJsMethod(response) {
+ // process received response
+ }
+ </script>
+</head>
+
+<body>
+ <h2>Apache Tuscany Asynchronous Servlet Sample</h2>
+
+ <h3>Stock Monitor</h3>
+
+ <input type="button" value="Start monitoring" onclick="">
+</body>
+
+</html> \ No newline at end of file
diff --git a/sca-java-2.x/contrib/samples/async-servlet-sca/src/main/webapp/WEB-INF/web.composite b/sca-java-2.x/contrib/samples/async-servlet-sca/src/main/webapp/WEB-INF/web.composite
index 6a2abb920b..738a79e450 100644
--- a/sca-java-2.x/contrib/samples/async-servlet-sca/src/main/webapp/WEB-INF/web.composite
+++ b/sca-java-2.x/contrib/samples/async-servlet-sca/src/main/webapp/WEB-INF/web.composite
@@ -22,12 +22,12 @@
targetNamespace="http://samples"
name="Stock">
- <component name="Servlet">
+ <component name="StockServlet">
<implementation.web web-uri=""/>
- <reference name="service" target="StockComponent"/>
+ <reference name="service" target="StockService"/>
</component>
- <component name="StockComponent">
+ <component name="StockService">
<implementation.java class="sample.StockServiceImpl"/>
</component>
diff --git a/sca-java-2.x/contrib/samples/async-servlet-sca/src/main/webapp/WEB-INF/web.xml b/sca-java-2.x/contrib/samples/async-servlet-sca/src/main/webapp/WEB-INF/web.xml
index 031f0d110f..fee71f3bcf 100644
--- a/sca-java-2.x/contrib/samples/async-servlet-sca/src/main/webapp/WEB-INF/web.xml
+++ b/sca-java-2.x/contrib/samples/async-servlet-sca/src/main/webapp/WEB-INF/web.xml
@@ -17,28 +17,31 @@
* specific language governing permissions and limitations
* under the License.
-->
-<web-app version="2.4"
- xmlns="http://java.sun.com/xml/ns/j2ee"
- 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_2_4.xsd" >
+<web-app xmlns="http://java.sun.com/xml/ns/javaee"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
+ http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
+ version="3.0"
+ metadata-complete="false">
- <display-name>Apache Tuscany Asynchronous Servlet Sample</display-name>
+ <display-name>Apache Tuscany Asynchronous Servlet Sample</display-name>
- <listener><listener-class>org.apache.tuscany.sca.host.webapp.TuscanyContextListener</listener-class></listener>
+ <listener>
+ <listener-class>org.apache.tuscany.sca.host.webapp.TuscanyContextListener</listener-class>
+ </listener>
- <servlet>
- <servlet-name>AsyncServlet</servlet-name>
- <servlet-class>sample.StockServlet</servlet-class>
- </servlet>
+ <context-param>
+ <param-name>log4jConfigLocation</param-name>
+ <param-value>WEB-INF/classes/log4j.properties</param-value>
+ </context-param>
- <servlet-mapping>
- <servlet-name>AsyncServlet</servlet-name>
- <url-pattern>/AsyncServlet</url-pattern>
- </servlet-mapping>
+ <listener>
+ <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
+ </listener>
- <welcome-file-list id="WelcomeFileList">
- <welcome-file>stock.html</welcome-file>
- </welcome-file-list>
+ <welcome-file-list id="WelcomeFileList">
+ <welcome-file>stock.html</welcome-file>
+ </welcome-file-list>
</web-app>
diff --git a/sca-java-2.x/contrib/samples/async-servlet-sca/src/main/webapp/stock.html b/sca-java-2.x/contrib/samples/async-servlet-sca/src/main/webapp/stock.html
index 394bee04b5..9fc1d37791 100644
--- a/sca-java-2.x/contrib/samples/async-servlet-sca/src/main/webapp/stock.html
+++ b/sca-java-2.x/contrib/samples/async-servlet-sca/src/main/webapp/stock.html
@@ -28,7 +28,12 @@
<h3>Stock Monitor</h3>
-<input type="button" value="Start monitoring" onclick="">
+<form action="stock" method="GET">
+ <label>Enter the update period (seconds): </label>
+ <input type="text" name="period" size="2"/>
+ <br/>
+ <button type="submit">Start monitoring</button>
+</form>
</body>
</html> \ No newline at end of file