All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.iceberg.ksyun.KsyunClientFactories Maven / Gradle / Ivy

The newest version!
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 org.apache.iceberg.ksyun;

import com.ksyun.ks3.http.HttpClientConfig;
import com.ksyun.ks3.service.Ks3;
import com.ksyun.ks3.service.Ks3Client;
import com.ksyun.ks3.service.Ks3ClientConfig;
import org.apache.iceberg.common.DynConstructors;
import org.apache.iceberg.util.PropertyUtil;

import java.util.Map;

public class KsyunClientFactories {

  private KsyunClientFactories() {
  }

  public static KsyunClientFactory from(Map properties) {
    String factoryImpl = PropertyUtil.propertyAsString(
        properties, KsyunProperties.CLIENT_FACTORY, DefaultKsyunClientFactory.class.getName());
    return loadClientFactory(factoryImpl, properties);
  }

  private static KsyunClientFactory loadClientFactory(String impl, Map properties) {
    DynConstructors.Ctor ctor;
    try {
      ctor = DynConstructors.builder(KsyunClientFactory.class).hiddenImpl(impl).buildChecked();
    } catch (NoSuchMethodException e) {
      throw new IllegalArgumentException(String.format(
          "Cannot initialize KsyunClientFactory, missing no-arg constructor: %s", impl), e);
    }

    KsyunClientFactory factory;
    try {
      factory = ctor.newInstance();
    } catch (ClassCastException e) {
      throw new IllegalArgumentException(
          String.format("Cannot initialize KsyunClientFactory, %s does not implement KsyunClientFactory.", impl), e);
    }

    factory.initialize(properties);
    return factory;
  }

  static class DefaultKsyunClientFactory implements KsyunClientFactory {
    private KsyunProperties ksyunProperties;

    DefaultKsyunClientFactory() {
    }

    @Override
    public Ks3 ks3Client() {
      Ks3ClientConfig ks3config = new Ks3ClientConfig();
      HttpClientConfig hconfig = new HttpClientConfig();
      hconfig.setMaxConnections(ksyunProperties.ks3ConnectionMax());
      hconfig.setConnectionTTL(ksyunProperties.ks3ConnectionTTL());
      hconfig.setConnectionTimeOut(ksyunProperties.ks3ConnectionTimeout());
      hconfig.setMaxPerRoute(ksyunProperties.ks3ConnectionMaxPerRoute());

      ks3config.setHttpClientConfig(hconfig);
      ks3config.setEndpoint(ksyunProperties.ks3Endpoint());
      ks3config.setProtocol(Ks3ClientConfig.PROTOCOL.http);
      ks3config.setPathStyleAccess(true);
      return new Ks3Client(ksyunProperties.ks3AccessKeyId(), ksyunProperties.ks3SecretAccessKey(), ks3config);
    }

    @Override
    public void initialize(Map properties) {
      this.ksyunProperties = new KsyunProperties(properties);
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy