Search in sources :

Example 1 with IMangoLifecycle

use of com.serotonin.m2m2.IMangoLifecycle in project ma-core-public by infiniteautomation.

the class StatusServlet method doGet.

/* (non-Javadoc)
	 * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    IMangoLifecycle lifecycle = Providers.get(IMangoLifecycle.class);
    String timeString = request.getParameter("time");
    long time = -1;
    if (timeString != null) {
        try {
            time = Long.parseLong(timeString);
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
        }
    }
    response.setContentType("application/json;charset=utf-8");
    response.setStatus(HttpServletResponse.SC_OK);
    response.setHeader("Access-Control-Allow-Origin", "*");
    // Get the Info and pack it up
    Map<String, Object> data = new HashMap<String, Object>();
    StringWriter sw = new StringWriter();
    JsonWriter writer = new JsonWriter(Common.JSON_CONTEXT, sw);
    // Limit to logged in users while running
    if ((lifecycle.getLifecycleState() != IMangoLifecycle.RUNNING) || (Common.getHttpUser() != null)) {
        data.put("messages", LoggingConsoleRT.instance.getMessagesSince(time));
    } else {
        data.put("messages", new ArrayList<String>());
    }
    data.put("startupProgress", lifecycle.getStartupProgress());
    data.put("shutdownProgress", lifecycle.getShutdownProgress());
    data.put("state", getLifecycleStateMessage(lifecycle.getLifecycleState()));
    // Can only get the Startup URI once the database is initalized
    if (lifecycle.getLifecycleState() > IMangoLifecycle.DATABASE_INITIALIZE) {
        Common.envProps.getBoolean("", false);
        Boolean isSsl = Common.envProps.getBoolean("ssl.on", false);
        String uri;
        if (isSsl) {
            int port = Common.envProps.getInt("ssl.port", 443);
            uri = "https://" + request.getServerName() + ":" + port + DefaultPagesDefinition.getLoginUri(request, response);
        } else {
            uri = DefaultPagesDefinition.getLoginUri(request, response);
        }
        data.put("startupUri", uri);
    }
    try {
        writer.writeObject(data);
        response.getWriter().write(sw.toString());
    } catch (JsonException e) {
        LOG.error(e);
    }
}
Also used : JsonException(com.serotonin.json.JsonException) HashMap(java.util.HashMap) JsonWriter(com.serotonin.json.JsonWriter) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) JsonException(com.serotonin.json.JsonException) StringWriter(java.io.StringWriter) IMangoLifecycle(com.serotonin.m2m2.IMangoLifecycle)

Example 2 with IMangoLifecycle

use of com.serotonin.m2m2.IMangoLifecycle in project ma-core-public by infiniteautomation.

the class ModulesDwr method scheduleShutdown.

@DwrPermission(admin = true)
public static ProcessResult scheduleShutdown() {
    ProcessResult result = new ProcessResult();
    synchronized (SHUTDOWN_TASK_LOCK) {
        if (SHUTDOWN_TASK == null) {
            long timeout = Common.getMillis(Common.TimePeriods.SECONDS, 10);
            // Ensure our lifecycle state is set to PRE_SHUTDOWN
            IMangoLifecycle lifecycle = Providers.get(IMangoLifecycle.class);
            SHUTDOWN_TASK = lifecycle.scheduleShutdown(timeout, false, Common.getHttpUser());
            // Get the redirect page
            result.addData("shutdownUri", "/shutdown.htm");
        } else {
            result.addData("message", Common.translate("modules.shutdownAlreadyScheduled"));
        }
    }
    return result;
}
Also used : ProcessResult(com.serotonin.m2m2.i18n.ProcessResult) IMangoLifecycle(com.serotonin.m2m2.IMangoLifecycle) DwrPermission(com.serotonin.m2m2.web.dwr.util.DwrPermission)

Example 3 with IMangoLifecycle

use of com.serotonin.m2m2.IMangoLifecycle in project ma-core-public by infiniteautomation.

the class StartupDwr method getStartupProgress.

@DwrPermission(anonymous = true)
public ProcessResult getStartupProgress(long since) {
    ProcessResult result = new ProcessResult();
    IMangoLifecycle lifecycle = Providers.get(IMangoLifecycle.class);
    float progress = lifecycle.getStartupProgress();
    float shutdownProgress = lifecycle.getShutdownProgress();
    List<String> messages = LoggingConsoleRT.instance.getMessagesSince(since);
    StringBuilder builder = new StringBuilder();
    for (String message : messages) {
        builder.append(message);
        builder.append("</br>");
    }
    result.addData("message", builder.toString());
    result.addData("startupProgress", progress);
    result.addData("shutdownProgress", shutdownProgress);
    result.addData("state", getLifecycleStateMessage(lifecycle.getLifecycleState()));
    if ((progress >= 100) && (shutdownProgress == 0)) {
        WebContext ctx = WebContextFactory.get();
        result.addData("startupUri", DefaultPagesDefinition.getLoginUri(ctx.getHttpServletRequest(), ctx.getHttpServletResponse()));
    }
    // Add the message to describe what process we are in
    if ((progress < 100) && (shutdownProgress == 0)) {
        result.addData("processMessage", this.translations.translate("startup.startingUp"));
    } else if ((shutdownProgress > 0) && (lifecycle.isRestarting())) {
        result.addData("processMessage", this.translations.translate("shutdown.restarting"));
    } else if (shutdownProgress > 0) {
        result.addData("processMessage", this.translations.translate("shutdown.shuttingDown"));
    }
    if ((progress == 100) && (shutdownProgress == 0)) {
        result.addData("processMessage", this.translations.translate("startup.state.running"));
    }
    return result;
}
Also used : WebContext(org.directwebremoting.WebContext) ProcessResult(com.serotonin.m2m2.i18n.ProcessResult) IMangoLifecycle(com.serotonin.m2m2.IMangoLifecycle) DwrPermission(com.serotonin.m2m2.web.dwr.util.DwrPermission)

Example 4 with IMangoLifecycle

use of com.serotonin.m2m2.IMangoLifecycle in project ma-core-public by infiniteautomation.

the class StartupContextHandler method handle.

@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
    if (restRequestMatcher.matches(request)) {
        // Return options response
        response.setStatus(HttpStatus.SERVICE_UNAVAILABLE_503);
        response.setContentLength(0);
        IMangoLifecycle lifecycle = Providers.get(IMangoLifecycle.class);
        Float progress = lifecycle.getStartupProgress();
        int state = lifecycle.getLifecycleState();
        response.setHeader("Mango-Startup-Progress", String.format("%d", progress.intValue()));
        response.setHeader("Mango-Startup-State", this.statusServlet.getLifecycleStateMessage(state));
        baseRequest.setHandled(true);
    } else if (statusRequestMatcher.matches(request)) {
        statusServlet.handleRequest(request, response);
        baseRequest.setHandled(true);
    } else if (resourceRequestMatcher.matches(request)) {
        super.handle(target, baseRequest, request, response);
    } else if (pageRequestMatcher.matches(request)) {
        // Check to see if there are any default pages defined for this
        String uri = DefaultPagesDefinition.getStartupUri(request, response);
        if (uri != null) {
            RequestDispatcher dispatcher = request.getRequestDispatcher(uri);
            dispatcher.forward(request, response);
        } else {
            response.setContentType("text/html;charset=utf-8");
            response.setStatus(HttpStatus.SERVICE_UNAVAILABLE_503);
            baseRequest.setHandled(true);
            // Load page template
            byte[] fileData = Files.readAllBytes(Paths.get(Common.MA_HOME + "/" + Constants.DIR_WEB + "/" + STARTUP_PAGE_TEMPLATE));
            pageTemplate = new String(fileData, Common.UTF8_CS);
            String processedTemplate = pageTemplate;
            response.getWriter().write(processedTemplate);
        }
    } else {
        response.sendError(HttpStatus.SERVICE_UNAVAILABLE_503);
    }
}
Also used : IMangoLifecycle(com.serotonin.m2m2.IMangoLifecycle) RequestDispatcher(javax.servlet.RequestDispatcher)

Example 5 with IMangoLifecycle

use of com.serotonin.m2m2.IMangoLifecycle in project ma-core-public by infiniteautomation.

the class DataPointDao method checkAddPoint.

public void checkAddPoint() {
    IMangoLifecycle lifecycle = Providers.get(IMangoLifecycle.class);
    Integer limit = lifecycle.dataPointLimit();
    if (limit != null && this.countMonitor.getValue() >= limit) {
        String licenseType;
        if (Common.license() != null)
            licenseType = Common.license().getLicenseType();
        else
            licenseType = "Free";
        throw new LicenseViolatedException(new TranslatableMessage("license.dataPointLimit", licenseType, limit));
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) LicenseViolatedException(com.serotonin.m2m2.LicenseViolatedException) IMangoLifecycle(com.serotonin.m2m2.IMangoLifecycle) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage)

Aggregations

IMangoLifecycle (com.serotonin.m2m2.IMangoLifecycle)7 ProcessResult (com.serotonin.m2m2.i18n.ProcessResult)3 DwrPermission (com.serotonin.m2m2.web.dwr.util.DwrPermission)3 JsonException (com.serotonin.json.JsonException)1 JsonWriter (com.serotonin.json.JsonWriter)1 LicenseViolatedException (com.serotonin.m2m2.LicenseViolatedException)1 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)1 IOException (java.io.IOException)1 StringWriter (java.io.StringWriter)1 HashMap (java.util.HashMap)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 RequestDispatcher (javax.servlet.RequestDispatcher)1 ServletException (javax.servlet.ServletException)1 WebContext (org.directwebremoting.WebContext)1