[jOOQ/jOOQ#11506] Log deprecation warning when Serialization of jOOQ API is triggered

This contains:

- [jOOQ/jOOQ#11507] Add Log.isWarnEnabled()
This commit is contained in:
Lukas Eder 2021-02-23 15:04:15 +01:00
parent 7cd606cd2f
commit 36ca56d188
4 changed files with 51 additions and 4 deletions

View File

@ -162,6 +162,11 @@ public interface Log {
*/
void info(Object message, Object details, Throwable throwable);
/**
* Check if <code>WARN</code> level logging is enabled.
*/
boolean isWarnEnabled();
/**
* Log a message in <code>WARN</code> level.
*

View File

@ -40,7 +40,11 @@ package org.jooq.impl;
import static org.jooq.impl.Tools.CTX;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.sql.SQLException;
import java.util.concurrent.atomic.AtomicInteger;
import org.jooq.Attachable;
import org.jooq.BindContext;
@ -55,6 +59,7 @@ import org.jooq.RenderContext;
import org.jooq.conf.SettingsTools;
import org.jooq.exception.DataAccessException;
import org.jooq.exception.SQLDialectNotSupportedException;
import org.jooq.tools.JooqLogger;
/**
* @author Lukas Eder
@ -240,4 +245,23 @@ abstract class AbstractQueryPart implements QueryPartInternal {
protected final DataAccessException translate(String sql, SQLException e) {
return Tools.translate(sql, e);
}
private static class SerializationDeprecation {}
private static final JooqLogger log = JooqLogger.getLogger(SerializationDeprecation.class);
private static final AtomicInteger warnCount = new AtomicInteger(0);
private static final int maxWarnings = 100;
private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException {
ois.defaultReadObject();
if (log.isWarnEnabled() && warnCount.getAndUpdate(i -> Math.min(i + 1, maxWarnings)) < maxWarnings)
log.warn("DEPRECATION", "A QueryPart of type " + getClass() + " has been deserialised. Serialization support is deprecated in jOOQ. Please contact https://github.com/jOOQ/jOOQ/issues/11506 and state your use-case to see if it can be implemented otherwise.");
}
private void writeObject(ObjectOutputStream oos) throws IOException {
oos.defaultWriteObject();
if (log.isWarnEnabled() && warnCount.getAndUpdate(i -> Math.min(i + 1, maxWarnings)) < maxWarnings)
log.warn("DEPRECATION", "A QueryPart of type " + getClass() + " has been serialised. Serialization support is deprecated in jOOQ. Please contact https://github.com/jOOQ/jOOQ/issues/11506 and state your use-case to see if it can be implemented otherwise.");
}
}

View File

@ -1591,11 +1591,9 @@ public class DefaultConfiguration implements Configuration {
private <E> E[] cloneSerializables(E[] array) {
E[] clone = array.clone();
for (int i = 0; i < clone.length; i++) {
if (!(clone[i] instanceof Serializable)) {
for (int i = 0; i < clone.length; i++)
if (!(clone[i] instanceof Serializable))
clone[i] = null;
}
}
return clone;
}

View File

@ -87,6 +87,11 @@ public final class JooqLogger implements Log {
*/
private boolean supportsInfo = true;
/**
* Whether calls to {@link #warn(Object)} are possible.
*/
private boolean supportsWarn = true;
/**
* Get a logger wrapper for a class.
*/
@ -344,6 +349,21 @@ public final class JooqLogger implements Log {
util.log(java.util.logging.Level.INFO, "" + getMessage(message, details), throwable);
}
/**
* Check if <code>INFO</code> level logging is enabled.
*/
@Override
public boolean isWarnEnabled() {
if (!globalThreshold.supports(Log.Level.WARN))
return false;
if (!supportsWarn)
return false;
else if (slf4j != null)
return slf4j.isWarnEnabled();
else
return util.isLoggable(java.util.logging.Level.WARNING);
}
/**
* Log a message in <code>WARN</code> level.
*