From 2b375b835dc838b8768791e227963f1106bf674f Mon Sep 17 00:00:00 2001 From: lucio Date: Fri, 13 May 2022 18:29:37 +0200 Subject: [PATCH] added smartgears app library --- pom.xml | 8 + .../monitor/SmartgearsMonitorServlet.java | 136 ++++++++- src/test/resources/META-INF/frontpage.html | 268 ++++++++++++++++++ 3 files changed, 404 insertions(+), 8 deletions(-) create mode 100644 src/test/resources/META-INF/frontpage.html diff --git a/pom.xml b/pom.xml index 7ebc733..e75314e 100644 --- a/pom.xml +++ b/pom.xml @@ -34,6 +34,14 @@ javax.servlet-api 4.0.1 + + org.gcube.core + common-smartgears + + + org.gcube.core + common-smartgears-app + net.bull.javamelody diff --git a/src/main/java/org/gcube/core/smartgears/monitor/SmartgearsMonitorServlet.java b/src/main/java/org/gcube/core/smartgears/monitor/SmartgearsMonitorServlet.java index fd8252f..0fbd323 100644 --- a/src/main/java/org/gcube/core/smartgears/monitor/SmartgearsMonitorServlet.java +++ b/src/main/java/org/gcube/core/smartgears/monitor/SmartgearsMonitorServlet.java @@ -1,24 +1,144 @@ package org.gcube.core.smartgears.monitor; +import static org.gcube.smartgears.Constants.frontpage_file_path; +import static org.gcube.smartgears.handlers.application.request.RequestError.application_error; +import static org.gcube.smartgears.provider.ProviderFactory.provider; +import static org.gcube.smartgears.utils.Utils.closeSafely; + +import java.io.BufferedReader; import java.io.IOException; -import java.io.PrintWriter; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.util.Collection; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -@WebServlet(urlPatterns = "/", name = "smarartgearsMonitorServlet") +import org.gcube.common.scope.impl.ScopeBean; +import org.gcube.smartgears.ContextProvider; +import org.gcube.smartgears.context.application.ApplicationContext; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +@WebServlet(urlPatterns = "/", name = "smartgearsManagementServlet") public class SmartgearsMonitorServlet extends HttpServlet { - - /** + + /** * */ private static final long serialVersionUID = 1L; + // the variable replacement pattern + private static Pattern pattern = Pattern.compile("\\$\\{(.+?)\\}"); + + // log on behalf of extension + private static final Logger log = LoggerFactory.getLogger(SmartgearsMonitorServlet.class); + + + public static final String mapping = "/"; + + public void doGet(HttpServletRequest request, HttpServletResponse response) - throws IOException { - PrintWriter out = response.getWriter(); - out.println("Smartgers Status is "); - } + throws IOException { + InputStream page = getClass().getResourceAsStream(frontpage_file_path); + + if (page == null) { + log.error("invalid distribution: missing {}", frontpage_file_path); + application_error.fire("invalid distribution: missing " + frontpage_file_path); + } + + Map values = values(); + + BufferedReader reader = null; + try { + + String line = null; + reader = new BufferedReader(new InputStreamReader(page)); + while ((line = reader.readLine()) != null) + response.getWriter().write(interpolate(line, values)); + + } catch (Exception e) { + application_error.fire("could not read " + frontpage_file_path, e); + } finally { + closeSafely(reader); + } + + } + + private Map values() { + + ApplicationContext context = ContextProvider.get(); + + Map values = new HashMap(); + + values.put("profile_link", "[profile link]"); + values.put("config_link", "[config link]"); + + values.put("name", context.name()); + values.put("version", context.configuration().version()); + + String infrastructure = context.container().configuration().infrastructure(); + StringBuilder voValue = new StringBuilder(); + + Collection scopes = context.profile().scopes().asCollection(); + Set vos = new HashSet(); + + //pre-process + for (String scope : scopes) { + ScopeBean bean = new ScopeBean(scope); + switch (bean.type()) { + case INFRASTRUCTURE: + infrastructure = bean.name(); + break; + case VO: + vos.add(bean.name()); + break; + case VRE: + vos.add(bean.enclosingScope().name()); + infrastructure=bean.enclosingScope().enclosingScope().name(); + } + } + + //build vo value + int i = 0; + int max = vos.size()-1; + for (String vo : vos) { + String voPrefix = i == 0 ? "" : (i==max?" and ":", "); + voValue.append(voPrefix+"" + vo + ""); + i++; + } + + values.put("infra", infrastructure); + values.put("vos", voValue.toString()); + + values.put("status", context.lifecycle().state().toString()); + + values.put("smartgears-version", provider().smartgearsConfiguration().version()); + + return values; + } + + public static String interpolate(String text, Map replacements) { + + Matcher matcher = pattern.matcher(text); + StringBuffer buffer = new StringBuffer(); + while (matcher.find()) { + String replacement = replacements.get(matcher.group(1)); + if (replacement != null) { + matcher.appendReplacement(buffer, ""); // safer in case replacements include some of the variable + // characters + buffer.append(replacement); + } + } + matcher.appendTail(buffer); + return buffer.toString(); + } } \ No newline at end of file diff --git a/src/test/resources/META-INF/frontpage.html b/src/test/resources/META-INF/frontpage.html new file mode 100644 index 0000000..2b587fb --- /dev/null +++ b/src/test/resources/META-INF/frontpage.html @@ -0,0 +1,268 @@ + + + + + ${name} + + + +
+
+

${name}

+

v.${version}

+

managed by gCube

+
+
+

Welcome to ${name},

+

a resource of the ${infra} infrastructure shared in ${vos} VOs.

+

The resource is ${status}.

+ +
+
+ + + profile + + + configuration + + SmartGears ${smartgears-version} +
+
+ + \ No newline at end of file