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