com.clustercontrol.logfile.session.MonitorLogfileControllerBean.java Source code

Java tutorial

Introduction

Here is the source code for com.clustercontrol.logfile.session.MonitorLogfileControllerBean.java

Source

/*
    
Copyright (C) 2011 NTT DATA Corporation
    
This program is free software; you can redistribute it and/or
Modify it under the terms of the GNU General Public License
as published by the Free Software Foundation, version 2.
    
This program 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 General Public License for more details.
    
 */

package com.clustercontrol.logfile.session;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.clustercontrol.bean.HinemosModuleConstant;
import com.clustercontrol.calendar.model.CalendarInfo;
import com.clustercontrol.calendar.session.CalendarControllerBean;
import com.clustercontrol.commons.bean.SettingUpdateInfo;
import com.clustercontrol.commons.util.AbstractCacheManager;
import com.clustercontrol.commons.util.CacheManagerFactory;
import com.clustercontrol.commons.util.HinemosSessionContext;
import com.clustercontrol.commons.util.ICacheManager;
import com.clustercontrol.commons.util.ILock;
import com.clustercontrol.commons.util.ILockManager;
import com.clustercontrol.commons.util.JpaTransactionManager;
import com.clustercontrol.commons.util.LockManagerFactory;
import com.clustercontrol.fault.HinemosUnknown;
import com.clustercontrol.fault.InvalidRole;
import com.clustercontrol.fault.MonitorNotFound;
import com.clustercontrol.jobmanagement.bean.MonitorJobEndNode;
import com.clustercontrol.jobmanagement.util.MonitorJobWorker;
import com.clustercontrol.logfile.bean.LogfileResultDTO;
import com.clustercontrol.logfile.factory.RunMonitorLogfileString;
import com.clustercontrol.logfile.util.LogfileManagerUtil;
import com.clustercontrol.monitor.run.factory.SelectMonitor;
import com.clustercontrol.monitor.run.model.MonitorInfo;
import com.clustercontrol.repository.session.RepositoryControllerBean;
import com.clustercontrol.util.HinemosTime;

/**
 * ??? Session Bean <BR>
 * ?? Entity Bean ????Session Bean ?????
 * 
 */
public class MonitorLogfileControllerBean {

    /** ? */
    private static Log m_log = LogFactory.getLog(MonitorLogfileControllerBean.class);

    private static final ILock _lock;

    static {
        ILockManager lockManager = LockManagerFactory.instance().create();
        _lock = lockManager.create(MonitorLogfileControllerBean.class.getName());

        try {
            _lock.writeLock();

            ArrayList<MonitorInfo> cache = getCache();
            if (cache == null) { // not null when clustered
                refreshCache();
            }
        } finally {
            _lock.writeUnlock();
            m_log.info("Static Initialization [Thread : " + Thread.currentThread() + ", User : "
                    + (String) HinemosSessionContext.instance().getProperty(HinemosSessionContext.LOGIN_USER_ID)
                    + "]");
        }
    }

    @SuppressWarnings("unchecked")
    private static ArrayList<MonitorInfo> getCache() {
        ICacheManager cm = CacheManagerFactory.instance().create();
        Serializable cache = cm.get(AbstractCacheManager.KEY_LOGFILE);
        if (m_log.isDebugEnabled())
            m_log.debug("get cache " + AbstractCacheManager.KEY_LOGFILE + " : " + cache);
        return cache == null ? null : (ArrayList<MonitorInfo>) cache;
    }

    private static void storeCache(ArrayList<MonitorInfo> newCache) {
        ICacheManager cm = CacheManagerFactory.instance().create();
        if (m_log.isDebugEnabled())
            m_log.debug("store cache " + AbstractCacheManager.KEY_LOGFILE + " : " + newCache);
        cm.store(AbstractCacheManager.KEY_LOGFILE, newCache);
    }

    /**
     * ???
     * 
     * 
     * @return Object?2?
     * @throws MonitorNotFound
     * @throws InvalidRole
     * @throws HinemosUnknown
     */
    public ArrayList<MonitorInfo> getLogfileList() throws MonitorNotFound, InvalidRole, HinemosUnknown {

        JpaTransactionManager jtm = null;
        ArrayList<MonitorInfo> list = null;
        try {
            jtm = new JpaTransactionManager();
            jtm.begin();
            list = new SelectMonitor().getMonitorListObjectPrivilegeModeNONE(HinemosModuleConstant.MONITOR_LOGFILE);
            jtm.commit();
        } catch (Exception e) {
            m_log.warn("getLogfileList() : " + e.getClass().getSimpleName() + ", " + e.getMessage(), e);
            if (jtm != null)
                jtm.rollback();
            throw new HinemosUnknown(e.getMessage(), e);
        } finally {
            if (jtm != null)
                jtm.close();
        }
        return list;
    }

    public static void refreshCache() {
        m_log.info("refreshCache()");

        long startTime = HinemosTime.currentTimeMillis();
        try {
            _lock.writeLock();

            new JpaTransactionManager().getEntityManager().clear();
            ArrayList<MonitorInfo> logfileCache = new MonitorLogfileControllerBean().getLogfileList();
            storeCache(logfileCache);

            m_log.info("refresh logfileCache " + (HinemosTime.currentTimeMillis() - startTime) + "ms. size="
                    + logfileCache.size());
        } catch (Exception e) {
            m_log.warn("failed refreshing cache.", e);
        } finally {
            _lock.writeUnlock();
        }
    }

    /**
     * 
     * <??> ???Agent????????
     * <??> ???Agent?????????
     * 
     * facilityID??????
     * withCalendartrue???MonitorInfo?calendarDTO????
     * 
     * 
     * @return Object?2?
     * @throws HinemosUnknown
     * @throws MonitorNotFound
     * 
     */
    public ArrayList<MonitorInfo> getLogfileListForFacilityId(String facilityId, boolean withCalendar)
            throws MonitorNotFound, HinemosUnknown {
        ArrayList<MonitorInfo> ret = new ArrayList<MonitorInfo>();
        JpaTransactionManager jtm = null;
        try {
            jtm = new JpaTransactionManager();
            jtm.begin();

            // ?????????????????????????
            // (?????????????????????????)
            ArrayList<MonitorInfo> monitorList = getCache();

            for (MonitorInfo monitorInfo : monitorList) {
                String scope = monitorInfo.getFacilityId();
                ArrayList<String> facilityIdList = new RepositoryControllerBean().getExecTargetFacilityIdList(scope,
                        monitorInfo.getOwnerRoleId());
                if (facilityIdList != null && facilityIdList.contains(facilityId)) {
                    if (withCalendar) {
                        String calendarId = monitorInfo.getCalendarId();
                        try {
                            CalendarInfo calendar = new CalendarControllerBean().getCalendarFull(calendarId);
                            monitorInfo.setCalendar(calendar);
                        } catch (Exception e) {
                            m_log.warn("getLogfileList() : " + e.getClass().getSimpleName() + ", " + e.getMessage(),
                                    e);
                            throw new HinemosUnknown(e.getMessage(), e);
                        }
                    }
                    ret.add(monitorInfo);
                }
            }

            jtm.commit();
        } catch (HinemosUnknown e) {
            jtm.rollback();
            throw e;
        } catch (Exception e) {
            m_log.warn("getLogfileListForFacilityId() : " + e.getClass().getSimpleName() + ", " + e.getMessage(),
                    e);
            if (jtm != null)
                jtm.rollback();
            throw new HinemosUnknown(e.getMessage(), e);
        } finally {
            if (jtm != null)
                jtm.close();
        }

        return ret;
    }

    /**
     * ??.
     * 
     * @param results ??
     * @throws HinemosUnknown 
     */
    public void run(String facilityId, List<LogfileResultDTO> results) throws HinemosUnknown {
        JpaTransactionManager jtm = null;
        List<MonitorJobEndNode> monitorJobEndNodeList = new ArrayList<>();
        try {
            jtm = new JpaTransactionManager();
            jtm.begin();

            if (results != null) {
                for (LogfileResultDTO result : results) {
                    RunMonitorLogfileString runMonitorLogfileString = new RunMonitorLogfileString();
                    runMonitorLogfileString.run(facilityId, result);
                    monitorJobEndNodeList.addAll(runMonitorLogfileString.getMonitorJobEndNodeList());
                }
            }

            jtm.commit();
        } catch (HinemosUnknown e) {
            m_log.warn("failed storeing result.", e);
            jtm.rollback();

            throw e;
        } finally {
            if (jtm != null)
                jtm.close();
        }

        // EndNode?
        try {
            if (monitorJobEndNodeList != null && monitorJobEndNodeList.size() > 0) {
                for (MonitorJobEndNode monitorJobEndNode : monitorJobEndNodeList) {
                    MonitorJobWorker.endMonitorJob(monitorJobEndNode.getRunInstructionInfo(),
                            monitorJobEndNode.getMonitorTypeId(), monitorJobEndNode.getMessage(),
                            monitorJobEndNode.getErrorMessage(), monitorJobEndNode.getStatus(),
                            monitorJobEndNode.getEndValue());
                }
                // ?HinemosAgent??
                SettingUpdateInfo.getInstance().setLogFileMonitorUpdateTime(HinemosTime.currentTimeMillis());
                LogfileManagerUtil.broadcastConfigured();
            }
        } catch (Exception e) {
            m_log.warn("run() MonitorJobWorker.endMonitorJob() : " + e.getClass().getSimpleName() + ", "
                    + e.getMessage(), e);
            throw new HinemosUnknown(e.getMessage(), e);
        }
    }
}