X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;f=osgi%2Fruntime%2Forg.argeo.osgi.boot%2Fsrc%2Fmain%2Fjava%2Forg%2Fargeo%2Fosgi%2Fboot%2FOsgiBootUtils.java;h=f31665578f3211912e579a34fe8da7071526f560;hb=c3f8e165014d2ef6ecec57c44a59e816791db01d;hp=a9067a213c04e912985fb139651db012c4b3ff3b;hpb=1b452a434924d7628c7b2eace3c5df8e62cdea1c;p=lgpl%2Fargeo-commons.git diff --git a/osgi/runtime/org.argeo.osgi.boot/src/main/java/org/argeo/osgi/boot/OsgiBootUtils.java b/osgi/runtime/org.argeo.osgi.boot/src/main/java/org/argeo/osgi/boot/OsgiBootUtils.java index a9067a213..f31665578 100644 --- a/osgi/runtime/org.argeo.osgi.boot/src/main/java/org/argeo/osgi/boot/OsgiBootUtils.java +++ b/osgi/runtime/org.argeo.osgi.boot/src/main/java/org/argeo/osgi/boot/OsgiBootUtils.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2010 Mathieu Baudier + * Copyright (C) 2007-2012 Mathieu Baudier * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,36 +13,50 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package org.argeo.osgi.boot; +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; +import java.util.StringTokenizer; + +import org.osgi.framework.Bundle; + +/** Utilities, mostly related to logging. */ public class OsgiBootUtils { + /** ISO8601 (as per log4j) and difference to UTC */ + private static DateFormat dateFormat = new SimpleDateFormat( + "yyyy-MM-dd HH:mm:ss,SSS Z"); public static void info(Object obj) { - System.out.println("# OSGiBOOT # " + obj); + System.out.println("# OSGiBOOT # " + dateFormat.format(new Date()) + + " # " + obj); } public static void debug(Object obj) { - System.out.println("# OSGiBOOT DBG # " + obj); + System.out.println("# OSGiBOOT DBG # " + dateFormat.format(new Date()) + + " # " + obj); } public static void warn(Object obj) { - System.out.println("# OSGiBOOT WARN # " + obj); - // Because of a weird bug under Windows when starting it in a forked VM - // if (System.getProperty("os.name").contains("Windows")) - // System.out.println("# WARN " + obj); - // else - // System.err.println("# WARN " + obj); + System.out.println("# OSGiBOOT WARN # " + dateFormat.format(new Date()) + + " # " + obj); } - //FIXE: returns null when defaultValue is "" + /** + * Gets a property value + * + * @return null when defaultValue is "" + */ public static String getProperty(String name, String defaultValue) { final String value; if (defaultValue != null) value = System.getProperty(name, defaultValue); else value = System.getProperty(name); - + if (value == null || value.equals("")) return null; else @@ -53,36 +67,76 @@ public class OsgiBootUtils { return getProperty(name, null); } - public static String getPropertyCompat(String name, String oldName) { - return getPropertyCompat(name, oldName, null); + public static String stateAsString(int state) { + switch (state) { + case Bundle.UNINSTALLED: + return "UNINSTALLED"; + case Bundle.INSTALLED: + return "INSTALLED"; + case Bundle.RESOLVED: + return "RESOLVED"; + case Bundle.STARTING: + return "STARTING"; + case Bundle.ACTIVE: + return "ACTIVE"; + case Bundle.STOPPING: + return "STOPPING"; + default: + return Integer.toString(state); + } } - public static String getPropertyCompat(String name, String oldName, - String defaultValue) { - String res = null; - - if (defaultValue != null) { - res = getProperty(name, defaultValue); - if (res.equals(defaultValue)) { - res = getProperty(oldName, defaultValue); - if (!res.equals(defaultValue)) - warnDeprecated(name, oldName); + /** + * @return ==0: versions are identical, <0: tested version is newer, >0: + * currentVersion is newer. + */ + public static int compareVersions(String currentVersion, + String testedVersion) { + List cToks = new ArrayList(); + StringTokenizer cSt = new StringTokenizer(currentVersion, "."); + while (cSt.hasMoreTokens()) + cToks.add(cSt.nextToken()); + List tToks = new ArrayList(); + StringTokenizer tSt = new StringTokenizer(currentVersion, "."); + while (tSt.hasMoreTokens()) + tToks.add(tSt.nextToken()); + + int comp = 0; + comp: for (int i = 0; i < cToks.size(); i++) { + if (tToks.size() <= i) { + // equals until then, tested shorter + comp = 1; + break comp; } - } else { - res = getProperty(name, null); - if (res == null) { - res = getProperty(oldName, null); - if (res != null) - warnDeprecated(name, oldName); + + String c = (String) cToks.get(i); + String t = (String) tToks.get(i); + + try { + int cInt = Integer.parseInt(c); + int tInt = Integer.parseInt(t); + if (cInt == tInt) + continue comp; + else { + comp = (cInt - tInt); + break comp; + } + } catch (NumberFormatException e) { + if (c.equals(t)) + continue comp; + else { + comp = c.compareTo(t); + break comp; + } } } - return res; + + if (comp == 0 && tToks.size() > cToks.size()) { + // equals until then, current shorter + comp = -1; + } + + return comp; } - public static void warnDeprecated(String name, String oldName) { - warn("Property '" + oldName - + "' is deprecated and will be removed soon, use '" + name - + "' instead."); - } - }