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
9 * hangum - initial API and implementation
10 ******************************************************************************/
11 package com
.hangum
.tadpole
.application
.start
.update
.checker
;
13 import java
.io
.InputStream
;
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
;
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() {
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
);
61 if(diffTimeMills
> (60 * 60 * 24) || newVersionObj
== null) {
62 lastCheckTimeMillis
= currentTimeMillis
;
63 return getVersionInfoData();
69 * get version info data
73 public boolean getVersionInfoData() {
74 InputStream is
= null;
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
])) {
95 } catch (Exception e
) {
96 logger
.error(String
.format("New version checkerer %s.", e
.getMessage()));
100 // IOUtils.toString(is);
101 IOUtils
.closeQuietly(is
);
112 public NewVersionObject
getNewVersionObj() {
113 return newVersionObj
;