]> git.argeo.org Git - lgpl/argeo-commons.git/blob - OsgiBootUtils.java
54efffc8056504f7e15234c4791b742a7a406366
[lgpl/argeo-commons.git] / 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 public static void info(Object obj) {
33 System.out.println("# OSGiBOOT # " + dateFormat.format(new Date()) + " # " + obj);
34 }
35
36 public static void debug(Object obj) {
37 System.out.println("# OSGiBOOT DBG # " + dateFormat.format(new Date()) + " # " + obj);
38 }
39
40 public static void warn(Object obj) {
41 System.out.println("# OSGiBOOT WARN # " + dateFormat.format(new Date()) + " # " + obj);
42 }
43
44 public static void error(Object obj, Throwable e) {
45 System.err.println("# OSGiBOOT ERR # " + dateFormat.format(new Date()) + " # " + obj);
46 if (e != null)
47 e.printStackTrace();
48 }
49
50 /**
51 * Gets a property value
52 *
53 * @return null when defaultValue is ""
54 */
55 public static String getProperty(String name, String defaultValue) {
56 final String value;
57 if (defaultValue != null)
58 value = System.getProperty(name, defaultValue);
59 else
60 value = System.getProperty(name);
61
62 if (value == null || value.equals(""))
63 return null;
64 else
65 return value;
66 }
67
68 public static String getProperty(String name) {
69 return getProperty(name, null);
70 }
71
72 public static String stateAsString(int state) {
73 switch (state) {
74 case Bundle.UNINSTALLED:
75 return "UNINSTALLED";
76 case Bundle.INSTALLED:
77 return "INSTALLED";
78 case Bundle.RESOLVED:
79 return "RESOLVED";
80 case Bundle.STARTING:
81 return "STARTING";
82 case Bundle.ACTIVE:
83 return "ACTIVE";
84 case Bundle.STOPPING:
85 return "STOPPING";
86 default:
87 return Integer.toString(state);
88 }
89 }
90
91 /**
92 * @return ==0: versions are identical, <0: tested version is newer, >0:
93 * currentVersion is newer.
94 */
95 public static int compareVersions(String currentVersion, String testedVersion) {
96 List<String> cToks = new ArrayList<String>();
97 StringTokenizer cSt = new StringTokenizer(currentVersion, ".");
98 while (cSt.hasMoreTokens())
99 cToks.add(cSt.nextToken());
100 List<String> tToks = new ArrayList<String>();
101 StringTokenizer tSt = new StringTokenizer(currentVersion, ".");
102 while (tSt.hasMoreTokens())
103 tToks.add(tSt.nextToken());
104
105 int comp = 0;
106 comp: for (int i = 0; i < cToks.size(); i++) {
107 if (tToks.size() <= i) {
108 // equals until then, tested shorter
109 comp = 1;
110 break comp;
111 }
112
113 String c = (String) cToks.get(i);
114 String t = (String) tToks.get(i);
115
116 try {
117 int cInt = Integer.parseInt(c);
118 int tInt = Integer.parseInt(t);
119 if (cInt == tInt)
120 continue comp;
121 else {
122 comp = (cInt - tInt);
123 break comp;
124 }
125 } catch (NumberFormatException e) {
126 if (c.equals(t))
127 continue comp;
128 else {
129 comp = c.compareTo(t);
130 break comp;
131 }
132 }
133 }
134
135 if (comp == 0 && tToks.size() > cToks.size()) {
136 // equals until then, current shorter
137 comp = -1;
138 }
139
140 return comp;
141 }
142
143 }