1 package org
.argeo
.init
.osgi
;
3 import java
.text
.DateFormat
;
4 import java
.text
.SimpleDateFormat
;
5 import java
.util
.ArrayList
;
7 import java
.util
.HashMap
;
10 import java
.util
.Optional
;
11 import java
.util
.ServiceLoader
;
12 import java
.util
.StringTokenizer
;
14 import org
.osgi
.framework
.Bundle
;
15 import org
.osgi
.framework
.BundleException
;
16 import org
.osgi
.framework
.launch
.Framework
;
17 import org
.osgi
.framework
.launch
.FrameworkFactory
;
19 /** Utilities, mostly related to logging. */
20 public class OsgiBootUtils
{
21 /** ISO8601 (as per log4j) and difference to UTC */
22 private static DateFormat dateFormat
= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss,SSS Z");
24 static boolean debug
= System
.getProperty(OsgiBoot
.PROP_ARGEO_OSGI_BOOT_DEBUG
) == null ?
false
25 : !System
.getProperty(OsgiBoot
.PROP_ARGEO_OSGI_BOOT_DEBUG
).trim().equals("false");
27 public static void info(Object obj
) {
28 System
.out
.println("# OSGiBOOT # " + dateFormat
.format(new Date()) + " # " + obj
);
31 public static void debug(Object obj
) {
33 System
.out
.println("# OSGiBOOT DBG # " + dateFormat
.format(new Date()) + " # " + obj
);
36 public static void warn(Object obj
) {
37 System
.out
.println("# OSGiBOOT WARN # " + dateFormat
.format(new Date()) + " # " + obj
);
40 public static void error(Object obj
, Throwable e
) {
41 System
.err
.println("# OSGiBOOT ERR # " + dateFormat
.format(new Date()) + " # " + obj
);
46 public static boolean isDebug() {
50 public static String
stateAsString(int state
) {
52 case Bundle
.UNINSTALLED
:
54 case Bundle
.INSTALLED
:
65 return Integer
.toString(state
);
70 * @return ==0: versions are identical, <0: tested version is newer, >0:
71 * currentVersion is newer.
73 public static int compareVersions(String currentVersion
, String testedVersion
) {
74 List
<String
> cToks
= new ArrayList
<String
>();
75 StringTokenizer cSt
= new StringTokenizer(currentVersion
, ".");
76 while (cSt
.hasMoreTokens())
77 cToks
.add(cSt
.nextToken());
78 List
<String
> tToks
= new ArrayList
<String
>();
79 StringTokenizer tSt
= new StringTokenizer(currentVersion
, ".");
80 while (tSt
.hasMoreTokens())
81 tToks
.add(tSt
.nextToken());
84 comp
: for (int i
= 0; i
< cToks
.size(); i
++) {
85 if (tToks
.size() <= i
) {
86 // equals until then, tested shorter
91 String c
= (String
) cToks
.get(i
);
92 String t
= (String
) tToks
.get(i
);
95 int cInt
= Integer
.parseInt(c
);
96 int tInt
= Integer
.parseInt(t
);
100 comp
= (cInt
- tInt
);
103 } catch (NumberFormatException e
) {
107 comp
= c
.compareTo(t
);
113 if (comp
== 0 && tToks
.size() > cToks
.size()) {
114 // equals until then, current shorter
121 public static Framework
launch(Map
<String
, String
> configuration
) {
122 Optional
<FrameworkFactory
> frameworkFactory
= ServiceLoader
.load(FrameworkFactory
.class).findFirst();
123 if (frameworkFactory
.isEmpty())
124 throw new IllegalStateException("No framework factory found");
125 return launch(frameworkFactory
.get(), configuration
);
128 /** Launch an OSGi framework. */
129 public static Framework
launch(FrameworkFactory frameworkFactory
, Map
<String
, String
> configuration
) {
131 Framework framework
= frameworkFactory
.newFramework(configuration
);
134 } catch (BundleException e
) {
135 throw new IllegalStateException("Cannot start OSGi framework", e
);
140 public static Map
<String
, String
> equinoxArgsToConfiguration(String
[] args
) {
141 // FIXME implement it
142 return new HashMap
<>();