]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.osgi.boot/src/org/argeo/osgi/boot/OsgiBootUtils.java
Rename container scripts.
[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.Map;
24 import java.util.StringTokenizer;
25
26 import org.osgi.framework.Bundle;
27 import org.osgi.framework.BundleException;
28 import org.osgi.framework.launch.Framework;
29 import org.osgi.framework.launch.FrameworkFactory;
30
31 /** Utilities, mostly related to logging. */
32 public class OsgiBootUtils {
33 /** ISO8601 (as per log4j) and difference to UTC */
34 private static DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss,SSS Z");
35
36 static boolean debug = System.getProperty(OsgiBoot.PROP_ARGEO_OSGI_BOOT_DEBUG) == null ? false
37 : !System.getProperty(OsgiBoot.PROP_ARGEO_OSGI_BOOT_DEBUG).trim().equals("false");
38
39 public static void info(Object obj) {
40 System.out.println("# OSGiBOOT # " + dateFormat.format(new Date()) + " # " + obj);
41 }
42
43 public static void debug(Object obj) {
44 if (debug)
45 System.out.println("# OSGiBOOT DBG # " + dateFormat.format(new Date()) + " # " + obj);
46 }
47
48 public static void warn(Object obj) {
49 System.out.println("# OSGiBOOT WARN # " + dateFormat.format(new Date()) + " # " + obj);
50 }
51
52 public static void error(Object obj, Throwable e) {
53 System.err.println("# OSGiBOOT ERR # " + dateFormat.format(new Date()) + " # " + obj);
54 if (e != null)
55 e.printStackTrace();
56 }
57
58 public static boolean isDebug() {
59 return debug;
60 }
61
62 public static String stateAsString(int state) {
63 switch (state) {
64 case Bundle.UNINSTALLED:
65 return "UNINSTALLED";
66 case Bundle.INSTALLED:
67 return "INSTALLED";
68 case Bundle.RESOLVED:
69 return "RESOLVED";
70 case Bundle.STARTING:
71 return "STARTING";
72 case Bundle.ACTIVE:
73 return "ACTIVE";
74 case Bundle.STOPPING:
75 return "STOPPING";
76 default:
77 return Integer.toString(state);
78 }
79 }
80
81 /**
82 * @return ==0: versions are identical, <0: tested version is newer, >0:
83 * currentVersion is newer.
84 */
85 public static int compareVersions(String currentVersion, String testedVersion) {
86 List<String> cToks = new ArrayList<String>();
87 StringTokenizer cSt = new StringTokenizer(currentVersion, ".");
88 while (cSt.hasMoreTokens())
89 cToks.add(cSt.nextToken());
90 List<String> tToks = new ArrayList<String>();
91 StringTokenizer tSt = new StringTokenizer(currentVersion, ".");
92 while (tSt.hasMoreTokens())
93 tToks.add(tSt.nextToken());
94
95 int comp = 0;
96 comp: for (int i = 0; i < cToks.size(); i++) {
97 if (tToks.size() <= i) {
98 // equals until then, tested shorter
99 comp = 1;
100 break comp;
101 }
102
103 String c = (String) cToks.get(i);
104 String t = (String) tToks.get(i);
105
106 try {
107 int cInt = Integer.parseInt(c);
108 int tInt = Integer.parseInt(t);
109 if (cInt == tInt)
110 continue comp;
111 else {
112 comp = (cInt - tInt);
113 break comp;
114 }
115 } catch (NumberFormatException e) {
116 if (c.equals(t))
117 continue comp;
118 else {
119 comp = c.compareTo(t);
120 break comp;
121 }
122 }
123 }
124
125 if (comp == 0 && tToks.size() > cToks.size()) {
126 // equals until then, current shorter
127 comp = -1;
128 }
129
130 return comp;
131 }
132
133 /** Launch an OSGi framework. */
134 public static Framework launch(FrameworkFactory frameworkFactory, Map<String, String> configuration) {
135 // start OSGi
136 Framework framework = frameworkFactory.newFramework(configuration);
137 try {
138 framework.start();
139 } catch (BundleException e) {
140 throw new OsgiBootException("Cannot start OSGi framework", e);
141 }
142 return framework;
143 }
144
145 }