package com.eactive.eai.agent.web; 

import java.util.ArrayList;

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

import com.eactive.eai.adapter.AdapterDAO;
import com.eactive.eai.adapter.AdapterGroupVO;
import com.eactive.eai.adapter.AdapterVO;
import com.eactive.eai.adapter.socket.config.SocketAdapterManager;
import com.eactive.eai.adapter.socket.service.SocketService;
import com.eactive.eai.common.dao.DAOException;
import com.eactive.eai.common.dao.DAOFactory;
import com.eactive.eai.common.util.Logger;

public class CheckServerSocket extends HttpServlet
{ 
	private static final long serialVersionUID = 1L;
	Logger logger = Logger.getLogger(Logger.LOGGER_DEFAULT);
   /** 
    * 1. 기능 :  요청으로 전달된 Command 실행
    * 2. 처리 개요 : 요청으로 전달된 Command 실행하고 실행된 결과를 ObjectOutStream을 통해 전송
    * 3. 주의사항
    *
    * @param    request     HttpServletRequest
    * @param    response    HttpServletResponse
    * @return
    * @exception
    **/
    public void service(HttpServletRequest request, HttpServletResponse response) {
        try {
            boolean     bAllOK = true;
            logger.info("[CheckServerSocket] Servlet starting .......");
            response.setContentType("text/html;charset=euc-kr");
            
            StringBuffer sb = new StringBuffer();
            
            ArrayList<AdapterGroupVO> groups = null;
            AdapterDAO dao = null;
            try {
                dao = (AdapterDAO)DAOFactory.newInstance().create(AdapterDAO.class);
                groups = dao.getAllAdapterGroups();
            } catch(DAOException e) {
                response.getWriter().println("[ERROR] AdapterDAO.getAllDapterGroups 조회시 장애 발생 ... ");
                return;
            }
            logger.info("[CheckServerSocket] Queried AdapterGroup count = " + groups.size());
            AdapterGroupVO gvo = null;
//            AdapterVO avo = null;            
            for(int i=0;i<groups.size();i++) {
                gvo = (AdapterGroupVO)groups.get(i);
                ArrayList<AdapterVO> all = null;
                try {
                    all = dao.getAllAdapters(gvo.getName());
                    for(int j=0;j<all.size();j++) {
                        AdapterVO vo = all.get(j);
//                        String ip = vo.getBatchServerIP();
//                        String port = vo.getBatchServerPortNo();
                        sb.append("[BatchJob : "+vo.getBatchProcessCode()+"]");
                        sb.append("["+vo.getBatchInstitutionName()+" - "+vo.getBatchInstitutionCode()+"]");
                        sb.append("["+vo.getAdapterGroupName()+" >> "+vo.getName()+"]");
                        sb.append("[ HOST : " + vo.getBatchServerIP() + " PORT : " + vo.getBatchServerPortNo() + " ]");

                        SocketService ss = SocketAdapterManager.getInstance().getSocketService(vo.getAdapterGroupName(), vo.getName());
                        if (ss == null) {
                            sb.append("[ SOCKET SERVICE : NOT EXISTS !!! ]");
                            bAllOK = false;
                        } else {
                            if (ss.isActive()) {
                                sb.append("[ SOCKET SERVICE : ACTIVE !!! ]");
                            } else  {
                                sb.append("[ SOCKET SERVICE : IN ACTIVE !!!]");
                                bAllOK = false;
                            }
                        }
                        sb.append("<BR>\n");
                    }   
                    
                } catch(DAOException e) {
                    response.getWriter().println("[ERROR] AdapterDAO.getAllDapterGroups 조회시 장애 발생 ... ");
                    return;
                }
            }             
            if (bAllOK) {
                response.getWriter().println("[SUCCESS]<BR>");
            } else {
                response.getWriter().println("[ERROR]<BR>");
            }
            response.getWriter().println(sb.toString());            
        } catch (Exception ex) {
        	logger.error( ex.getMessage(), ex);
            response.setStatus(500);
            try {
                response.getWriter().println(ex.getMessage());
                throw ex;
            } catch (Exception e) {
                // IGNORE
            }
        } 
    }
} 
