in odps-console-basic/src/main/java/com/aliyun/openservices/odps/console/ExecutionContext.java [526:765]
public static ExecutionContext load(String config) throws ODPSConsoleException {
ExecutionContext context = new ExecutionContext();
if (config != null) {
config = FileUtil.expandUserHomeInPath(config);
}
// 从文件读取配置信息
String configFile = (config != null ? config : ODPSConsoleUtils.getConfigFilePath());
if (configFile == null) {
return context;
}
try {
// configFile如果有空格的话,File(configFile)是找不到的
configFile = URLDecoder.decode(configFile, "utf-8");
} catch (Exception e1) {
// do nothing
}
File file = new File(configFile);
if (!file.exists()) {
// 如果文件不存在,则不通过文件加载ExecutionContext
// 用户可以通过命令行来设置ExecutionContext
return context;
}
context.setConfigFile(configFile);
FileInputStream configInputStream = null;
try {
configInputStream = new FileInputStream(configFile);
Properties properties = new ExtProperties();
properties.load(configInputStream);
String projectName = properties.getProperty(ODPSConsoleConstants.PROJECT_NAME);
//TODO schema support in config.ini?
String schemaName = properties.getProperty(ODPSConsoleConstants.SCHEMA_NAME);
String endpoint = properties.getProperty(ODPSConsoleConstants.END_POINT);
String accessId = properties.getProperty(ODPSConsoleConstants.ACCESS_ID);
String accessKey = properties.getProperty(ODPSConsoleConstants.ACCESS_KEY);
String appAccessId = properties.getProperty(ODPSConsoleConstants.APP_ACCESS_ID);
String appAccessKey = properties.getProperty(ODPSConsoleConstants.APP_ACCESS_KEY);
String dataSizeConfirm = properties.getProperty(ODPSConsoleConstants.DATA_SIZE_CONFIRM);
String host = properties.getProperty(ODPSConsoleConstants.PROXY_HOST);
String port = properties.getProperty(ODPSConsoleConstants.PROXY_PORT);
String hooks = properties.getProperty(ODPSConsoleConstants.POST_HOOK_CLASS);
String tunnelEndpoint = properties.getProperty(ODPSConsoleConstants.TUNNEL_ENDPOINT);
String datahubEndpoint = properties.getProperty(ODPSConsoleConstants.DATAHUB_ENDPOINT);
String runningCluster = properties.getProperty(ODPSConsoleConstants.RUNNING_CLUSTER);
String logViewHost = properties.getProperty(ODPSConsoleConstants.LOG_VIEW_HOST);
String logViewLife = properties.getProperty(ODPSConsoleConstants.LOG_VIEW_LIFE);
String updateUrl = properties.getProperty(ODPSConsoleConstants.UPDATE_URL);
String odpsCupidProxyEndpoint = properties.getProperty(ODPSConsoleConstants.CUPID_PROXY_END_POINT);
String interactiveSessionMode = properties.getProperty(ODPSConsoleConstants.ENABLE_INTERACTIVE_MODE);
String interactiveSessionName = properties.getProperty(ODPSConsoleConstants.INTERACTIVE_SERVICE_NAME);
String interactiveOutputCompatible = properties.getProperty(ODPSConsoleConstants.INTERACTIVE_OUTPUT_COMPATIBLE);
context.setOdpsCupidProxyEndpoint(odpsCupidProxyEndpoint);
context.setLogViewHost(logViewHost);
if (!StringUtils.isNullOrEmpty(logViewLife)) {
context.setLogViewLife(Integer.parseInt(logViewLife));
}
context.setUpdateUrl(updateUrl);
context.setProxyHost(host);
if (!StringUtils.isNullOrEmpty(port)) {
context.setProxyPort(Integer.valueOf(port));
}
context.setAccessId(accessId);
context.setAccessKey(accessKey);
context.setAppAccessId(appAccessId);
context.setAppAccessKey(appAccessKey);
context.setEndpoint(endpoint);
context.setProjectName(projectName);
//TODO schema support in config.ini?
// context.setSchemaName(schemaName);
if (!StringUtils.isNullOrEmpty(dataSizeConfirm)) {
Double value = Double.valueOf(dataSizeConfirm);
if (value <= 0) {
value = null;
}
context.setConfirmDataSize(value);
}
context.setOdpsHooks(hooks);
context.setTunnelEndpoint(tunnelEndpoint);
context.setDatahubEndpoint(datahubEndpoint);
context.setRunningCluster(runningCluster);
// load 用户定义的Interactive Command
context.setUserCommands(properties.getProperty(ODPSConsoleConstants.USER_COMMANDS));
// debug mode
String isDebug = properties.getProperty(ODPSConsoleConstants.IS_DEBUG);
if (isDebug != null) {
context.setDebug(Boolean.valueOf(isDebug));
}
// console联接的是否为测试环境
String testEnv = properties.getProperty("test_env");
if (testEnv != null) {
context.setTestEnv(Boolean.valueOf(testEnv));
}
// Account provider
String accountProviderStr = properties.getProperty(ODPSConsoleConstants.ACCOUNT_PROVIDER);
if (accountProviderStr != null) {
context.setAccountProvider(AccountProvider.valueOf(accountProviderStr.toUpperCase()));
}
String instancePriority = properties.getProperty(ODPSConsoleConstants.INSTANCE_PRIORITY);
if (instancePriority != null) {
context.setPriority(Integer.valueOf(instancePriority));
context.setPaiPriority(Integer.valueOf(instancePriority));
}
String httpsCheck = properties.getProperty(ODPSConsoleConstants.HTTPS_CHECK);
if (httpsCheck != null) {
context.setHttpsCheck(Boolean.valueOf(httpsCheck));
}
String useInstanceTunnel = properties.getProperty(ODPSConsoleConstants.USE_INSTANCE_TUNNEL);
if (!StringUtils.isNullOrEmpty(useInstanceTunnel)) {
context.setUseInstanceTunnel(Boolean.valueOf(useInstanceTunnel));
}
String maxAttachSessionInstances =
properties.getProperty(ODPSConsoleConstants.INTERACTIVE_MAX_ATTACH);
if (!StringUtils.isNullOrEmpty(maxAttachSessionInstances)) {
Long num = Long.parseLong(maxAttachSessionInstances);
if (num <= 0) {
num = null;
}
context.setMaxAttachSessionInstances(num);
}
String attachSessionTimeout = properties.getProperty(ODPSConsoleConstants.ATTACH_SESSION_TIMEOUT);
if (!StringUtils.isNullOrEmpty(attachSessionTimeout)) {
context.setAttachTimeout(Long.valueOf(attachSessionTimeout));
}
String instanceTunnelMaxRecord =
properties.getProperty(ODPSConsoleConstants.INSTANCE_TUNNEL_MAX_RECORD);
if (!StringUtils.isNullOrEmpty(instanceTunnelMaxRecord)) {
Long num = Long.parseLong(instanceTunnelMaxRecord);
if (num <= 0) {
num = null;
}
context.setInstanceTunnelMaxRecord(num);
}
String instanceTunnelMaxSize =
properties.getProperty(ODPSConsoleConstants.INSTANCE_TUNNEL_MAX_SIZE);
if (!StringUtils.isNullOrEmpty(instanceTunnelMaxSize)) {
Long size = Long.parseLong(instanceTunnelMaxSize);
if (size <= 0) {
size = null;
}
context.setInstanceTunnelMaxSize(size);
}
String sessionAutoRerun = properties.getProperty(ODPSConsoleConstants.INTERACTIVE_AUTO_RERUN);
if (!StringUtils.isNullOrEmpty(sessionAutoRerun)) {
if (Boolean.valueOf(sessionAutoRerun)) {
context.fallbackPolicy = FallbackPolicy.alwaysFallbackExceptAttachPolicy();
} else {
context.fallbackPolicy = FallbackPolicy.nonFallbackPolicy();
}
}
if (!StringUtils.isNullOrEmpty(interactiveSessionMode) && Boolean.valueOf(interactiveSessionMode)) {
context.setAutoSessionMode(true);
if (!StringUtils.isNullOrEmpty(interactiveSessionName)) {
context.setInteractiveSessionName(interactiveSessionName);
}
if (!StringUtils.isNullOrEmpty(maxAttachSessionInstances)) {
LocalCacheUtils.enableMultiAttachSessionMode(context.getMaxAttachSessionInstances());
int maxRetry = 10;
boolean readCacheSucc = false;
while (maxRetry-- > 0) {
try {
// load once from file, will use .session/${config_hash} as session dir
LocalCacheUtils.setCacheDir(configFile, endpoint, projectName, accessId);
context.localCache = LocalCacheUtils.readCache();
readCacheSucc = true;
break;
} catch (Exception e) {
Thread.sleep(200);
}
}
if (!readCacheSucc) {
throw new ODPSConsoleException("Try to find available attach session failed after retry");
}
} else {
// load once from file, will use .session/${config_hash} as session dir
LocalCacheUtils.setCacheDir(configFile, endpoint, projectName, accessId);
context.localCache = LocalCacheUtils.readCache();
}
}
String sessionOutputCompatible = properties.getProperty(ODPSConsoleConstants.INTERACTIVE_OUTPUT_COMPATIBLE);
if (!StringUtils.isNullOrEmpty(sessionOutputCompatible)) {
if (Boolean.valueOf(sessionOutputCompatible)) {
context.interactiveOutputCompatible = true;
} else {
context.interactiveOutputCompatible = false;
}
}
String liteMode = properties.getProperty(ODPSConsoleConstants.LITE_MODE);
if (!StringUtils.isNullOrEmpty(liteMode)) {
context.setLiteMode(Boolean.valueOf(liteMode));
}
// 取console屏幕的宽度
context.setConsoleWidth(ODPSConsoleUtils.getConsoleWidth());
// load predefined set commands, will execute them later
for (String propertyName : properties.stringPropertyNames()) {
if (propertyName.toLowerCase().startsWith(SET_COMMAND_PREFIX)) {
String key = propertyName.substring(SET_COMMAND_PREFIX.length()).trim();
String value = properties.getProperty(propertyName).trim();
context.predefinedSetCommands.put(key, value);
}
}
} catch (Exception e) {
throw new ODPSConsoleException(ODPSConsoleConstants.LOAD_CONFIG_ERROR, e);
} finally {
if (configInputStream != null) {
try {
configInputStream.close();
} catch (IOException e) {
}
}
}
return context;
}