diff options
author | dims <dims@13f79535-47bb-0310-9956-ffa450edef68> | 2008-06-17 00:23:01 +0000 |
---|---|---|
committer | dims <dims@13f79535-47bb-0310-9956-ffa450edef68> | 2008-06-17 00:23:01 +0000 |
commit | bdd0a41aed7edf21ec2a65cfa17a86af2ef8c48a (patch) | |
tree | 38a92061c0793434c4be189f1d70c3458b6bc41d /tags/java/tsss-demo/demo.app/src |
Move Tuscany from Incubator to top level.
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@668359 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'tags/java/tsss-demo/demo.app/src')
2 files changed, 222 insertions, 0 deletions
diff --git a/tags/java/tsss-demo/demo.app/src/main/java/org/apache/tuscany/demo/app/CalculatorForm.java b/tags/java/tsss-demo/demo.app/src/main/java/org/apache/tuscany/demo/app/CalculatorForm.java new file mode 100644 index 0000000000..e997032217 --- /dev/null +++ b/tags/java/tsss-demo/demo.app/src/main/java/org/apache/tuscany/demo/app/CalculatorForm.java @@ -0,0 +1,92 @@ +/* + * 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.demo.app; + +import java.io.IOException; +import java.io.PrintWriter; + +import javax.servlet.Servlet; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.apache.tuscany.spi.host.ServletHost; +import org.osoa.sca.annotations.EagerInit; +import org.osoa.sca.annotations.Reference; +import org.osoa.sca.annotations.Service; + +/** + * @version $Revision$ $Date$ + */ +@SuppressWarnings("serial") +@EagerInit +@Service(Servlet.class) +public class CalculatorForm extends HttpServlet { + + /** + * Injects the servlet host. + * + * @param servletHost Servlet host to use. + */ + @Reference + public void setServletHost(ServletHost servletHost) { + servletHost.registerMapping("/calculatorForm", this); + } + + /** + * Processes the request. + * + * @param req Servlet request. + * @param res Servlet response. + * @throws ServletException Servlet exception. + * @throws IOException IO Exception. + */ + protected void process(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { + + PrintWriter writer = res.getWriter(); + writer.println("<form name='' method='post' action='calculate'>"); + writer.println("<input name='op1' type='text' size='10'>"); + writer.println("<br/>"); + writer.println("<input name='op2' type='text' size='10'>"); + writer.println("<br/>"); + writer.println("<input type='submit' value='Add'/>"); + writer.println("</form>"); + writer.flush(); + writer.close(); + + } + + /** + * Handles get requests. + */ + @Override + protected final void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { + process(req, res); + } + + /** + * Handles post request. + */ + @Override + protected final void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { + process(req, res); + } + +} diff --git a/tags/java/tsss-demo/demo.app/src/main/java/org/apache/tuscany/demo/app/CalculatorHandler.java b/tags/java/tsss-demo/demo.app/src/main/java/org/apache/tuscany/demo/app/CalculatorHandler.java new file mode 100644 index 0000000000..771f0828ca --- /dev/null +++ b/tags/java/tsss-demo/demo.app/src/main/java/org/apache/tuscany/demo/app/CalculatorHandler.java @@ -0,0 +1,130 @@ +/* + * 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.demo.app; + +import java.io.IOException; +import java.io.PrintWriter; +import java.net.URI; +import java.net.URISyntaxException; + +import javax.servlet.Servlet; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.apache.tuscany.core.component.SimpleWorkContext; +import org.apache.tuscany.core.implementation.PojoWorkContextTunnel; +import org.apache.tuscany.core.implementation.java.JavaComponent; +import org.apache.tuscany.spi.component.ComponentManager; +import org.apache.tuscany.spi.component.InstanceWrapper; +import org.apache.tuscany.spi.component.ScopeContainer; +import org.apache.tuscany.spi.component.TargetResolutionException; +import org.apache.tuscany.spi.host.ServletHost; +import org.osoa.sca.annotations.EagerInit; +import org.osoa.sca.annotations.Reference; +import org.osoa.sca.annotations.Service; + +import calculator.CalculatorService; + +/** + * @version $Revision$ $Date$ + */ +@SuppressWarnings("serial") +@EagerInit +@Service(Servlet.class) +public class CalculatorHandler extends HttpServlet { + + // Calculator service + private ComponentManager componentManager; + + /** + * Injects the servlet host. + * + * @param servletHost Servlet host to use. + */ + @Reference + public void setServletHost(ServletHost servletHost) { + servletHost.registerMapping("/calculate", this); + } + + /** + * Injects the calculator. + * + * @param componentManager Component manager. + */ + @Reference + public void setComponentManager(ComponentManager componentManager) { + this.componentManager = componentManager; + } + + /** + * Processes the request. + * + * @param req Servlet request. + * @param res Servlet response. + * @throws ServletException Servlet exception. + * @throws IOException IO Exception. + */ + protected void process(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { + + + try { + + PojoWorkContextTunnel.setThreadWorkContext(new SimpleWorkContext()); + + URI calculatorServiceUri = new URI("tuscany://./domain/CalculatorServiceComponent"); + + JavaComponent javaComponent = (JavaComponent) componentManager.getComponent(calculatorServiceUri); + + ScopeContainer scopeContainer = javaComponent.getScopeContainer(); + InstanceWrapper wrapper = scopeContainer.getWrapper(javaComponent, new URI("tuscany://./domain")); + CalculatorService calculatorService = (CalculatorService) wrapper.getInstance(); + double op1 = Double.parseDouble(req.getParameter("op1")); + double op2 = Double.parseDouble(req.getParameter("op2")); + double result = calculatorService.add(op1, op2); + + PrintWriter writer = res.getWriter(); + writer.println("Result: " + result); + writer.flush(); + writer.close(); + } catch (URISyntaxException ex) { + throw new ServletException(ex); + } catch (TargetResolutionException ex) { + throw new ServletException(ex); + } + } + + /** + * Handles get requests. + */ + @Override + protected final void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { + process(req, res); + } + + /** + * Handles post request. + */ + @Override + protected final void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { + process(req, res); + } + +} |