<commit_before>package info.ganglia.jmxetric;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.lang.management.ManagementFactory;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Vector;

import javax.management.AttributeNotFoundException;
import javax.management.InstanceNotFoundException;
import javax.management.IntrospectionException;
import javax.management.MBeanAttributeInfo;
import javax.management.MBeanException;
import javax.management.MBeanInfo;
import javax.management.MBeanServer;
import javax.management.ObjectInstance;
import javax.management.ObjectName;
import javax.management.ReflectionException;
import javax.management.RuntimeMBeanException;
import javax.management.openmbean.CompositeData;
import javax.management.openmbean.CompositeType;

/**
 * A utility class that scans the platform MBeanServer for registered
 * MBeans/MXBeans. The MBeans are queried and represented as private objects
 * These objects are then written using {@link ConfigWriter} to a
 * {@link java.io.PrintStream}.
 * 
 * @see Config
 * @see MBeanConfig
 * @see MBeanAttributeConfig
 * @see MBeanCompositeConfig
 * @see ConfigWriter
 */
public class MBeanScanner {
	private static final String ERR_FILE = "%s can not be written to, using System.out instead.\n";
	/* Platform MBean Server */
	private final MBeanServer mBeanServer = ManagementFactory
			.getPlatformMBeanServer();

	/**
	 * Used mainly for testing purposed to output a test configuration file to
	 * System.out. Also shows how to use this class.
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		PrintStream out = System.out;
		if (args.length > 0) {
			try {
				out = new PrintStream(new File(args[0]));
			} catch (FileNotFoundException e) {
				System.out.printf(ERR_FILE, args[0]);
			}
		}
		MBeanScanner mBeanScanner = new MBeanScanner();
		List<Config> configs = mBeanScanner.scan();
		ConfigWriter cw;
		cw = new ConfigWriter(out, configs);
		cw.write();
	}

	/**
	 * Scans the platform MBean server for registered MBeans, creating
	 * 
	 * @see Config objects to represent these MBeans.
	 */
	public List<Config> scan() {
		Set<ObjectInstance> mBeanObjects = mBeanServer.queryMBeans(null, null);
		List<Config> configs = getConfigForAllMBeans(mBeanObjects);
		return configs;
	}

	/**
	 * Constructs a configuration for all MBeans.
	 * 
	 * @param mBeanObjects
	 * @return
	 */
	private List<Config> getConfigForAllMBeans(Set<ObjectInstance> mBeanObjects) {
		List<Config> configs = new Vector<Config>();
		for (ObjectInstance objectInstance : mBeanObjects) {
			Config configMB = scanOneMBeanObject(objectInstance);
			configs.add(configMB);
		}
		return configs;
	}

	/**
	 * Constructs the configuration for a single MBean. The configuration
	 * includes the name, e.g. "java.util.loggin:type=Logging", and the
	 * attributes belonging to that MBean.
	 * 
	 * @param objectInstance
	 *            MBean object instance
	 * @return configuration representing this MBean
	 */
	private Config scanOneMBeanObject(ObjectInstance objectInstance) {
		MBeanConfig mBeanConfig = new MBeanConfig();
		ObjectName objectName = objectInstance.getObjectName();
		mBeanConfig.addField("name", objectName.getCanonicalName());
		scanMBeanAttributes(mBeanConfig, objectName);
		return mBeanConfig;
	}

	/**
	 * Stores all attributes of an MBean into its MBeanConfig object
	 * 
	 * @param mBeanConfig
	 *            the configuration object to store the attributes into
	 * @param mBeanName
	 *            the name of the MBean object we are getting the attributes
	 *            from
	 */
	private void scanMBeanAttributes(MBeanConfig mBeanConfig,
			ObjectName mBeanName) {
		MBeanInfo mBeanInfo;
		try {
			mBeanInfo = mBeanServer.getMBeanInfo(mBeanName);
			MBeanAttributeInfo[] infos = mBeanInfo.getAttributes();
			for (int i = 0; i < infos.length; i++) {
				MBeanAttributeConfig cMBAttr = makeConfigMBeanAttribute(
						mBeanName, infos[i]);
				mBeanConfig.addChild(cMBAttr);
			}
		} catch (IntrospectionException | InstanceNotFoundException
				| ReflectionException e) {
			System.err.println(e.getMessage());
		}
	}

	/**
	 * Creates an object to represent a single attribute of an MBean. An
	 * attribute can be a simple attribute, or made up composites.
	 * 
	 * @param mBeanName
	 * @param attributeInfo
	 * @return
	 */<commit_msg>Replace try-multi-catch with normal try-catch<commit_after>package info.ganglia.jmxetric;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.lang.management.ManagementFactory;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Vector;

import javax.management.AttributeNotFoundException;
import javax.management.InstanceNotFoundException;
import javax.management.IntrospectionException;
import javax.management.MBeanAttributeInfo;
import javax.management.MBeanException;
import javax.management.MBeanInfo;
import javax.management.MBeanServer;
import javax.management.ObjectInstance;
import javax.management.ObjectName;
import javax.management.ReflectionException;
import javax.management.RuntimeMBeanException;
import javax.management.openmbean.CompositeData;
import javax.management.openmbean.CompositeType;

/**
 * A utility class that scans the platform MBeanServer for registered
 * MBeans/MXBeans. The MBeans are queried and represented as private objects
 * These objects are then written using {@link ConfigWriter} to a
 * {@link java.io.PrintStream}.
 * 
 * @see Config
 * @see MBeanConfig
 * @see MBeanAttributeConfig
 * @see MBeanCompositeConfig
 * @see ConfigWriter
 */
public class MBeanScanner {
	private static final String ERR_FILE = "%s can not be written to, using System.out instead.\n";
	/* Platform MBean Server */
	private final MBeanServer mBeanServer = ManagementFactory
			.getPlatformMBeanServer();

	/**
	 * Used mainly for testing purposed to output a test configuration file to
	 * System.out. Also shows how to use this class.
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		PrintStream out = System.out;
		if (args.length > 0) {
			try {
				out = new PrintStream(new File(args[0]));
			} catch (FileNotFoundException e) {
				System.out.printf(ERR_FILE, args[0]);
			}
		}
		MBeanScanner mBeanScanner = new MBeanScanner();
		List<Config> configs = mBeanScanner.scan();
		ConfigWriter cw;
		cw = new ConfigWriter(out, configs);
		cw.write();
	}

	/**
	 * Scans the platform MBean server for registered MBeans, creating
	 * 
	 * @see Config objects to represent these MBeans.
	 */
	public List<Config> scan() {
		Set<ObjectInstance> mBeanObjects = mBeanServer.queryMBeans(null, null);
		List<Config> configs = getConfigForAllMBeans(mBeanObjects);
		return configs;
	}

	/**
	 * Constructs a configuration for all MBeans.
	 * 
	 * @param mBeanObjects
	 * @return
	 */
	private List<Config> getConfigForAllMBeans(Set<ObjectInstance> mBeanObjects) {
		List<Config> configs = new Vector<Config>();
		for (ObjectInstance objectInstance : mBeanObjects) {
			Config configMB = scanOneMBeanObject(objectInstance);
			configs.add(configMB);
		}
		return configs;
	}

	/**
	 * Constructs the configuration for a single MBean. The configuration
	 * includes the name, e.g. "java.util.loggin:type=Logging", and the
	 * attributes belonging to that MBean.
	 * 
	 * @param objectInstance
	 *            MBean object instance
	 * @return configuration representing this MBean
	 */
	private Config scanOneMBeanObject(ObjectInstance objectInstance) {
		MBeanConfig mBeanConfig = new MBeanConfig();
		ObjectName objectName = objectInstance.getObjectName();
		mBeanConfig.addField("name", objectName.getCanonicalName());
		scanMBeanAttributes(mBeanConfig, objectName);
		return mBeanConfig;
	}

	/**
	 * Stores all attributes of an MBean into its MBeanConfig object
	 * 
	 * @param mBeanConfig
	 *            the configuration object to store the attributes into
	 * @param mBeanName
	 *            the name of the MBean object we are getting the attributes
	 *            from
	 */
	private void scanMBeanAttributes(MBeanConfig mBeanConfig,
			ObjectName mBeanName) {
		MBeanInfo mBeanInfo;
		try {
			mBeanInfo = mBeanServer.getMBeanInfo(mBeanName);
			MBeanAttributeInfo[] infos = mBeanInfo.getAttributes();
			for (int i = 0; i < infos.length; i++) {
				MBeanAttributeConfig cMBAttr = makeConfigMBeanAttribute(
						mBeanName, infos[i]);
				mBeanConfig.addChild(cMBAttr);
			}
		} catch (IntrospectionException e) {
			System.err.println(e.getMessage());
		} catch (InstanceNotFoundException e) {
			System.err.println(e.getMessage());
		} catch (ReflectionException e) {
			System.err.println(e.getMessage());
		}
	}

	/**
	 * Creates an object to represent a single attribute of an MBean. An
	 * attribute can be a simple attribute, or made up composites.
	 * 
	 * @param mBeanName
	 * @param attributeInfo
	 * @return
	 */
{'fraction_non_alphanumeric': 0.09400665926748057, 'fraction_numerical': 0.0008879023307436182, 'mean_word_length': 4.214699074074074, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, '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': '5391397', 'n_tokens_mistral': 2843, 'n_tokens_neox': 2600, 'n_words': 746}