- 시작하는 제품에 따라 제품이 보여지고 관리되도록 수정.
[Tadpole.git] / com.hangum.tadpole.applicaion.start / src / com / hangum / tadpole / application / start / update / checker / NewVersionChecker.java
blob4fd21f3c181b2d902a4987e428bc9acc6bbdde31
1 /*******************************************************************************
2 * Copyright (c) 2016 hangum.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the GNU Lesser Public License v2.1
5 * which accompanies this distribution, and is available at
6 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
7 *
8 * Contributors:
9 * hangum - initial API and implementation
10 ******************************************************************************/
11 package com.hangum.tadpole.application.start.update.checker;
13 import java.io.InputStream;
14 import java.net.URL;
16 import org.apache.commons.io.IOUtils;
17 import org.apache.commons.lang.StringUtils;
18 import org.apache.log4j.Logger;
20 import com.google.gson.Gson;
21 import com.hangum.tadpole.commons.libs.core.define.SystemDefine;
23 /**
24 * Update checker
26 * @author hangum
29 public class NewVersionChecker {
30 private static final Logger logger = Logger.getLogger(NewVersionChecker.class);
31 public static final String CHECK_URI = "https://raw.githubusercontent.com/hangum/TadpoleForDBTools/gh-pages/versionInfo.json";
33 private long lastCheckTimeMillis = 0l;
34 private NewVersionObject newVersionObj;
36 public static NewVersionChecker instance = new NewVersionChecker();
37 private NewVersionChecker() {}
39 public static synchronized NewVersionChecker getInstance() {
40 return instance;
43 /**
44 * new version checker
45 * @return
47 public boolean check() {
48 if(lastCheckTimeMillis == 0l) lastCheckTimeMillis = System.currentTimeMillis();
50 // 60분에 한번씩 check 하여 값을 돌려준다.
51 long currentTimeMillis = System.currentTimeMillis();
52 long diffTimeMills = (currentTimeMillis - lastCheckTimeMillis) / 1000l;
54 if(logger.isDebugEnabled()) {
55 logger.debug("currentTimeMillis mills " + currentTimeMillis);
56 logger.debug("lastCheckTimeMillis mills " + lastCheckTimeMillis);
57 logger.debug("diff mills " + diffTimeMills);
60 // 24시간에 한번씩 검사한다.
61 if(diffTimeMills > (60 * 60 * 24) || newVersionObj == null) {
62 lastCheckTimeMillis = currentTimeMillis;
63 return getVersionInfoData();
65 return false;
68 /**
69 * get version info data
71 * @return
73 public boolean getVersionInfoData() {
74 InputStream is = null;
75 try {
76 is = new URL(CHECK_URI).openConnection().getInputStream();
77 String strJson = IOUtils.toString(is);
78 if(logger.isDebugEnabled()) {
79 logger.debug("==[start]===================================");
80 logger.debug(strJson);
81 logger.debug("==[end]===================================");
83 Gson gson = new Gson();
84 newVersionObj = gson.fromJson(strJson, NewVersionObject.class);
85 if(newVersionObj == null) return false;
87 String[] arryCurVer = StringUtils.split(SystemDefine.DBHUB_MAJOR_VERSION, ".");
88 String[] arryNewVer = StringUtils.split(newVersionObj.getMajorVer(), ".");
89 for (int i=0; i<arryCurVer.length; i++) {
90 if(Integer.parseInt(arryNewVer[i]) > Integer.parseInt(arryCurVer[i])) {
91 return true;
95 } catch (Exception e) {
96 logger.error(String.format("New version checkerer %s.", e.getMessage()));
97 } finally {
98 if(is != null) {
99 try {
100 // IOUtils.toString(is);
101 IOUtils.closeQuietly(is);
103 catch(Exception e) {
104 //igoner excetpion
109 return false;
112 public NewVersionObject getNewVersionObj() {
113 return newVersionObj;