Repository: aries-containers Updated Branches: refs/heads/master e99817a18 -> 32d7d671e
OSGi-based demo. Project: http://git-wip-us.apache.org/repos/asf/aries-containers/repo Commit: http://git-wip-us.apache.org/repos/asf/aries-containers/commit/32d7d671 Tree: http://git-wip-us.apache.org/repos/asf/aries-containers/tree/32d7d671 Diff: http://git-wip-us.apache.org/repos/asf/aries-containers/diff/32d7d671 Branch: refs/heads/master Commit: 32d7d671ec55139675f8db920bbb9963e5db5bad Parents: e99817a Author: David Bosschaert <[email protected]> Authored: Mon May 29 15:46:49 2017 +0100 Committer: David Bosschaert <[email protected]> Committed: Mon May 29 15:46:49 2017 +0100 ---------------------------------------------------------------------- .../containers-example-osgiservlet/pom.xml | 66 ++++++++++ .../examples/osgiservlet/MyServletContext.java | 30 +++++ .../osgiservlet/ServiceManagerServlet.java | 119 +++++++++++++++++++ 3 files changed, 215 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/aries-containers/blob/32d7d671/containers-examples/containers-example-osgiservlet/pom.xml ---------------------------------------------------------------------- diff --git a/containers-examples/containers-example-osgiservlet/pom.xml b/containers-examples/containers-example-osgiservlet/pom.xml new file mode 100644 index 0000000..8dc0764 --- /dev/null +++ b/containers-examples/containers-example-osgiservlet/pom.xml @@ -0,0 +1,66 @@ +<?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 xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> + + <modelVersion>4.0.0</modelVersion> + + <parent> + <groupId>org.apache.aries.containers</groupId> + <artifactId>org.apache.aries.containers.parent</artifactId> + <version>0.0.1-SNAPSHOT</version> + <relativePath>../containers-parent</relativePath> + </parent> + + <artifactId>org.apache.aries.containers.examples.osgiservlet</artifactId> + <packaging>jar</packaging> + <name>Container Example OSGi Servlet</name> + <description>A simple example OSGi bundle providing a basic servlet to interact with Aries Containers</description> + + <dependencies> + <dependency> + <groupId>${project.groupId}</groupId> + <artifactId>org.apache.aries.containers.api</artifactId> + <version>${project.version}</version> + <scope>provided</scope> + </dependency> + + <dependency> + <groupId>javax.servlet</groupId> + <artifactId>javax.servlet-api</artifactId> + <version>3.0.1</version> + <scope>provided</scope> + </dependency> + + <dependency> + <groupId>org.osgi</groupId> + <artifactId>org.osgi.service.component.annotations</artifactId> + <version>1.3.0</version> + <scope>provided</scope> + </dependency> + + <dependency> + <groupId>org.osgi</groupId> + <artifactId>org.osgi.service.http.whiteboard</artifactId> + <version>1.0.0</version> + <scope>provided</scope> + </dependency> + </dependencies> +</project> + http://git-wip-us.apache.org/repos/asf/aries-containers/blob/32d7d671/containers-examples/containers-example-osgiservlet/src/main/java/org/apache/aries/containers/examples/osgiservlet/MyServletContext.java ---------------------------------------------------------------------- diff --git a/containers-examples/containers-example-osgiservlet/src/main/java/org/apache/aries/containers/examples/osgiservlet/MyServletContext.java b/containers-examples/containers-example-osgiservlet/src/main/java/org/apache/aries/containers/examples/osgiservlet/MyServletContext.java new file mode 100644 index 0000000..74d8736 --- /dev/null +++ b/containers-examples/containers-example-osgiservlet/src/main/java/org/apache/aries/containers/examples/osgiservlet/MyServletContext.java @@ -0,0 +1,30 @@ +/* + * 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.aries.containers.examples.osgiservlet; + +import org.osgi.service.component.annotations.Component; +import org.osgi.service.http.context.ServletContextHelper; +import org.osgi.service.http.whiteboard.HttpWhiteboardConstants; + +@Component(service = ServletContextHelper.class, property = { + HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_NAME + "=" + MyServletContext.NAME, + HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_PATH + "=/containers" }) +public class MyServletContext extends ServletContextHelper { + public static final String NAME = "org.apache.aries.containers.examples.osgiservlet"; +} http://git-wip-us.apache.org/repos/asf/aries-containers/blob/32d7d671/containers-examples/containers-example-osgiservlet/src/main/java/org/apache/aries/containers/examples/osgiservlet/ServiceManagerServlet.java ---------------------------------------------------------------------- diff --git a/containers-examples/containers-example-osgiservlet/src/main/java/org/apache/aries/containers/examples/osgiservlet/ServiceManagerServlet.java b/containers-examples/containers-example-osgiservlet/src/main/java/org/apache/aries/containers/examples/osgiservlet/ServiceManagerServlet.java new file mode 100644 index 0000000..5783718 --- /dev/null +++ b/containers-examples/containers-example-osgiservlet/src/main/java/org/apache/aries/containers/examples/osgiservlet/ServiceManagerServlet.java @@ -0,0 +1,119 @@ +/* + * 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.aries.containers.examples.osgiservlet; + +import java.io.IOException; +import java.io.PrintWriter; +import java.util.Arrays; + +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.aries.containers.ServiceConfig; +import org.apache.aries.containers.ServiceConfig.Builder; +import org.apache.aries.containers.ServiceManager; +import org.osgi.service.component.annotations.Component; +import org.osgi.service.component.annotations.Reference; +import org.osgi.service.http.whiteboard.HttpWhiteboardConstants; + +@Component(service = Servlet.class, + property = {HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN + "=/manager", + HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_SELECT + "=(" + + HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_NAME + "=" + MyServletContext.NAME + ")" + }) +public class ServiceManagerServlet extends HttpServlet { + private static final long serialVersionUID = 1L; + + @Reference + ServiceManager serviceManager; + + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + resp.setContentType("text/html"); + + PrintWriter pw = resp.getWriter(); + pw.println("<HTML>"); + pw.println(getHeader()); + pw.println("<BODY><H1>Service Deployments</H1>"); + + pw.println("<UL>"); + try { + for (String dep : serviceManager.listServices()) { + pw.println("<LI>" + dep); + } + } catch (Exception e) { + throw new ServletException(e); + } + pw.println("</UL>"); + + pw.println("<FORM METHOD='POST'>New Container: <BR>" + + "<LABEL CLASS='l1' FOR='name'>Name: </LABEL><INPUT TYPE='text' NAME='name' VALUE='myapache' CLASS='l1'><BR>" + + "<LABEL CLASS='l1' FOR='image'>Image: </LABEL><INPUT TYPE='text' NAME='image' VALUE='httpd' CLASS='l1'><BR>" + + "<LABEL CLASS='l1' FOR='cpu'>CPU: </LABEL><INPUT TYPE='text' NAME='cpu' VALUE='0.2' CLASS='l1'>units<BR>" + + "<LABEL CLASS='l1' FOR='memory'>Memory: </LABEL><INPUT TYPE='text' NAME='memory' VALUE='64' CLASS='l1'>mb<BR>" + + "<LABEL CLASS='l1' FOR='ports'>Ports: </LABEL><INPUT TYPE='text' NAME='ports' VALUE='80' CLASS='l1'>(space separated)<BR>" + + "<INPUT TYPE='submit' VALUE='create!'>"); + pw.println("</FORM>"); + pw.println("</BODY></HTML>"); + } + + @Override + protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + resp.setContentType("text/html"); + + String name = req.getParameter("name"); + String image = req.getParameter("image"); + double cpu = Double.parseDouble(req.getParameter("cpu")); + int memory = Integer.parseInt(req.getParameter("memory")); + int[] ports = Arrays.stream(req.getParameter("ports").split(" ")).mapToInt(Integer::parseInt).toArray(); + + Builder builder = ServiceConfig.builder(name, image).cpu(cpu).memory(memory); + for (int p : ports) { + builder.port(p); + } + + try { + serviceManager.getService(builder.build()); + } catch (Exception e) { + throw new ServletException(e); + } + + PrintWriter pw = resp.getWriter(); + pw.println("<HTML>"); + pw.println(getHeader()); + pw.println("<BODY><H1>Service Created!</H1>"); + pw.println("<A HREF='manager'>List Services</A>"); + pw.println("</BODY>"); + } + + private String getHeader() { + return ("<HEAD><TITLE>Service Deployments</TITLE>" + + "<STYLE>" + + "label.l1 {" +// + " text-align: right;" + + " width: 100px;" + + " float: left;" + + "}" + + "</STYLE>" + + "</HEAD>"); + } +}
