<commit_before>/*
 * dsutils - a collection of generally useful utility classes
 *
 * Copyright (c) 2001-2006 <NAME>
 * 418 Richmond St., El Cerrito, CA  94530
 * <EMAIL>
 *
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or (at
 * your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307,
 * USA.
 *
 */

package com.davidsoergel.dsutils;

import org.apache.log4j.Logger;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @author lorax
 * @version 1.0
 */
public class PluginManager<T>
	{
	private static Logger logger = Logger.getLogger(PluginManager.class);

	private static ThreadLocal<Map<Class, PluginManager>> _managers_tl = new ThreadLocal<Map<Class, PluginManager>>();

	public static <T> PluginManager<T> getManagerForInterface(Class T)
		{
		Map<Class, PluginManager> _managers = _managers_tl.get();
		if (_managers == null)
			{
			_managers = new HashMap<Class, PluginManager>();
			_managers_tl.set(_managers);
			}
		PluginManager<T> result = _managers.get(T);
		if (result == null)
			{
			result = new PluginManager<T>(T);
			_managers.put(T, result);
			}
		return result;
		}

	/*
	 * Note plugins are registered only within a thread!
	 */
	public static <T> void registerPackage(String packagename, Class T)
		{
		PluginManager<T> m = getManagerForInterface(T);
		m.registerPackage(packagename);
		}


	public static <T> T getSingletonByName(Class T, String s) throws PluginException
		{
		return (T) getManagerForInterface(T).getSingletonByName(s);
		}


	public static Class getClassByName(Class T, String s) throws PluginException
		{
		return getManagerForInterface(T).getClassByName(s);
		}


	public static Set<String> getKeySet(Class T) throws PluginException
		{
		return getManagerForInterface(T).getKeySet();
		}


	public static <T> T getNewInstanceByName(Class T, String s) throws PluginException
		{
		return (T) getManagerForInterface(T).getNewInstanceByName(s);
		}

	private Class theInterface;
	private HashMap<String, Class> classes = new HashMap<String, Class>();
	private HashMap<String, T> instances = new HashMap<String, T>();

	public PluginManager(Class T)
		{
		theInterface = T;
		}

	public void registerPackage(String packagename)
		{
		for (Class c : SubclassFinder.findRecursive(packagename, theInterface))
			{
			//put((String)dm.getMethod("getName").invoke(), dm);
			try
				{
				classes.put(c.getSimpleName(), c);
				instances.put(c.getSimpleName(), (T) c.newInstance());
				}
			catch (InstantiationException e)
				{
				logger.debug(e);
				}
			catch (IllegalAccessException e)
				{
				logger.debug(e);
				}
			}
		}


	public T getSingletonByName(String s) throws PluginException
		{
		T result = instances.get(s);
		if (result == null)
			{
			throw new PluginException("Can't find plugin " + s + ".  Available plugins of type "
					+ theInterface.getSimpleName() + ": \n" + org.apache.commons.lang.StringUtils
					.join(instances.keySet().iterator(), "\n"));
			}
		return result;
		}


	public Class getClassByName(String s) throws PluginException
		{
		Class result = classes.get(s);
		if (result == null)
			{
			System.err.println("Classes: " + classes);
			System.err.println("Classpath: " + System.getProperty("java.class.path"));


			ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();<commit_msg>Jandy MVC and UI actions mostly working<commit_after>/*
 * dsutils - a collection of generally useful utility classes
 *
 * Copyright (c) 2001-2006 <NAME>
 * 418 Richmond St., El Cerrito, CA  94530
 * <EMAIL>
 *
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or (at
 * your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307,
 * USA.
 *
 */

package com.davidsoergel.dsutils;

import org.apache.log4j.Logger;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @author lorax
 * @version 1.0
 */
public class PluginManager<T>
	{
	private static Logger logger = Logger.getLogger(PluginManager.class);

	private static ThreadLocal<Map<Class, PluginManager>> _managers_tl = new ThreadLocal<Map<Class, PluginManager>>();

	public static <T> PluginManager<T> getManagerForInterface(Class T)
		{
		Map<Class, PluginManager> _managers = _managers_tl.get();
		if (_managers == null)
			{
			_managers = new HashMap<Class, PluginManager>();
			_managers_tl.set(_managers);
			}
		PluginManager<T> result = _managers.get(T);
		if (result == null)
			{
			result = new PluginManager<T>(T);
			_managers.put(T, result);
			}
		return result;
		}

	/*
	 * Note plugins are registered only within a thread!
	 */
	public static <T> void registerPackage(String packagename, Class T)
		{
		PluginManager<T> m = getManagerForInterface(T);
		m.registerPackage(packagename);
		}


	public static <T> T getSingletonByName(Class T, String s) throws PluginException
		{
		return (T) getManagerForInterface(T).getSingletonByName(s);
		}


	public static Class getClassByName(Class T, String s) throws PluginException
		{
		return getManagerForInterface(T).getClassByName(s);
		}


	public static Set<String> getKeySet(Class T) throws PluginException
		{
		return getManagerForInterface(T).getKeySet();
		}


	public static <T> T getNewInstanceByName(Class T, String s) throws PluginException
		{
		return (T) getManagerForInterface(T).getNewInstanceByName(s);
		}

	private Class theInterface;
	private HashMap<String, Class> classes = new HashMap<String, Class>();
	private HashMap<String, T> instances = new HashMap<String, T>();

	public PluginManager(Class T)
		{
		theInterface = T;
		}

	public void registerPackage(String packagename)
		{
		for (Class c : SubclassFinder.findRecursive(packagename, theInterface))
			{
			classes.put(c.getSimpleName(), c);
			//put((String)dm.getMethod("getName").invoke(), dm);
			/*try
				{
				classes.put(c.getSimpleName(), c);
				//instances.put(c.getSimpleName(), (T) c.newInstance());
				}
			catch (InstantiationException e)
				{
				logger.debug(e);
				}
			catch (IllegalAccessException e)
				{
				logger.debug(e);
				}*/
			}
		}


	public T getSingletonByName(String s) throws PluginException
		{
		T result = instances.get(s);
		if (result == null)
			{
			Class c = classes.get(s);
			if (c != null)
				{
				try
					{
					instances.put(c.getSimpleName(), (T) c.newInstance());
					result = instances.get(s);
					}
				catch (InstantiationException e)
					{
					logger.debug(e);
					}
				catch (IllegalAccessException e)
					{
					logger.debug(e);
					}
				}
			else
				{
				throw new PluginException("Can't find plugin " + s + ".  Available plugins of type "
						+ theInterface.getSimpleName() + ": \n" + org.apache.commons.lang.StringUtils
						.join(classes.keySet().iterator(), "\n"));
				}
			}
		return result;
		}


	public Class getClassByName(String s) throws PluginException
		{
		Class result = classes.get(s);
		if (result == null)
			{
			System.err.println("Classes: " + classes);
			System.err.println("Classpath: " + System.getProperty("java.class.path"));


			ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
{'fraction_non_alphanumeric': 0.11353238622682399, 'fraction_numerical': 0.00842764266795088, 'mean_word_length': 3.8156521739130436, 'pattern_counts': {'":': 2, '<': 47, '<?xml version=': 0, '>': 47, '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': '6717545', 'n_tokens_mistral': 2951, 'n_tokens_neox': 2543, 'n_words': 746}