com.mysql.cj.util.Util Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mysql-connector-java Show documentation
Show all versions of mysql-connector-java Show documentation
JDBC Type 4 driver for MySQL
The newest version!
/*
* Copyright (c) 2002, 2020, Oracle and/or its affiliates.
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, version 2.0, as published by the
* Free Software Foundation.
*
* This program is also distributed with certain software (including but not
* limited to OpenSSL) that is licensed under separate terms, as designated in a
* particular file or component or in included license documentation. The
* authors of MySQL hereby grant you an additional permission to link the
* program and your derivative works with the separately licensed software that
* they have included with MySQL.
*
* Without limiting anything contained in the foregoing, this file, which is
* part of MySQL Connector/J, is also subject to the Universal FOSS Exception,
* version 1.0, a copy of which can be found at
* http://oss.oracle.com/licenses/universal-foss-exception.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
* for more details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package com.mysql.cj.util;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.Reader;
import java.io.StringWriter;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import com.mysql.cj.Constants;
import com.mysql.cj.Messages;
import com.mysql.cj.exceptions.CJException;
import com.mysql.cj.exceptions.ExceptionFactory;
import com.mysql.cj.exceptions.ExceptionInterceptor;
import com.mysql.cj.exceptions.WrongArgumentException;
/**
* Various utility methods for the driver.
*/
public class Util {
private static int jvmVersion = 8; // use default base version supported
private static int jvmUpdateNumber = -1;
static {
int startPos = Constants.JVM_VERSION.indexOf('.');
int endPos = startPos + 1;
if (startPos != -1) {
while (Character.isDigit(Constants.JVM_VERSION.charAt(endPos)) && ++endPos < Constants.JVM_VERSION.length()) {
// continue
}
}
startPos++;
if (endPos > startPos) {
jvmVersion = Integer.parseInt(Constants.JVM_VERSION.substring(startPos, endPos));
}
startPos = Constants.JVM_VERSION.indexOf("_");
endPos = startPos + 1;
if (startPos != -1) {
while (Character.isDigit(Constants.JVM_VERSION.charAt(endPos)) && ++endPos < Constants.JVM_VERSION.length()) {
// continue
}
}
startPos++;
if (endPos > startPos) {
jvmUpdateNumber = Integer.parseInt(Constants.JVM_VERSION.substring(startPos, endPos));
}
}
public static int getJVMVersion() {
return jvmVersion;
}
public static boolean jvmMeetsMinimum(int version, int updateNumber) {
return getJVMVersion() > version || getJVMVersion() == version && getJVMUpdateNumber() >= updateNumber;
}
public static int getJVMUpdateNumber() {
return jvmUpdateNumber;
}
/**
* Checks whether the given server version string is a MySQL Community edition
*
* @param serverVersion
* full server version string
* @return true if version does not contain "enterprise", "commercial" or "advanced"
*/
public static boolean isCommunityEdition(String serverVersion) {
return !isEnterpriseEdition(serverVersion);
}
/**
* Checks whether the given server version string is a MySQL Enterprise edition
*
* @param serverVersion
* full server version string
* @return true if version contains "enterprise", "commercial" or "advanced"
*/
public static boolean isEnterpriseEdition(String serverVersion) {
return serverVersion.contains("enterprise") || serverVersion.contains("commercial") || serverVersion.contains("advanced");
}
/**
* Converts a nested exception into a nicer message
*
* @param ex
* the exception to expand into a message.
*
* @return a message containing the exception, the message (if any), and a
* stacktrace.
*/
public static String stackTraceToString(Throwable ex) {
StringBuilder traceBuf = new StringBuilder();
traceBuf.append(Messages.getString("Util.1"));
if (ex != null) {
traceBuf.append(ex.getClass().getName());
String message = ex.getMessage();
if (message != null) {
traceBuf.append(Messages.getString("Util.2"));
traceBuf.append(message);
}
StringWriter out = new StringWriter();
PrintWriter printOut = new PrintWriter(out);
ex.printStackTrace(printOut);
traceBuf.append(Messages.getString("Util.3"));
traceBuf.append(out.toString());
}
traceBuf.append(Messages.getString("Util.4"));
return traceBuf.toString();
}
public static Object getInstance(String className, Class>[] argTypes, Object[] args, ExceptionInterceptor exceptionInterceptor, String errorMessage) {
try {
return handleNewInstance(Class.forName(className).getConstructor(argTypes), args, exceptionInterceptor);
} catch (SecurityException | NoSuchMethodException | ClassNotFoundException e) {
throw ExceptionFactory.createException(WrongArgumentException.class, errorMessage, e, exceptionInterceptor);
}
}
public static Object getInstance(String className, Class>[] argTypes, Object[] args, ExceptionInterceptor exceptionInterceptor) {
return getInstance(className, argTypes, args, exceptionInterceptor, "Can't instantiate required class");
}
/**
* Handles constructing new instance with the given constructor and wrapping
* (or not, as required) the exceptions that could possibly be generated
*
* @param ctor
* constructor
* @param args
* arguments for constructor
* @param exceptionInterceptor
* exception interceptor
* @return object
*/
public static Object handleNewInstance(Constructor> ctor, Object[] args, ExceptionInterceptor exceptionInterceptor) {
try {
return ctor.newInstance(args);
} catch (IllegalArgumentException | InstantiationException | IllegalAccessException e) {
throw ExceptionFactory.createException(WrongArgumentException.class, "Can't instantiate required class", e, exceptionInterceptor);
} catch (InvocationTargetException e) {
Throwable target = e.getTargetException();
if (target instanceof ExceptionInInitializerError) {
target = ((ExceptionInInitializerError) target).getException();
} else if (target instanceof CJException) {
throw (CJException) target;
}
throw ExceptionFactory.createException(WrongArgumentException.class, target.getMessage(), target, exceptionInterceptor);
}
}
/**
* Does a network interface exist locally with the given hostname?
*
* @param hostname
* the hostname (or IP address in string form) to check
* @return true if it exists, false if no, or unable to determine due to VM
* version support of java.net.NetworkInterface
*/
public static boolean interfaceExists(String hostname) {
try {
Class> networkInterfaceClass = Class.forName("java.net.NetworkInterface");
return networkInterfaceClass.getMethod("getByName", (Class[]) null).invoke(networkInterfaceClass, new Object[] { hostname }) != null;
} catch (Throwable t) {
return false;
}
}
public static Map
© 2015 - 2024 Weber Informatics LLC | Privacy Policy