2 * Copyright (C) 2007-2012 Argeo GmbH
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org
.argeo
.osgi
.boot
;
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
;
25 import org
.osgi
.framework
.Bundle
;
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");
32 static boolean debug
= Boolean
.valueOf(System
.getProperty(OsgiBoot
.PROP_ARGEO_OSGI_BOOT_DEBUG
, "false"))
35 public static void info(Object obj
) {
36 System
.out
.println("# OSGiBOOT # " + dateFormat
.format(new Date()) + " # " + obj
);
39 public static void debug(Object obj
) {
41 System
.out
.println("# OSGiBOOT DBG # " + dateFormat
.format(new Date()) + " # " + obj
);
44 public static void warn(Object obj
) {
45 System
.out
.println("# OSGiBOOT WARN # " + dateFormat
.format(new Date()) + " # " + obj
);
48 public static void error(Object obj
, Throwable e
) {
49 System
.err
.println("# OSGiBOOT ERR # " + dateFormat
.format(new Date()) + " # " + obj
);
54 public static String
stateAsString(int state
) {
56 case Bundle
.UNINSTALLED
:
58 case Bundle
.INSTALLED
:
69 return Integer
.toString(state
);
74 * @return ==0: versions are identical, <0: tested version is newer, >0:
75 * currentVersion is newer.
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());
88 comp
: for (int i
= 0; i
< cToks
.size(); i
++) {
89 if (tToks
.size() <= i
) {
90 // equals until then, tested shorter
95 String c
= (String
) cToks
.get(i
);
96 String t
= (String
) tToks
.get(i
);
99 int cInt
= Integer
.parseInt(c
);
100 int tInt
= Integer
.parseInt(t
);
104 comp
= (cInt
- tInt
);
107 } catch (NumberFormatException e
) {
111 comp
= c
.compareTo(t
);
117 if (comp
== 0 && tToks
.size() > cToks
.size()) {
118 // equals until then, current shorter