<commit_before>package us.kbase.kbasetrees;

import java.util.List;
import java.util.Map;
import us.kbase.auth.AuthToken;
import us.kbase.common.service.JsonServerMethod;
import us.kbase.common.service.JsonServerServlet;
import us.kbase.common.service.Tuple11;

//BEGIN_HEADER
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Properties;
import java.util.ArrayList;

import org.ini4j.Ini;

import us.kbase.auth.TokenFormatException;
import us.kbase.common.service.JsonClientException;
import us.kbase.common.service.UnauthorizedException;
import us.kbase.common.taskqueue.JobStatuses;
import us.kbase.common.taskqueue.TaskQueue;
import us.kbase.common.taskqueue.TaskQueueConfig;
import us.kbase.kbasetrees.SpeciesTreeBuilder;
import us.kbase.kbasetrees.util.CdsUtil;
import us.kbase.kbasetrees.util.TreeImportPackage;
import us.kbase.tree.TreeClient;
import us.kbase.userandjobstate.InitProgress;
import us.kbase.userandjobstate.Results;
import us.kbase.userandjobstate.UserAndJobStateClient;
import us.kbase.workspace.ObjectSaveData;
import us.kbase.workspace.SaveObjectsParams;
import us.kbase.common.service.UObject;
import us.kbase.workspace.WorkspaceClient;

import org.forester.io.parsers.nhx.NHXParser;
import org.forester.phylogeny.Phylogeny;
import org.forester.phylogeny.PhylogenyMethods;
import org.forester.phylogeny.PhylogenyNode;
import org.forester.phylogeny.iterators.PhylogenyNodeIterator;
//END_HEADER

/**
 * <p>Original spec-file module name: KBaseTrees</p>
 * <pre>
 * Phylogenetic Tree and Multiple Sequence Alignment Services
 * This service provides a set of data types and methods for operating with multiple
 * sequence alignments (MSAs) and phylogenetic trees.
 * Authors
 * ---------
 * <NAME>, LBL (<EMAIL>)
 * <NAME>, ANL (<EMAIL>)
 * <NAME>, LBL (<EMAIL>)
 * <NAME>, LBL (<EMAIL>)
 * <NAME>, LBL (<EMAIL>)
 * <NAME>, LBL (<EMAIL>)
 * </pre>
 */
public class KBaseTreesServer extends JsonServerServlet {
    private static final long serialVersionUID = 1L;

    //BEGIN_CLASS_HEADER
    private static TaskQueue taskHolder = null;
    private static TaskQueueConfig taskConfig = null;
    
	private static final String defaultWsUrl = "http://dev04.berkeley.kbase.us:7058";
	//private static final String defaultWsUrl = "https://kbase.us/services/ws/";
	private static final String defaultCdmiUrl = "https://kbase.us/services/cdmi_api/";
    private static final String defaultUjsUrl = "https://kbase.us/services/userandjobstate/";
    private static final String specServiceName = "trees";
    
    public static final String SYS_PROP_KB_DEPLOYMENT_CONFIG = "KB_DEPLOYMENT_CONFIG";
    
    public static final String CFG_PROP_THREAD_COUNT = "thread.count";
    public static final String CFG_PROP_QUEUE_DB_DIR = "queue.db.dir";
    public static final String CFG_PROP_WS_SRV_URL = "workspace.srv.url";
    public static final String CFG_PROP_JSS_SRV_URL = "jobstatus.srv.url";
    public static final String CFG_PROP_TEMP_DIR = "scratch";
    public static final String CFG_PROP_DATA_DIR = "data.dir";

    static {
    	// Setup service name
    	String KB_SERVNAME = "KB_SERVICE_NAME";
    	System.setProperty(KB_SERVNAME, specServiceName);
    	System.out.println(KBaseTreesServer.class.getName() + ": Service name was defined: " + specServiceName);
    	// Setup deployment configuration path
		String configPath = System.getProperty(SYS_PROP_KB_DEPLOYMENT_CONFIG);
		if (configPath == null)
			configPath = System.getenv(SYS_PROP_KB_DEPLOYMENT_CONFIG);
		if (configPath == null) {
			InputStream is = KBaseTreesServer.class.getResourceAsStream("config_path.properties");
			try {
				Properties props = new Properties();
				props.load(is);
				configPath = props.getProperty("config_path");
				System.setProperty(SYS_PROP_KB_DEPLOYMENT_CONFIG, configPath);
			} catch (Exception ex) {
				ex.printStackTrace();
			} finally {
				try { is.close(); } catch (Exception ignore) {}
			}
		}
    }
    
    public static synchronized TaskQueueConfig getTaskConfig() throws Exception {
    	if (taskConfig == null) {
    		int threadCount = 1;
    		File queueDbDir = new File(".");
    		String wsUrl = defaultWsUrl;
    		String ujsUrl = defaultUjsUrl;
    		
    		Map<String, String> allConfigProps = loadConfig();
    		if (allConfigProps.containsKey(CFG_PROP_THREAD_COUNT))
    			threadCount = Integer.parseInt(allConfigProps.get(CFG_PROP_THREAD_COUNT));
    		if (allConfigProps.containsKey(CFG_PROP_QUEUE_DB_DIR))
    			queueDbDir = new File(allConfigProps.get(CFG_PROP_QUEUE_DB_DIR));
    		if (allConfigProps.containsKey(CFG_PROP_WS_SRV_URL))
    			wsUrl = allConfigProps.get(CFG_PROP_WS_SRV_URL);
    		if (allConfigProps.containsKey(CFG_PROP_JSS_SRV_URL))
    			ujsUrl = allConfigProps.get(CFG_PROP_JSS_SRV_URL);
    		for (Object key : allConfigProps.keySet())
    			allConfigProps.put(key.toString(), allConfigProps.get(key.toString()));
    		final String finalWsUrl = wsUrl;
    		final String finalUjsUrl = ujsUrl;
    		JobStatuses jobStatuses = new JobStatuses() {
				@Override
				public String createAndStartJob(String token, String status, String desc,
						String initProgressPtype, String estComplete) throws Exception {
    				return createJobClient(finalUjsUrl, token).createAndStartJob(token, status, desc, 
    						new InitProgress().withPtype(initProgressPtype), estComplete);
				}
				@Override
				public void updateJob(String job, String token, String status,
						String estComplete) throws Exception {
    				createJobClient(finalUjsUrl, token).updateJob(job, token, status, estComplete);
				}
				@Override
				public void completeJob(String job, String token, String status,
						String error, String wsUrl, String outRef) throws Exception {
    				createJobClient(finalUjsUrl, token).completeJob(job, token, status, error, 
    						new Results().withWorkspaceurl(finalWsUrl).withWorkspaceids(Arrays.asList(outRef)));<commit_msg>Fix for bug in registering error of long running job in UJS.<commit_after>package us.kbase.kbasetrees;

import java.util.List;
import java.util.Map;
import us.kbase.auth.AuthToken;
import us.kbase.common.service.JsonServerMethod;
import us.kbase.common.service.JsonServerServlet;
import us.kbase.common.service.Tuple11;

//BEGIN_HEADER
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Properties;
import java.util.ArrayList;

import org.ini4j.Ini;

import us.kbase.auth.TokenFormatException;
import us.kbase.common.service.JsonClientException;
import us.kbase.common.service.UnauthorizedException;
import us.kbase.common.taskqueue.JobStatuses;
import us.kbase.common.taskqueue.TaskQueue;
import us.kbase.common.taskqueue.TaskQueueConfig;
import us.kbase.kbasetrees.SpeciesTreeBuilder;
import us.kbase.kbasetrees.util.CdsUtil;
import us.kbase.kbasetrees.util.TreeImportPackage;
import us.kbase.tree.TreeClient;
import us.kbase.userandjobstate.InitProgress;
import us.kbase.userandjobstate.Results;
import us.kbase.userandjobstate.UserAndJobStateClient;
import us.kbase.workspace.ObjectSaveData;
import us.kbase.workspace.SaveObjectsParams;
import us.kbase.common.service.UObject;
import us.kbase.workspace.WorkspaceClient;

import org.forester.io.parsers.nhx.NHXParser;
import org.forester.phylogeny.Phylogeny;
import org.forester.phylogeny.PhylogenyMethods;
import org.forester.phylogeny.PhylogenyNode;
import org.forester.phylogeny.iterators.PhylogenyNodeIterator;
//END_HEADER

/**
 * <p>Original spec-file module name: KBaseTrees</p>
 * <pre>
 * Phylogenetic Tree and Multiple Sequence Alignment Services
 * This service provides a set of data types and methods for operating with multiple
 * sequence alignments (MSAs) and phylogenetic trees.
 * Authors
 * ---------
 * <NAME>, LBL (<EMAIL>)
 * <NAME>, ANL (<EMAIL>)
 * <NAME>, LBL (<EMAIL>)
 * <NAME>, LBL (<EMAIL>)
 * <NAME>, LBL (<EMAIL>)
 * <NAME>, LBL (<EMAIL>)
 * </pre>
 */
public class KBaseTreesServer extends JsonServerServlet {
    private static final long serialVersionUID = 1L;

    //BEGIN_CLASS_HEADER
    private static TaskQueue taskHolder = null;
    private static TaskQueueConfig taskConfig = null;
    
	private static final String defaultWsUrl = "http://dev04.berkeley.kbase.us:7058";
	//private static final String defaultWsUrl = "https://kbase.us/services/ws/";
	private static final String defaultCdmiUrl = "https://kbase.us/services/cdmi_api/";
    private static final String defaultUjsUrl = "https://kbase.us/services/userandjobstate/";
    private static final String specServiceName = "trees";
    
    public static final String SYS_PROP_KB_DEPLOYMENT_CONFIG = "KB_DEPLOYMENT_CONFIG";
    
    public static final String CFG_PROP_THREAD_COUNT = "thread.count";
    public static final String CFG_PROP_QUEUE_DB_DIR = "queue.db.dir";
    public static final String CFG_PROP_WS_SRV_URL = "workspace.srv.url";
    public static final String CFG_PROP_JSS_SRV_URL = "jobstatus.srv.url";
    public static final String CFG_PROP_TEMP_DIR = "scratch";
    public static final String CFG_PROP_DATA_DIR = "data.dir";

    static {
    	// Setup service name
    	String KB_SERVNAME = "KB_SERVICE_NAME";
    	System.setProperty(KB_SERVNAME, specServiceName);
    	System.out.println(KBaseTreesServer.class.getName() + ": Service name was defined: " + specServiceName);
    	// Setup deployment configuration path
		String configPath = System.getProperty(SYS_PROP_KB_DEPLOYMENT_CONFIG);
		if (configPath == null)
			configPath = System.getenv(SYS_PROP_KB_DEPLOYMENT_CONFIG);
		if (configPath == null) {
			InputStream is = KBaseTreesServer.class.getResourceAsStream("config_path.properties");
			try {
				Properties props = new Properties();
				props.load(is);
				configPath = props.getProperty("config_path");
				System.setProperty(SYS_PROP_KB_DEPLOYMENT_CONFIG, configPath);
			} catch (Exception ex) {
				ex.printStackTrace();
			} finally {
				try { is.close(); } catch (Exception ignore) {}
			}
		}
    }
    
    public static synchronized TaskQueueConfig getTaskConfig() throws Exception {
    	if (taskConfig == null) {
    		int threadCount = 1;
    		File queueDbDir = new File(".");
    		String wsUrl = defaultWsUrl;
    		String ujsUrl = defaultUjsUrl;
    		
    		Map<String, String> allConfigProps = loadConfig();
    		if (allConfigProps.containsKey(CFG_PROP_THREAD_COUNT))
    			threadCount = Integer.parseInt(allConfigProps.get(CFG_PROP_THREAD_COUNT));
    		if (allConfigProps.containsKey(CFG_PROP_QUEUE_DB_DIR))
    			queueDbDir = new File(allConfigProps.get(CFG_PROP_QUEUE_DB_DIR));
    		if (allConfigProps.containsKey(CFG_PROP_WS_SRV_URL))
    			wsUrl = allConfigProps.get(CFG_PROP_WS_SRV_URL);
    		if (allConfigProps.containsKey(CFG_PROP_JSS_SRV_URL))
    			ujsUrl = allConfigProps.get(CFG_PROP_JSS_SRV_URL);
    		for (Object key : allConfigProps.keySet())
    			allConfigProps.put(key.toString(), allConfigProps.get(key.toString()));
    		final String finalWsUrl = wsUrl;
    		final String finalUjsUrl = ujsUrl;
    		JobStatuses jobStatuses = new JobStatuses() {
				@Override
				public String createAndStartJob(String token, String status, String desc,
						String initProgressPtype, String estComplete) throws Exception {
    				return createJobClient(finalUjsUrl, token).createAndStartJob(token, status, desc, 
    						new InitProgress().withPtype(initProgressPtype), estComplete);
				}
				@Override
				public void updateJob(String job, String token, String status,
						String estComplete) throws Exception {
    				createJobClient(finalUjsUrl, token).updateJob(job, token, status, estComplete);
				}
				@Override
				public void completeJob(String job, String token, String status,
						String error, String wsUrl, String outRef) throws Exception {
    				createJobClient(finalUjsUrl, token).completeJob(job, token, status, error, 
    						new Results().withWorkspaceurl(finalWsUrl).withWorkspaceids(
    								outRef == null ? Collections.<String>emptyList() : Arrays.asList(outRef)));
{'fraction_non_alphanumeric': 0.10217124129455141, 'fraction_numerical': 0.0018025399426464565, 'mean_word_length': 4.441818992420865, 'pattern_counts': {'":': 2, '<': 38, '<?xml version=': 0, '>': 38, 'https://': 6, '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': '5927682', 'n_tokens_mistral': 4154, 'n_tokens_neox': 3882, 'n_words': 826}