com.google.apphosting.utils.servlet.ModulesServlet Maven / Gradle / Ivy
/*
* Copyright 2021 Google LLC
*
* Licensed 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
*
* https://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 com.google.apphosting.utils.servlet;
import com.google.appengine.tools.development.ModulesController;
import com.google.apphosting.api.ApiProxy;
import com.google.appengine.repackaged.com.google.common.base.Function;
import com.google.appengine.repackaged.com.google.common.collect.ImmutableList;
import com.google.appengine.repackaged.com.google.common.collect.ImmutableMap;
import com.google.appengine.repackaged.com.google.common.collect.Iterables;
import java.io.IOException;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.Map;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Handler for the modules viewer.
*
* Shows a list of all configured modules and the state of each module instance.
*
*/
@SuppressWarnings("serial")
public class ModulesServlet extends HttpServlet {
private static final String AH_ADMIN_MODULES_PATH = "/_ah/admin/modules";
private static final Logger logger = Logger.getLogger(ModulesServlet.class.getName());
private static final String DEFAULT_MODULE_NAME = "defaultModuleName";
private static final String APPLICATION_NAME = "applicationName";
private static final String MODULES_STATE_INFO = "modulesStateInfo";
// "ACTIONS" posted to this servlet
private static final String ACTION_MODULE = "action:module";
public ModulesServlet() {}
private ModulesController getModulesController() {
return (ModulesController)
ApiProxy.getCurrentEnvironment()
.getAttributes()
.get(ModulesController.MODULES_CONTROLLER_ATTRIBUTE_KEY);
}
private ImmutableList getAllInstanceHostnames(String moduleName, String version) {
ImmutableList.Builder hostnameListBuilder = ImmutableList.builder();
for (int i = 0; i < getModulesController().getNumInstances(moduleName, version); i++) {
hostnameListBuilder.add(getModulesController().getHostname(moduleName, version, i));
}
return hostnameListBuilder.build();
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
req.setAttribute(APPLICATION_NAME, ApiProxy.getCurrentEnvironment().getAppId());
final ModulesController modulesController = getModulesController();
req.setAttribute(DEFAULT_MODULE_NAME,
Iterables.getFirst(modulesController.getModuleNames(), ""));
Iterable