1 package org.metricshub.ssh; 2 3 /*- 4 * ╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲ 5 * SSH Java Client 6 * ჻჻჻჻჻჻ 7 * Copyright 2023 Metricshub 8 * ჻჻჻჻჻჻ 9 * Licensed under the Apache License, Version 2.0 (the "License"); 10 * you may not use this file except in compliance with the License. 11 * You may obtain a copy of the License at 12 * 13 * http://www.apache.org/licenses/LICENSE-2.0 14 * 15 * Unless required by applicable law or agreed to in writing, software 16 * distributed under the License is distributed on an "AS IS" BASIS, 17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 * See the License for the specific language governing permissions and 19 * limitations under the License. 20 * ╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱ 21 */ 22 23 import java.nio.charset.Charset; 24 import java.nio.charset.IllegalCharsetNameException; 25 import java.nio.charset.StandardCharsets; 26 import java.nio.charset.UnsupportedCharsetException; 27 28 /** 29 * Utility Class (static), to be used anywhere in Matsya, including in 30 * standalone JARs (in CLI mode) 31 * 32 * @author Bertrand 33 */ 34 public class Utils { 35 36 private Utils() {} 37 38 /** 39 * Returns the proper Charset that can decode/encode the specified locale 40 * 41 * @param locale The locale we're dealing with, as formatted in the LANG environment variable (e.g. zh_CN.utf8) 42 * @param defaultCharset The default Charset to use if the specified locale doesn't match any supported Charset 43 * @return The appropriate Charset instance 44 */ 45 public static Charset getCharsetFromLocale(final String locale, final Charset defaultCharset) { 46 // What charset will we be dealing with coming from and going to the PATROL Agent 47 Charset charset = defaultCharset; 48 if (locale != null && !locale.isEmpty()) { 49 final String[] localeElements = locale.split("\\."); 50 if (localeElements.length > 0) { 51 String charsetName = localeElements[localeElements.length - 1]; 52 if (charsetName != null) { 53 charsetName = charsetName.trim(); 54 if (!charsetName.isEmpty() && !"c".equalsIgnoreCase(charsetName)) { 55 if ("gb".equalsIgnoreCase(charsetName)) { 56 charsetName = "GBK"; 57 } 58 try { 59 charset = Charset.forName(charsetName); 60 } catch (IllegalCharsetNameException | UnsupportedCharsetException e) { 61 /* Unfortunately, there is nothing we can do here, as debug is not even set yet */ 62 } 63 } 64 } 65 } 66 } 67 68 return charset; 69 } 70 71 /** 72 * Returns the proper Charset that can decode/encode the specified locale, or UTF-8 if specified locale 73 * cannot be converted to a Charset. 74 * 75 * @param locale The locale we're dealing with, as formatted in the LANG environment variable (e.g. zh_CN.utf8) 76 * @return The appropriate Charset instance 77 */ 78 public static Charset getCharsetFromLocale(final String locale) { 79 return getCharsetFromLocale(locale, StandardCharsets.UTF_8); 80 } 81 82 /** 83 * Check if the required field is not null. 84 * 85 * @param field 86 * @param name 87 * @throws IllegalStateException if the argument is null 88 */ 89 public static <T> void checkNonNullField(final T field, final String name) { 90 if (field == null) { 91 throw new IllegalStateException(name + " must not be null."); 92 } 93 } 94 95 /** 96 * Check if the required argument is not negative or zero. 97 * 98 * @param argument 99 * @param name 100 * @throws IllegalArgumentException if the argument is null 101 */ 102 public static void checkArgumentNotZeroOrNegative(final long argument, final String name) { 103 if (argument <= 0) { 104 throw new IllegalArgumentException(String.format("%s=%d must not be negative or zero.", name, argument)); 105 } 106 } 107 }