/*
 * Licensed 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 io.prestosql.plugin.jdbc;

import com.google.inject.Injector;
import com.google.inject.Module;
import io.airlift.bootstrap.Bootstrap;
import io.prestosql.plugin.jdbc.credential.CredentialProviderModule;
import io.prestosql.spi.connector.Connector;
import io.prestosql.spi.connector.ConnectorContext;
import io.prestosql.spi.connector.ConnectorFactory;
import io.prestosql.spi.connector.ConnectorHandleResolver;
import io.prestosql.spi.type.TypeManager;

import java.util.Map;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Strings.isNullOrEmpty;
import static java.util.Objects.requireNonNull;

public class JdbcConnectorFactory
        implements ConnectorFactory
{
    private final String name;
    private final Module module;

    public JdbcConnectorFactory(String name, Module module)
    {
        checkArgument(!isNullOrEmpty(name), "name is null or empty");
        this.name = name;
        this.module = requireNonNull(module, "module is null");
    }

    @Override
    public String getName()
    {
        return name;
    }

    @Override
    public ConnectorHandleResolver getHandleResolver()
    {
        return new JdbcHandleResolver();
    }

    @Override
    public Connector create(String catalogName, Map<String, String> requiredConfig, ConnectorContext context)
    {
        requireNonNull(requiredConfig, "requiredConfig is null");

        Bootstrap app = new Bootstrap(
                binder -> binder.bind(TypeManager.class).toInstance(context.getTypeManager()),
                new JdbcModule(catalogName),
                new CredentialProviderModule(),
                module);

        Injector injector = app
                .strictConfig()
                .doNotInitializeLogging()
                .setRequiredConfigurationProperties(requiredConfig)
                .initialize();

        return injector.getInstance(JdbcConnector.class);
    }
}

@@ -1336,29 +1336,49 @@
 e final 
+Jdbc
 Module
- module
+Provider moduleProvider
 ;%0A%0A    p
@@ -1430,32 +1430,176 @@
 e module)%0A    %7B%0A
+        this(name, JdbcModuleProvider.of(module));%0A    %7D%0A%0A    public JdbcConnectorFactory(String name, JdbcModuleProvider moduleProvider)%0A    %7B%0A
         checkArg
@@ -1697,16 +1697,24 @@
 s.module
+Provider
  = requi
@@ -1729,16 +1729,24 @@
 l(module
+Provider
 , %22modul
@@ -1746,16 +1746,24 @@
  %22module
+Provider
  is null
@@ -2418,16 +2418,47 @@
   module
+Provider.getModule(catalogName)
 );%0A%0A    
@@ -2655,24 +2655,24 @@
 tialize();%0A%0A
-
         retu
@@ -2722,11 +2722,279 @@
 );%0A    %7D
+%0A%0A    public interface JdbcModuleProvider%0A    %7B%0A        Module getModule(String catalogName);%0A%0A        static JdbcModuleProvider of(Module module)%0A        %7B%0A            requireNonNull(module, %22module is null%22);%0A            return catalogName -%3E module;%0A        %7D%0A    %7D
 %0A%7D%0A

Allow JDBC connector modules to access catalog name