Git Product home page Git Product logo

Comments (7)

dsyer avatar dsyer commented on March 29, 2024

Sounds fine to me (except that obviously UTF-8 is not going to work for everyone, so we should only assert that if it isn't explicitly set). Any idea where to insert those checks? Pull requests always welcome.

from spring-boot.

dsyer avatar dsyer commented on March 29, 2024

Updated research: 'sun.jnu.encoding' is (obviously) platform specific, but it seems to be dependent (at least on *nix machines) on the LANG environment variable, so maybe it makes sense to check that variable as well or instead?

from spring-boot.

mikaelhg avatar mikaelhg commented on March 29, 2024

Both of these system properties get their default values, if not specifically set, from the environmental variables LANG and LC_* on Linux and Solaris. I suspect that the *BSD ports have followed a similar convention, but haven't checked it.

http://hg.openjdk.java.net/jdk7u/jdk7u/jdk/file/tip/src/solaris/native/java/lang/java_props_md.c

365 java_props_t *
366 GetJavaProperties(JNIEnv *env)

http://hg.openjdk.java.net/jdk7u/jdk7u/jdk/file/tip/src/share/native/java/lang/System.c

236     PUTPROP(props, "file.encoding", sprops->encoding);
237     PUTPROP(props, "sun.jnu.encoding", sprops->sun_jnu_encoding);   

from spring-boot.

mikaelhg avatar mikaelhg commented on March 29, 2024

On application level, I've currently implemented this like so:

@Bean
public CommandLineRunner requireInternationalEncodingOrCrash(final ConfigurableApplicationContext context) {
    return new CommandLineRunner() {
        @Override public void run(final String... args) throws Exception {
            final String encoding = System.getProperty("file.encoding");
            if (encoding != null && !"UTF-8".equals(encoding.toUpperCase())) {
                log.error("Your system property \"file.encoding\" is currently \"{}\". It should be \"UTF-8\". ", encoding);
                log.error("Your environmental variable LANG is currently \"{}\". It should end \".UTF-8\". ", System.getenv("LANG"));
                log.error("Your environmental variable LC_ALL is currently \"{}\". It should end \".UTF-8\". ", System.getenv("LC_ALL"));
                log.error("We can't run this application properly if the Java Virtual Machine it runs in hasn't been configured to use the UTF-8 encoding. Closing down.");
                context.close();
            }
        }
    };
}

With a little more complexity for various LC_* combinations.

I assume a properly explicit and contained solution would be to create a "sanity check" stage where we check whether the application can be run in org.springframework.boot.SpringApplication::run?

from spring-boot.

dsyer avatar dsyer commented on March 29, 2024

That's a good start, but yes, I think a better solution might be to use an ApplicationContextInitializer (one that runs after the ConfigFile* one so that it can be parameterized externally).

from spring-boot.

mikaelhg avatar mikaelhg commented on March 29, 2024
private final static ApplicationContextInitializer<ConfigurableApplicationContext> REQUIRE_UTF8 =
        new ApplicationContextInitializer<ConfigurableApplicationContext>() {
    private final static String REFUSE_TO_START =
            "We can't run this application properly, since the Java Virtual Machine it runs in hasn't " +
                    " been configured to use the UTF-8 default character encoding. Closing down.";
    @Override public void initialize(final ConfigurableApplicationContext ctx) {
        final String encoding = System.getProperty("file.encoding");
        if (encoding != null && !"UTF-8".equals(encoding.toUpperCase())) {
            log.error("Your system property \"file.encoding\" is currently \"{}\". It should be \"UTF-8\". ", encoding);
            log.error("Your environmental variable LANG is currently \"{}\". You must use a UTF-8 locale setting.",
                    System.getenv("LANG"));
            log.error("Your environmental variable LC_ALL is currently \"{}\". You must use a UTF-8 locale setting.",
                    System.getenv("LC_ALL"));
            log.error(REFUSE_TO_START);
            throw new IllegalStateException(REFUSE_TO_START);
        }
    }
};

public static void main(final String ... args) {
    new SpringApplicationBuilder(ExampleController.class).initializers(REQUIRE_UTF8).run(args);
}

from spring-boot.

mikaelhg avatar mikaelhg commented on March 29, 2024

Thanks :)

from spring-boot.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.