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-j Show documentation
Show all versions of mysql-connector-j Show documentation
JDBC Type 4 driver for MySQL.
The newest version!
/*
* Copyright (c) 2002, 2024, 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 designed to work with certain software 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 either included with the program or referenced in the documentation.
*
* 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 java.util.stream.Collectors;
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/Commercial 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();
}
/**
* Creates an instance of the specified class name through reflection using the given arguments, as long as the class implements the return type specified.
*
* @param
* The return type of the new instance.
* @param returnType
* the class of the return type of the new instance, usually an interface.
* @param className
* the name of the class to instantiate through reflection.
* @param argTypes
* the type of the arguments of the constructor to be used for instantiating the class.
* @param args
* the arguments to supply when exectuing the new instance constructor.
* @param exceptionInterceptor
* the {@link ExceptionInterceptor} to handle new exceptions thrown.
* @return
* if all validations succeed, an instance of the class with the specified name.
*/
@SuppressWarnings("unchecked")
public static T getInstance(Class returnType, String className, Class>[] argTypes, Object[] args, ExceptionInterceptor exceptionInterceptor) {
/*
* TODO: consider overloading this method with a version that uses Super Type Tokens instead of Class in order to support parameterized types.
*/
try {
Class> clazz = Class.forName(className, false, Util.class.getClassLoader());
if (!returnType.isAssignableFrom(clazz)) {
throw ExceptionFactory.createException(WrongArgumentException.class,
Messages.getString("Util.WrongImplementation", new Object[] { className, returnType.getName() }), exceptionInterceptor);
}
return handleNewInstance(((Class) clazz).getConstructor(argTypes), args, exceptionInterceptor);
} catch (ClassNotFoundException | NoSuchMethodException | SecurityException e) {
throw ExceptionFactory.createException(WrongArgumentException.class, Messages.getString("Util.FailLoadClass", new Object[] { className }), e,
exceptionInterceptor);
}
}
/**
* Handles constructing new instance with the given constructor and wrapping (or not, as required) the exceptions that could possibly be generated.
*
* @param
* The type of the new class instance to return.
*
* @param ctor
* constructor
* @param args
* arguments for constructor
* @param exceptionInterceptor
* exception interceptor
* @return object
*/
public static T handleNewInstance(Constructor ctor, Object[] args, ExceptionInterceptor exceptionInterceptor) {
try {
return ctor.newInstance(args);
} catch (IllegalArgumentException | InstantiationException | IllegalAccessException e) {
throw ExceptionFactory.createException(WrongArgumentException.class,
Messages.getString("Util.FailCreateInstance", new Object[] { ctor.getDeclaringClass().getName() }), e, exceptionInterceptor);
} catch (InvocationTargetException e) {
Throwable target = e.getCause();
if (target instanceof ExceptionInInitializerError) {
target = target.getCause();
} else if (target instanceof CJException) {
throw (CJException) target;
}
throw ExceptionFactory.createException(WrongArgumentException.class, target.getMessage(), target, exceptionInterceptor);
}
}
public static Map
© 2015 - 2024 Weber Informatics LLC | Privacy Policy