You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@beam.apache.org by GitBox <gi...@apache.org> on 2020/05/14 21:30:48 UTC

[GitHub] [beam] ibzib commented on a change in pull request #11708: [BEAM-9577] Artifact v2 support for uber jars.

ibzib commented on a change in pull request #11708:
URL: https://github.com/apache/beam/pull/11708#discussion_r425439878



##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/io/ClassLoaderFileSystem.java
##########
@@ -0,0 +1,154 @@
+/*
+ * 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.beam.sdk.io;
+
+import static org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.Preconditions.checkArgument;
+
+import com.google.auto.service.AutoService;
+import java.io.IOException;
+import java.io.InputStream;
+import java.nio.channels.Channels;
+import java.nio.channels.ReadableByteChannel;
+import java.nio.channels.WritableByteChannel;
+import java.util.Collection;
+import java.util.List;
+import javax.annotation.Nullable;
+import org.apache.beam.sdk.annotations.Experimental;
+import org.apache.beam.sdk.io.fs.CreateOptions;
+import org.apache.beam.sdk.io.fs.MatchResult;
+import org.apache.beam.sdk.io.fs.ResolveOptions;
+import org.apache.beam.sdk.io.fs.ResourceId;
+import org.apache.beam.sdk.options.PipelineOptions;
+import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableList;
+
+/** A read-only {@link FileSystem} implementation looking up resources using a ClassLoader. */
+public class ClassLoaderFileSystem extends FileSystem<ClassLoaderFileSystem.ClassLoaderResourceId> {
+
+  public static final String SCHEMA = "classpath";
+  private static final String PREFIX = SCHEMA + "://";
+
+  ClassLoaderFileSystem() {}
+
+  @Override
+  protected List<MatchResult> match(List<String> specs) throws IOException {
+    throw new UnsupportedOperationException("Un-globable filesystem.");
+  }
+
+  @Override
+  protected WritableByteChannel create(
+      ClassLoaderResourceId resourceId, CreateOptions createOptions) throws IOException {
+    throw new UnsupportedOperationException("Read-only filesystem.");
+  }
+
+  @Override
+  protected ReadableByteChannel open(ClassLoaderResourceId resourceId) throws IOException {
+    ClassLoader classLoader = getClass().getClassLoader();
+    InputStream inputStream =
+        classLoader.getResourceAsStream(resourceId.path.substring(PREFIX.length()));
+    if (inputStream == null) {
+      throw new IOException("Unable to load " + resourceId.path + " with " + classLoader);
+    }
+    return Channels.newChannel(inputStream);
+  }
+
+  @Override
+  protected void copy(
+      List<ClassLoaderResourceId> srcResourceIds, List<ClassLoaderResourceId> destResourceIds)
+      throws IOException {
+    throw new UnsupportedOperationException("Read-only filesystem.");
+  }
+
+  @Override
+  protected void rename(
+      List<ClassLoaderResourceId> srcResourceIds, List<ClassLoaderResourceId> destResourceIds)
+      throws IOException {
+    throw new UnsupportedOperationException("Read-only filesystem.");
+  }
+
+  @Override
+  protected void delete(Collection<ClassLoaderResourceId> resourceIds) throws IOException {
+    throw new UnsupportedOperationException("Read-only filesystem.");
+  }
+
+  @Override
+  protected ClassLoaderResourceId matchNewResource(String path, boolean isDirectory) {
+    return new ClassLoaderResourceId(path);
+  }
+
+  @Override
+  protected String getScheme() {
+    return SCHEMA;
+  }
+
+  public static class ClassLoaderResourceId implements ResourceId {
+
+    private final String path;
+
+    private ClassLoaderResourceId(String path) {
+      checkArgument(path.startsWith(PREFIX), path);
+      this.path = path;
+    }
+
+    @Override
+    public ResourceId resolve(String other, ResolveOptions resolveOptions) {

Review comment:
       Can we add a couple trivial unit tests as sanity checks / documentation for `resolve` and `getCurrentDirectory`?

##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/io/ClassLoaderFileSystem.java
##########
@@ -0,0 +1,154 @@
+/*
+ * 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.beam.sdk.io;
+
+import static org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.Preconditions.checkArgument;
+
+import com.google.auto.service.AutoService;
+import java.io.IOException;
+import java.io.InputStream;
+import java.nio.channels.Channels;
+import java.nio.channels.ReadableByteChannel;
+import java.nio.channels.WritableByteChannel;
+import java.util.Collection;
+import java.util.List;
+import javax.annotation.Nullable;
+import org.apache.beam.sdk.annotations.Experimental;
+import org.apache.beam.sdk.io.fs.CreateOptions;
+import org.apache.beam.sdk.io.fs.MatchResult;
+import org.apache.beam.sdk.io.fs.ResolveOptions;
+import org.apache.beam.sdk.io.fs.ResourceId;
+import org.apache.beam.sdk.options.PipelineOptions;
+import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableList;
+
+/** A read-only {@link FileSystem} implementation looking up resources using a ClassLoader. */

Review comment:
       Nice! But I don't see this actually used anywhere?

##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/io/ClassLoaderFileSystem.java
##########
@@ -0,0 +1,154 @@
+/*
+ * 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.beam.sdk.io;
+
+import static org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.Preconditions.checkArgument;
+
+import com.google.auto.service.AutoService;
+import java.io.IOException;
+import java.io.InputStream;
+import java.nio.channels.Channels;
+import java.nio.channels.ReadableByteChannel;
+import java.nio.channels.WritableByteChannel;
+import java.util.Collection;
+import java.util.List;
+import javax.annotation.Nullable;
+import org.apache.beam.sdk.annotations.Experimental;
+import org.apache.beam.sdk.io.fs.CreateOptions;
+import org.apache.beam.sdk.io.fs.MatchResult;
+import org.apache.beam.sdk.io.fs.ResolveOptions;
+import org.apache.beam.sdk.io.fs.ResourceId;
+import org.apache.beam.sdk.options.PipelineOptions;
+import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableList;
+
+/** A read-only {@link FileSystem} implementation looking up resources using a ClassLoader. */
+public class ClassLoaderFileSystem extends FileSystem<ClassLoaderFileSystem.ClassLoaderResourceId> {
+
+  public static final String SCHEMA = "classpath";
+  private static final String PREFIX = SCHEMA + "://";
+
+  ClassLoaderFileSystem() {}
+
+  @Override
+  protected List<MatchResult> match(List<String> specs) throws IOException {
+    throw new UnsupportedOperationException("Un-globable filesystem.");

Review comment:
       ```suggestion
       throw new UnsupportedOperationException("Un-globbable filesystem.");
   ```

##########
File path: sdks/java/core/src/test/java/org/apache/beam/sdk/io/ClassLoaderFileSystemTest.java
##########
@@ -0,0 +1,57 @@
+/*
+ * 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.beam.sdk.io;
+
+import static java.nio.channels.Channels.newInputStream;
+import static org.junit.Assert.assertArrayEquals;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.nio.channels.ReadableByteChannel;
+import org.apache.beam.sdk.options.PipelineOptionsFactory;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+@RunWith(JUnit4.class)
+public class ClassLoaderFileSystemTest {
+
+  private static final String SOME_CLASS =
+      "classpath://org/apache/beam/sdk/io/ClassLoaderFilesystem.class";
+
+  @Test
+  public void testOpen() throws IOException {
+    ClassLoaderFileSystem filesystem = new ClassLoaderFileSystem();
+    ReadableByteChannel channel = filesystem.open(filesystem.matchNewResource(SOME_CLASS, false));
+    checkIsClass(channel);
+  }
+
+  @Test
+  public void testRegistrar() throws IOException {
+    ReadableByteChannel channel = FileSystems.open(FileSystems.matchNewResource(SOME_CLASS, false));
+    checkIsClass(channel);
+  }
+
+  public void checkIsClass(ReadableByteChannel channel) throws IOException {
+    FileSystems.setDefaultPipelineOptions(PipelineOptionsFactory.create());
+    InputStream inputStream = newInputStream(channel);
+    byte[] magic = new byte[4];
+    inputStream.read(magic);
+    assertArrayEquals(magic, new byte[] {(byte) 0xCA, (byte) 0xFE, (byte) 0xBA, (byte) 0xBE});

Review comment:
       How did I never know this? 😆 




----------------------------------------------------------------
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:
users@infra.apache.org