<commit_before>package wicket.spring.injection.annot;

import java.io.FileNotFoundException;
import java.io.Serializable;

import javax.servlet.ServletContext;

import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;
import org.springframework.util.ResourceUtils;
import org.springframework.web.context.support.WebApplicationContextUtils;

import wicket.Application;
import wicket.MetaDataKey;
import wicket.Session;
import wicket.application.IComponentInstantiationListener;
import wicket.injection.ComponentInjector;
import wicket.injection.web.InjectorHolder;
import wicket.model.Model;
import wicket.protocol.http.WebApplication;
import wicket.protocol.http.portlet.PortletApplication;
import wicket.spring.ISpringContextLocator;

/**
 * {@link IComponentInstantiationListener} that injects component properties
 * annotated with {@link SpringBean} annotations.
 * 
 * To install in yourapplication.init() call
 * <code>addComponentInstantiationListener(new SpringComponentInjector(this));</code>
 * 
 * Non-wicket components such as {@link Session}, {@link Model}, and any other
 * pojo can be injected by calling
 * <code>InjectorHolder.getInjector().inject(this)</code> in their
 * constructor.
 * 
 * @author <NAME> (ivaynberg)
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * 
 */
public class SpringComponentInjector extends ComponentInjector {

	/**
	 * Metadata key used to store application context holder in application's
	 * metadata
	 */
	private static MetaDataKey CONTEXT_KEY = new MetaDataKey(
			ApplicationContextHolder.class) {

		private static final long serialVersionUID = 1L;

	};

	/**
	 * Constructor for web appliactions
	 * 
	 * @param webapp
	 */
	public SpringComponentInjector(WebApplication webapp) {
		// locate spring's application context ...
		ServletContext sc = webapp.getWicketServlet().getServletContext();

		final ApplicationContext ctx = WebApplicationContextUtils
				.getRequiredWebApplicationContext(sc);

		// ... stash the holder to the context in the application's metadata ...
		webapp.setMetaData(CONTEXT_KEY, new ApplicationContextHolder(ctx));

		// ... and create and register the annotation aware injector
		InjectorHolder
				.setInjector(new AnnotSpringInjector(new ContextLocator()));
	}

	/**
	 * Constructor for portlet applications
	 * 
	 * @param portletapp
	 */
	public SpringComponentInjector(PortletApplication portletapp) {
		GenericApplicationContext ctx = new GenericApplicationContext();

		// locate spring's application context ...
		String configLocation = portletapp.getWicketPortlet().getInitParameter(
				"contextConfigLocation");
		Resource resource = null;
		try {
			resource = new UrlResource(ResourceUtils.getURL(configLocation));
		} catch (FileNotFoundException e) {
			throw new RuntimeException(e.getMessage());
		}

		new XmlBeanDefinitionReader(ctx).loadBeanDefinitions(resource);
		ctx.refresh();<commit_msg>allow user to provide applicationcontext explicitly<commit_after>package wicket.spring.injection.annot;

import java.io.FileNotFoundException;
import java.io.Serializable;

import javax.servlet.ServletContext;

import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;
import org.springframework.util.ResourceUtils;
import org.springframework.web.context.support.WebApplicationContextUtils;

import wicket.Application;
import wicket.MetaDataKey;
import wicket.Session;
import wicket.application.IComponentInstantiationListener;
import wicket.injection.ComponentInjector;
import wicket.injection.web.InjectorHolder;
import wicket.model.Model;
import wicket.protocol.http.WebApplication;
import wicket.protocol.http.portlet.PortletApplication;
import wicket.spring.ISpringContextLocator;

/**
 * {@link IComponentInstantiationListener} that injects component properties
 * annotated with {@link SpringBean} annotations.
 * 
 * To install in yourapplication.init() call
 * <code>addComponentInstantiationListener(new SpringComponentInjector(this));</code>
 * 
 * Non-wicket components such as {@link Session}, {@link Model}, and any other
 * pojo can be injected by calling
 * <code>InjectorHolder.getInjector().inject(this)</code> in their
 * constructor.
 * 
 * @author <NAME> (ivaynberg)
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * 
 */
public class SpringComponentInjector extends ComponentInjector {

	/**
	 * Metadata key used to store application context holder in application's
	 * metadata
	 */
	private static MetaDataKey CONTEXT_KEY = new MetaDataKey(
			ApplicationContextHolder.class) {

		private static final long serialVersionUID = 1L;

	};

	/**
	 * Constructor used when spring application context is declared in the
	 * spring standard way and can be located through
	 * {@link WebApplicationContextUtils#getRequiredWebApplicationContext(ServletContext)}
	 * 
	 * @param webapp
	 *            wicket web application
	 */
	public SpringComponentInjector(WebApplication webapp) {
		// locate application context through spring's default location
		// mechanism and pass it on to the proper constructor
		this(webapp, WebApplicationContextUtils
				.getRequiredWebApplicationContext(webapp.getWicketServlet()
						.getServletContext()));
	}

	/**
	 * Constructor
	 * 
	 * @param webapp
	 *            wicket web application
	 * @param ctx
	 *            spring's application context
	 */
	public SpringComponentInjector(WebApplication webapp, ApplicationContext ctx) {
		if (webapp == null) {
			throw new IllegalArgumentException(
					"Argument [[webapp]] cannot be null");
		}

		if (ctx == null) {
			throw new IllegalArgumentException(
					"Argument [[ctx]] cannot be null");
		}

		// store context in application's metadata ...
		webapp.setMetaData(CONTEXT_KEY, new ApplicationContextHolder(ctx));

		// ... and create and register the annotation aware injector
		InjectorHolder
				.setInjector(new AnnotSpringInjector(new ContextLocator()));
	}

	/**
	 * Constructor for portlet applications
	 * 
	 * @param portletapp
	 */
	public SpringComponentInjector(PortletApplication portletapp) {
		GenericApplicationContext ctx = new GenericApplicationContext();

		// locate spring's application context ...
		String configLocation = portletapp.getWicketPortlet().getInitParameter(
				"contextConfigLocation");
		Resource resource = null;
		try {
			resource = new UrlResource(ResourceUtils.getURL(configLocation));
		} catch (FileNotFoundException e) {
			throw new RuntimeException(e.getMessage());
		}

		new XmlBeanDefinitionReader(ctx).loadBeanDefinitions(resource);
		ctx.refresh();
{'fraction_non_alphanumeric': 0.0946372239747634, 'fraction_numerical': 0.00028677946659019213, 'mean_word_length': 5.118421052631579, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '1798383', 'n_tokens_mistral': 1981, 'n_tokens_neox': 1843, 'n_words': 464}