<commit_before>package net.anotheria.moskito.core.util;

import net.anotheria.moskito.core.predefined.OSStats;
import net.anotheria.moskito.core.producers.IStats;
import net.anotheria.moskito.core.producers.IStatsProducer;
import net.anotheria.moskito.core.registry.ProducerRegistryFactory;
import org.apache.log4j.Logger;

import java.lang.management.ManagementFactory;
import java.lang.management.OperatingSystemMXBean;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.TimerTask;

/**
 * Builtin producer for values supplied by jmx for the operation system.
 * @author lrosenberg
 */
public class BuiltInOSProducer extends AbstractBuiltInProducer implements IStatsProducer, BuiltInProducer{
	/**
	 * Associated stats.
	 */
	private OSStats stats;
	/**
	 * Stats container
	 */
	private List<IStats> statsList;

	/**
	 * The monitored pool.
	 */
	private OperatingSystemMXBean mxBean;

	/**
	 * Name of the mxbean class.
	 */
	private static final String clazzname = "com.sun.management.UnixOperatingSystemMXBean";

	/**
	 * Resolved class of the mx bean.
	 */
	private Class<?> clazz;

	private final boolean isUnixOS;
	
	/**
	 * Logger.
	 */
	private static Logger log = Logger.getLogger(BuiltInOSProducer.class);
	
	public BuiltInOSProducer(){
		mxBean = ManagementFactory.getOperatingSystemMXBean();
		statsList = new ArrayList<IStats>(1);
		stats = new OSStats();
		statsList.add(stats);
		
		try{
			clazz = Class.forName(clazzname);
		}catch(ClassNotFoundException e){
			log.warn("Couldn't find unix version of os class: "+clazzname+", osstats won't operate properly.");
		}

		isUnixOS = mxBean.getClass().getName().equals("com.sun.management.UnixOperatingSystem");
		
        if (!isUnixOS) {
            log.warn("Couldn't find unix version of os class: " + clazzname
                    + ", osstats won't operate properly. Current type is: "
                    + mxBean.getClass().getName());
        }

		BuiltinUpdater.addTask(new TimerTask() {
			@Override
			public void run() {
				readMbean();
			}});

		ProducerRegistryFactory.getProducerRegistryInstance().registerProducer(this);
	}
	
	@Override
	public String getCategory() {
		return "os";
	}

	@Override
	public String getProducerId() {
		return "OS";
	}

	@Override
	public List<IStats> getStats() {
		return statsList;
	}

	@Override
	public String getSubsystem() {
		return SUBSYSTEM_BUILTIN;
	}

	/**
	 * Reads the management bean and extracts monitored data on regular base.
	 */
	private void readMbean() {
		if (clazz==null){
			log.warn("couldn't find instance of "+clazzname+", can't call methods.");<commit_msg>do not try to update os stats if sun management class not found<commit_after>package net.anotheria.moskito.core.util;

import net.anotheria.moskito.core.predefined.OSStats;
import net.anotheria.moskito.core.producers.IStats;
import net.anotheria.moskito.core.producers.IStatsProducer;
import net.anotheria.moskito.core.registry.ProducerRegistryFactory;
import org.apache.log4j.Logger;

import java.lang.management.ManagementFactory;
import java.lang.management.OperatingSystemMXBean;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.TimerTask;

/**
 * Builtin producer for values supplied by jmx for the operation system.
 * @author lrosenberg
 */
public class BuiltInOSProducer extends AbstractBuiltInProducer implements IStatsProducer, BuiltInProducer{
	/**
	 * Associated stats.
	 */
	private OSStats stats;
	/**
	 * Stats container
	 */
	private List<IStats> statsList;

	/**
	 * The monitored pool.
	 */
	private OperatingSystemMXBean mxBean;

	/**
	 * Name of the mxbean class.
	 */
	private static final String clazzname = "com.sun.management.UnixOperatingSystemMXBean";

	/**
	 * Resolved class of the mx bean.
	 */
	private Class<?> clazz;

	/**
	 * If true indicates.
	 */
	private final boolean isUnixOS;
	
	/**
	 * Logger.
	 */
	private static Logger log = Logger.getLogger(BuiltInOSProducer.class);
	
	public BuiltInOSProducer(){
		mxBean = ManagementFactory.getOperatingSystemMXBean();
		statsList = new ArrayList<IStats>(1);
		stats = new OSStats();
		statsList.add(stats);
		
		try{
			clazz = Class.forName(clazzname);
		}catch(ClassNotFoundException e){
			log.warn("Couldn't find unix version of os class: "+clazzname+", osstats won't operate properly - "+e.getMessage());
		}

		isUnixOS = mxBean.getClass().getName().equals("com.sun.management.UnixOperatingSystem");
		
        if (!isUnixOS) {
            log.warn("Couldn't find unix version of os class: " + clazzname
                    + ", osstats won't operate properly. Current type is: "
                    + mxBean.getClass().getName());
        }

		BuiltinUpdater.addTask(new TimerTask() {
			@Override
			public void run() {
				readMbean();
			}});

		ProducerRegistryFactory.getProducerRegistryInstance().registerProducer(this);
	}
	
	@Override
	public String getCategory() {
		return "os";
	}

	@Override
	public String getProducerId() {
		return "OS";
	}

	@Override
	public List<IStats> getStats() {
		return statsList;
	}

	@Override
	public String getSubsystem() {
		return SUBSYSTEM_BUILTIN;
	}

	/**
	 * Reads the management bean and extracts monitored data on regular base.
	 */
	private void readMbean() {
		if (clazz==null){
{'fraction_non_alphanumeric': 0.1140593329648056, 'fraction_numerical': 0.0007370554634236226, 'mean_word_length': 3.8681614349775786, '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': '5045685', 'n_tokens_mistral': 1826, 'n_tokens_neox': 1678, 'n_words': 401}