com.fujitsu.dc.core.rs.StatusResource.java Source code

Java tutorial

Introduction

Here is the source code for com.fujitsu.dc.core.rs.StatusResource.java

Source

/**
 * personium.io
 * Copyright 2014 FUJITSU LIMITED
 *
 * 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
 *
 *     http://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.fujitsu.dc.core.rs;

import java.util.Properties;

import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;

import org.apache.commons.dbcp.BasicDataSourceFactory;
import org.apache.http.HttpStatus;
import org.json.simple.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fujitsu.dc.common.es.EsClient;
import com.fujitsu.dc.core.DcCoreConfig;
import com.fujitsu.dc.core.model.impl.es.EsModel;

/**
 * StatusResource??JAX-RS Resource .
 */
public class StatusResource {
    /** . */
    static Logger log = LoggerFactory.getLogger(StatusResource.class);
    /** ?URL???. */
    public static final String PROP_TARGET_URL = "com.fujitsu.dc.test.target";

    /**
     * GET???.
     * @return JAS-RS Response
     */
    @SuppressWarnings("unchecked")
    @GET
    @Produces("application/json")
    public Response get() {
        StringBuilder sb = new StringBuilder();

        // 
        Properties props = DcCoreConfig.getProperties();
        JSONObject responseJson = new JSONObject();
        JSONObject propertiesJson = new JSONObject();
        for (String key : props.stringPropertyNames()) {
            String value = props.getProperty(key);
            propertiesJson.put(key, value);
        }
        responseJson.put("properties", propertiesJson);

        // Cell?/
        //responseJson.put("service", checkServiceStatus());

        // Ads??
        responseJson.put("ads", checkAds());

        // ElasticSearch Health
        EsClient client = EsModel.client();
        JSONObject esJson = new JSONObject();
        esJson.put("health", client.checkHealth());
        responseJson.put("ElasticSearch", esJson);

        sb.append(responseJson.toJSONString());
        return Response.status(HttpStatus.SC_OK).entity(sb.toString()).build();
    }

    /**
     * POST???.
     * @return JAS-RS Response
     */
    @POST
    public Response post() {
        // 
        DcCoreConfig.reload();
        return Response.status(HttpStatus.SC_NO_CONTENT).build();
    }

    /**
     * Adb(MasterDatabase)???.
     * @return ?????True
     */
    Boolean checkAds() {
        try {
            Properties p = DcCoreConfig.getEsAdsDbcpProps();
            BasicDataSourceFactory.createDataSource(p);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

}