smartgears-management/src/main/java/org/gcube/core/smartgears/monitor/SmartgearsMonitorServlet.java

144 lines
4.1 KiB
Java

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.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;
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 {
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<String, String> 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<String, String> values() {
ApplicationContext context = ContextProvider.get();
Map<String, String> values = new HashMap<String, String>();
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<String> scopes = context.profile().scopes().asCollection();
Set<String> vos = new HashSet<String>();
//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+"<em>" + vo + "</em>");
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<String, String> 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();
}
}