|
| 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.session; |
| 15 | + |
| 16 | +import com.facebook.presto.spi.session.SessionConfigurationContext; |
| 17 | +import com.facebook.presto.spi.session.SessionPropertyConfigurationManager; |
| 18 | +import com.fasterxml.jackson.databind.JsonMappingException; |
| 19 | +import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException; |
| 20 | +import com.google.common.collect.ImmutableMap; |
| 21 | +import io.airlift.json.JsonCodec; |
| 22 | +import io.airlift.json.JsonCodecFactory; |
| 23 | +import io.airlift.json.ObjectMapperProvider; |
| 24 | + |
| 25 | +import javax.inject.Inject; |
| 26 | + |
| 27 | +import java.io.IOException; |
| 28 | +import java.io.UncheckedIOException; |
| 29 | +import java.nio.file.Files; |
| 30 | +import java.nio.file.Path; |
| 31 | +import java.util.HashMap; |
| 32 | +import java.util.List; |
| 33 | +import java.util.Map; |
| 34 | + |
| 35 | +import static com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES; |
| 36 | +import static java.lang.String.format; |
| 37 | +import static java.util.Objects.requireNonNull; |
| 38 | + |
| 39 | +public class FileSessionPropertyManager |
| 40 | + implements SessionPropertyConfigurationManager |
| 41 | +{ |
| 42 | + public static final JsonCodec<List<SessionMatchSpec>> CODEC = new JsonCodecFactory( |
| 43 | + () -> new ObjectMapperProvider().get().enable(FAIL_ON_UNKNOWN_PROPERTIES)) |
| 44 | + .listJsonCodec(SessionMatchSpec.class); |
| 45 | + |
| 46 | + private final List<SessionMatchSpec> sessionMatchSpecs; |
| 47 | + |
| 48 | + @Inject |
| 49 | + public FileSessionPropertyManager(FileSessionPropertyManagerConfig config) |
| 50 | + { |
| 51 | + requireNonNull(config, "config is null"); |
| 52 | + |
| 53 | + Path configurationFile = config.getConfigFile().toPath(); |
| 54 | + try { |
| 55 | + sessionMatchSpecs = CODEC.fromJson(Files.readAllBytes(configurationFile)); |
| 56 | + } |
| 57 | + catch (IOException e) { |
| 58 | + throw new UncheckedIOException(e); |
| 59 | + } |
| 60 | + catch (IllegalArgumentException e) { |
| 61 | + Throwable cause = e.getCause(); |
| 62 | + if (cause instanceof UnrecognizedPropertyException) { |
| 63 | + UnrecognizedPropertyException ex = (UnrecognizedPropertyException) cause; |
| 64 | + String message = format("Unknown property at line %s:%s: %s", |
| 65 | + ex.getLocation().getLineNr(), |
| 66 | + ex.getLocation().getColumnNr(), |
| 67 | + ex.getPropertyName()); |
| 68 | + throw new IllegalArgumentException(message, e); |
| 69 | + } |
| 70 | + if (cause instanceof JsonMappingException) { |
| 71 | + // remove the extra "through reference chain" message |
| 72 | + if (cause.getCause() != null) { |
| 73 | + cause = cause.getCause(); |
| 74 | + } |
| 75 | + throw new IllegalArgumentException(cause.getMessage(), e); |
| 76 | + } |
| 77 | + throw e; |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public Map<String, String> getSystemSessionProperties(SessionConfigurationContext context) |
| 83 | + { |
| 84 | + // later properties override earlier properties |
| 85 | + Map<String, String> combinedProperties = new HashMap<>(); |
| 86 | + for (SessionMatchSpec sessionMatchSpec : sessionMatchSpecs) { |
| 87 | + combinedProperties.putAll(sessionMatchSpec.match(context)); |
| 88 | + } |
| 89 | + |
| 90 | + return ImmutableMap.copyOf(combinedProperties); |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + public Map<String, Map<String, String>> getCatalogSessionProperties(SessionConfigurationContext context) |
| 95 | + { |
| 96 | + return ImmutableMap.of(); |
| 97 | + } |
| 98 | +} |
0 commit comments