<commit_before>/*******************************************************************************
 * Copyright (c) 2010, 2014, <NAME>., Budapest University of Technology and Economics
 * All rights reserved. This program and the accompanying materials 
 * are made available under the terms of the Eclipse Public License v1.0 
 * which accompanies this distribution, and is available at 
 * http://www.eclipse.org/legal/epl-v10.html 
 *
 * Contributors: 
 *     <NAME> - initial API and implementation 
 *******************************************************************************/
package hu.bme.mit.massif.communication.commandevaluation;

import hu.bme.mit.massif.communication.ConnectorCreationException;
import hu.bme.mit.massif.communication.ICommandEvaluator;
import hu.bme.mit.massif.communication.ICommandEvaluatorFactory;

import java.util.List;
import java.util.Map;

import br.com.embraer.massif.commandevaluation.client.MatlabClient;
import br.com.embraer.massif.commandevaluation.client.util.MatlabProcessInformation;
import br.com.embraer.massif.commandevaluation.client.util.MatlabRunningManager;
import br.com.embraer.massif.commandevaluation.exception.MatlabRMIException;
import br.com.embraer.massif.commandevaluation.exception.MatlabRegistryException;
import br.com.embraer.massif.commandevaluation.util.MatlabProviderProperties;

/**
 * This class is responsible for returning the appropriate object with the ICommandEvaluator interface for command
 * execution.
 */
public class CEServerCommandEvaluatorFactory implements ICommandEvaluatorFactory {

    private static final String MATLAB_NOT_FOUND_WITH_GIVEN_PARAMETERS = "No running MATLAB server was found with the given parameters specified in Window/Preferences/Simulink Preferences";
    protected static final String MATLAB_RUNNING_CHECK_ERROR = "Exception occurred checking running Matlabs!";

	@Override
	public ICommandEvaluator createCommandEvaluator(
			Map<String, Object> parameters) throws ConnectorCreationException {
		ICommandEvaluator result = null;
		
		// Get the required parameters from the parameter map and cast them to the right type
		String hostAddress = (String) parameters.get("hostAddress");
		int hostPort = (Integer) parameters.get("hostPort");
		String serviceName = (String) parameters.get("serviceName");

		boolean existingSession = checkExistingMatlabSession(serviceName);

        if (!existingSession) {
            String errorMsg = MATLAB_NOT_FOUND_WITH_GIVEN_PARAMETERS;
            throw new ConnectorCreationException(errorMsg, null);
        }
        
        try {
            MatlabClient matlabClient = new MatlabClient(hostAddress, hostPort, serviceName);
            result = new CommandEvaluatorImpl(matlabClient);
        } catch (MatlabRMIException e) {
            throw new ConnectorCreationException("Exception occurred while preparing command evaluator!", e);
        }
        return result;
	}

    protected boolean checkExistingMatlabSession(String serviceName) throws ConnectorCreationException {
        try {
        	List<MatlabProcessInformation> matlabs = MatlabRunningManager.getRunningMatlabs();
            for (MatlabProcessInformation matlabProcessInformation : matlabs) {
            	if (serviceName.replace(MatlabProviderProperties.MATLAB_SERVER_DEFAULT_SERVICE_NAME, "").equalsIgnoreCase(
            			matlabProcessInformation.release + matlabProcessInformation.pid))
            		return true;
            }
        } catch (MatlabRegistryException e) {
            throw new ConnectorCreationException(MATLAB_RUNNING_CHECK_ERROR, e);
        }
        return false;
    }

	
	@Override
	public String getConnectorName() {<commit_msg>Only check for running matlab instances on Windows #4<commit_after>/*******************************************************************************
 * Copyright (c) 2010, 2014, <NAME>., Budapest University of Technology and Economics
 * All rights reserved. This program and the accompanying materials 
 * are made available under the terms of the Eclipse Public License v1.0 
 * which accompanies this distribution, and is available at 
 * http://www.eclipse.org/legal/epl-v10.html 
 *
 * Contributors: 
 *     <NAME> - initial API and implementation 
 *******************************************************************************/
package hu.bme.mit.massif.communication.commandevaluation;

import hu.bme.mit.massif.communication.ConnectorCreationException;
import hu.bme.mit.massif.communication.ICommandEvaluator;
import hu.bme.mit.massif.communication.ICommandEvaluatorFactory;

import java.util.List;
import java.util.Map;

import br.com.embraer.massif.commandevaluation.client.MatlabClient;
import br.com.embraer.massif.commandevaluation.client.util.MatlabProcessInformation;
import br.com.embraer.massif.commandevaluation.client.util.MatlabRunningManager;
import br.com.embraer.massif.commandevaluation.exception.MatlabRMIException;
import br.com.embraer.massif.commandevaluation.exception.MatlabRegistryException;
import br.com.embraer.massif.commandevaluation.util.MatlabProviderProperties;

/**
 * This class is responsible for returning the appropriate object with the ICommandEvaluator interface for command
 * execution.
 */
public class CEServerCommandEvaluatorFactory implements ICommandEvaluatorFactory {

    private static final String MATLAB_NOT_FOUND_WITH_GIVEN_PARAMETERS = "No running MATLAB server was found with the given parameters specified in Window/Preferences/Simulink Preferences";
    protected static final String MATLAB_RUNNING_CHECK_ERROR = "Exception occurred checking running Matlabs!";

	@Override
	public ICommandEvaluator createCommandEvaluator(
			Map<String, Object> parameters) throws ConnectorCreationException {
		ICommandEvaluator result = null;
		
		// Get the required parameters from the parameter map and cast them to the right type
		String hostAddress = (String) parameters.get("hostAddress");
		int hostPort = (Integer) parameters.get("hostPort");
		String serviceName = (String) parameters.get("serviceName");

		String osName = System.getProperty("os.name");
		if (osName != null && osName.indexOf("win") >= 0) {
		    // Check whether there is an open matlab session on this computer
		    // The checking only work on windows thus the system property check
		    boolean existingSession = checkExistingMatlabSession(serviceName);
    
            if (!existingSession) {
                String errorMsg = MATLAB_NOT_FOUND_WITH_GIVEN_PARAMETERS;
                throw new ConnectorCreationException(errorMsg, null);
            }
		}
        try {
            MatlabClient matlabClient = new MatlabClient(hostAddress, hostPort, serviceName);
            result = new CommandEvaluatorImpl(matlabClient);
        } catch (MatlabRMIException e) {
            throw new ConnectorCreationException("Exception occurred while preparing command evaluator!", e);
        }
        return result;
	}

    protected boolean checkExistingMatlabSession(String serviceName) throws ConnectorCreationException {
        try {
        	List<MatlabProcessInformation> matlabs = MatlabRunningManager.getRunningMatlabs();
            for (MatlabProcessInformation matlabProcessInformation : matlabs) {
            	if (serviceName.replace(MatlabProviderProperties.MATLAB_SERVER_DEFAULT_SERVICE_NAME, "").equalsIgnoreCase(
            			matlabProcessInformation.release + matlabProcessInformation.pid))
            		return true;
            }
        } catch (MatlabRegistryException e) {
            throw new ConnectorCreationException(MATLAB_RUNNING_CHECK_ERROR, e);
        }
        return false;
    }

	
	@Override
	public String getConnectorName() {
{'fraction_non_alphanumeric': 0.11140031233732431, 'fraction_numerical': 0.00338365434669443, 'mean_word_length': 4.3331020124913255, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '266258', 'n_tokens_mistral': 2055, 'n_tokens_neox': 1951, 'n_words': 500}