|
| 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, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.apache.polaris.service.quarkus.config; |
| 20 | + |
| 21 | +import io.quarkus.arc.InstanceHandle; |
| 22 | +import java.util.List; |
| 23 | +import javax.sql.DataSource; |
| 24 | +import org.apache.polaris.extension.persistence.relational.jdbc.DatasourceSupplier; |
| 25 | +import org.apache.polaris.extension.persistence.relational.jdbc.RelationalJdbcConfiguration; |
| 26 | + |
| 27 | +public class QuarkusDatasourceSupplier implements DatasourceSupplier { |
| 28 | + private final List<InstanceHandle<DataSource>> dataSources; |
| 29 | + private final RelationalJdbcConfiguration relationalJdbcConfiguration; |
| 30 | + |
| 31 | + public static final String DEFAULT_DATA_SOURCE_NAME = "<default>"; |
| 32 | + |
| 33 | + public QuarkusDatasourceSupplier( |
| 34 | + RelationalJdbcConfiguration relationalJdbcConfiguration, |
| 35 | + List<InstanceHandle<DataSource>> dataSources) { |
| 36 | + this.relationalJdbcConfiguration = relationalJdbcConfiguration; |
| 37 | + this.dataSources = dataSources; |
| 38 | + } |
| 39 | + |
| 40 | + @Override |
| 41 | + public DataSource fromRealmId(String realmId) { |
| 42 | + String dataSourceName = |
| 43 | + relationalJdbcConfiguration.datasource().getOrDefault(realmId, DEFAULT_DATA_SOURCE_NAME); |
| 44 | + for (InstanceHandle<DataSource> handle : dataSources) { |
| 45 | + String name = handle.getBean().getName(); |
| 46 | + name = name == null ? DEFAULT_DATA_SOURCE_NAME : name; |
| 47 | + // if realm isolation is DB then there should be only one DS configured. |
| 48 | + if (name.equals(dataSourceName)) { |
| 49 | + return handle.get(); |
| 50 | + } |
| 51 | + } |
| 52 | + throw new IllegalStateException("No datasource configured with name: " + realmId); |
| 53 | + } |
| 54 | +} |
0 commit comments