]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.osgi.boot/src/org/argeo/osgi/boot/OsgiBootUtils.java
212e6baa4f208437cc217d4ab3a76df668b52549
[lgpl/argeo-commons.git] / org.argeo.osgi.boot / src / org / argeo / osgi / boot / OsgiBootUtils.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.argeo.osgi.boot;
17
18 import java.text.DateFormat;
19 import java.text.SimpleDateFormat;
20 import java.util.ArrayList;
21 import java.util.Date;
22 import java.util.List;
23 import java.util.StringTokenizer;
24
25 import org.osgi.framework.Bundle;
26
27 /** Utilities, mostly related to logging. */
28 public class OsgiBootUtils {
29 /** ISO8601 (as per log4j) and difference to UTC */
30 private static DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss,SSS Z");
31
32 static boolean debug = Boolean.valueOf(System.getProperty(OsgiBoot.PROP_ARGEO_OSGI_BOOT_DEBUG, "false"))
33 .booleanValue();
34
35 public static void info(Object obj) {
36 System.out.println("# OSGiBOOT # " + dateFormat.format(new Date()) + " # " + obj);
37 }
38
39 public static void debug(Object obj) {
40 if (debug)
41 System.out.println("# OSGiBOOT DBG # " + dateFormat.format(new Date()) + " # " + obj);
42 }
43
44 public static void warn(Object obj) {
45 System.out.println("# OSGiBOOT WARN # " + dateFormat.format(new Date()) + " # " + obj);
46 }
47
48 public static void error(Object obj, Throwable e) {
49 System.err.println("# OSGiBOOT ERR # " + dateFormat.format(new Date()) + " # " + obj);
50 if (e != null)
51 e.printStackTrace();
52 }
53
54 public static String stateAsString(int state) {
55 switch (state) {
56 case Bundle.UNINSTALLED:
57 return "UNINSTALLED";
58 case Bundle.INSTALLED:
59 return "INSTALLED";
60 case Bundle.RESOLVED:
61 return "RESOLVED";
62 case Bundle.STARTING:
63 return "STARTING";
64 case Bundle.ACTIVE:
65 return "ACTIVE";
66 case Bundle.STOPPING:
67 return "STOPPING";
68 default:
69 return Integer.toString(state);
70 }
71 }
72
73 /**
74 * @return ==0: versions are identical, <0: tested version is newer, >0:
75 * currentVersion is newer.
76 */
77 public static int compareVersions(String currentVersion, String testedVersion) {
78 List<String> cToks = new ArrayList<String>();
79 StringTokenizer cSt = new StringTokenizer(currentVersion, ".");
80 while (cSt.hasMoreTokens())
81 cToks.add(cSt.nextToken());
82 List<String> tToks = new ArrayList<String>();
83 StringTokenizer tSt = new StringTokenizer(currentVersion, ".");
84 while (tSt.hasMoreTokens())
85 tToks.add(tSt.nextToken());
86
87 int comp = 0;
88 comp: for (int i = 0; i < cToks.size(); i++) {
89 if (tToks.size() <= i) {
90 // equals until then, tested shorter
91 comp = 1;
92 break comp;
93 }
94
95 String c = (String) cToks.get(i);
96 String t = (String) tToks.get(i);
97
98 try {
99 int cInt = Integer.parseInt(c);
100 int tInt = Integer.parseInt(t);
101 if (cInt == tInt)
102 continue comp;
103 else {
104 comp = (cInt - tInt);
105 break comp;
106 }
107 } catch (NumberFormatException e) {
108 if (c.equals(t))
109 continue comp;
110 else {
111 comp = c.compareTo(t);
112 break comp;
113 }
114 }
115 }
116
117 if (comp == 0 && tToks.size() > cToks.size()) {
118 // equals until then, current shorter
119 comp = -1;
120 }
121
122 return comp;
123 }
124
125 }