Unit test for docker local health checks.
[aries-containers.git] / containers-examples / containers-example-osgiservlet / src / main / java / org / apache / aries / containers / examples / osgiservlet / ServiceManagerServlet.java
1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19 package org.apache.aries.containers.examples.osgiservlet;
20
21 import java.io.IOException;
22 import java.io.PrintWriter;
23 import java.util.Arrays;
24
25 import javax.servlet.Servlet;
26 import javax.servlet.ServletException;
27 import javax.servlet.http.HttpServlet;
28 import javax.servlet.http.HttpServletRequest;
29 import javax.servlet.http.HttpServletResponse;
30
31 import org.apache.aries.containers.ServiceConfig;
32 import org.apache.aries.containers.ServiceConfig.Builder;
33 import org.apache.aries.containers.ServiceManager;
34 import org.osgi.service.component.annotations.Component;
35 import org.osgi.service.component.annotations.Reference;
36 import org.osgi.service.http.whiteboard.HttpWhiteboardConstants;
37
38 @Component(service = Servlet.class,
39 property = {HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN + "=/manager",
40 HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_SELECT + "=(" +
41 HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_NAME + "=" + MyServletContext.NAME + ")"
42 })
43 public class ServiceManagerServlet extends HttpServlet {
44 private static final long serialVersionUID = 1L;
45
46 @Reference
47 ServiceManager serviceManager;
48
49 @Override
50 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
51 resp.setContentType("text/html");
52
53 PrintWriter pw = resp.getWriter();
54 pw.println("<HTML>");
55 pw.println(getHeader());
56 pw.println("<BODY><H1>Service Deployments</H1>");
57
58 pw.println("<UL>");
59 try {
60 for (String dep : serviceManager.listServices()) {
61 pw.println("<LI>" + dep);
62 }
63 } catch (Exception e) {
64 throw new ServletException(e);
65 }
66 pw.println("</UL>");
67
68 pw.println("<FORM METHOD='POST'>New Container: <BR>"
69 + "<LABEL CLASS='l1' FOR='name'>Name: </LABEL><INPUT TYPE='text' NAME='name' VALUE='myapache' CLASS='l1'><BR>"
70 + "<LABEL CLASS='l1' FOR='image'>Image: </LABEL><INPUT TYPE='text' NAME='image' VALUE='httpd' CLASS='l1'><BR>"
71 + "<LABEL CLASS='l1' FOR='cpu'>CPU: </LABEL><INPUT TYPE='text' NAME='cpu' VALUE='0.2' CLASS='l1'>units<BR>"
72 + "<LABEL CLASS='l1' FOR='memory'>Memory: </LABEL><INPUT TYPE='text' NAME='memory' VALUE='64' CLASS='l1'>mb<BR>"
73 + "<LABEL CLASS='l1' FOR='ports'>Ports: </LABEL><INPUT TYPE='text' NAME='ports' VALUE='80' CLASS='l1'>(space separated)<BR>"
74 + "<INPUT TYPE='submit' VALUE='create!'>");
75 pw.println("</FORM>");
76 pw.println("</BODY></HTML>");
77 }
78
79 @Override
80 protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
81 resp.setContentType("text/html");
82
83 String name = req.getParameter("name");
84 String image = req.getParameter("image");
85 double cpu = Double.parseDouble(req.getParameter("cpu"));
86 int memory = Integer.parseInt(req.getParameter("memory"));
87 int[] ports = Arrays.stream(req.getParameter("ports").split(" ")).mapToInt(Integer::parseInt).toArray();
88
89 Builder builder = ServiceConfig.builder(name, image).cpu(cpu).memory(memory);
90 for (int p : ports) {
91 builder.port(p);
92 }
93
94 try {
95 serviceManager.getService(builder.build());
96 } catch (Exception e) {
97 throw new ServletException(e);
98 }
99
100 PrintWriter pw = resp.getWriter();
101 pw.println("<HTML>");
102 pw.println(getHeader());
103 pw.println("<BODY><H1>Service Created!</H1>");
104 pw.println("<A HREF='manager'>List Services</A>");
105 pw.println("</BODY>");
106 }
107
108 private String getHeader() {
109 return ("<HEAD><TITLE>Service Deployments</TITLE>"
110 + "<STYLE>"
111 + "label.l1 {"
112 + " width: 100px;"
113 + " float: left;"
114 + "}"
115 + "</STYLE>"
116 + "</HEAD>");
117 }
118 }