<commit_before>/*
 * PluginModule.java
 *
 * Created on June 16, 2005, 2:36 PM
 */

package uk.co.uwcs.choob.modules;

import uk.co.uwcs.choob.plugins.*;
import uk.co.uwcs.choob.support.*;
import uk.co.uwcs.choob.support.events.*;
import uk.co.uwcs.choob.*;
import java.util.*;
import java.util.concurrent.*;
import java.net.*;
import java.sql.*;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.security.AccessControlException;

/**
 * Module that performs functions relating to the plugin architecture of the bot.
 * @author sadiq
 */
public final class PluginModule
{
	private DbConnectionBroker broker;
	private Modules mods;
	private ChoobPluginManager hsPlugMan;
	private ChoobPluginManager dPlugMan;
	private ChoobPluginManager jsPlugMan;
	private Choob bot;
	private IRCInterface irc;

	/**
	 * Creates a new instance of the PluginModule.
	 * @param pluginMap Map containing currently loaded plugins.
	 */
	PluginModule(Map pluginMap, DbConnectionBroker broker, Modules mods, IRCInterface irc, Choob bot) throws ChoobException {
		this.broker = broker;
		this.mods = mods;
		this.hsPlugMan = new HaxSunPluginManager(mods, irc);
		this.dPlugMan = new ChoobDistributingPluginManager();
		this.jsPlugMan = new JavaScriptPluginManager(mods, irc);
		this.bot = bot;
		this.irc = irc;
	}

	public ChoobPluginManager getPlugMan()
	{
		// XXX Need better permission name.
		AccessController.checkPermission(new ChoobPermission("getPluginManager"));

		return dPlugMan;
	}

	/**
	 * Adds a plugin to the loaded plugin map but first unloads any plugin already there.
	 *
	 * This method also calls the create() method on any new plugin.
	 * @param URL URL to the source of the plugin.
	 * @param pluginName Name for the class of the new plugin.
	 * @throws ChoobException Thrown if there's a syntactical error in the plugin's source.
	 */
	public void addPlugin(final String pluginName, String URL) throws ChoobException {
		final URL srcURL;
		try
		{
			srcURL = new URL(URL);
		}
		catch (MalformedURLException e)
		{
			throw new ChoobException("URL " + URL + " is malformed: " + e);
		}

		// Small hack to allow the ChoobTask to return a value.
		final boolean[] ok = new boolean[] { true };
		
		ChoobTask task = new ChoobTask(null, "getConcurrencyLimit") {
			public void run() {
				try {
					boolean existed;
					if (srcURL.getFile().endsWith(".js"))
						existed = jsPlugMan.loadPlugin(pluginName, srcURL);
					else
						existed = hsPlugMan.loadPlugin(pluginName, srcURL);

					// Inform plugins, if they want to know.
					if (existed)
						bot.onPluginReLoaded(pluginName);
					else
						bot.onPluginLoaded(pluginName);
				} catch (Exception e) {
					// Don't care about anything. Lalala.
					ok[0] = false;
				}
			}
		};
		ChoobThread thread = new ChoobThread(task, "choob-loadPlugin-" + pluginName);
		thread.pushPlugin(pluginName);
		
		thread.run();
		try {
			thread.join();
		} catch (InterruptedException e) {}
		
		if (ok[0])
			addPluginToDb(pluginName, URL);
	}

	/**
	 * Reloads a plugin which has been loaded previously, but may not be loaded currently.
	 *
	 * This method simply looks up the last source URL for the plugin, and calls addPlugin with it.
	 * @param pluginName Name of the plugin to reload.
	 * @throws ChoobException Thrown if there's a syntactical error in the plugin's source.
	 */
	public void reloadPlugin(String pluginName) throws ChoobException {
		String URL = getPluginURL(pluginName);
		pluginName = getPluginName(pluginName); // Fix the case of the param
		if (URL == null)
			throw new ChoobNoSuchPluginException(pluginName);
		addPlugin(pluginName, URL);
	}
<commit_msg>Less copy-paste ktnxbye.<commit_after>/*
 * PluginModule.java
 *
 * Created on June 16, 2005, 2:36 PM
 */

package uk.co.uwcs.choob.modules;

import uk.co.uwcs.choob.plugins.*;
import uk.co.uwcs.choob.support.*;
import uk.co.uwcs.choob.support.events.*;
import uk.co.uwcs.choob.*;
import java.util.*;
import java.util.concurrent.*;
import java.net.*;
import java.sql.*;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.security.AccessControlException;

/**
 * Module that performs functions relating to the plugin architecture of the bot.
 * @author sadiq
 */
public final class PluginModule
{
	private DbConnectionBroker broker;
	private Modules mods;
	private ChoobPluginManager hsPlugMan;
	private ChoobPluginManager dPlugMan;
	private ChoobPluginManager jsPlugMan;
	private Choob bot;
	private IRCInterface irc;

	/**
	 * Creates a new instance of the PluginModule.
	 * @param pluginMap Map containing currently loaded plugins.
	 */
	PluginModule(Map pluginMap, DbConnectionBroker broker, Modules mods, IRCInterface irc, Choob bot) throws ChoobException {
		this.broker = broker;
		this.mods = mods;
		this.hsPlugMan = new HaxSunPluginManager(mods, irc);
		this.dPlugMan = new ChoobDistributingPluginManager();
		this.jsPlugMan = new JavaScriptPluginManager(mods, irc);
		this.bot = bot;
		this.irc = irc;
	}

	public ChoobPluginManager getPlugMan()
	{
		// XXX Need better permission name.
		AccessController.checkPermission(new ChoobPermission("getPluginManager"));

		return dPlugMan;
	}

	/**
	 * Adds a plugin to the loaded plugin map but first unloads any plugin already there.
	 *
	 * This method also calls the create() method on any new plugin.
	 * @param URL URL to the source of the plugin.
	 * @param pluginName Name for the class of the new plugin.
	 * @throws ChoobException Thrown if there's a syntactical error in the plugin's source.
	 */
	public void addPlugin(final String pluginName, String URL) throws ChoobException {
		final URL srcURL;
		try
		{
			srcURL = new URL(URL);
		}
		catch (MalformedURLException e)
		{
			throw new ChoobException("URL " + URL + " is malformed: " + e);
		}

		// Small hack to allow the ChoobTask to return a value.
		final boolean[] ok = new boolean[] { true };
		
		ChoobTask task = new ChoobTask(null, "addPlugin-" + pluginName) {
			public void run() {
				try {
					boolean existed;
					if (srcURL.getFile().endsWith(".js"))
						existed = jsPlugMan.loadPlugin(pluginName, srcURL);
					else
						existed = hsPlugMan.loadPlugin(pluginName, srcURL);

					// Inform plugins, if they want to know.
					if (existed)
						bot.onPluginReLoaded(pluginName);
					else
						bot.onPluginLoaded(pluginName);
				} catch (Exception e) {
					// Don't care about anything. Lalala.
					ok[0] = false;
				}
			}
		};
		ChoobThread thread = new ChoobThread(task, "choob-addPlugin-" + pluginName);
		thread.pushPlugin(pluginName);
		
		thread.run();
		try {
			thread.join();
		} catch (InterruptedException e) {}
		
		if (ok[0])
			addPluginToDb(pluginName, URL);
	}

	/**
	 * Reloads a plugin which has been loaded previously, but may not be loaded currently.
	 *
	 * This method simply looks up the last source URL for the plugin, and calls addPlugin with it.
	 * @param pluginName Name of the plugin to reload.
	 * @throws ChoobException Thrown if there's a syntactical error in the plugin's source.
	 */
	public void reloadPlugin(String pluginName) throws ChoobException {
		String URL = getPluginURL(pluginName);
		pluginName = getPluginName(pluginName); // Fix the case of the param
		if (URL == null)
			throw new ChoobNoSuchPluginException(pluginName);
		addPlugin(pluginName, URL);
	}

{'fraction_non_alphanumeric': 0.0985342019543974, 'fraction_numerical': 0.002985884907709012, 'mean_word_length': 3.7116368286445014, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '5555439', 'n_tokens_mistral': 2570, 'n_tokens_neox': 2217, 'n_words': 729}