]> git.argeo.org Git - lgpl/argeo-commons.git/blob - osgi/OsgiBootUtils.java
Prepare next development cycle
[lgpl/argeo-commons.git] / osgi / OsgiBootUtils.java
1 package org.argeo.init.osgi;
2
3 import java.lang.System.Logger;
4 import java.lang.System.Logger.Level;
5 import java.util.ArrayList;
6 import java.util.HashMap;
7 import java.util.List;
8 import java.util.Map;
9 import java.util.Objects;
10 import java.util.Optional;
11 import java.util.ServiceLoader;
12 import java.util.StringTokenizer;
13
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;
18
19 /** Utilities, mostly related to logging. */
20 public class OsgiBootUtils {
21 private final static Logger logger = System.getLogger(OsgiBootUtils.class.getName());
22
23 // /** ISO8601 (as per log4j) and difference to UTC */
24 // private static DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss,SSS Z");
25
26 // static boolean debug = System.getProperty(OsgiBoot.PROP_ARGEO_OSGI_BOOT_DEBUG) == null ? false
27 // : !System.getProperty(OsgiBoot.PROP_ARGEO_OSGI_BOOT_DEBUG).trim().equals("false");
28
29 @Deprecated
30 public static void info(Object obj) {
31 // System.out.println("# OSGiBOOT # " + dateFormat.format(new Date()) + " # " + obj);
32 logger.log(Level.INFO, () -> Objects.toString(obj));
33 }
34
35 @Deprecated
36 public static void debug(Object obj) {
37 // if (debug)
38 // System.out.println("# OSGiBOOT DBG # " + dateFormat.format(new Date()) + " # " + obj);
39 logger.log(Level.TRACE, () -> Objects.toString(obj));
40 }
41
42 @Deprecated
43 public static void warn(Object obj) {
44 // System.out.println("# OSGiBOOT WARN # " + dateFormat.format(new Date()) + " # " + obj);
45 logger.log(Level.WARNING, () -> Objects.toString(obj));
46 }
47
48 @Deprecated
49 public static void error(Object obj, Throwable e) {
50 // System.err.println("# OSGiBOOT ERR # " + dateFormat.format(new Date()) + " # " + obj);
51 // if (e != null)
52 // e.printStackTrace();
53 logger.log(Level.ERROR, () -> Objects.toString(obj), e);
54 }
55
56 @Deprecated
57 public static boolean isDebug() {
58 // return debug;
59 return logger.isLoggable(Level.TRACE);
60 }
61
62 public static String stateAsString(int state) {
63 switch (state) {
64 case Bundle.UNINSTALLED:
65 return "UNINSTALLED";
66 case Bundle.INSTALLED:
67 return "INSTALLED";
68 case Bundle.RESOLVED:
69 return "RESOLVED";
70 case Bundle.STARTING:
71 return "STARTING";
72 case Bundle.ACTIVE:
73 return "ACTIVE";
74 case Bundle.STOPPING:
75 return "STOPPING";
76 default:
77 return Integer.toString(state);
78 }
79 }
80
81 /**
82 * @return ==0: versions are identical, <0: tested version is newer, >0:
83 * currentVersion is newer.
84 */
85 public static int compareVersions(String currentVersion, String testedVersion) {
86 List<String> cToks = new ArrayList<String>();
87 StringTokenizer cSt = new StringTokenizer(currentVersion, ".");
88 while (cSt.hasMoreTokens())
89 cToks.add(cSt.nextToken());
90 List<String> tToks = new ArrayList<String>();
91 StringTokenizer tSt = new StringTokenizer(currentVersion, ".");
92 while (tSt.hasMoreTokens())
93 tToks.add(tSt.nextToken());
94
95 int comp = 0;
96 comp: for (int i = 0; i < cToks.size(); i++) {
97 if (tToks.size() <= i) {
98 // equals until then, tested shorter
99 comp = 1;
100 break comp;
101 }
102
103 String c = (String) cToks.get(i);
104 String t = (String) tToks.get(i);
105
106 try {
107 int cInt = Integer.parseInt(c);
108 int tInt = Integer.parseInt(t);
109 if (cInt == tInt)
110 continue comp;
111 else {
112 comp = (cInt - tInt);
113 break comp;
114 }
115 } catch (NumberFormatException e) {
116 if (c.equals(t))
117 continue comp;
118 else {
119 comp = c.compareTo(t);
120 break comp;
121 }
122 }
123 }
124
125 if (comp == 0 && tToks.size() > cToks.size()) {
126 // equals until then, current shorter
127 comp = -1;
128 }
129
130 return comp;
131 }
132
133 public static Framework launch(Map<String, String> configuration) {
134 Optional<FrameworkFactory> frameworkFactory = ServiceLoader.load(FrameworkFactory.class).findFirst();
135 if (frameworkFactory.isEmpty())
136 throw new IllegalStateException("No framework factory found");
137 return launch(frameworkFactory.get(), configuration);
138 }
139
140 /** Launch an OSGi framework. */
141 public static Framework launch(FrameworkFactory frameworkFactory, Map<String, String> configuration) {
142 // start OSGi
143 Framework framework = frameworkFactory.newFramework(configuration);
144 try {
145 framework.start();
146 } catch (BundleException e) {
147 throw new IllegalStateException("Cannot start OSGi framework", e);
148 }
149 return framework;
150 }
151
152 @Deprecated
153 public static Map<String, String> equinoxArgsToConfiguration(String[] args) {
154 // FIXME implement it
155 return new HashMap<>();
156 }
157
158 }