Search in sources :

Example 51 with LogService

use of org.osgi.service.log.LogService in project eclipse.platform.runtime by eclipse.

the class ResourceBundleHelper method toLocale.

/**
 * <p>
 * Converts a String to a Locale.
 * </p>
 *
 * <p>
 * This method takes the string format of a locale and creates the locale object from it.
 * </p>
 *
 * <pre>
 *   MessageFactoryServiceImpl.toLocale("en")         = new Locale("en", "")
 *   MessageFactoryServiceImpl.toLocale("en_GB")      = new Locale("en", "GB")
 *   MessageFactoryServiceImpl.toLocale("en_GB_xxx")  = new Locale("en", "GB", "xxx")
 * </pre>
 *
 * <p>
 * This method validates the input strictly. The language code must be lowercase. The country
 * code must be uppercase. The separator must be an underscore. The length must be correct.
 * </p>
 *
 * <p>
 * This method is inspired by <code>org.apache.commons.lang.LocaleUtils.toLocale(String)</code>
 * by fixing the parsing error for uncommon Locales like having a language and a variant code
 * but no country code, or a Locale that only consists of a country code.
 * </p>
 *
 * <p>
 * <b>Note:</b> This is the same logic as used in <code>EquinoxConfiguration.toLocale()</code>
 * </p>
 *
 * @param localeString
 *            the locale String to convert
 * @param defaultLocale
 *            the Locale that should be returned in case of an invalid Locale String
 * @return a Locale that matches the specified locale String. If the given input String is
 *         <code>null</code> or can not be parsed because of an invalid format, the given
 *         default {@link Locale} will be returned.
 */
public static Locale toLocale(String localeString, Locale defaultLocale) {
    LogService logService = logTracker.getService();
    if (localeString == null) {
        if (logService != null)
            logService.log(LogService.LOG_ERROR, "Given locale String is null" + // $NON-NLS-1$
            " - Default Locale will be used instead.");
        return defaultLocale;
    }
    // $NON-NLS-1$
    String language = "";
    // $NON-NLS-1$
    String country = "";
    // $NON-NLS-1$
    String variant = "";
    // $NON-NLS-1$
    String[] localeParts = localeString.split("_");
    if (localeParts.length == 0 || localeParts.length > 3 || (localeParts.length == 1 && localeParts[0].length() == 0)) {
        logInvalidFormat(localeString, logService);
        return defaultLocale;
    }
    if (localeParts[0].length() > 0 && !localeParts[0].matches("[a-zA-Z]{2,8}")) {
        logInvalidFormat(localeString, logService);
        return defaultLocale;
    }
    language = localeParts[0];
    if (localeParts.length > 1) {
        if (localeParts[1].length() > 0 && !localeParts[1].matches("[a-zA-Z]{2}|[0-9]{3}")) {
            if (language.length() > 0) {
                if (logService != null)
                    logService.log(LogService.LOG_ERROR, "Invalid locale format: " + localeString + // $NON-NLS-1$
                    " - Only language part will be used to create the Locale.");
                return new Locale(language);
            }
            logInvalidFormat(localeString, logService);
            return defaultLocale;
        }
        country = localeParts[1];
    }
    if (localeParts.length == 3) {
        if (localeParts[2].length() == 0) {
            if (logService != null)
                logService.log(LogService.LOG_ERROR, "Invalid locale format: " + localeString + // $NON-NLS-1$
                " - Only language and country part will be used to create the Locale.");
            return new Locale(language, country);
        }
        variant = localeParts[2];
    }
    return new Locale(language, country, variant);
}
Also used : Locale(java.util.Locale) LogService(org.osgi.service.log.LogService)

Example 52 with LogService

use of org.osgi.service.log.LogService in project ecf by eclipse.

the class RssPlugin method getLogService.

protected LogService getLogService() {
    if (logServiceTracker == null) {
        logServiceTracker = new ServiceTracker(this.context, LogService.class.getName(), null);
        logServiceTracker.open();
    }
    return (LogService) logServiceTracker.getService();
}
Also used : ServiceTracker(org.osgi.util.tracker.ServiceTracker) LogService(org.osgi.service.log.LogService)

Example 53 with LogService

use of org.osgi.service.log.LogService in project ecf by eclipse.

the class Activator method getLogService.

protected LogService getLogService() {
    synchronized (this) {
        if (this.context == null)
            return null;
        if (logServiceTracker == null) {
            logServiceTracker = new ServiceTracker(this.context, LogService.class.getName(), null);
            logServiceTracker.open();
        }
        return (LogService) logServiceTracker.getService();
    }
}
Also used : ServiceTracker(org.osgi.util.tracker.ServiceTracker) LogService(org.osgi.service.log.LogService)

Example 54 with LogService

use of org.osgi.service.log.LogService in project ecf by eclipse.

the class Activator method getLogService.

protected LogService getLogService() {
    if (logServiceTracker == null) {
        logServiceTracker = new ServiceTracker(this.context, LogService.class.getName(), null);
        logServiceTracker.open();
    }
    return (LogService) logServiceTracker.getService();
}
Also used : ServiceTracker(org.osgi.util.tracker.ServiceTracker) LogService(org.osgi.service.log.LogService)

Example 55 with LogService

use of org.osgi.service.log.LogService in project ecf by eclipse.

the class Activator method getLogService.

protected LogService getLogService() {
    if (logServiceTracker == null) {
        logServiceTracker = new ServiceTracker(this.context, LogService.class.getName(), null);
        logServiceTracker.open();
    }
    return (LogService) logServiceTracker.getService();
}
Also used : ServiceTracker(org.osgi.util.tracker.ServiceTracker) LogService(org.osgi.service.log.LogService)

Aggregations

LogService (org.osgi.service.log.LogService)57 ServiceTracker (org.osgi.util.tracker.ServiceTracker)15 File (java.io.File)10 ServiceReference (org.osgi.framework.ServiceReference)10 AbstractDeploymentPackage (org.apache.felix.deploymentadmin.AbstractDeploymentPackage)7 Bundle (org.osgi.framework.Bundle)7 HashMap (java.util.HashMap)6 PrintStream (java.io.PrintStream)5 StringWriter (java.io.StringWriter)5 Builder (aQute.bnd.osgi.Builder)4 Jar (aQute.bnd.osgi.Jar)4 Resource (aQute.bnd.osgi.Resource)4 XmlTester (aQute.bnd.test.XmlTester)4 Attributes (java.util.jar.Attributes)4 BundleInfoImpl (org.apache.felix.deploymentadmin.BundleInfoImpl)4 Before (org.junit.Before)4 BundleContext (org.osgi.framework.BundleContext)4 IOException (java.io.IOException)3 Date (java.util.Date)3 Matchers.anyString (org.mockito.Matchers.anyString)3