Document A2 OSGi Boot. Add classpath provisioning source.
[lgpl/argeo-commons.git] / org.argeo.osgi.boot / src / org / argeo / osgi / boot / OsgiBootUtils.java
index 20891c7f9ef3ada558496662063117755102d743..9593b0cde8bf8babdec9a79fe959aba90c0a679c 100644 (file)
@@ -20,51 +20,43 @@ import java.text.SimpleDateFormat;
 import java.util.ArrayList;\r
 import java.util.Date;\r
 import java.util.List;\r
+import java.util.Map;\r
 import java.util.StringTokenizer;\r
 \r
 import org.osgi.framework.Bundle;\r
+import org.osgi.framework.BundleException;\r
+import org.osgi.framework.launch.Framework;\r
+import org.osgi.framework.launch.FrameworkFactory;\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
+       private static DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss,SSS Z");\r
+\r
+       static boolean debug = System.getProperty(OsgiBoot.PROP_ARGEO_OSGI_BOOT_DEBUG) == null ? false\r
+                       : !System.getProperty(OsgiBoot.PROP_ARGEO_OSGI_BOOT_DEBUG).trim().equals("false");\r
 \r
        public static void info(Object obj) {\r
-               System.out.println("# OSGiBOOT      # " + dateFormat.format(new Date())\r
-                               + " # " + obj);\r
+               System.out.println("# OSGiBOOT      # " + dateFormat.format(new Date()) + " # " + obj);\r
        }\r
 \r
        public static void debug(Object obj) {\r
-               System.out.println("# OSGiBOOT DBG  # " + dateFormat.format(new Date())\r
-                               + " # " + obj);\r
+               if (debug)\r
+                       System.out.println("# OSGiBOOT DBG  # " + dateFormat.format(new Date()) + " # " + obj);\r
        }\r
 \r
        public static void warn(Object obj) {\r
-               System.out.println("# OSGiBOOT WARN # " + dateFormat.format(new Date())\r
-                               + " # " + obj);\r
+               System.out.println("# OSGiBOOT WARN # " + dateFormat.format(new Date()) + " # " + obj);\r
        }\r
 \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
-               if (value == null || value.equals(""))\r
-                       return null;\r
-               else\r
-                       return value;\r
+       public static void error(Object obj, Throwable e) {\r
+               System.err.println("# OSGiBOOT ERR  # " + dateFormat.format(new Date()) + " # " + obj);\r
+               if (e != null)\r
+                       e.printStackTrace();\r
        }\r
 \r
-       public static String getProperty(String name) {\r
-               return getProperty(name, null);\r
+       public static boolean isDebug() {\r
+               return debug;\r
        }\r
 \r
        public static String stateAsString(int state) {\r
@@ -87,20 +79,19 @@ public class OsgiBootUtils {
        }\r
 \r
        /**\r
-        * @return ==0: versions are identical, <0: tested version is newer, >0:\r
+        * @return ==0: versions are identical, &lt;0: tested version is newer, &gt;0:\r
         *         currentVersion is newer.\r
         */\r
-       public static int compareVersions(String currentVersion,\r
-                       String testedVersion) {\r
-               List cToks = new ArrayList();\r
+       public static int compareVersions(String currentVersion, String testedVersion) {\r
+               List<String> cToks = new ArrayList<String>();\r
                StringTokenizer cSt = new StringTokenizer(currentVersion, ".");\r
                while (cSt.hasMoreTokens())\r
                        cToks.add(cSt.nextToken());\r
-               List tToks = new ArrayList();\r
+               List<String> tToks = new ArrayList<String>();\r
                StringTokenizer tSt = new StringTokenizer(currentVersion, ".");\r
                while (tSt.hasMoreTokens())\r
                        tToks.add(tSt.nextToken());\r
-       \r
+\r
                int comp = 0;\r
                comp: for (int i = 0; i < cToks.size(); i++) {\r
                        if (tToks.size() <= i) {\r
@@ -108,10 +99,10 @@ public class OsgiBootUtils {
                                comp = 1;\r
                                break comp;\r
                        }\r
-       \r
+\r
                        String c = (String) cToks.get(i);\r
                        String t = (String) tToks.get(i);\r
-       \r
+\r
                        try {\r
                                int cInt = Integer.parseInt(c);\r
                                int tInt = Integer.parseInt(t);\r
@@ -130,13 +121,25 @@ public class OsgiBootUtils {
                                }\r
                        }\r
                }\r
-       \r
+\r
                if (comp == 0 && tToks.size() > cToks.size()) {\r
                        // equals until then, current shorter\r
                        comp = -1;\r
                }\r
-       \r
+\r
                return comp;\r
        }\r
 \r
+       /** Launch an OSGi framework. */\r
+       public static Framework launch(FrameworkFactory frameworkFactory, Map<String, String> configuration) {\r
+               // start OSGi\r
+               Framework framework = frameworkFactory.newFramework(configuration);\r
+               try {\r
+                       framework.start();\r
+               } catch (BundleException e) {\r
+                       throw new OsgiBootException("Cannot start OSGi framework", e);\r
+               }\r
+               return framework;\r
+       }\r
+\r
 }\r