From ceba39c03b300d779c35413c65528b87fca90253 Mon Sep 17 00:00:00 2001 From: Georgios Andrianakis Date: Mon, 25 May 2020 16:12:26 +0300 Subject: [PATCH 1/2] Add support for Charset in Quarkus Config system --- .../configuration/CharsetConverter.java | 28 +++++++++++++ ....eclipse.microprofile.config.spi.Converter | 1 + .../CharsetConverterTestCase.java | 39 +++++++++++++++++++ 3 files changed, 68 insertions(+) create mode 100644 core/runtime/src/main/java/io/quarkus/runtime/configuration/CharsetConverter.java create mode 100644 core/runtime/src/test/java/io/quarkus/runtime/configuration/CharsetConverterTestCase.java diff --git a/core/runtime/src/main/java/io/quarkus/runtime/configuration/CharsetConverter.java b/core/runtime/src/main/java/io/quarkus/runtime/configuration/CharsetConverter.java new file mode 100644 index 0000000000000..33e03cd274bd8 --- /dev/null +++ b/core/runtime/src/main/java/io/quarkus/runtime/configuration/CharsetConverter.java @@ -0,0 +1,28 @@ +package io.quarkus.runtime.configuration; + +import static io.quarkus.runtime.configuration.ConverterSupport.DEFAULT_QUARKUS_CONVERTER_PRIORITY; + +import java.io.Serializable; +import java.nio.charset.Charset; + +import javax.annotation.Priority; + +import org.eclipse.microprofile.config.spi.Converter; + +/** + * A converter which converts a Charset string into an instance of {@link java.nio.charset.Charset}. + */ +@Priority(DEFAULT_QUARKUS_CONVERTER_PRIORITY) +public class CharsetConverter implements Converter, Serializable { + + private static final long serialVersionUID = 2320905063828247874L; + + @Override + public Charset convert(String value) { + try { + return Charset.forName(value); + } catch (Exception e) { + throw new IllegalArgumentException("Unable to create Charset from: '" + value + "'", e); + } + } +} diff --git a/core/runtime/src/main/resources/META-INF/services/org.eclipse.microprofile.config.spi.Converter b/core/runtime/src/main/resources/META-INF/services/org.eclipse.microprofile.config.spi.Converter index 1e06ce609f6d6..9c6a04c4ca0b8 100644 --- a/core/runtime/src/main/resources/META-INF/services/org.eclipse.microprofile.config.spi.Converter +++ b/core/runtime/src/main/resources/META-INF/services/org.eclipse.microprofile.config.spi.Converter @@ -1,4 +1,5 @@ io.quarkus.runtime.configuration.InetSocketAddressConverter +io.quarkus.runtime.configuration.CharsetConverter io.quarkus.runtime.configuration.CidrAddressConverter io.quarkus.runtime.configuration.InetAddressConverter io.quarkus.runtime.configuration.RegexConverter diff --git a/core/runtime/src/test/java/io/quarkus/runtime/configuration/CharsetConverterTestCase.java b/core/runtime/src/test/java/io/quarkus/runtime/configuration/CharsetConverterTestCase.java new file mode 100644 index 0000000000000..b93e0a4c9219c --- /dev/null +++ b/core/runtime/src/test/java/io/quarkus/runtime/configuration/CharsetConverterTestCase.java @@ -0,0 +1,39 @@ +package io.quarkus.runtime.configuration; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import java.nio.charset.StandardCharsets; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class CharsetConverterTestCase { + + private CharsetConverter charsetConverter; + + @BeforeEach + public void setup() { + charsetConverter = new CharsetConverter(); + } + + @Test + public void testUTF8Uppercase() { + assertEquals(charsetConverter.convert("UTF-8"), StandardCharsets.UTF_8); + } + + @Test + public void testUTF8Lowercase() { + assertEquals(charsetConverter.convert("utf-8"), StandardCharsets.UTF_8); + } + + @Test + public void testOther() { + assertEquals(charsetConverter.convert("US-ASCII"), StandardCharsets.US_ASCII); + } + + @Test + public void testInvalidCharset() { + assertThrows(IllegalArgumentException.class, () -> charsetConverter.convert("whatever")); + } +} From 94256b9c08f7b25d4d80f4b10147877382e3c498 Mon Sep 17 00:00:00 2001 From: Georgios Andrianakis Date: Mon, 25 May 2020 16:13:36 +0300 Subject: [PATCH 2/2] Use Charset in HibernateOrmConfig and default to UTF-8 --- .../hibernate/orm/deployment/HibernateOrmConfig.java | 11 ++++++++--- .../orm/deployment/HibernateOrmProcessor.java | 5 +++-- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/extensions/hibernate-orm/deployment/src/main/java/io/quarkus/hibernate/orm/deployment/HibernateOrmConfig.java b/extensions/hibernate-orm/deployment/src/main/java/io/quarkus/hibernate/orm/deployment/HibernateOrmConfig.java index d3e3106ee4d7e..9f55bc64cf7ef 100644 --- a/extensions/hibernate-orm/deployment/src/main/java/io/quarkus/hibernate/orm/deployment/HibernateOrmConfig.java +++ b/extensions/hibernate-orm/deployment/src/main/java/io/quarkus/hibernate/orm/deployment/HibernateOrmConfig.java @@ -1,5 +1,6 @@ package io.quarkus.hibernate.orm.deployment; +import java.nio.charset.Charset; import java.time.Duration; import java.util.Map; import java.util.Optional; @@ -219,6 +220,8 @@ public boolean isAnyPropertySet() { @ConfigGroup public static class HibernateOrmConfigDatabase { + private static final String DEFAULT_CHARSET = "UTF-8"; + /** * Select whether the database schema is generated or not. * @@ -249,9 +252,11 @@ public static class HibernateOrmConfigDatabase { /** * The charset of the database. + *

+ * Used for DDL generation and also for the SQL import scripts. */ - @ConfigItem - public Optional charset; + @ConfigItem(defaultValue = "UTF-8") + public Charset charset; /** * Whether Hibernate should quote all identifiers. @@ -262,7 +267,7 @@ public static class HibernateOrmConfigDatabase { public boolean isAnyPropertySet() { return !"none".equals(generation) || defaultCatalog.isPresent() || defaultSchema.isPresent() || generationHaltOnError - || charset.isPresent() + || !DEFAULT_CHARSET.equals(charset.name()) || globallyQuotedIdentifiers; } } diff --git a/extensions/hibernate-orm/deployment/src/main/java/io/quarkus/hibernate/orm/deployment/HibernateOrmProcessor.java b/extensions/hibernate-orm/deployment/src/main/java/io/quarkus/hibernate/orm/deployment/HibernateOrmProcessor.java index 86c17a6138f86..50eb46eca5eb5 100644 --- a/extensions/hibernate-orm/deployment/src/main/java/io/quarkus/hibernate/orm/deployment/HibernateOrmProcessor.java +++ b/extensions/hibernate-orm/deployment/src/main/java/io/quarkus/hibernate/orm/deployment/HibernateOrmProcessor.java @@ -784,8 +784,9 @@ private void handleHibernateORMWithNoPersistenceXml( desc.getProperties().setProperty(AvailableSettings.HBM2DDL_HALT_ON_ERROR, "true"); } - hibernateConfig.database.charset.ifPresent( - charset -> desc.getProperties().setProperty(AvailableSettings.HBM2DDL_CHARSET_NAME, charset)); + //charset + desc.getProperties().setProperty(AvailableSettings.HBM2DDL_CHARSET_NAME, + hibernateConfig.database.charset.name()); hibernateConfig.database.defaultCatalog.ifPresent( catalog -> desc.getProperties().setProperty(AvailableSettings.DEFAULT_CATALOG, catalog));