-/*\r
- * Copyright (C) 2007-2012 Argeo GmbH\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
- * You may obtain a copy of the License at\r
- *\r
- * http://www.apache.org/licenses/LICENSE-2.0\r
- *\r
- * Unless required by applicable law or agreed to in writing, software\r
- * distributed under the License is distributed on an "AS IS" BASIS,\r
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- * See the License for the specific language governing permissions and\r
- * limitations under the License.\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("yyyy-MM-dd HH:mm:ss,SSS Z");\r
-\r
- public static void info(Object 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()) + " # " + obj);\r
- }\r
-\r
- public static void warn(Object obj) {\r
- System.out.println("# OSGiBOOT WARN # " + dateFormat.format(new Date()) + " # " + obj);\r
- }\r
-\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
-\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
- /**\r
- * @return ==0: versions are identical, <0: tested version is newer, >0:\r
- * currentVersion is newer.\r
- */\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<String> tToks = new ArrayList<String>();\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
-\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
-\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
-}\r
+package org.argeo.osgi.boot;
+
+import java.text.DateFormat;
+import java.text.SimpleDateFormat;
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.List;
+import java.util.Map;
+import java.util.StringTokenizer;
+
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleException;
+import org.osgi.framework.launch.Framework;
+import org.osgi.framework.launch.FrameworkFactory;
+
+/** Utilities, mostly related to logging. */
+public class OsgiBootUtils {
+ /** ISO8601 (as per log4j) and difference to UTC */
+ private static DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss,SSS Z");
+
+ static boolean debug = System.getProperty(OsgiBoot.PROP_ARGEO_OSGI_BOOT_DEBUG) == null ? false
+ : !System.getProperty(OsgiBoot.PROP_ARGEO_OSGI_BOOT_DEBUG).trim().equals("false");
+
+ public static void info(Object obj) {
+ System.out.println("# OSGiBOOT # " + dateFormat.format(new Date()) + " # " + obj);
+ }
+
+ public static void debug(Object obj) {
+ if (debug)
+ System.out.println("# OSGiBOOT DBG # " + dateFormat.format(new Date()) + " # " + obj);
+ }
+
+ public static void warn(Object obj) {
+ System.out.println("# OSGiBOOT WARN # " + dateFormat.format(new Date()) + " # " + obj);
+ }
+
+ public static void error(Object obj, Throwable e) {
+ System.err.println("# OSGiBOOT ERR # " + dateFormat.format(new Date()) + " # " + obj);
+ if (e != null)
+ e.printStackTrace();
+ }
+
+ public static boolean isDebug() {
+ return debug;
+ }
+
+ public static String stateAsString(int state) {
+ switch (state) {
+ case Bundle.UNINSTALLED:
+ return "UNINSTALLED";
+ case Bundle.INSTALLED:
+ return "INSTALLED";
+ case Bundle.RESOLVED:
+ return "RESOLVED";
+ case Bundle.STARTING:
+ return "STARTING";
+ case Bundle.ACTIVE:
+ return "ACTIVE";
+ case Bundle.STOPPING:
+ return "STOPPING";
+ default:
+ return Integer.toString(state);
+ }
+ }
+
+ /**
+ * @return ==0: versions are identical, <0: tested version is newer, >0:
+ * currentVersion is newer.
+ */
+ public static int compareVersions(String currentVersion, String testedVersion) {
+ List<String> cToks = new ArrayList<String>();
+ StringTokenizer cSt = new StringTokenizer(currentVersion, ".");
+ while (cSt.hasMoreTokens())
+ cToks.add(cSt.nextToken());
+ List<String> tToks = new ArrayList<String>();
+ StringTokenizer tSt = new StringTokenizer(currentVersion, ".");
+ while (tSt.hasMoreTokens())
+ tToks.add(tSt.nextToken());
+
+ int comp = 0;
+ comp: for (int i = 0; i < cToks.size(); i++) {
+ if (tToks.size() <= i) {
+ // equals until then, tested shorter
+ comp = 1;
+ break comp;
+ }
+
+ String c = (String) cToks.get(i);
+ String t = (String) tToks.get(i);
+
+ try {
+ int cInt = Integer.parseInt(c);
+ int tInt = Integer.parseInt(t);
+ if (cInt == tInt)
+ continue comp;
+ else {
+ comp = (cInt - tInt);
+ break comp;
+ }
+ } catch (NumberFormatException e) {
+ if (c.equals(t))
+ continue comp;
+ else {
+ comp = c.compareTo(t);
+ break comp;
+ }
+ }
+ }
+
+ if (comp == 0 && tToks.size() > cToks.size()) {
+ // equals until then, current shorter
+ comp = -1;
+ }
+
+ return comp;
+ }
+
+ /** Launch an OSGi framework. */
+ public static Framework launch(FrameworkFactory frameworkFactory, Map<String, String> configuration) {
+ // start OSGi
+ Framework framework = frameworkFactory.newFramework(configuration);
+ try {
+ framework.start();
+ } catch (BundleException e) {
+ throw new OsgiBootException("Cannot start OSGi framework", e);
+ }
+ return framework;
+ }
+
+}