]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.osgi.boot/src/org/argeo/osgi/boot/OsgiBootUtils.java
Fix child node access in FreeMarker
[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 = System.getProperty(OsgiBoot.PROP_ARGEO_OSGI_BOOT_DEBUG) == null ? false
33 : !System.getProperty(OsgiBoot.PROP_ARGEO_OSGI_BOOT_DEBUG).trim().equals("false");
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 boolean isDebug() {
55 return debug;
56 }
57
58 public static String stateAsString(int state) {
59 switch (state) {
60 case Bundle.UNINSTALLED:
61 return "UNINSTALLED";
62 case Bundle.INSTALLED:
63 return "INSTALLED";
64 case Bundle.RESOLVED:
65 return "RESOLVED";
66 case Bundle.STARTING:
67 return "STARTING";
68 case Bundle.ACTIVE:
69 return "ACTIVE";
70 case Bundle.STOPPING:
71 return "STOPPING";
72 default:
73 return Integer.toString(state);
74 }
75 }
76
77 /**
78 * @return ==0: versions are identical, <0: tested version is newer, >0:
79 * currentVersion is newer.
80 */
81 public static int compareVersions(String currentVersion, String testedVersion) {
82 List<String> cToks = new ArrayList<String>();
83 StringTokenizer cSt = new StringTokenizer(currentVersion, ".");
84 while (cSt.hasMoreTokens())
85 cToks.add(cSt.nextToken());
86 List<String> tToks = new ArrayList<String>();
87 StringTokenizer tSt = new StringTokenizer(currentVersion, ".");
88 while (tSt.hasMoreTokens())
89 tToks.add(tSt.nextToken());
90
91 int comp = 0;
92 comp: for (int i = 0; i < cToks.size(); i++) {
93 if (tToks.size() <= i) {
94 // equals until then, tested shorter
95 comp = 1;
96 break comp;
97 }
98
99 String c = (String) cToks.get(i);
100 String t = (String) tToks.get(i);
101
102 try {
103 int cInt = Integer.parseInt(c);
104 int tInt = Integer.parseInt(t);
105 if (cInt == tInt)
106 continue comp;
107 else {
108 comp = (cInt - tInt);
109 break comp;
110 }
111 } catch (NumberFormatException e) {
112 if (c.equals(t))
113 continue comp;
114 else {
115 comp = c.compareTo(t);
116 break comp;
117 }
118 }
119 }
120
121 if (comp == 0 && tToks.size() > cToks.size()) {
122 // equals until then, current shorter
123 comp = -1;
124 }
125
126 return comp;
127 }
128
129 }