]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.osgi.boot/src/org/argeo/osgi/boot/OsgiBootUtils.java
Add Jcr utilities.
[lgpl/argeo-commons.git] / org.argeo.osgi.boot / src / org / argeo / osgi / boot / OsgiBootUtils.java
1 package org.argeo.osgi.boot;
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.List;
8 import java.util.Map;
9 import java.util.StringTokenizer;
10
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;
15
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");
20
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");
23
24 public static void info(Object obj) {
25 System.out.println("# OSGiBOOT # " + dateFormat.format(new Date()) + " # " + obj);
26 }
27
28 public static void debug(Object obj) {
29 if (debug)
30 System.out.println("# OSGiBOOT DBG # " + dateFormat.format(new Date()) + " # " + obj);
31 }
32
33 public static void warn(Object obj) {
34 System.out.println("# OSGiBOOT WARN # " + dateFormat.format(new Date()) + " # " + obj);
35 }
36
37 public static void error(Object obj, Throwable e) {
38 System.err.println("# OSGiBOOT ERR # " + dateFormat.format(new Date()) + " # " + obj);
39 if (e != null)
40 e.printStackTrace();
41 }
42
43 public static boolean isDebug() {
44 return debug;
45 }
46
47 public static String stateAsString(int state) {
48 switch (state) {
49 case Bundle.UNINSTALLED:
50 return "UNINSTALLED";
51 case Bundle.INSTALLED:
52 return "INSTALLED";
53 case Bundle.RESOLVED:
54 return "RESOLVED";
55 case Bundle.STARTING:
56 return "STARTING";
57 case Bundle.ACTIVE:
58 return "ACTIVE";
59 case Bundle.STOPPING:
60 return "STOPPING";
61 default:
62 return Integer.toString(state);
63 }
64 }
65
66 /**
67 * @return ==0: versions are identical, <0: tested version is newer, >0:
68 * currentVersion is newer.
69 */
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());
79
80 int comp = 0;
81 comp: for (int i = 0; i < cToks.size(); i++) {
82 if (tToks.size() <= i) {
83 // equals until then, tested shorter
84 comp = 1;
85 break comp;
86 }
87
88 String c = (String) cToks.get(i);
89 String t = (String) tToks.get(i);
90
91 try {
92 int cInt = Integer.parseInt(c);
93 int tInt = Integer.parseInt(t);
94 if (cInt == tInt)
95 continue comp;
96 else {
97 comp = (cInt - tInt);
98 break comp;
99 }
100 } catch (NumberFormatException e) {
101 if (c.equals(t))
102 continue comp;
103 else {
104 comp = c.compareTo(t);
105 break comp;
106 }
107 }
108 }
109
110 if (comp == 0 && tToks.size() > cToks.size()) {
111 // equals until then, current shorter
112 comp = -1;
113 }
114
115 return comp;
116 }
117
118 /** Launch an OSGi framework. */
119 public static Framework launch(FrameworkFactory frameworkFactory, Map<String, String> configuration) {
120 // start OSGi
121 Framework framework = frameworkFactory.newFramework(configuration);
122 try {
123 framework.start();
124 } catch (BundleException e) {
125 throw new OsgiBootException("Cannot start OSGi framework", e);
126 }
127 return framework;
128 }
129
130 }