]> git.argeo.org Git - lgpl/argeo-commons.git/blobdiff - osgi/runtime/org.argeo.osgi.boot/src/main/java/org/argeo/osgi/boot/OsgiBootUtils.java
Move to Commons Base
[lgpl/argeo-commons.git] / osgi / runtime / org.argeo.osgi.boot / src / main / java / org / argeo / osgi / boot / OsgiBootUtils.java
index a9067a213c04e912985fb139651db012c4b3ff3b..f31665578f3211912e579a34fe8da7071526f560 100644 (file)
@@ -1,5 +1,5 @@
 /*\r
- * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>\r
+ * Copyright (C) 2007-2012 Mathieu Baudier\r
  *\r
  * Licensed under the Apache License, Version 2.0 (the "License");\r
  * you may not use this file except in compliance with the License.\r
  * See the License for the specific language governing permissions and\r
  * limitations under the License.\r
  */\r
-\r
 package org.argeo.osgi.boot;\r
 \r
+import java.text.DateFormat;\r
+import java.text.SimpleDateFormat;\r
+import java.util.ArrayList;\r
+import java.util.Date;\r
+import java.util.List;\r
+import java.util.StringTokenizer;\r
+\r
+import org.osgi.framework.Bundle;\r
+\r
+/** Utilities, mostly related to logging. */\r
 public class OsgiBootUtils {\r
+       /** ISO8601 (as per log4j) and difference to UTC */\r
+       private static DateFormat dateFormat = new SimpleDateFormat(\r
+                       "yyyy-MM-dd HH:mm:ss,SSS Z");\r
 \r
        public static void info(Object obj) {\r
-               System.out.println("# OSGiBOOT      # " + obj);\r
+               System.out.println("# OSGiBOOT      # " + dateFormat.format(new Date())\r
+                               + " # " + obj);\r
        }\r
 \r
        public static void debug(Object obj) {\r
-                       System.out.println("# OSGiBOOT DBG  # " + obj);\r
+               System.out.println("# OSGiBOOT DBG  # " + dateFormat.format(new Date())\r
+                               + " # " + obj);\r
        }\r
 \r
        public static void warn(Object obj) {\r
-               System.out.println("# OSGiBOOT WARN # " + obj);\r
-               // Because of a weird bug under Windows when starting it in a forked VM\r
-               // if (System.getProperty("os.name").contains("Windows"))\r
-               // System.out.println("# WARN " + obj);\r
-               // else\r
-               // System.err.println("# WARN " + obj);\r
+               System.out.println("# OSGiBOOT WARN # " + dateFormat.format(new Date())\r
+                               + " # " + obj);\r
        }\r
 \r
-       //FIXE: returns null when defaultValue is ""\r
+       /**\r
+        * Gets a property value\r
+        * \r
+        * @return null when defaultValue is ""\r
+        */\r
        public static String getProperty(String name, String defaultValue) {\r
                final String value;\r
                if (defaultValue != null)\r
                        value = System.getProperty(name, defaultValue);\r
                else\r
                        value = System.getProperty(name);\r
-               \r
+\r
                if (value == null || value.equals(""))\r
                        return null;\r
                else\r
@@ -53,36 +67,76 @@ public class OsgiBootUtils {
                return getProperty(name, null);\r
        }\r
 \r
-       public static String getPropertyCompat(String name, String oldName) {\r
-               return getPropertyCompat(name, oldName, null);\r
+       public static String stateAsString(int state) {\r
+               switch (state) {\r
+               case Bundle.UNINSTALLED:\r
+                       return "UNINSTALLED";\r
+               case Bundle.INSTALLED:\r
+                       return "INSTALLED";\r
+               case Bundle.RESOLVED:\r
+                       return "RESOLVED";\r
+               case Bundle.STARTING:\r
+                       return "STARTING";\r
+               case Bundle.ACTIVE:\r
+                       return "ACTIVE";\r
+               case Bundle.STOPPING:\r
+                       return "STOPPING";\r
+               default:\r
+                       return Integer.toString(state);\r
+               }\r
        }\r
 \r
-       public static String getPropertyCompat(String name, String oldName,\r
-                       String defaultValue) {\r
-               String res = null;\r
-\r
-               if (defaultValue != null) {\r
-                       res = getProperty(name, defaultValue);\r
-                       if (res.equals(defaultValue)) {\r
-                               res = getProperty(oldName, defaultValue);\r
-                               if (!res.equals(defaultValue))\r
-                                       warnDeprecated(name, oldName);\r
+       /**\r
+        * @return ==0: versions are identical, <0: tested version is newer, >0:\r
+        *         currentVersion is newer.\r
+        */\r
+       public static int compareVersions(String currentVersion,\r
+                       String testedVersion) {\r
+               List cToks = new ArrayList();\r
+               StringTokenizer cSt = new StringTokenizer(currentVersion, ".");\r
+               while (cSt.hasMoreTokens())\r
+                       cToks.add(cSt.nextToken());\r
+               List tToks = new ArrayList();\r
+               StringTokenizer tSt = new StringTokenizer(currentVersion, ".");\r
+               while (tSt.hasMoreTokens())\r
+                       tToks.add(tSt.nextToken());\r
+       \r
+               int comp = 0;\r
+               comp: for (int i = 0; i < cToks.size(); i++) {\r
+                       if (tToks.size() <= i) {\r
+                               // equals until then, tested shorter\r
+                               comp = 1;\r
+                               break comp;\r
                        }\r
-               } else {\r
-                       res = getProperty(name, null);\r
-                       if (res == null) {\r
-                               res = getProperty(oldName, null);\r
-                               if (res != null)\r
-                                       warnDeprecated(name, oldName);\r
+       \r
+                       String c = (String) cToks.get(i);\r
+                       String t = (String) tToks.get(i);\r
+       \r
+                       try {\r
+                               int cInt = Integer.parseInt(c);\r
+                               int tInt = Integer.parseInt(t);\r
+                               if (cInt == tInt)\r
+                                       continue comp;\r
+                               else {\r
+                                       comp = (cInt - tInt);\r
+                                       break comp;\r
+                               }\r
+                       } catch (NumberFormatException e) {\r
+                               if (c.equals(t))\r
+                                       continue comp;\r
+                               else {\r
+                                       comp = c.compareTo(t);\r
+                                       break comp;\r
+                               }\r
                        }\r
                }\r
-               return res;\r
+       \r
+               if (comp == 0 && tToks.size() > cToks.size()) {\r
+                       // equals until then, current shorter\r
+                       comp = -1;\r
+               }\r
+       \r
+               return comp;\r
        }\r
 \r
-       public static void warnDeprecated(String name, String oldName) {\r
-               warn("Property '" + oldName\r
-                               + "' is deprecated and will be removed soon, use '" + name\r
-                               + "' instead.");\r
-       }       \r
-       \r
 }\r