ctubbsii commented on code in PR #48:
URL:
https://github.com/apache/accumulo-classloaders/pull/48#discussion_r2699961399
##########
modules/local-caching-classloader/src/main/java/org/apache/accumulo/classloader/lcc/LocalCachingContextClassLoaderFactory.java:
##########
@@ -90,6 +92,14 @@
*/
public class LocalCachingContextClassLoaderFactory implements
ContextClassLoaderFactory {
+ // Configure the URL class to know about the "hdfs://" protocol. This can
only be
+ // called once in the JVM, so it has to be in a static initializer vs the
init
+ // method. We are not using org.apache.hadoop.fs.FsUrlStreamHandlerFactory
+ // because it overrides the handling of the "file" protocol.
+ static {
+ URL.setURLStreamHandlerFactory(new HdfsUrlStreamHandlerFactory(new
Configuration()));
Review Comment:
Apparently, in Java 9+, there's a better way to do this, that avoids the
problem of needing to call this only once in the JVM. Instead of setting the
factory, you can register a
[URLStreamHandlerProvider](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/net/spi/URLStreamHandlerProvider.html)
to handle the hdfs URLs. We can use `@AutoService` to generate the appropriate
`META-INF/services` file.
##########
modules/local-caching-classloader/src/main/java/org/apache/accumulo/classloader/lcc/util/HdfsUrlStreamHandlerFactory.java:
##########
@@ -0,0 +1,115 @@
+/*
+ * 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
+ *
+ * https://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.accumulo.classloader.lcc.util;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.net.URL;
+import java.net.URLConnection;
+import java.net.URLStreamHandler;
+import java.net.URLStreamHandlerFactory;
+import java.util.Objects;
+
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.fs.FileSystem;
+import org.apache.hadoop.fs.Path;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.base.Preconditions;
+
+public class HdfsUrlStreamHandlerFactory implements URLStreamHandlerFactory {
+
+ private static final Logger LOG =
LoggerFactory.getLogger(HdfsUrlStreamHandlerFactory.class);
+
+ private class HdfsUrlConnection extends URLConnection {
+
+ private final Configuration conf;
+
+ private InputStream is;
+
+ public HdfsUrlConnection(Configuration conf, URL url) {
+ super(url);
+ Objects.requireNonNull(conf, "null conf argument");
+ Objects.requireNonNull(url, "null url argument");
+ this.conf = conf;
+ }
+
+ @Override
+ public void connect() throws IOException {
+ Preconditions.checkState(is == null, "Already connected");
+ try {
+ LOG.debug("Connecting to {}", url);
+ URI uri = url.toURI();
+ FileSystem fs = FileSystem.get(uri, conf);
+ // URI#getPath returns null value if path contains relative path
+ // i.e file:root/dir1/file1
+ // So path can not be constructed from URI.
+ // We can only use schema specific part in URI.
+ // Uri#isOpaque return true if path is relative.
Review Comment:
This comment seems to be addressing the fact that a URI could contain a
relative path. However, that is not possible if it came from a file: URL,
because URLs, by definition, are absolute paths. While it is possible to have a
URI that represents a relative file: path, it is not possible for a URL to do
that, and this URI came from a URL, so the relative Path situation is not
possible here.
--
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.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]