1 package org
.argeo
.osgi
.boot
;
3 import java
.text
.DateFormat
;
4 import java
.text
.SimpleDateFormat
;
5 import java
.util
.ArrayList
;
9 import java
.util
.StringTokenizer
;
11 import org
.osgi
.framework
.Bundle
;
12 import org
.osgi
.framework
.BundleException
;
13 import org
.osgi
.framework
.launch
.Framework
;
14 import org
.osgi
.framework
.launch
.FrameworkFactory
;
16 /** Utilities, mostly related to logging. */
17 public class OsgiBootUtils
{
18 /** ISO8601 (as per log4j) and difference to UTC */
19 private static DateFormat dateFormat
= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss,SSS Z");
21 static boolean debug
= System
.getProperty(OsgiBoot
.PROP_ARGEO_OSGI_BOOT_DEBUG
) == null ?
false
22 : !System
.getProperty(OsgiBoot
.PROP_ARGEO_OSGI_BOOT_DEBUG
).trim().equals("false");
24 public static void info(Object obj
) {
25 System
.out
.println("# OSGiBOOT # " + dateFormat
.format(new Date()) + " # " + obj
);
28 public static void debug(Object obj
) {
30 System
.out
.println("# OSGiBOOT DBG # " + dateFormat
.format(new Date()) + " # " + obj
);
33 public static void warn(Object obj
) {
34 System
.out
.println("# OSGiBOOT WARN # " + dateFormat
.format(new Date()) + " # " + obj
);
37 public static void error(Object obj
, Throwable e
) {
38 System
.err
.println("# OSGiBOOT ERR # " + dateFormat
.format(new Date()) + " # " + obj
);
43 public static boolean isDebug() {
47 public static String
stateAsString(int state
) {
49 case Bundle
.UNINSTALLED
:
51 case Bundle
.INSTALLED
:
62 return Integer
.toString(state
);
67 * @return ==0: versions are identical, <0: tested version is newer, >0:
68 * currentVersion is newer.
70 public static int compareVersions(String currentVersion
, String testedVersion
) {
71 List
<String
> cToks
= new ArrayList
<String
>();
72 StringTokenizer cSt
= new StringTokenizer(currentVersion
, ".");
73 while (cSt
.hasMoreTokens())
74 cToks
.add(cSt
.nextToken());
75 List
<String
> tToks
= new ArrayList
<String
>();
76 StringTokenizer tSt
= new StringTokenizer(currentVersion
, ".");
77 while (tSt
.hasMoreTokens())
78 tToks
.add(tSt
.nextToken());
81 comp
: for (int i
= 0; i
< cToks
.size(); i
++) {
82 if (tToks
.size() <= i
) {
83 // equals until then, tested shorter
88 String c
= (String
) cToks
.get(i
);
89 String t
= (String
) tToks
.get(i
);
92 int cInt
= Integer
.parseInt(c
);
93 int tInt
= Integer
.parseInt(t
);
100 } catch (NumberFormatException e
) {
104 comp
= c
.compareTo(t
);
110 if (comp
== 0 && tToks
.size() > cToks
.size()) {
111 // equals until then, current shorter
118 /** Launch an OSGi framework. */
119 public static Framework
launch(FrameworkFactory frameworkFactory
, Map
<String
, String
> configuration
) {
121 Framework framework
= frameworkFactory
.newFramework(configuration
);
124 } catch (BundleException e
) {
125 throw new OsgiBootException("Cannot start OSGi framework", e
);