PageRenderTime 26ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/MyFramework/src/com/site/AccessCheck.java

https://github.com/sks9602/MyFramework
Java | 313 lines | 217 code | 79 blank | 17 comment | 19 complexity | 0083d6f92a28202c8848594130221cb1 MD5 | raw file
  1. package com.site;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.FileNotFoundException;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;
  7. import java.io.InputStream;
  8. import java.io.OutputStream;
  9. import java.io.UnsupportedEncodingException;
  10. import java.net.InetAddress;
  11. import java.net.URL;
  12. import java.net.URLConnection;
  13. import java.text.SimpleDateFormat;
  14. import java.util.ArrayList;
  15. import java.util.List;
  16. import org.apache.poi.hssf.usermodel.HSSFCell;
  17. import org.apache.poi.hssf.usermodel.HSSFCellStyle;
  18. import org.apache.poi.hssf.usermodel.HSSFFont;
  19. import org.apache.poi.hssf.usermodel.HSSFRichTextString;
  20. import org.apache.poi.hssf.usermodel.HSSFRow;
  21. import org.apache.poi.hssf.usermodel.HSSFSheet;
  22. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  23. import org.apache.poi.ss.usermodel.CellStyle;
  24. import org.quartz.JobExecutionContext;
  25. import org.quartz.JobExecutionException;
  26. import org.springframework.context.ApplicationContext;
  27. import org.springframework.context.support.ClassPathXmlApplicationContext;
  28. import org.springframework.scheduling.quartz.QuartzJobBean;
  29. import project.jun.util.HoExcelReader;
  30. import project.jun.util.HoExcelWriter;
  31. import project.jun.util.HoHttpClient;
  32. public class AccessCheck extends QuartzJobBean {
  33. List serverList = new ArrayList();
  34. String excelFileName = null;
  35. public void setServerList(List serverList) {
  36. this.serverList = serverList;
  37. }
  38. public List getServerList() {
  39. return this.serverList;
  40. }
  41. public void setExcelFileName(String excelFileName) {
  42. this.excelFileName = excelFileName;
  43. }
  44. public String getExcelFileName() {
  45. return this.excelFileName;
  46. }
  47. public boolean ping(String server) {
  48. boolean isALive = false;
  49. try {
  50. InetAddress address = InetAddress.getByName(server);
  51. isALive = address.isReachable(3000);
  52. } catch(Exception e) {
  53. }
  54. return isALive;
  55. }
  56. /**
  57. * 서버가 살아있는지 확인
  58. * @param server
  59. * @return
  60. */
  61. public boolean isAlive(String server) {
  62. boolean isALive = false;
  63. try {
  64. final URLConnection connection = new URL(server).openConnection();
  65. connection.connect();
  66. isALive = true;
  67. } catch(Exception e) {
  68. isALive = false;
  69. }
  70. return isALive;
  71. }
  72. public int readDataToExcelFile(){
  73. try{
  74. FileInputStream fis = new FileInputStream(getExcelFileName());
  75. HSSFWorkbook workbook = new HSSFWorkbook(fis);
  76. HSSFSheet sheet = workbook.getSheetAt(0);
  77. fis.close();
  78. workbook = null;
  79. return sheet.getLastRowNum()+1;
  80. }catch(Exception e){
  81. e.printStackTrace();
  82. return -1;
  83. }
  84. }
  85. private HSSFCellStyle createTitleCellStyle(HSSFWorkbook workbook) {
  86. HSSFCellStyle cell_style = workbook.createCellStyle();
  87. HSSFFont font = workbook.createFont();
  88. // Border, Foreground Color Setting
  89. cell_style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
  90. cell_style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
  91. cell_style.setBorderRight(HSSFCellStyle.BORDER_THIN);
  92. cell_style.setBorderTop(HSSFCellStyle.BORDER_THIN);
  93. cell_style.setFillPattern((short)1);
  94. cell_style.setFillForegroundColor((short)23);
  95. cell_style.setAlignment(CellStyle.ALIGN_CENTER);
  96. // Font Setting
  97. font.setFontHeightInPoints((short)10);
  98. font.setColor((short)9);
  99. font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
  100. cell_style.setFont(font);
  101. return cell_style;
  102. }
  103. private HSSFCellStyle createDataCellStyle(HSSFWorkbook workbook, int align) {
  104. HSSFCellStyle cell_style = workbook.createCellStyle();
  105. HSSFFont font = workbook.createFont();
  106. // Border, Foreground Color Setting
  107. cell_style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
  108. cell_style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
  109. cell_style.setBorderRight(HSSFCellStyle.BORDER_THIN);
  110. cell_style.setBorderTop(HSSFCellStyle.BORDER_THIN);
  111. if( align == 0 ) {
  112. cell_style.setAlignment(CellStyle.ALIGN_LEFT);
  113. } else if( align == 4 || align == 7) {
  114. cell_style.setAlignment(CellStyle.ALIGN_RIGHT);
  115. } else {
  116. cell_style.setAlignment(CellStyle.ALIGN_CENTER);
  117. }
  118. // Font Setting
  119. font.setFontHeightInPoints((short)9);
  120. cell_style.setFont(font);
  121. return cell_style;
  122. }
  123. public int writeDataToExcelFile(int rowNum, String [] values) {
  124. try {
  125. HSSFWorkbook workBook = new HSSFWorkbook();
  126. if( rowNum <= 0 ) {
  127. workBook = new HSSFWorkbook();
  128. } else {
  129. FileInputStream fis = new FileInputStream(getExcelFileName());
  130. workBook = new HSSFWorkbook(fis);
  131. }
  132. HSSFSheet sheet = workBook.getSheet("SiteCheck");
  133. if( sheet == null ) {
  134. sheet = workBook.createSheet("SiteCheck");
  135. }
  136. HSSFRow row;
  137. HSSFCell cell;
  138. String [] titles = {"서버주소","서버상태","응답코드","응답","응답 길이","호출 시간","응답 시간","소요 시간"};
  139. sheet.setColumnWidth(0, 6000);
  140. sheet.setColumnWidth(1, 3000);
  141. sheet.setColumnWidth(2, 3000);
  142. sheet.setColumnWidth(3, 2000);
  143. sheet.setColumnWidth(4, 3500);
  144. sheet.setColumnWidth(5, 5500);
  145. sheet.setColumnWidth(6, 5500);
  146. sheet.setColumnWidth(7, 3500);
  147. if( rowNum <= 0 ) {
  148. row = sheet.createRow(0);
  149. for( int cellNum=0; cellNum<titles.length; cellNum++) {
  150. cell = row.createCell(cellNum);
  151. cell.setCellValue(new HSSFRichTextString(titles[cellNum]));
  152. cell.setCellStyle(createTitleCellStyle(workBook));
  153. }
  154. rowNum = 1;
  155. }
  156. row = sheet.createRow(rowNum);
  157. System.out.println(" rowNum : " + rowNum );
  158. for( int cellNum=0; cellNum<titles.length; cellNum++) {
  159. cell = row.createCell(cellNum);
  160. cell.setCellValue(new HSSFRichTextString(values[cellNum]));
  161. cell.setCellStyle(createDataCellStyle(workBook, cellNum));
  162. //sheet.autoSizeColumn(cellNum);
  163. }
  164. FileOutputStream out = new FileOutputStream(getExcelFileName());
  165. workBook.write(out);
  166. out.flush();
  167. out.close();
  168. workBook = null;
  169. } catch (Exception e) {
  170. e.printStackTrace();
  171. }
  172. return rowNum++;
  173. }
  174. @Override
  175. protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
  176. SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
  177. SimpleDateFormat sheetSdf = new SimpleDateFormat("yyyy.MM");
  178. setExcelFileName(((String) context.getJobDetail().getJobDataMap().get("excelFileName")).replaceAll("#YYYY.MM#", sheetSdf.format(System.currentTimeMillis())));
  179. setServerList((List) context.getJobDetail().getJobDataMap().get("serverList"));
  180. /*
  181. setExcelFileName("d:/TEMP/SiteCheck_"+sheetSdf.format(System.currentTimeMillis())+".xls");
  182. serverList.add("http://www.daum.net");
  183. serverList.add("http://www.naver.com");
  184. serverList.add("https://www.kdn.com");
  185. */
  186. String server;
  187. HoHttpClient http = new HoHttpClient("euc-kr");
  188. int statucCode = 0;
  189. String statucText = null;
  190. int length = 0;
  191. int pos = 0;
  192. int lastRow = readDataToExcelFile();
  193. for( Object serverName : serverList.toArray() ) {
  194. server = (String) serverName;
  195. statucCode = 0;
  196. statucText = null;
  197. length = 0;
  198. long startTime = System.currentTimeMillis();
  199. long endTime = 0L;
  200. boolean isAlive = isAlive(server);
  201. try {
  202. http.post(server);
  203. statucCode = http.getStatusCode();
  204. statucText = http.getStatusText();
  205. length = http.getResponse().length();
  206. if( statucCode == 0 ) {
  207. statucText = "ERROR";
  208. length = 0;
  209. }
  210. } catch (UnsupportedEncodingException e) {
  211. statucCode = -1;
  212. statucText = "ERROR";
  213. length = 0;
  214. }
  215. endTime = System.currentTimeMillis();
  216. long estimagedTime = endTime - startTime;
  217. String [] values = {server , isAlive ? "ALIVE" : "DEAD", String.valueOf(statucCode), statucText, String.valueOf(length), sdf.format(startTime), sdf.format(endTime), String.valueOf(estimagedTime) };
  218. System.out.println(" lastRow : " + lastRow );
  219. writeDataToExcelFile( lastRow, values);
  220. if(lastRow <= 0 ) {
  221. lastRow = 1;
  222. }
  223. lastRow++;
  224. System.out.println(server + " : " + isAlive + " : " + statucCode + " : " + statucText + " : " + length + " : " + sdf.format(startTime) + " / " + sdf.format(endTime) + " -> " + estimagedTime );
  225. }
  226. }
  227. public static void main(String [] args) {
  228. String applicationContexts[] = {
  229. "project/config/applicationContext-quartz.xml"
  230. };
  231. ApplicationContext context = new ClassPathXmlApplicationContext(applicationContexts);
  232. }
  233. }