]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.init/src/org/argeo/init/osgi/OsgiBootUtils.java
Remove unused configurations in build.properties
[lgpl/argeo-commons.git] / org.argeo.init / src / org / argeo / init / osgi / OsgiBootUtils.java
1 package org.argeo.init.osgi;
2
3 import java.text.DateFormat;
4 import java.text.SimpleDateFormat;
5 import java.util.ArrayList;
6 import java.util.Date;
7 import java.util.HashMap;
8 import java.util.List;
9 import java.util.Map;
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 /** ISO8601 (as per log4j) and difference to UTC */
22 private static DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss,SSS Z");
23
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");
26
27 public static void info(Object obj) {
28 System.out.println("# OSGiBOOT # " + dateFormat.format(new Date()) + " # " + obj);
29 }
30
31 public static void debug(Object obj) {
32 if (debug)
33 System.out.println("# OSGiBOOT DBG # " + dateFormat.format(new Date()) + " # " + obj);
34 }
35
36 public static void warn(Object obj) {
37 System.out.println("# OSGiBOOT WARN # " + dateFormat.format(new Date()) + " # " + obj);
38 }
39
40 public static void error(Object obj, Throwable e) {
41 System.err.println("# OSGiBOOT ERR # " + dateFormat.format(new Date()) + " # " + obj);
42 if (e != null)
43 e.printStackTrace();
44 }
45
46 public static boolean isDebug() {
47 return debug;
48 }
49
50 public static String stateAsString(int state) {
51 switch (state) {
52 case Bundle.UNINSTALLED:
53 return "UNINSTALLED";
54 case Bundle.INSTALLED:
55 return "INSTALLED";
56 case Bundle.RESOLVED:
57 return "RESOLVED";
58 case Bundle.STARTING:
59 return "STARTING";
60 case Bundle.ACTIVE:
61 return "ACTIVE";
62 case Bundle.STOPPING:
63 return "STOPPING";
64 default:
65 return Integer.toString(state);
66 }
67 }
68
69 /**
70 * @return ==0: versions are identical, <0: tested version is newer, >0:
71 * currentVersion is newer.
72 */
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());
82
83 int comp = 0;
84 comp: for (int i = 0; i < cToks.size(); i++) {
85 if (tToks.size() <= i) {
86 // equals until then, tested shorter
87 comp = 1;
88 break comp;
89 }
90
91 String c = (String) cToks.get(i);
92 String t = (String) tToks.get(i);
93
94 try {
95 int cInt = Integer.parseInt(c);
96 int tInt = Integer.parseInt(t);
97 if (cInt == tInt)
98 continue comp;
99 else {
100 comp = (cInt - tInt);
101 break comp;
102 }
103 } catch (NumberFormatException e) {
104 if (c.equals(t))
105 continue comp;
106 else {
107 comp = c.compareTo(t);
108 break comp;
109 }
110 }
111 }
112
113 if (comp == 0 && tToks.size() > cToks.size()) {
114 // equals until then, current shorter
115 comp = -1;
116 }
117
118 return comp;
119 }
120
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);
126 }
127
128 /** Launch an OSGi framework. */
129 public static Framework launch(FrameworkFactory frameworkFactory, Map<String, String> configuration) {
130 // start OSGi
131 Framework framework = frameworkFactory.newFramework(configuration);
132 try {
133 framework.start();
134 } catch (BundleException e) {
135 throw new IllegalStateException("Cannot start OSGi framework", e);
136 }
137 return framework;
138 }
139
140 public static Map<String, String> equinoxArgsToConfiguration(String[] args) {
141 // FIXME implement it
142 return new HashMap<>();
143 }
144
145 }