tomee-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rmannibu...@apache.org
Subject svn commit: r1239201 - in /openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/util: IO.java UpdateChecker.java
Date Wed, 01 Feb 2012 16:48:04 GMT
Author: rmannibucau
Date: Wed Feb  1 16:48:04 2012
New Revision: 1239201

URL: http://svn.apache.org/viewvc?rev=1239201&view=rev
Log:
adding UpdateChecker -> need to be added in the startup

Added:
    openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/util/UpdateChecker.java
Modified:
    openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/util/IO.java

Modified: openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/util/IO.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/util/IO.java?rev=1239201&r1=1239200&r2=1239201&view=diff
==============================================================================
--- openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/util/IO.java
(original)
+++ openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/util/IO.java
Wed Feb  1 16:48:04 2012
@@ -34,6 +34,7 @@ import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.io.OutputStream;
 import java.net.URL;
+import java.net.URLConnection;
 import java.util.zip.ZipInputStream;
 import java.util.zip.ZipOutputStream;
 
@@ -41,6 +42,24 @@ import java.util.zip.ZipOutputStream;
  * @version $Revision$ $Date$
  */
 public class IO {
+    private static final int MAX_TIMEOUT = Integer.getInteger("openejb.io.util.timeout",
5000);
+
+    public static String readFileAsString(final URL url) throws IOException {
+        final URLConnection connection = url.openConnection();
+        connection.setConnectTimeout(MAX_TIMEOUT);
+        final InputStream in = connection.getInputStream();
+        final StringBuilder builder = new StringBuilder("");
+        String line;
+        try {
+            final BufferedReader reader = new BufferedReader(new InputStreamReader(in));
+            while ((line = reader.readLine()) != null) {
+                builder.append(line);
+            }
+        } finally {
+            close(in);
+        }
+        return builder.toString();
+    }
 
     public static String readString(URL url) throws IOException {
         final InputStream in = url.openStream();

Added: openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/util/UpdateChecker.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/util/UpdateChecker.java?rev=1239201&view=auto
==============================================================================
--- openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/util/UpdateChecker.java
(added)
+++ openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/util/UpdateChecker.java
Wed Feb  1 16:48:04 2012
@@ -0,0 +1,90 @@
+package org.apache.openejb.util;
+
+import java.net.URL;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.concurrent.atomic.AtomicReference;
+
+public class UpdateChecker implements Runnable {
+    private static final String SKIP_CHECK = "openen.version.check.skip";
+    private static final String REPO_URL = System.getProperty("openejb.version.check.repo.url",
"http://repo1.maven.org/maven2/org/apache/openejb/");
+    private static final String URL = System.getProperty("openejb.version.check.url", REPO_URL
+ "openejb/maven-metadata.xml");
+    private static final String TAG = "latest";
+    private static final AtomicBoolean DONE = new AtomicBoolean(false);
+    private static final AtomicReference<String> RESULT = new AtomicReference<String>("");
+    private static final CountDownLatch LATCH = new CountDownLatch(1);
+    private static final String ERROR_MESSAGE = "can't check last version";
+    public static final String UNDEFINED = "undefined";
+
+    @Override
+    public void run() {
+        if (DONE.get() || isSkipped()) {
+            return;
+        }
+
+        try {
+            final URL url = new URL(URL);
+            final String metaData = IO.readFileAsString(url);
+            final String latest = extractLatest(metaData);
+            RESULT.set(message(latest, OpenEjbVersion.get().getVersion()));
+        } catch (Exception e) {
+            DONE.set(true);
+            RESULT.set(ERROR_MESSAGE);
+        }
+        LATCH.countDown();
+    }
+
+    private static String message(final String latest, final String version) {
+        if (UNDEFINED.equals(latest)) {
+            return "can't determine latest version";
+        }
+        if (version.equals(latest)) {
+            return "running on the latest version";
+        }
+        return new StringBuilder("current version => ").append(version)
+                .append(", latest stable version ").append(latest).append(" is available
")
+                .append(" on ").append(REPO_URL).toString();
+    }
+
+    private static String extractLatest(final String metaData) {
+        if (metaData != null) {
+            boolean found = false;
+            for (String s : metaData.replace(">", ">\n").split("\n")) {
+                if (found) {
+                    return trim(s).replace("</" + TAG + ">", "");
+                }
+                if (!s.isEmpty() && trim(s).endsWith("<" + TAG + ">")) {
+                    found = true;
+                }
+            }
+        }
+        return UNDEFINED;
+    }
+
+    private static String trim(final String s) {
+        return s.replace("\t", "").replace(" ", "");
+    }
+
+    public static String message() {
+        if (isSkipped()) {
+            return "version check is skipped";
+        }
+
+        try {
+            LATCH.await();
+        } catch (InterruptedException e) {
+            return ERROR_MESSAGE;
+        }
+        return RESULT.get();
+    }
+
+    public static boolean isSkipped() {
+        return System.getProperty(SKIP_CHECK) != null;
+    }
+
+    public static void main(String[] args) {
+        UpdateChecker checker = new UpdateChecker();
+        checker.run();
+        System.out.println(UpdateChecker.message());
+    }
+}



Mime
View raw message