All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jlot.stats.client.StatsConfig Maven / Gradle / Ivy

package org.jlot.stats.client;

import java.util.Properties;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import org.jlot.client.config.JlotClient;
import org.jlot.client.remote.StatsPushRestCommand;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class StatsConfig
{
	private String						server;
	private String						projectName;
	private String						username;
	private String						password;
	private StatsDumper					statsDumper;
	private long						delayInSeconds	= 30;
	private JlotClient					jlotClient		= new JlotClient();
	private ScheduledExecutorService	executor;
	private static final Logger			logger			= LoggerFactory.getLogger(StatsConfig.class);
	private Boolean						started			= false;

	public void stop ( )
	{
		synchronized (started)
		{
			if (!started)
			{
				logger.info("Can't stop JlotStats, it was already stopped");
				return;
			}
			MessageSourceStatsAspect.setActive(false);
			if (jlotClient.isStarted())
			{
				jlotClient.stop();
			}
			if (executor != null)
			{
				executor.shutdown();
			}
			started = false;
			logger.info("Jlot Stats stopped");
		}
	}

	public void start ( )
	{
		synchronized (started)
		{
			if (started)
			{
				logger.info("Can't start JlotStats, it was already started");
				return;
			}
			MessageSourceStatsAspect.setActive(true);
			if (!jlotClient.isStarted())
			{
				jlotClient.start(getProperties());
			}
			if (executor != null)
			{
				executor.shutdown();
			}
			StatsPushRestCommand statsPushRestCommand = jlotClient.getRestCommand(StatsPushRestCommand.class);
			StatsHarvester statsHarvester = MessageSourceStatsAspect.getStatsHarvester();
			this.statsDumper = new StatsDumper(projectName, statsHarvester, statsPushRestCommand);
			executor = Executors.newScheduledThreadPool(1);
			executor.scheduleWithFixedDelay(statsDumper, delayInSeconds, delayInSeconds, TimeUnit.SECONDS);
			started = true;
			logger.info("Jlot Stats started");
		}
	}

	public long getDelayInSeconds ( )
	{
		return delayInSeconds;
	}

	public void setDelayInSeconds ( long delayInSeconds )
	{
		this.delayInSeconds = delayInSeconds;
	}

	public String getServer ( )
	{
		return server;
	}

	public void setServer ( String server )
	{
		this.server = server;
	}

	public String getProjectName ( )
	{
		return projectName;
	}

	public void setProjectName ( String projectName )
	{
		this.projectName = projectName;
	}

	public String getUsername ( )
	{
		return username;
	}

	public void setUsername ( String username )
	{
		this.username = username;
	}

	public String getPassword ( )
	{
		return password;
	}

	public void setPassword ( String password )
	{
		this.password = password;
	}

	public Properties getProperties ( )
	{
		// server.https\://localhost\:8444/[email protected]
		// server.https\://localhost\:8444/.password=*******

		Properties properties = new Properties();
		String serverPrefix = String.format("server.%s.", getServer());
		properties.put(serverPrefix + "password", getPassword());
		properties.put(serverPrefix + "email", getUsername());
		properties.put("projectName", getProjectName());
		properties.put("serverUrl", getServer());
		return properties;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy