blob: c14409aa81bed0fae09fef459f27dbaa6594949f [file] [log] [blame]
Simon Hunt0c85f112017-06-12 21:02:17 -07001/*
2 * Copyright 2017-present Open Networking Laboratory
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 */
17
18package org.onosproject.ui.lion;
19
Simon Huntb8042032017-06-13 15:03:23 -070020import org.slf4j.Logger;
21import org.slf4j.LoggerFactory;
22
23import java.util.Locale;
Simon Hunt0c85f112017-06-12 21:02:17 -070024import java.util.ResourceBundle;
Simon Huntb8042032017-06-13 15:03:23 -070025import java.util.Set;
Simon Hunt0c85f112017-06-12 21:02:17 -070026
27/**
28 * Utility methods for dealing with Localization Bundles etc.
29 */
30public final class LionUtils {
31
Simon Huntb8042032017-06-13 15:03:23 -070032 private static final Logger log = LoggerFactory.getLogger(LionUtils.class);
33
34 private static final String USER_LANGUAGE = "user.language";
35 private static final String USER_COUNTRY = "user.country";
36 private static final String ONOS_LOCALE = "ONOS_LOCALE";
37 private static final String EMPTY = "";
Simon Hunt0c85f112017-06-12 21:02:17 -070038 private static final String DOT = ".";
Simon Huntb8042032017-06-13 15:03:23 -070039 private static final String LOBAR = "_";
Simon Hunt0c85f112017-06-12 21:02:17 -070040
41 // no instantiation
42 private LionUtils() {
43 }
44
45 /**
Simon Huntb8042032017-06-13 15:03:23 -070046 * Parses the given string into language and country codes, and returns
47 * a {@link Locale} instance initialized with those parameters.
48 * For example:
49 * <pre>
50 * Locale locale = LionUtils.localeFromString("en_GB");
51 * locale.getLanguage(); // "en"
52 * locale.getCountry(); // "GB"
53 *
54 * locale = LionUtils.localeFromString("ru");
55 * locale.getLanguage(); // "ru"
56 * locale.getCountry(); // ""
57 * </pre>
58 *
59 * @param s the locale string
60 * @return a locale instance
61 */
62 public static Locale localeFromString(String s) {
63
64 if (!s.contains(LOBAR)) {
65 return new Locale(s);
66 }
67 String[] items = s.split(LOBAR);
68 return new Locale(items[0], items[1]);
69 }
70
71 /**
72 * Sets the default locale, based on the Java properties shown below.
73 * <pre>
74 * user.language
75 * user.country
76 * </pre>
77 * It is expected that the host system will have set these properties
78 * appropriately. Note, however, that the default values can be
79 * overridden by use of the environment variable {@code ONOS_LOCALE}.
80 * <p>
81 * For example, to set the Locale to French-Canadian one can invoke
82 * (from the shell)...
83 * <pre>
84 * $ ONOS_LOCALE=fr_CA {command-to-invoke-onos} ...
85 * </pre>
86 *
87 * @return the runtime locale
88 */
89 public static Locale setupRuntimeLocale() {
90 Locale systemDefault = Locale.getDefault();
91 log.info("System Default Locale: [{}]", systemDefault);
92 // TODO: Review- do we need to store the system default anywhere?
93
94 // Useful to log the "user.*" properties for debugging...
95 Set<String> pn = System.getProperties().stringPropertyNames();
96 pn.removeIf(f -> !(f.startsWith("user.")));
97 for (String ukey : pn) {
98 log.debug(" {}: {}", ukey, System.getProperty(ukey));
99 }
100
101 String language = System.getProperty(USER_LANGUAGE);
102 String country = System.getProperty(USER_COUNTRY);
103 log.info("Language: [{}], Country: [{}]", language, country);
104 Locale runtime = new Locale(language != null ? language : EMPTY,
105 country != null ? country : EMPTY);
106
107 String override = System.getenv(ONOS_LOCALE);
108 if (override != null) {
109 log.warn("Override with ONOS_LOCALE: [{}]", override);
110 runtime = localeFromString(override);
111 }
112
113 log.info("Setting runtime locale to: [{}]", runtime);
114 Locale.setDefault(runtime);
115 return runtime;
116 }
117
118 /**
Simon Hunt0c85f112017-06-12 21:02:17 -0700119 * This method takes a fully qualified name and returns a
120 * {@link ResourceBundle} which is loaded from a properties file with
121 * that base name.
122 * <p>
123 * For example, supposing the jar file contains:
Simon Hunt0c85f112017-06-12 21:02:17 -0700124 * <pre>
125 * org/onosproject/util/example/SomeBundle.properties
126 * </pre>
127 * <p>
128 * Then, to correctly load the resource bundle associated with
129 * <code>SomeBundle</code>, call:
130 * <pre>
131 * String fqname = "org.onosproject.util.example.SomeBundle";
132 * ResourceBundle res = ResourceUtils.getBundledResource(fqname);
133 * </pre>
134 * <p>
135 * Note that no error is thrown if the properties file does not exist.
136 * This condition will not become apparent until you try and access
137 * a property from the bundle, at which time a
138 * {@link java.util.MissingResourceException} will be thrown.
139 *
140 * @param basename the (fully qualified) basename of the bundle properties
141 * file
142 * @return the associated resource bundle
143 */
144 public static ResourceBundle getBundledResource(String basename) {
145 return ResourceBundle.getBundle(basename);
146 }
147
148 /**
149 * This method takes a class and returns a {@link ResourceBundle} which is
150 * loaded from a properties file with the same base name as the class.
151 * Note that both the class and the properties file(s) need to be in
152 * the same jar file.
153 * <p>
154 * For example, supposing the jar file contains:
Simon Hunt0c85f112017-06-12 21:02:17 -0700155 * <pre>
156 * org/onosproject/util/example/SomeObject.class
157 * org/onosproject/util/example/SomeObject.properties
158 * </pre>
159 * <p>
160 * Then, to correctly load the resource bundle associated with
161 * <code>SomeObject</code>, call:
162 * <pre>
163 * ResourceBundle res = ResourceUtils.getBundledResource(SomeObject.class);
164 * </pre>
165 * <p>
166 * Note that no error is thrown if the properties file does not exist.
167 * This condition will not become apparent until you try and access
168 * a property from the bundle, at which time a
169 * {@link java.util.MissingResourceException} will be thrown.
170 *
171 * @param c the class
172 * @return the associated resource bundle
173 */
174 public static ResourceBundle getBundledResource(Class<?> c) {
175 return ResourceBundle.getBundle(c.getName());
176 }
177
178 /**
179 * This method returns a {@link ResourceBundle} which is loaded from
180 * a properties file with the specified base name from the same package
181 * as the specified class.
182 * Note that both the class and the properties file(s) need to be in
183 * the same jar file.
184 * <p>
185 * For example, supposing the jar file contains:
Simon Hunt0c85f112017-06-12 21:02:17 -0700186 * <pre>
187 * org/onosproject/util/example/SomeObject.class
188 * org/onosproject/util/example/DisplayStrings.properties
189 * </pre>
190 * <p>
191 * Then, to correctly load the resource bundle call:
192 * <pre>
193 * ResourceBundle res = ResourceUtils.getBundledResource(SomeObject.class,
194 * "DisplayStrings");
195 * </pre>
196 * <p>
197 * Note that no error is thrown if the properties file does not exist.
198 * This condition will not become apparent until you try and access
199 * a property from the bundle, at which time a
200 * {@link java.util.MissingResourceException} will be thrown.
201 *
202 * @param c the class requesting the bundle
203 * @param baseName the base name of the resource bundle
204 * @return the associated resource bundle
205 */
206 public static ResourceBundle getBundledResource(Class<?> c, String baseName) {
207 String className = c.getName();
208 StringBuilder sb = new StringBuilder();
209 int dot = className.lastIndexOf(DOT);
210 sb.append(className.substring(0, dot));
211 sb.append(DOT).append(baseName);
212 return ResourceBundle.getBundle(sb.toString());
213 }
214}