|
| 1 | +/* |
| 2 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | + * you may not use this file except in compliance with the License. |
| 4 | + * You may obtain a copy of the License at |
| 5 | + * |
| 6 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + * |
| 8 | + * Unless required by applicable law or agreed to in writing, software |
| 9 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | + * See the License for the specific language governing permissions and |
| 12 | + * limitations under the License. |
| 13 | + */ |
| 14 | +package com.facebook.presto.spi.connector.classloader; |
| 15 | + |
| 16 | +import com.facebook.presto.spi.ConnectorSession; |
| 17 | +import com.facebook.presto.spi.classloader.ThreadContextClassLoader; |
| 18 | +import com.facebook.presto.spi.connector.ConnectorTransactionHandle; |
| 19 | +import com.facebook.presto.spi.function.table.Argument; |
| 20 | +import com.facebook.presto.spi.function.table.ArgumentSpecification; |
| 21 | +import com.facebook.presto.spi.function.table.ConnectorTableFunction; |
| 22 | +import com.facebook.presto.spi.function.table.ReturnTypeSpecification; |
| 23 | +import com.facebook.presto.spi.function.table.TableFunctionAnalysis; |
| 24 | + |
| 25 | +import java.util.List; |
| 26 | +import java.util.Map; |
| 27 | + |
| 28 | +import static java.util.Objects.requireNonNull; |
| 29 | + |
| 30 | +public class ClassLoaderSafeConnectorTableFunction |
| 31 | + implements ConnectorTableFunction |
| 32 | +{ |
| 33 | + private final ConnectorTableFunction delegate; |
| 34 | + private final ClassLoader classLoader; |
| 35 | + |
| 36 | + public ClassLoaderSafeConnectorTableFunction(ConnectorTableFunction delegate, ClassLoader classLoader) |
| 37 | + { |
| 38 | + this.delegate = requireNonNull(delegate, "delegate is null"); |
| 39 | + this.classLoader = requireNonNull(classLoader, "classLoader is null"); |
| 40 | + } |
| 41 | + |
| 42 | + @Override |
| 43 | + public String getSchema() |
| 44 | + { |
| 45 | + try (ThreadContextClassLoader a = new ThreadContextClassLoader(classLoader)) { |
| 46 | + return delegate.getSchema(); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public String getName() |
| 52 | + { |
| 53 | + try (ThreadContextClassLoader a = new ThreadContextClassLoader(classLoader)) { |
| 54 | + return delegate.getName(); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public List<ArgumentSpecification> getArguments() |
| 60 | + { |
| 61 | + try (ThreadContextClassLoader a = new ThreadContextClassLoader(classLoader)) { |
| 62 | + return delegate.getArguments(); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public ReturnTypeSpecification getReturnTypeSpecification() |
| 68 | + { |
| 69 | + try (ThreadContextClassLoader a = new ThreadContextClassLoader(classLoader)) { |
| 70 | + return delegate.getReturnTypeSpecification(); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public TableFunctionAnalysis analyze(ConnectorSession session, |
| 76 | + ConnectorTransactionHandle transaction, |
| 77 | + Map<String, Argument> arguments) |
| 78 | + { |
| 79 | + try (ThreadContextClassLoader a = new ThreadContextClassLoader(classLoader)) { |
| 80 | + return delegate.analyze(session, transaction, arguments); |
| 81 | + } |
| 82 | + } |
| 83 | +} |
0 commit comments