<commit_before>package com.gmail.chibitopoochan.soqlexec.api;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.ResourceBundle;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.gmail.chibitopoochan.soqlexec.model.FieldMetaInfo;
import com.gmail.chibitopoochan.soqlexec.model.SObjectMetaInfo;
import com.gmail.chibitopoochan.soqlexec.soap.MetaInformationProvider;
import com.gmail.chibitopoochan.soqlexec.soap.SOQLExecutor;
import com.gmail.chibitopoochan.soqlexec.soap.SalesforceConnectionFactory;
import com.gmail.chibitopoochan.soqlexec.util.Constants;
import com.gmail.chibitopoochan.soqlexec.util.Constants.Message.Error;
import com.gmail.chibitopoochan.soqlexec.util.Message;
import com.sforce.ws.ConnectionException;

public class Connector {
	// クラス共通の参照
	private static final Logger logger = LoggerFactory.getLogger(Connector.class);
	private static final ResourceBundle resources = ResourceBundle.getBundle(Constants.Message.RESOURCE);
	private static final ResourceBundle properties = ResourceBundle.getBundle(Constants.Properties.RESOURCE);

	// Proxy情報の保持
	private static String proxyHost;
	private static int proxyPort;
	private static String proxyId;
	private static String proxyPassword;
	private static boolean useProxy;

	// 接続状態の保持
	private Optional<SOQLExecutor.QueryMore> queryMore;
	private SalesforceConnectionFactory factory;
	private SOQLExecutor executor;
	private MetaInformationProvider provider;
	private boolean close = false;
	private int size;

	/**
	 * Salesforceへの接続
	 * @param factory ログイン後の接続情報
	 */
	private Connector(SalesforceConnectionFactory factory) {
		this.factory = factory;
		this.executor = new SOQLExecutor(factory.getPartnerConnection());
		this.provider = new MetaInformationProvider(factory.getPartnerConnection());
	}

	/**
	 * メタ情報提供クラスを指定
	 * @param provider メタ情報提供クラス
	 */
	public void setMetaInfoProvieder(MetaInformationProvider provider) {
		this.provider = provider;
		this.provider.setPartnerConnection(factory.getPartnerConnection());
	}

	/**
	 * SOQL実行クラスを指定
	 * @param executor SOQL実行クラス
	 */
	public void setSOQLExecutor(SOQLExecutor executor) {
		this.executor = executor;
		this.executor.setPartnerConnection(factory.getPartnerConnection());
	}

	/**
	 * Salesforceへのログイン
	 * @param username ユーザ名
	 * @param password <PASSWORD>
	 * @param env authEndPoint
	 * @return Salesforceへの接続
	 * @throws ConnectionException ログインエラー
	 */
	public static Connector login(String username, String password, String env) throws Exception {
		SalesforceConnectionFactory factory;

		if(Connector.useProxy) {
			factory = SalesforceConnectionFactory.newInstance(env, username, password
					,Connector.proxyHost, Connector.proxyPort, Connector.proxyId, Connector.proxyPassword);
		} else {
			factory = SalesforceConnectionFactory.newInstance(env, username, password);
		}

		if(!factory.login()) {
			throw new ConnectionException(Message.get(Error.ERR_001, username, password, env));
		}

		Connector.resetProxySetting();

		return new Connector(factory);

	}

	/**
	 * Proxy接続の設定
	 * @param proxyHost ホスト名
	 * @param proxyPort ポート番号
	 * @param proxyId ユーザID
	 * @param proxyPassword <PASSWORD>
	 */
	public static void setProxySetting(String proxyHost, int proxyPort, String proxyId, String proxyPassword) {
		Connector.useProxy = true;
		Connector.proxyHost = proxyHost;
		Connector.proxyPort = proxyPort;
		Connector.proxyId = proxyId;
		Connector.proxyPassword = proxyPassword;
		logger.info("set proxy setting");
		logger.info(String.format("useProxy=[%s]",useProxy));
		logger.info(String.format("useHost=[%s]",proxyHost));
		logger.info(String.format("usePort=[%s]",proxyPort));
		logger.info(String.format("useId=[%s]",proxyId));
		logger.info(String.format("usePassword=[%s]",proxyPassword));
	}

	/**
	 * Proxy接続の設定解除
	 */
	public static void resetProxySetting() {
		Connector.useProxy = true;
		Connector.proxyHost = null;
		Connector.proxyPort = 0;
		Connector.proxyId = null;
		Connector.proxyPassword = null;

	}

	/**
	 * Salesforceへのログイン
	 * @param username ユーザ名<commit_msg>切断時にプロキシが有効になる不具合の修正<commit_after>package com.gmail.chibitopoochan.soqlexec.api;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.ResourceBundle;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.gmail.chibitopoochan.soqlexec.model.FieldMetaInfo;
import com.gmail.chibitopoochan.soqlexec.model.SObjectMetaInfo;
import com.gmail.chibitopoochan.soqlexec.soap.MetaInformationProvider;
import com.gmail.chibitopoochan.soqlexec.soap.SOQLExecutor;
import com.gmail.chibitopoochan.soqlexec.soap.SalesforceConnectionFactory;
import com.gmail.chibitopoochan.soqlexec.util.Constants;
import com.gmail.chibitopoochan.soqlexec.util.Constants.Message.Error;
import com.gmail.chibitopoochan.soqlexec.util.Message;
import com.sforce.ws.ConnectionException;

public class Connector {
	// クラス共通の参照
	private static final Logger logger = LoggerFactory.getLogger(Connector.class);
	private static final ResourceBundle resources = ResourceBundle.getBundle(Constants.Message.RESOURCE);
	private static final ResourceBundle properties = ResourceBundle.getBundle(Constants.Properties.RESOURCE);

	// Proxy情報の保持
	private static String proxyHost;
	private static int proxyPort;
	private static String proxyId;
	private static String proxyPassword;
	private static boolean useProxy;

	// 接続状態の保持
	private Optional<SOQLExecutor.QueryMore> queryMore;
	private SalesforceConnectionFactory factory;
	private SOQLExecutor executor;
	private MetaInformationProvider provider;
	private boolean close = false;
	private int size;

	/**
	 * Salesforceへの接続
	 * @param factory ログイン後の接続情報
	 */
	private Connector(SalesforceConnectionFactory factory) {
		this.factory = factory;
		this.executor = new SOQLExecutor(factory.getPartnerConnection());
		this.provider = new MetaInformationProvider(factory.getPartnerConnection());
	}

	/**
	 * メタ情報提供クラスを指定
	 * @param provider メタ情報提供クラス
	 */
	public void setMetaInfoProvieder(MetaInformationProvider provider) {
		this.provider = provider;
		this.provider.setPartnerConnection(factory.getPartnerConnection());
	}

	/**
	 * SOQL実行クラスを指定
	 * @param executor SOQL実行クラス
	 */
	public void setSOQLExecutor(SOQLExecutor executor) {
		this.executor = executor;
		this.executor.setPartnerConnection(factory.getPartnerConnection());
	}

	/**
	 * Salesforceへのログイン
	 * @param username ユーザ名
	 * @param password <PASSWORD>
	 * @param env authEndPoint
	 * @return Salesforceへの接続
	 * @throws ConnectionException ログインエラー
	 */
	public static Connector login(String username, String password, String env) throws Exception {
		SalesforceConnectionFactory factory;

		if(Connector.useProxy) {
			factory = SalesforceConnectionFactory.newInstance(env, username, password
					,Connector.proxyHost, Connector.proxyPort, Connector.proxyId, Connector.proxyPassword);
		} else {
			factory = SalesforceConnectionFactory.newInstance(env, username, password);
		}

		if(!factory.login()) {
			throw new ConnectionException(Message.get(Error.ERR_001, username, password, env));
		}

		Connector.resetProxySetting();

		return new Connector(factory);

	}

	/**
	 * Proxy接続の設定
	 * @param proxyHost ホスト名
	 * @param proxyPort ポート番号
	 * @param proxyId ユーザID
	 * @param proxyPassword <PASSWORD>
	 */
	public static void setProxySetting(String proxyHost, int proxyPort, String proxyId, String proxyPassword) {
		Connector.useProxy = true;
		Connector.proxyHost = proxyHost;
		Connector.proxyPort = proxyPort;
		Connector.proxyId = proxyId;
		Connector.proxyPassword = proxyPassword;
		logger.info("set proxy setting");
		logger.info(String.format("useProxy=[%s]",useProxy));
		logger.info(String.format("useHost=[%s]",proxyHost));
		logger.info(String.format("usePort=[%s]",proxyPort));
		logger.info(String.format("useId=[%s]",proxyId));
		logger.info(String.format("usePassword=[%s]",proxyPassword));
	}

	/**
	 * Proxy接続の設定解除
	 */
	public static void resetProxySetting() {
		Connector.useProxy = false;
		Connector.proxyHost = null;
		Connector.proxyPort = 0;
		Connector.proxyId = null;
		Connector.proxyPassword = null;

	}

	/**
	 * Salesforceへのログイン
	 * @param username ユーザ名
{'fraction_non_alphanumeric': 0.10304789550072568, 'fraction_numerical': 0.001451378809869376, 'mean_word_length': 5.4350194552529185, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6125931', 'n_tokens_mistral': 2816, 'n_tokens_neox': 2709, 'n_words': 495}