|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package org.apache.hadoop.fs.s3a; |
| 20 | + |
| 21 | +import java.io.IOException; |
| 22 | +import java.lang.reflect.Constructor; |
| 23 | +import java.lang.reflect.InvocationTargetException; |
| 24 | + |
| 25 | +import org.apache.hadoop.conf.Configuration; |
| 26 | +import static org.assertj.core.api.Assertions.*; |
| 27 | +import org.junit.Test; |
| 28 | + |
| 29 | +public class ITestS3AFileSystemIsolatedClassloader extends AbstractS3ATestBase { |
| 30 | + |
| 31 | + public static class CustomClassLoader extends ClassLoader { |
| 32 | + } |
| 33 | + |
| 34 | + private final ClassLoader customClassLoader = new CustomClassLoader(); |
| 35 | + |
| 36 | + private <T> T createClass(ClassLoader clsLoader, Class<T> toLoad) { |
| 37 | + try { |
| 38 | + Class<?> cls = clsLoader.loadClass(toLoad.getCanonicalName()); |
| 39 | + Constructor<?> constructor = cls.getConstructor(); |
| 40 | + return (T) constructor.newInstance(); |
| 41 | + } catch(ClassNotFoundException | InstantiationException | IllegalAccessException | |
| 42 | + IllegalArgumentException | InvocationTargetException | NoSuchMethodException | |
| 43 | + SecurityException e) { |
| 44 | + throw new RuntimeException(e); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + public void isolatedClassloader() throws IOException { |
| 50 | + try (S3AFileSystem fs = getFileSystem()) { |
| 51 | + Configuration conf = createClass(customClassLoader, Configuration.class); |
| 52 | + assertEquals(fs.getConf().getClassLoader(), customClassLoader); |
| 53 | + conf.setBoolean(Constants.AWS_S3_CLASSLOADER_ISOLATION, true); |
| 54 | + assertNotEquals(fs.getClass().getClassLoader(), customClassLoader); |
| 55 | + fs.initialize(fs.getUri(), conf); |
| 56 | + assertEquals(fs.getConf().getClassLoader(), fs.getClass().getClassLoader()); |
| 57 | + assertNotEquals(fs.getConf().getClassLoader(), customClassLoader); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + public void isolatedClassloaderDefault() throws IOException { |
| 63 | + try (S3AFileSystem fs = getFileSystem()) { |
| 64 | + Configuration conf = createClass(customClassLoader, Configuration.class); |
| 65 | + assertEquals(fs.getConf().getClassLoader(), customClassLoader); |
| 66 | + assertNotEquals(fs.getClass().getClassLoader(), customClassLoader); |
| 67 | + fs.initialize(fs.getUri(), conf); |
| 68 | + assertEquals(fs.getConf().getClassLoader(), fs.getClass().getClassLoader()); |
| 69 | + assertNotEquals(fs.getConf().getClassLoader(), customClassLoader); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + public void notIsolatedClassloader() throws IOException { |
| 75 | + try (S3AFileSystem fs = getFileSystem()) { |
| 76 | + Configuration conf = createClass(customClassLoader, Configuration.class); |
| 77 | + assertEquals(fs.getConf().getClassLoader(), customClassLoader); |
| 78 | + conf.setBoolean(Constants.AWS_S3_CLASSLOADER_ISOLATION, false); |
| 79 | + assertNotEquals(fs.getClass().getClassLoader(), customClassLoader); |
| 80 | + fs.initialize(fs.getUri(), conf); |
| 81 | + assertNotEquals(fs.getConf().getClassLoader(), fs.getClass().getClassLoader()); |
| 82 | + assertEquals(fs.getConf().getClassLoader(), customClassLoader); |
| 83 | + } |
| 84 | + } |
| 85 | +} |
0 commit comments