<commit_before>package edu.umd.cs.findbugs;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URISyntaxException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.prefs.Preferences;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.annotation.WillClose;

import edu.umd.cs.findbugs.util.MultiMap;
import edu.umd.cs.findbugs.util.Util;
import edu.umd.cs.findbugs.xml.OutputStreamXMLOutput;
import edu.umd.cs.findbugs.xml.XMLOutput;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

class UsageTracker {
    private static final Logger LOGGER = Logger.getLogger(UsageTracker.class.getName());
    private static final String KEY_DISABLE_ALL_USAGE_TRACKING = "disableAllUsageTracking";
    private static final String KEY_REDIRECT_ALL_USAGE_TRACKING = "redirectAllUsageTracking";

    void trackUsage(DetectorFactoryCollection dfc, Collection<Plugin> plugins) {
        if (trackingIsGloballyDisabled(dfc))
            return;

        String redirect = dfc.getGlobalOption(KEY_REDIRECT_ALL_USAGE_TRACKING);
        Plugin setter = dfc.getGlobalOptionSetter(KEY_REDIRECT_ALL_USAGE_TRACKING);
        URI redirectUri = null;
        String pluginName = setter == null ? "<unknown plugin>" : setter.getShortDescription();
        if (redirect != null && !redirect.trim().equals("")) {
            try {
                redirectUri = new URI(redirect);
            } catch (URISyntaxException e) {
                String error = "Invalid usage tracking redirect URI in " + pluginName + ": " + redirect;
                logError(Level.SEVERE, error);
                throw new IllegalStateException(error);
            }
        }

        if (redirectUri != null) {
            logError(Level.INFO, "Redirecting all plugin usage tracking to " + redirectUri + " (" + pluginName + ")");
            trackUsage(redirectUri, plugins);
        } else {
            MultiMap<URI,Plugin> pluginsByTracker = new MultiMap<URI, Plugin>(HashSet.class);
            for (Plugin plugin : plugins) {
                URI uri = plugin.getUsageTracker();
                if (uri == null) {
                    logError(Level.FINE, "Not logging usage for " + plugin.getShortDescription()
                            + " - no usageTracker attribute in plugin XML file");
                    continue;
                }
                pluginsByTracker.add(uri, plugin);
            }
            for (URI uri : pluginsByTracker.keySet()) {
                trackUsage(uri, pluginsByTracker.get(uri));
            }
        }
    }

    private boolean trackingIsGloballyDisabled(DetectorFactoryCollection dfc) {
        String disable = dfc.getGlobalOption(KEY_DISABLE_ALL_USAGE_TRACKING);
        Plugin setter = dfc.getGlobalOptionSetter(KEY_DISABLE_ALL_USAGE_TRACKING);
        String pluginName = setter == null ? "<unknown plugin>" : setter.getShortDescription();
        if ("true".equalsIgnoreCase(disable)) {
            logError(Level.INFO, "Skipping usage tracking due to disableAllUsageTracking=true set by "
                    + pluginName);
            return true;
        }
        if (disable != null && !"false".equalsIgnoreCase(disable)) {
            String error = "Unknown value '" + disable + "' for disableAllUsageTracking in " + pluginName;
            logError(Level.SEVERE, error);
            throw new IllegalStateException(error);
        }
        return false;
    }

    private void trackUsage(final URI trackerUrl, final Collection<Plugin> plugins) {
        if (trackerUrl == null) {
            logError(Level.INFO, "Not submitting usage tracking for plugins with blank URL: " + getPluginNames(plugins));
            return;
        }
        final String entryPoint = getEntryPoint();
        if (entryPoint.contains("edu.umd.cs.findbugs.FindBugsTestCase")
                && (trackerUrl.getScheme().equals("http") || trackerUrl.getScheme().equals("https"))) {
            LOGGER.fine("Skipping usage tracking because we're running in FindBugsTestCase and using "
                    + trackerUrl.getScheme());
            return;
        }
        Thread thread = new Thread(new Runnable() {
            public void run() {
                try {
                    actuallyTrackUsage(trackerUrl, plugins, entryPoint);
                } catch (Exception e) {
                    logError(e, "Error submitting usage tracking data to " + trackerUrl);
                }
            }<commit_msg>Remove more unnecessary usage tracking during unit tests. Feel like there should be a better way to do this.<commit_after>package edu.umd.cs.findbugs;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URISyntaxException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.prefs.Preferences;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.annotation.WillClose;

import edu.umd.cs.findbugs.util.MultiMap;
import edu.umd.cs.findbugs.util.Util;
import edu.umd.cs.findbugs.xml.OutputStreamXMLOutput;
import edu.umd.cs.findbugs.xml.XMLOutput;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

class UsageTracker {
    private static final Logger LOGGER = Logger.getLogger(UsageTracker.class.getName());
    private static final String KEY_DISABLE_ALL_USAGE_TRACKING = "disableAllUsageTracking";
    private static final String KEY_REDIRECT_ALL_USAGE_TRACKING = "redirectAllUsageTracking";

    void trackUsage(DetectorFactoryCollection dfc, Collection<Plugin> plugins) {
        if (trackingIsGloballyDisabled(dfc))
            return;

        String redirect = dfc.getGlobalOption(KEY_REDIRECT_ALL_USAGE_TRACKING);
        Plugin setter = dfc.getGlobalOptionSetter(KEY_REDIRECT_ALL_USAGE_TRACKING);
        URI redirectUri = null;
        String pluginName = setter == null ? "<unknown plugin>" : setter.getShortDescription();
        if (redirect != null && !redirect.trim().equals("")) {
            try {
                redirectUri = new URI(redirect);
            } catch (URISyntaxException e) {
                String error = "Invalid usage tracking redirect URI in " + pluginName + ": " + redirect;
                logError(Level.SEVERE, error);
                throw new IllegalStateException(error);
            }
        }

        if (redirectUri != null) {
            logError(Level.INFO, "Redirecting all plugin usage tracking to " + redirectUri + " (" + pluginName + ")");
            trackUsage(redirectUri, plugins);
        } else {
            MultiMap<URI,Plugin> pluginsByTracker = new MultiMap<URI, Plugin>(HashSet.class);
            for (Plugin plugin : plugins) {
                URI uri = plugin.getUsageTracker();
                if (uri == null) {
                    logError(Level.FINE, "Not logging usage for " + plugin.getShortDescription()
                            + " - no usageTracker attribute in plugin XML file");
                    continue;
                }
                pluginsByTracker.add(uri, plugin);
            }
            for (URI uri : pluginsByTracker.keySet()) {
                trackUsage(uri, pluginsByTracker.get(uri));
            }
        }
    }

    private boolean trackingIsGloballyDisabled(DetectorFactoryCollection dfc) {
        String disable = dfc.getGlobalOption(KEY_DISABLE_ALL_USAGE_TRACKING);
        Plugin setter = dfc.getGlobalOptionSetter(KEY_DISABLE_ALL_USAGE_TRACKING);
        String pluginName = setter == null ? "<unknown plugin>" : setter.getShortDescription();
        if ("true".equalsIgnoreCase(disable)) {
            logError(Level.INFO, "Skipping usage tracking due to disableAllUsageTracking=true set by "
                    + pluginName);
            return true;
        }
        if (disable != null && !"false".equalsIgnoreCase(disable)) {
            String error = "Unknown value '" + disable + "' for disableAllUsageTracking in " + pluginName;
            logError(Level.SEVERE, error);
            throw new IllegalStateException(error);
        }
        return false;
    }

    private void trackUsage(final URI trackerUrl, final Collection<Plugin> plugins) {
        if (trackerUrl == null) {
            logError(Level.INFO, "Not submitting usage tracking for plugins with blank URL: " + getPluginNames(plugins));
            return;
        }
        final String entryPoint = getEntryPoint();
        if ((entryPoint.contains("edu.umd.cs.findbugs.FindBugsTestCase") 
                || entryPoint.contains("edu.umd.cs.findbugs.cloud.appEngine.AbstractWebCloudTest"))
                && (trackerUrl.getScheme().equals("http") || trackerUrl.getScheme().equals("https"))) {
            LOGGER.fine("Skipping usage tracking because we're running in FindBugsTestCase and using "
                    + trackerUrl.getScheme());
            return;
        }
        Thread thread = new Thread(new Runnable() {
            public void run() {
                try {
                    actuallyTrackUsage(trackerUrl, plugins, entryPoint);
                } catch (Exception e) {
                    logError(e, "Error submitting usage tracking data to " + trackerUrl);
                }
            }
{'fraction_non_alphanumeric': 0.09915761696945093, 'fraction_numerical': 0.0006089515883487262, 'mean_word_length': 2.553552109628561, 'pattern_counts': {'":': 2, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7483655', 'n_tokens_mistral': 2725, 'n_tokens_neox': 2634, 'n_words': 609}