org.codice.ddf.status.Status.java Source code

Java tutorial

Introduction

Here is the source code for org.codice.ddf.status.Status.java

Source

/*
 * Copyright (c) 2012, Codice Foundation. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301  USA
 */

package org.codice.ddf.status;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.LinkedHashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.simple.JSONValue;

public class Status extends HttpServlet {

    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        PrintWriter writer = response.getWriter();

        writer.append(toJSON());
    }

    static String toJSON() {
        Map<String, Object> obj = new LinkedHashMap<String, Object>();
        try {
            obj.put("hostname", InetAddress.getLocalHost().getHostName());
        } catch (UnknownHostException e) {
            obj.put("hostname", "unresolved");
        }
        try {
            obj.put("ip", InetAddress.getLocalHost().getHostAddress());
        } catch (UnknownHostException e) {
            obj.put("ip", "unresolved");
        }
        obj.put("cpu", CpuService.getCpuStatus());
        obj.put("disk", DiskService.getDiskStatus());
        obj.put("mem", MemoryService.getMemoryStatus());
        obj.put("jvm", JvmService.getJvmStatus());
        return JSONValue.toJSONString(obj);
    }
}