You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ReportUsage.java 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * Copyright 2000-2018 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.tools;
  17. import java.io.IOException;
  18. import java.io.InputStream;
  19. import java.net.MalformedURLException;
  20. import java.net.URL;
  21. import java.net.URLConnection;
  22. import java.util.concurrent.Callable;
  23. import java.util.concurrent.FutureTask;
  24. import java.util.logging.Logger;
  25. import java.util.prefs.Preferences;
  26. import org.apache.commons.io.IOUtils;
  27. import com.google.gwt.dev.shell.CheckForUpdates;
  28. import com.vaadin.shared.Version;
  29. public class ReportUsage {
  30. public static final String ANONYMOUS_ID = loadFirstLaunch();
  31. // for compatibility with old checks
  32. private static final String USER_AGENT_BASE = "GWT Freshness Checker";
  33. private static final String COMPILER = "Compiler"; //$NON-NLS-1$
  34. private static final String E_QPARAM = "&e="; //$NON-NLS-1$
  35. private static final String R_QPARAM = "&r=unknown"; //$NON-NLS-1$
  36. private static final String ID_QPARAM = "&id="; //$NON-NLS-1$
  37. private static final String V_QPARAM = "?v="; //$NON-NLS-1$
  38. private static final String USER_AGENT = "User-Agent"; //$NON-NLS-1$
  39. // Use the GWT Freshness checker URL to store usage reports.
  40. private static final String QUERY_URL = "https://tools.vaadin.com/version/currentversion.xml"; //$NON-NLS-1$
  41. // Preferences keys
  42. private static final String FIRST_LAUNCH = "firstLaunch"; //$NON-NLS-1$
  43. private static final String LAST_PING = "lastPing";
  44. public static final long ONE_DAY = 24 * 60 * 60 * 1000;
  45. // for testing only
  46. public static void main(String[] args) {
  47. report();
  48. }
  49. public static FutureTask<Void> checkForUpdatesInBackgroundThread() {
  50. FutureTask<Void> task = new FutureTask<>(new Callable<Void>() {
  51. @Override
  52. public Void call() throws Exception {
  53. ReportUsage.report();
  54. return null;
  55. }
  56. });
  57. Thread checkerThread = new Thread(task, "Vaadin Update Checker");
  58. checkerThread.setDaemon(true);
  59. checkerThread.start();
  60. return task;
  61. }
  62. public static void report() {
  63. long currentTimeMillis = System.currentTimeMillis();
  64. Preferences prefs = Preferences.userNodeForPackage(ReportUsage.class);
  65. String lastPing = prefs.get(LAST_PING, "0");
  66. if (lastPing != null) {
  67. try {
  68. long lastPingTime = Long.parseLong(lastPing);
  69. if (currentTimeMillis < lastPingTime + ONE_DAY) {
  70. return;
  71. }
  72. } catch (NumberFormatException e) {
  73. // error parsing last ping time, ignore and ping
  74. }
  75. }
  76. StringBuilder url = new StringBuilder(QUERY_URL);
  77. url.append(V_QPARAM);
  78. url.append(Version.getFullVersion());
  79. url.append(ID_QPARAM);
  80. url.append(ANONYMOUS_ID).append(R_QPARAM);
  81. // TODO add more relevant entry point if feasible
  82. String entryPoint = COMPILER;
  83. if (entryPoint != null) {
  84. url.append(E_QPARAM).append(entryPoint);
  85. }
  86. doHttpGet(makeUserAgent(), url.toString());
  87. prefs.put(LAST_PING, String.valueOf(currentTimeMillis));
  88. }
  89. private static void doHttpGet(String userAgent, String url) {
  90. Throwable caught;
  91. InputStream is = null;
  92. try {
  93. URL urlToGet = new URL(url);
  94. URLConnection conn = urlToGet.openConnection();
  95. conn.setRequestProperty(USER_AGENT, userAgent);
  96. is = conn.getInputStream();
  97. // TODO use the results
  98. IOUtils.toByteArray(is);
  99. return;
  100. } catch (MalformedURLException e) {
  101. caught = e;
  102. } catch (IOException e) {
  103. caught = e;
  104. } finally {
  105. IOUtils.closeQuietly(is);
  106. }
  107. Logger.getLogger(ReportUsage.class.getName())
  108. .fine("Caught an exception while executing HTTP query: "
  109. + caught.getMessage());
  110. }
  111. private static String makeUserAgent() {
  112. String userAgent = USER_AGENT_BASE;
  113. StringBuilder extra = new StringBuilder();
  114. appendUserAgentProperty(extra, "java.vendor");
  115. appendUserAgentProperty(extra, "java.version");
  116. appendUserAgentProperty(extra, "os.arch");
  117. appendUserAgentProperty(extra, "os.name");
  118. appendUserAgentProperty(extra, "os.version");
  119. if (extra.length() != 0) {
  120. userAgent += " (" + extra + ")";
  121. }
  122. return userAgent;
  123. }
  124. private static void appendUserAgentProperty(StringBuilder sb,
  125. String propName) {
  126. String propValue = System.getProperty(propName);
  127. if (propValue != null) {
  128. if (sb.length() != 0) {
  129. sb.append(';').append(' ');
  130. }
  131. sb.append(propName);
  132. sb.append('=');
  133. sb.append(propValue);
  134. }
  135. }
  136. private static String loadFirstLaunch() {
  137. Preferences prefs = Preferences
  138. .userNodeForPackage(CheckForUpdates.class);
  139. String firstLaunch = prefs.get(FIRST_LAUNCH, null);
  140. if (firstLaunch == null) {
  141. long currentTimeMillis = System.currentTimeMillis();
  142. firstLaunch = Long.toHexString(currentTimeMillis);
  143. prefs.put(FIRST_LAUNCH, firstLaunch);
  144. }
  145. return firstLaunch;
  146. }
  147. }