[jOOQ/jOOQ#8914] Consistently use MiniJAXB

This commit is contained in:
Knut Wannheden 2019-07-09 14:34:17 +02:00
parent 44611c9548
commit 9020e4eb42
4 changed files with 4 additions and 145 deletions

View File

@ -50,7 +50,6 @@ import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.StringReader;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.SQLException;
@ -60,17 +59,10 @@ import java.util.List;
import java.util.Properties;
import javax.sql.DataSource;
import javax.xml.XMLConstants;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.ValidationEvent;
import javax.xml.bind.ValidationEventHandler;
import javax.xml.validation.SchemaFactory;
import org.jooq.Constants;
import org.jooq.Log.Level;
import org.jooq.conf.MiniJAXB;
import org.jooq.exception.ExceptionTools;
import org.jooq.meta.CatalogVersionProvider;
import org.jooq.meta.Database;
import org.jooq.meta.Databases;
@ -91,7 +83,6 @@ import org.jooq.meta.jaxb.Target;
import org.jooq.tools.JooqLogger;
import org.jooq.tools.StringUtils;
import org.jooq.tools.jdbc.JDBCUtils;
import org.jooq.util.xml.jaxb.InformationSchema;
/**
@ -978,41 +969,6 @@ public class GenerationTool {
"<(\\w+:)?configuration xmlns(:\\w+)?=\"http://www.jooq.org/xsd/jooq-codegen-\\d+\\.\\d+\\.\\d+.xsd\">",
"<$1configuration xmlns$2=\"" + Constants.NS_CODEGEN + "\">");
xml = xml.replace(
"<configuration>",
"<configuration xmlns=\"" + Constants.NS_CODEGEN + "\">");
try {
SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
javax.xml.validation.Schema schema = sf.newSchema(
GenerationTool.class.getResource("/xsd/" + Constants.XSD_CODEGEN)
);
JAXBContext ctx = JAXBContext.newInstance(Configuration.class);
Unmarshaller unmarshaller = ctx.createUnmarshaller();
unmarshaller.setSchema(schema);
unmarshaller.setEventHandler(new ValidationEventHandler() {
@Override
public boolean handleEvent(ValidationEvent event) {
log.warn("Unmarshal warning", event.getMessage());
return true;
}
});
// [#7579] [#8044] Workaround for obscure JAXB bug on JDK 9+
xml = MiniJAXB.jaxbNamespaceBugWorkaround(xml, new InformationSchema());
return (Configuration) unmarshaller.unmarshal(new StringReader(xml));
}
catch (Throwable t) {
// [#7734] If JAXB cannot be loaded, try using our own
if (ExceptionTools.getCause(t, ClassNotFoundException.class) != null ||
ExceptionTools.getCause(t, Error.class) != null) {
return MiniJAXB.unmarshal(xml, Configuration.class);
}
throw new GeneratorException("Error while reading XML configuration", t);
}
return MiniJAXB.unmarshal(xml, Configuration.class);
}
}

View File

@ -64,7 +64,6 @@ import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
import javax.xml.bind.JAXB;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
@ -80,7 +79,6 @@ import org.jooq.Name;
import org.jooq.SQLDialect;
import org.jooq.SortOrder;
import org.jooq.conf.MiniJAXB;
import org.jooq.exception.ExceptionTools;
import org.jooq.impl.DSL;
import org.jooq.meta.AbstractDatabase;
import org.jooq.meta.AbstractIndexDefinition;
@ -217,22 +215,7 @@ public class XMLDatabase extends AbstractDatabase {
"<information_schema>",
"<information_schema xmlns=\"" + Constants.NS_META + "\">");
// [#7579] [#8044] Workaround for obscure JAXB bug on JDK 9+
content = MiniJAXB.jaxbNamespaceBugWorkaround(content, new InformationSchema());
try {
info = MiniJAXB.append(info, JAXB.unmarshal(new StringReader(content), InformationSchema.class));
}
catch (Throwable t) {
if (ExceptionTools.getCause(t, ClassNotFoundException.class) != null ||
ExceptionTools.getCause(t, Error.class) != null) {
info = MiniJAXB.append(info, MiniJAXB.unmarshal(content, InformationSchema.class));
}
else
// required due to Java 6
ExceptionTools.sneakyThrow(t);
}
info = MiniJAXB.append(info, MiniJAXB.unmarshal(content, InformationSchema.class));
}
private byte[] bytes(InputStream in) throws IOException {

View File

@ -53,7 +53,6 @@ import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.xml.bind.JAXB;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlEnum;
@ -66,9 +65,7 @@ import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.jooq.exception.ConfigurationException;
import org.jooq.exception.ExceptionTools;
import org.jooq.tools.Convert;
import org.jooq.tools.JooqLogger;
import org.jooq.tools.reflect.Reflect;
import org.jooq.tools.reflect.ReflectException;
@ -91,9 +88,6 @@ import org.xml.sax.InputSource;
*/
public class MiniJAXB {
private static final JooqLogger log = JooqLogger.getLogger(MiniJAXB.class);
private static volatile Boolean jaxbAvailable;
public static String marshal(Object object) {
StringWriter writer = new StringWriter();
marshal(object, writer);
@ -105,18 +99,6 @@ public class MiniJAXB {
}
public static void marshal(Object object, Writer out) {
if (!Boolean.FALSE.equals(jaxbAvailable)) {
try {
JAXB.marshal(object, out);
jaxbAvailable = true;
log.debug("JAXB is available from the classpath / module path");
return;
}
catch (Throwable t) {
handleJaxbException(t);
}
}
try {
XmlRootElement e = object.getClass().getAnnotation(XmlRootElement.class);
if (e != null)
@ -132,24 +114,6 @@ public class MiniJAXB {
}
}
private static void handleJaxbException(Throwable t) {
if (t instanceof Error) {
jaxbAvailable = true;
log.debug("JAXB is available from the classpath / module path");
ExceptionTools.sneakyThrow(t);
}
else if (ExceptionTools.getCause(t, ClassNotFoundException.class) != null ||
ExceptionTools.getCause(t, Error.class) != null) {
jaxbAvailable = false;
log.debug("JAXB is not available from the classpath / module path");
}
else {
jaxbAvailable = true;
log.debug("JAXB is available from the classpath / module path");
throw new ConfigurationException("Error while reading xml", t);
}
}
public static <T> T unmarshal(InputStream in, Class<T> type) {
return unmarshal0(new InputSource(in), type);
}
@ -168,22 +132,6 @@ public class MiniJAXB {
}
private static <T> T unmarshal0(InputSource in, Class<T> type) {
if (!Boolean.FALSE.equals(jaxbAvailable)) {
try {
T result = in.getByteStream() != null
? JAXB.unmarshal(in.getByteStream(), type)
: in.getCharacterStream() != null
? JAXB.unmarshal(in.getCharacterStream(), type)
: null;
jaxbAvailable = true;
log.debug("JAXB is available from the classpath / module path");
return result;
}
catch (Throwable t) {
handleJaxbException(t);
}
}
try {
Document document = builder().parse(in);
T result = Reflect.on(type).create().get();
@ -325,32 +273,6 @@ public class MiniJAXB {
}
}
/**
* This method is used internally by jOOQ to patch XML content in order to
* work around a bug in JAXB.
* <p>
* [#7579] [#8044] On JDK 9, 10, depending on how JAXB is loaded onto the
* classpath / module path, the xmlns seems to be considered for
* (un)marshalling, or not. This seems to be a bug in JAXB, with no known
* tracking ID as of yet.
* <p>
* The following quick fix tests the presence of the xmlns when marshalling,
* and if absent removes it prior to unmarshalling.
*/
public static String jaxbNamespaceBugWorkaround(String xml, Object annotated) {
StringWriter test = new StringWriter();
try {
JAXB.marshal(annotated, test);
if (!test.toString().contains("xmlns"))
xml = xml.replaceAll("xmlns=\"[^\"]*\"", "");
}
catch (Exception ignore) {}
return xml;
}
/**
* Appends a <code>second</code> JAXB annotated object to a
* <code>first</code> one using Maven's

View File

@ -40,8 +40,6 @@ package org.jooq.conf;
import java.io.ByteArrayOutputStream;
import java.io.Serializable;
import javax.xml.bind.JAXB;
/**
* This base class is extended by all XJC-generated {@link Settings} classes
* <p>
@ -49,8 +47,8 @@ import javax.xml.bind.JAXB;
* of those many JAXB / XJC plugins. Besides, cloning objects through the
* standard Java {@link Cloneable} mechanism is around factor 1000x faster than
* using {@link Serializable}, and even 10000x faster than using
* {@link JAXB#marshal(Object, java.io.OutputStream)}, marshalling a JAXB object
* into a {@link ByteArrayOutputStream}.
* {@link javax.xml.bind.JAXB#marshal(Object, java.io.OutputStream)},
* marshalling a JAXB object into a {@link ByteArrayOutputStream}.
*
* @author Lukas Eder
*/