<commit_before>/*
 * $Id$
 * $Revision$ $Date$
 * 
 * ==============================================================================
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package wicket.contrib.groovy;

import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.codehaus.groovy.control.CompilationFailedException;

import wicket.Application;
import wicket.DefaultClassResolver;
import wicket.IClassResolver;
import wicket.WicketRuntimeException;
import wicket.util.listener.IChangeListener;
import wicket.util.resource.IResourceStream;
import wicket.util.watch.ModificationWatcher;
import EDU.oswego.cs.dl.util.concurrent.ConcurrentHashMap;

/**
 * Extends the default Page Factory to allow for Groovy based classes.
 * Modifications to groovy files are tracked and files are reloaded if modified.
 * 
 * @author <NAME>
 */
public class GroovyClassResolver implements IClassResolver
{
	/** Logging */
	private static final Log log = LogFactory.getLog(GroovyClassResolver.class);

	/**
	 * Caching map of class name to groovy class; not sure if GroovyClassLoader
	 * does it as well
	 */
	private final Map classCache = new ConcurrentHashMap();

	/** Default class resolver */
	private final IClassResolver defaultClassResolver = new DefaultClassResolver();

	/** Application */
	private final Application application;

	/**
	 * Constructor
	 * 
	 * @param application
	 *            Application
	 */
	public GroovyClassResolver(final Application application)
	{
		this.application = application;
	}

	/**
	 * Resolve the class for the given classname. First try standard java
	 * classes, then groovy files. Groovy file name must be
	 * &lt;classname&gt;.groovy.
	 * 
	 * @param classname
	 *            The object's class name
	 * @return The class
	 * @see wicket.IClassResolver#resolveClass(String)
	 */
	public Class resolveClass(final String classname)
	{
		try
		{
			return defaultClassResolver.resolveClass(classname);
		}
		catch (WicketRuntimeException ex)
		{
			; // default resolver failed. Try the groovy specific loader next
		}

		// If definition already loaded, ...
		Class groovyPageClass = (Class) classCache.get(classname);
		if (groovyPageClass != null)
		{
			return groovyPageClass;
		}

		// Else, try Groovy.
		final IResourceStream resource = application.getResourceLocator().locate(classname,
				null, null, ".groovy");
		if (resource != null)
		{<commit_msg>Minor name changes missing from resource -> resourcestream refactor.<commit_after>/*
 * $Id$
 * $Revision$ $Date$
 * 
 * ==============================================================================
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package wicket.contrib.groovy;

import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.codehaus.groovy.control.CompilationFailedException;

import wicket.Application;
import wicket.DefaultClassResolver;
import wicket.IClassResolver;
import wicket.WicketRuntimeException;
import wicket.util.listener.IChangeListener;
import wicket.util.resource.IResourceStream;
import wicket.util.watch.ModificationWatcher;
import EDU.oswego.cs.dl.util.concurrent.ConcurrentHashMap;

/**
 * Extends the default Page Factory to allow for Groovy based classes.
 * Modifications to groovy files are tracked and files are reloaded if modified.
 * 
 * @author <NAME>
 */
public class GroovyClassResolver implements IClassResolver
{
	/** Logging */
	private static final Log log = LogFactory.getLog(GroovyClassResolver.class);

	/**
	 * Caching map of class name to groovy class; not sure if GroovyClassLoader
	 * does it as well
	 */
	private final Map classCache = new ConcurrentHashMap();

	/** Default class resolver */
	private final IClassResolver defaultClassResolver = new DefaultClassResolver();

	/** Application */
	private final Application application;

	/**
	 * Constructor
	 * 
	 * @param application
	 *            Application
	 */
	public GroovyClassResolver(final Application application)
	{
		this.application = application;
	}

	/**
	 * Resolve the class for the given classname. First try standard java
	 * classes, then groovy files. Groovy file name must be
	 * &lt;classname&gt;.groovy.
	 * 
	 * @param classname
	 *            The object's class name
	 * @return The class
	 * @see wicket.IClassResolver#resolveClass(String)
	 */
	public Class resolveClass(final String classname)
	{
		try
		{
			return defaultClassResolver.resolveClass(classname);
		}
		catch (WicketRuntimeException ex)
		{
			; // default resolver failed. Try the groovy specific loader next
		}

		// If definition already loaded, ...
		Class groovyPageClass = (Class) classCache.get(classname);
		if (groovyPageClass != null)
		{
			return groovyPageClass;
		}

		// Else, try Groovy.
		final IResourceStream resource = application.getResourceStreamLocator().locate(classname,
				null, null, ".groovy");
		if (resource != null)
		{
{'fraction_non_alphanumeric': 0.11314884558703127, 'fraction_numerical': 0.0013099721630915344, 'mean_word_length': 4.111297071129707, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '2556634', 'n_tokens_mistral': 1800, 'n_tokens_neox': 1635, 'n_words': 553}