<commit_before>package com.specmate.connectors.internal;

import static com.specmate.connectors.internal.config.ConnectorServiceConfig.KEY_POLL_SCHEDULE;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ScheduledExecutorService;

import org.eclipse.emf.cdo.common.id.CDOWithID;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.ConfigurationPolicy;
import org.osgi.service.component.annotations.Deactivate;
import org.osgi.service.component.annotations.Reference;
import org.osgi.service.component.annotations.ReferenceCardinality;
import org.osgi.service.component.annotations.ReferencePolicy;
import org.osgi.service.log.LogService;

import com.specmate.common.SpecmateException;
import com.specmate.common.SpecmateValidationException;
import com.specmate.connectors.api.IRequirementsSource;
import com.specmate.connectors.internal.config.ConnectorServiceConfig;
import com.specmate.persistency.IPersistencyService;
import com.specmate.persistency.ITransaction;

import it.sauronsoftware.cron4j.Scheduler;
import it.sauronsoftware.cron4j.SchedulingPattern;

@Component(immediate = true, configurationPid = ConnectorServiceConfig.PID, configurationPolicy = ConfigurationPolicy.REQUIRE)
public class ConnectorService {
	CDOWithID id;
	List<IRequirementsSource> requirementsSources = new ArrayList<>();
	private LogService logService;
	private IPersistencyService persistencyService;
	private ScheduledExecutorService scheduler;
	private ITransaction transaction;

	@Activate
	public void activate(Map<String, Object> properties) throws SpecmateValidationException, SpecmateException {
		validateConfig(properties);
		
		String cronStr = (String) properties.get(KEY_POLL_SCHEDULE);
		if(cronStr == null) {
			return;
		}
		
		this.transaction = this.persistencyService.openTransaction();
		Runnable connectorRunnable = new ConnectorJobRunnable(requirementsSources, transaction, logService);
		
		Scheduler scheduler = new Scheduler();
		scheduler.schedule(cronStr, connectorRunnable);
		scheduler.start();
	}

	private void validateConfig(Map<String, Object> properties) throws SpecmateValidationException {
		String cronStr = (String) properties.get(KEY_POLL_SCHEDULE);
		if(!SchedulingPattern.validate(cronStr)) {
			String message = "Cron " + cronStr + " invalid!";
			logService.log(LogService.LOG_ERROR, message);
			throw new SpecmateValidationException(message);
			
		}
		logService.log(LogService.LOG_DEBUG, "Connector service config validated.");
	}

	@Deactivate
	public void deactivate() {
		this.scheduler.shutdown();
		transaction.close();
	}
<commit_msg>Triggering connectors at start in own thread<commit_after>package com.specmate.connectors.internal;

import static com.specmate.connectors.internal.config.ConnectorServiceConfig.KEY_POLL_SCHEDULE;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ScheduledExecutorService;

import org.eclipse.emf.cdo.common.id.CDOWithID;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.ConfigurationPolicy;
import org.osgi.service.component.annotations.Deactivate;
import org.osgi.service.component.annotations.Reference;
import org.osgi.service.component.annotations.ReferenceCardinality;
import org.osgi.service.component.annotations.ReferencePolicy;
import org.osgi.service.log.LogService;

import com.specmate.common.SpecmateException;
import com.specmate.common.SpecmateValidationException;
import com.specmate.connectors.api.IRequirementsSource;
import com.specmate.connectors.internal.config.ConnectorServiceConfig;
import com.specmate.persistency.IPersistencyService;
import com.specmate.persistency.ITransaction;

import it.sauronsoftware.cron4j.Scheduler;
import it.sauronsoftware.cron4j.SchedulingPattern;

@Component(immediate = true, configurationPid = ConnectorServiceConfig.PID, configurationPolicy = ConfigurationPolicy.REQUIRE)
public class ConnectorService {
	CDOWithID id;
	List<IRequirementsSource> requirementsSources = new ArrayList<>();
	private LogService logService;
	private IPersistencyService persistencyService;
	private ScheduledExecutorService scheduler;
	private ITransaction transaction;

	@Activate
	public void activate(Map<String, Object> properties) throws SpecmateValidationException, SpecmateException {
		validateConfig(properties);
		
		String cronStr = (String) properties.get(KEY_POLL_SCHEDULE);
		if(cronStr == null) {
			return;
		}
		
		this.transaction = this.persistencyService.openTransaction();
				
		new Thread(new Runnable() {
			@Override
			public void run() {
				Runnable connectorRunnable = new ConnectorJobRunnable(requirementsSources, transaction, logService);
				connectorRunnable.run();
				
				Scheduler scheduler = new Scheduler();
				scheduler.schedule(cronStr, connectorRunnable);
				scheduler.start();
			}
		}, "connector-service-initializer").start();
		
	}

	private void validateConfig(Map<String, Object> properties) throws SpecmateValidationException {
		String cronStr = (String) properties.get(KEY_POLL_SCHEDULE);
		if(!SchedulingPattern.validate(cronStr)) {
			String message = "Cron " + cronStr + " invalid!";
			logService.log(LogService.LOG_ERROR, message);
			throw new SpecmateValidationException(message);
			
		}
		logService.log(LogService.LOG_DEBUG, "Connector service config validated.");
	}

	@Deactivate
	public void deactivate() {
		this.scheduler.shutdown();
		transaction.close();
	}

{'fraction_non_alphanumeric': 0.09441301272984441, 'fraction_numerical': 0.0007072135785007072, 'mean_word_length': 6.433639947437582, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '423362', 'n_tokens_mistral': 1747, 'n_tokens_neox': 1621, 'n_words': 293}