Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * 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 com.facebook.presto.connector;
 
 
 
 import java.util.Map;
 
 import static com.google.common.base.Preconditions.checkArgument;
 import static com.google.common.base.Preconditions.checkNotNull;
 import static com.google.common.base.Preconditions.checkState;
 
 public class ConnectorManager
 {
     public static final String INFORMATION_SCHEMA_CONNECTOR_PREFIX = "$info_schema@";
 
     private final MetadataManager metadataManager;
     private final SplitManager splitManager;
     private final PageSourceManager pageSourceManager;
     private final IndexManager indexManager;
 
     private final PageSinkManager pageSinkManager;
     private final HandleResolver handleResolver;
     private final NodeManager nodeManager;
 
     private final ConcurrentMap<StringConnectorFactoryconnectorFactories = new ConcurrentHashMap<>();
 
     private final ConcurrentMap<StringConnectorconnectors = new ConcurrentHashMap<>();
 
     @Inject
     public ConnectorManager(MetadataManager metadataManager,
             SplitManager splitManager,
             PageSourceManager pageSourceManager,
             IndexManager indexManager,
             PageSinkManager pageSinkManager,
             HandleResolver handleResolver,
             Map<StringConnectorFactoryconnectorFactories,
             NodeManager nodeManager)
     {
         this. = metadataManager;
         this. = splitManager;
         this. = pageSourceManager;
         this. = indexManager;
         this. = pageSinkManager;
         this. = handleResolver;
         this. = nodeManager;
         this..putAll(connectorFactories);
     }
 
     public void addConnectorFactory(ConnectorFactory connectorFactory)
     {
         ConnectorFactory existingConnectorFactory = .putIfAbsent(connectorFactory.getName(), connectorFactory);
         checkArgument(existingConnectorFactory == null"Connector %s is already registered"connectorFactory.getName());
     }
 
     public synchronized void createConnection(String catalogNameString connectorNameMap<StringStringproperties)
     {
         checkNotNull(catalogName"catalogName is null");
         checkNotNull(connectorName"connectorName is null");
         checkNotNull(properties"properties is null");
 
         ConnectorFactory connectorFactory = .get(connectorName);
        checkArgument(connectorFactory != null"No factory for connector %s"connectorName);
        try (ThreadContextClassLoader ignored = new ThreadContextClassLoader(connectorFactory.getClass().getClassLoader())) {
            createConnection(catalogNameconnectorFactoryproperties);
        }
    }
    public synchronized void createConnection(String catalogNameConnectorFactory connectorFactoryMap<StringStringproperties)
    {
        checkNotNull(catalogName"catalogName is null");
        checkNotNull(properties"properties is null");
        checkNotNull(connectorFactory"connectorFactory is null");
        // for now connectorId == catalogName
        String connectorId = catalogName;
        checkState(!.containsKey(connectorId), "A connector %s already exists"connectorId);
        Connector connector = connectorFactory.create(connectorIdproperties);
        .put(connectorIdconnector);
        addConnector(catalogNameconnectorIdconnector);
    }
    private void addConnector(String catalogNameString connectorIdConnector connector)
    {
        ConnectorMetadata connectorMetadata = connector.getMetadata();
        checkState(connectorMetadata != null"Connector %s can not provide metadata"connectorId);
        ConnectorSplitManager connectorSplitManager = connector.getSplitManager();
        checkState(connectorSplitManager != null"Connector %s does not have a split manager"connectorId);
        ConnectorPageSourceProvider connectorPageSourceProvider = null;
        try {
            connectorPageSourceProvider = connector.getPageSourceProvider();
            checkNotNull(connectorPageSourceProvider"Connector %s returned a null page source provider"connectorId);
        }
        catch (UnsupportedOperationException ignored) {
        }
        if (connectorPageSourceProvider == null) {
            ConnectorRecordSetProvider connectorRecordSetProvider = null;
            try {
                connectorRecordSetProvider = connector.getRecordSetProvider();
                checkNotNull(connectorRecordSetProvider"Connector %s returned a null record set provider"connectorId);
            }
            catch (UnsupportedOperationException ignored) {
            }
            checkState(connectorRecordSetProvider != null"Connector %s has neither a PageSource or RecordSet provider"connectorId);
            connectorPageSourceProvider = new RecordPageSourceProvider(connectorRecordSetProvider);
        }
        ConnectorHandleResolver connectorHandleResolver = connector.getHandleResolver();
        checkNotNull(connectorHandleResolver"Connector %s does not have a handle resolver"connectorId);
        ConnectorPageSinkProvider connectorPageSinkProvider = null;
        try {
            connectorPageSinkProvider = connector.getPageSinkProvider();
            checkNotNull(connectorPageSinkProvider"Connector %s returned a null page sink provider"connectorId);
        }
        catch (UnsupportedOperationException ignored) {
        }
        if (connectorPageSinkProvider == null) {
            ConnectorRecordSinkProvider connectorRecordSinkProvider = null;
            try {
                connectorRecordSinkProvider = connector.getRecordSinkProvider();
                checkNotNull(connectorRecordSinkProvider"Connector %s returned a null record sink provider"connectorId);
                connectorPageSinkProvider = new RecordPageSinkProvider(connectorRecordSinkProvider);
            }
            catch (UnsupportedOperationException ignored) {
            }
        }
        ConnectorIndexResolver indexResolver = null;
        try {
            indexResolver = connector.getIndexResolver();
            checkNotNull(indexResolver"Connector %s returned a null index resolver"connectorId);
        }
        catch (UnsupportedOperationException ignored) {
        }
        // IMPORTANT: all the instances need to be fetched from the connector *before* we add them to the corresponding managers.
        // Otherwise, a broken connector would leave the managers in an inconsistent state with respect to each other
        .addConnectorMetadata(connectorIdcatalogNameconnectorMetadata);
        .addConnectorSplitManager(connectorIdconnectorSplitManager);
        .addHandleResolver(connectorIdconnectorHandleResolver);
        .addConnectorPageSourceProvider(connectorIdconnectorPageSourceProvider);
        if (connectorPageSinkProvider != null) {
            .addConnectorPageSinkProvider(connectorIdconnectorPageSinkProvider);
        }
        if (indexResolver != null) {
            .addIndexResolver(connectorIdindexResolver);
        }
    }
    private static String makeInformationSchemaConnectorId(String connectorId)
    {
        return  + connectorId;
    }
New to GrepCode? Check out our FAQ X