/*
 * 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.NodeManager;
import io.prestosql.spi.VersionEmbedder;
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 JdbcModuleProvider moduleProvider;

    public JdbcConnectorFactory(String name, Module module)
    {
        this(name, JdbcModuleProvider.of(module));
    }

    public JdbcConnectorFactory(String name, JdbcModuleProvider moduleProvider)
    {
        checkArgument(!isNullOrEmpty(name), "name is null or empty");
        this.name = name;
        this.moduleProvider = requireNonNull(moduleProvider, "moduleProvider 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()),
                binder -> binder.bind(NodeManager.class).toInstance(context.getNodeManager()),
                binder -> binder.bind(VersionEmbedder.class).toInstance(context.getVersionEmbedder()),
                new JdbcModule(catalogName),
                new CredentialProviderModule(),
                moduleProvider.getModule(catalogName));

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

        return injector.getInstance(JdbcConnector.class);
    }

    public interface JdbcModuleProvider
    {
        Module getModule(String catalogName);

        static JdbcModuleProvider of(Module module)
        {
            requireNonNull(module, "module is null");
            return catalogName -> module;
        }
    }
}

@@ -1244,16 +1244,90 @@
 rEmpty;%0A
+import static io.airlift.configuration.ConfigurationAwareModules.combine;%0A
 import s
@@ -1628,18 +1628,38 @@
 rovider.
-of
+withCredentialProvider
 (module)
@@ -2722,56 +2722,8 @@
 e),%0A
-                new CredentialProviderModule(),%0A
     
@@ -3173,18 +3173,38 @@
 rovider 
-of
+withCredentialProvider
 (Module 
@@ -3308,23 +3308,64 @@
 gName -%3E
+ combine(new CredentialProviderModule(),
  module
+)
 ;%0A      

Allow creating a JDBC connector without CredentialProviderModule