<commit_before>/*******************************************************************************
  * Copyright (c) 2017-2019 DocDoku.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
  * http://www.eclipse.org/legal/epl-v10.html
  *
  * Contributors:
  *    DocDoku - initial API and implementation
  *******************************************************************************/

package org.polarsys.eplmp.server;

import org.polarsys.eplmp.core.exceptions.PlatformHealthException;
import org.polarsys.eplmp.core.services.IBinaryStorageManagerLocal;
import org.polarsys.eplmp.core.services.IIndexerManagerLocal;
import org.polarsys.eplmp.core.services.IPlatformHealthManagerLocal;
import org.polarsys.eplmp.server.resourcegetters.OfficeConfig;

import javax.ejb.Local;
import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.logging.Level;
import java.util.logging.Logger;

@Local(IPlatformHealthManagerLocal.class)
@Stateless(name = "PlatformHealthManagerBean")
public class PlatformHealthManagerBean implements IPlatformHealthManagerLocal {

    private static final Logger LOGGER = Logger.getLogger(PlatformHealthManagerBean.class.getName());

    @Inject
    private EntityManager em;

    @Inject
    private IIndexerManagerLocal indexerManager;

    @Inject
    private IBinaryStorageManagerLocal storageManager;

    @Inject
    private OfficeConfig officeConfig;

    @Inject
    private ConfigManager configManager;

    @Override
    public void runHealthCheck() throws PlatformHealthException {

        boolean check = true;

        // Database check
        try {
            Long one = (Long) em.createNativeQuery("select 1 from dual").getSingleResult();
            if (one != 1) {
                LOGGER.log(Level.SEVERE, "Database doesn't seem to be reachable");
                check = false;
            }
        } catch (Exception e) {
            LOGGER.log(Level.SEVERE, "Database doesn't seem to be reachable", e);
            check = false;
        }

        // Indexer check
        boolean ping = indexerManager.ping();
        if (!ping) {
            LOGGER.log(Level.SEVERE, "Indexer doesn't seem to be reachable");
            check = false;
        }

        // LibreOffice check
        String officeHome = null;
        try {
            officeHome = officeConfig.getOfficeHome();
        } catch (Exception e) {
            LOGGER.log(Level.SEVERE, "Office properties object not accessible", e);
            check = false;
        }

        if (officeHome == null || officeHome.isEmpty()) {
            LOGGER.log(Level.SEVERE, "Office is not configured");
            check = false;
        } else {<commit_msg>fix docdoku/docdoku-plm#1198<commit_after>/*******************************************************************************
  * Copyright (c) 2017-2019 DocDoku.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
  * http://www.eclipse.org/legal/epl-v10.html
  *
  * Contributors:
  *    DocDoku - initial API and implementation
  *******************************************************************************/

package org.polarsys.eplmp.server;

import org.polarsys.eplmp.core.exceptions.PlatformHealthException;
import org.polarsys.eplmp.core.services.IBinaryStorageManagerLocal;
import org.polarsys.eplmp.core.services.IIndexerManagerLocal;
import org.polarsys.eplmp.core.services.IPlatformHealthManagerLocal;
import org.polarsys.eplmp.server.resourcegetters.OfficeConfig;

import javax.ejb.Local;
import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.logging.Level;
import java.util.logging.Logger;

@Local(IPlatformHealthManagerLocal.class)
@Stateless(name = "PlatformHealthManagerBean")
public class PlatformHealthManagerBean implements IPlatformHealthManagerLocal {

    private static final Logger LOGGER = Logger.getLogger(PlatformHealthManagerBean.class.getName());

    @Inject
    private EntityManager em;

    @Inject
    private IIndexerManagerLocal indexerManager;

    @Inject
    private IBinaryStorageManagerLocal storageManager;

    @Inject
    private OfficeConfig officeConfig;

    @Inject
    private ConfigManager configManager;

    @Override
    public void runHealthCheck() throws PlatformHealthException {

        boolean check = true;

        // Database check
        try {
            Long two = (Long) em.createNativeQuery("select 1+1").getSingleResult();
            if (two != 2) {
                LOGGER.log(Level.SEVERE, "Database doesn't seem to be reachable");
                check = false;
            }
        } catch (Exception e) {
            LOGGER.log(Level.SEVERE, "Database doesn't seem to be reachable", e);
            check = false;
        }

        // Indexer check
        boolean ping = indexerManager.ping();
        if (!ping) {
            LOGGER.log(Level.SEVERE, "Indexer doesn't seem to be reachable");
            check = false;
        }

        // LibreOffice check
        String officeHome = null;
        try {
            officeHome = officeConfig.getOfficeHome();
        } catch (Exception e) {
            LOGGER.log(Level.SEVERE, "Office properties object not accessible", e);
            check = false;
        }

        if (officeHome == null || officeHome.isEmpty()) {
            LOGGER.log(Level.SEVERE, "Office is not configured");
            check = false;
        } else {
{'fraction_non_alphanumeric': 0.13985296631902888, 'fraction_numerical': 0.005641990083775005, 'mean_word_length': 3.056865464632455, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1095057', 'n_tokens_mistral': 1614, 'n_tokens_neox': 1555, 'n_words': 389}