<commit_before>/*
 * (C) Copyright 2013 Kurento (http://kurento.org/)
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser General Public License
 * (LGPL) version 2.1 which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl-2.1.html
 *
 * 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.
 *
 */
package com.kurento.kmf.spring;

import java.io.IOException;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;

import javax.servlet.ServletContext;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor;
import org.springframework.beans.factory.config.PropertyOverrideConfigurer;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.util.Assert;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.ServletContextResource;
import org.springframework.web.context.support.WebApplicationContextUtils;

public final class KurentoApplicationContextUtils {

	private static final Logger log = LoggerFactory
			.getLogger(KurentoApplicationContextUtils.class);

	private static final String KURENTO_SERVLET_CONTEXT_LISTENER_ATTRIBUTE_NAME = KurentoApplicationContextUtils.class
			+ "AttributeName";

	// Is there any better mechanisms for enabling direct recovery of beans
	// (e.g. configurations)
	private static AnnotationConfigApplicationContext kurentoApplicationContextInternalReference;
	private static ConcurrentHashMap<String, AnnotationConfigApplicationContext> childContexts;

	private KurentoApplicationContextUtils() {
	}

	/**
	 * This class returns the Spring KurentoApplicationContext, which is the
	 * parent context for all specific Kurento Servlet contexts. In case a
	 * pre-exiting Spring root WebApplicationContext if found, the returned
	 * KurentoApplicationContext will be made child of this root context. When
	 * necessary, this method creates the KurentoApplicationContext, so it
	 * should never return null.
	 * 
	 * This method MUST NOT be called in ServletContextListeners, given that at
	 * that stage there might not be information about the presence of a root
	 * Spring root WebApplicationConext.
	 * 
	 * @param ctx
	 * @return
	 * 
	 */
	public static AnnotationConfigApplicationContext createKurentoApplicationContext(
			ServletContext ctx) {
		Assert.notNull(ctx,
				"Cannot recover KurentoApplicationContext from a null ServletContext");
		Assert.isNull(kurentoApplicationContextInternalReference,
				"Pre-existing Kurento ApplicationContext found. Cannot create a new instance.");

		kurentoApplicationContextInternalReference = new AnnotationConfigApplicationContext();

		// Add or remove packages when required
		kurentoApplicationContextInternalReference.scan("com.kurento.kmf");

		// Recover root WebApplicationContext context just in case
		// application developer is using Spring
		WebApplicationContext rootContext = WebApplicationContextUtils
				.getWebApplicationContext(ctx);
		if (rootContext != null) {
			kurentoApplicationContextInternalReference.setParent(rootContext);
		}

		// Custom properties
		ServletContextResource servletContextResource = new ServletContextResource(
				ctx, "/WEB-INF/kurento.properties");
		if (servletContextResource.exists()) {
			log.info("Found custom properties (/WEB-INF/kurento.properties)");
			Properties properties = new Properties();
			try {
				properties.load(servletContextResource.getInputStream());
			} catch (IOException e) {
				log.error(
						"Exception loading custom properties (/WEB-INF/kurento.properties)",
						e);
				throw new RuntimeException(e);
			}
			PropertyOverrideConfigurer propertyOverrideConfigurer = new PropertyOverrideConfigurer();
			propertyOverrideConfigurer.setProperties(properties);
			kurentoApplicationContextInternalReference
					.addBeanFactoryPostProcessor(propertyOverrideConfigurer);
		}

		kurentoApplicationContextInternalReference.refresh();
		return kurentoApplicationContextInternalReference;
	}

	public static boolean kurentoApplicationContextExists() {<commit_msg>Overriding kurento.properties location through env variables<commit_after>/*
 * (C) Copyright 2013 Kurento (http://kurento.org/)
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser General Public License
 * (LGPL) version 2.1 which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl-2.1.html
 *
 * 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.
 *
 */
package com.kurento.kmf.spring;

import java.io.IOException;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;

import javax.servlet.ServletContext;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor;
import org.springframework.beans.factory.config.PropertyOverrideConfigurer;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.util.Assert;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.ServletContextResource;
import org.springframework.web.context.support.WebApplicationContextUtils;

public final class KurentoApplicationContextUtils {

	private static final Logger log = LoggerFactory
			.getLogger(KurentoApplicationContextUtils.class);

	private static final String KURENTO_SERVLET_CONTEXT_LISTENER_ATTRIBUTE_NAME = KurentoApplicationContextUtils.class
			+ "AttributeName";

	// Is there any better mechanisms for enabling direct recovery of beans
	// (e.g. configurations)
	private static AnnotationConfigApplicationContext kurentoApplicationContextInternalReference;
	private static ConcurrentHashMap<String, AnnotationConfigApplicationContext> childContexts;

	private KurentoApplicationContextUtils() {
	}

	/**
	 * This class returns the Spring KurentoApplicationContext, which is the
	 * parent context for all specific Kurento Servlet contexts. In case a
	 * pre-exiting Spring root WebApplicationContext if found, the returned
	 * KurentoApplicationContext will be made child of this root context. When
	 * necessary, this method creates the KurentoApplicationContext, so it
	 * should never return null.
	 * 
	 * This method MUST NOT be called in ServletContextListeners, given that at
	 * that stage there might not be information about the presence of a root
	 * Spring root WebApplicationConext.
	 * 
	 * @param ctx
	 * @return
	 * 
	 */
	public static AnnotationConfigApplicationContext createKurentoApplicationContext(
			ServletContext ctx) {
		Assert.notNull(ctx,
				"Cannot recover KurentoApplicationContext from a null ServletContext");
		Assert.isNull(kurentoApplicationContextInternalReference,
				"Pre-existing Kurento ApplicationContext found. Cannot create a new instance.");

		kurentoApplicationContextInternalReference = new AnnotationConfigApplicationContext();

		// Add or remove packages when required
		kurentoApplicationContextInternalReference.scan("com.kurento.kmf");

		// Recover root WebApplicationContext context just in case
		// application developer is using Spring
		WebApplicationContext rootContext = WebApplicationContextUtils
				.getWebApplicationContext(ctx);
		if (rootContext != null) {
			kurentoApplicationContextInternalReference.setParent(rootContext);
		}

		//TODO: first look into env for this variable:
		//Map<String, String> env = System.getenv();
		//env.get("jboss.server.config.dir");
		//if variable exists, look for properties into an input stream associated to a File in that dir.
		//look for env.get("kurento.properties.dir") 
		//if that variable exists, look for properties file there
		//else look for properties file in /WEB-INF using the code below.
		
		//TODO: this makes 3 different ways of creating property files, we must test all of them.
		
		// Custom properties
		ServletContextResource servletContextResource = new ServletContextResource(
				ctx, "/WEB-INF/kurento.properties");
		if (servletContextResource.exists()) {
			log.info("Found custom properties (/WEB-INF/kurento.properties)");
			Properties properties = new Properties();
			try {
				properties.load(servletContextResource.getInputStream());
			} catch (IOException e) {
				log.error(
						"Exception loading custom properties (/WEB-INF/kurento.properties)",
						e);
				throw new RuntimeException(e);
			}
			PropertyOverrideConfigurer propertyOverrideConfigurer = new PropertyOverrideConfigurer();
			propertyOverrideConfigurer.setProperties(properties);
			kurentoApplicationContextInternalReference
					.addBeanFactoryPostProcessor(propertyOverrideConfigurer);
		}

		kurentoApplicationContextInternalReference.refresh();
		return kurentoApplicationContextInternalReference;
	}

	public static boolean kurentoApplicationContextExists() {
{'fraction_non_alphanumeric': 0.07090946843853821, 'fraction_numerical': 0.002180232558139535, 'mean_word_length': 5.283757338551859, 'pattern_counts': {'":': 0, '<': 6, '<?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': '7152374', 'n_tokens_mistral': 2606, 'n_tokens_neox': 2367, 'n_words': 744}