jvz commented on a change in pull request #335: Import of LogstashLayout as JsonTemplateLayout URL: https://github.com/apache/logging-log4j2/pull/335#discussion_r373564119
########## File path: log4j-layout-jackson-json-template/src/main/java/org/apache/logging/log4j/jackson/json/template/layout/util/Uris.java ########## @@ -0,0 +1,101 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache license, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the license for the specific language governing permissions and + * limitations under the license. + */ +package org.apache.logging.log4j.jackson.json.template.layout.util; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.net.URI; +import java.net.URL; +import java.nio.charset.Charset; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; + +public enum Uris {; + + public static String readUri(final String spec, final Charset charset) { + try { + return unsafeReadUri(spec, charset); + } catch (final Exception error) { + final String message = String.format( + "failed reading URI (spec=%s, charset=%s)", + spec, charset); + throw new RuntimeException(message, error); + } + } + + private static String unsafeReadUri( + final String spec, + final Charset charset) + throws Exception { + final URI uri = new URI(spec); + final String uriScheme = uri.getScheme().toLowerCase(); + switch (uriScheme) { + case "classpath": + return readClassPathUri(uri, charset); + case "file": + return readFileUri(uri, charset); + default: { + final String message = String.format("unknown URI scheme (spec=%s)", spec); + throw new IllegalArgumentException(message); + } + } + + } + + private static String readFileUri( + final URI uri, + final Charset charset) + throws IOException { + final Path path = Paths.get(uri); + try (final BufferedReader fileReader = Files.newBufferedReader(path, charset)) { + return consumeReader(fileReader); + } + } + + private static String readClassPathUri( + final URI uri, + final Charset charset) + throws IOException { + final String spec = uri.toString(); + final String path = spec.substring("classpath:".length()); + final URL resource = Uris.class.getClassLoader().getResource(path); Review comment: Use whatever the appropriate ClassLoader is for the context. The Loader/Util classes are practically hacks at this point due to most Java applications being either flat classpaths or web applications with a well-known ClassLoader model. The more fun environments like Java module paths and OSGi are why these util classes exist, and we sometimes get that wrong. In the revamped system I've been working on, I've been doing everything I can to avoid the need to ever load a class by name like this, though your best bet currently would be to obtain the ClassLoader of a relevant parent class or known class in the jar to ensure you've selected the correct one. For loading files here, you'll want the ClassLoader of a Class from the jar where the resource is located as usual. If you're trying to load arbitrary files like `log4j2.xml`, that can likely be loaded from the TCCL as that should be set to the main application's loader which is where you typically expect to start from. If you're loading extensions from other jars dynamically, that's where it gets tricky and might require separate implementations for different module systems. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services