<commit_before>/**
 * The contents of this file are subject to the OpenMRS Public License
 * Version 1.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://license.openmrs.org
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations
 * under the License.
 *
 * Copyright (C) OpenMRS, LLC.  All Rights Reserved.
 */
package org.openmrs.module.web;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.StringReader;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Vector;
import java.util.Map.Entry;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.log4j.xml.DOMConfigurator;
import org.openmrs.module.Module;
import org.openmrs.module.ModuleException;
import org.openmrs.module.ModuleFactory;
import org.openmrs.module.ModuleUtil;
import org.openmrs.util.OpenmrsUtil;
import org.openmrs.web.DispatcherServlet;
import org.openmrs.web.dwr.OpenmrsDWRServlet;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.context.support.XmlWebApplicationContext;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

public class WebModuleUtil {
	
	private static Log log = LogFactory.getLog(WebModuleUtil.class);
	
	private static DispatcherServlet dispatcherServlet = null;
	private static OpenmrsDWRServlet dwrServlet = null;
	
	// caches all of the modules' mapped servlets
	private static Map<Module, Map<String, HttpServlet>> moduleServlets = Collections.synchronizedMap(new HashMap<Module, Map<String, HttpServlet>>());
	
	/**
	 * Performs the webapp specific startup needs for modules
	 * 
	 * Normal startup is done in {@link ModuleFactory#startModule(Module)}
	 * 
	 * If delayContextRefresh is true, the spring context is not rerun.  This will
	 * save a lot of time, but it also means that the calling method is responsible
	 * for restarting the context if necessary.  If delayContextRefresh is true and 
	 * this module should have caused a context refresh, a true value is returned.
	 * Otherwise, false is returned
	 * 
	 * @param mod Module to start
	 * @param ServletContext the current ServletContext
	 * @param delayContextRefresh true/false whether or not to do the context refresh
	 * 
	 * @return boolean whether or not the spring context need to be refreshed
	 */
	public static boolean startModule(Module mod, ServletContext servletContext, boolean delayContextRefresh) {
		if (log.isDebugEnabled())
			log.debug("trying to start module " + mod);
		
		// only try and start this module if the api started it without a problem.
		if (ModuleFactory.isModuleStarted(mod) && !mod.hasStartupError()) {
			
			String realPath = servletContext.getRealPath("");
			
			// copy the messages into the webapp
			String path = "/WEB-INF/module_messages@LANG@.properties";
			
			for (Entry<String, Properties> entry : mod.getMessages().entrySet()) {
				if (log.isDebugEnabled())
					log.debug("Copying message property file: " + entry.getKey());<commit_msg>-- openmrs trunk<commit_after>/**
 * The contents of this file are subject to the OpenMRS Public License
 * Version 1.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://license.openmrs.org
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations
 * under the License.
 *
 * Copyright (C) OpenMRS, LLC.  All Rights Reserved.
 */
package org.openmrs.module.web;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.StringReader;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Vector;
import java.util.Map.Entry;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.log4j.xml.DOMConfigurator;
import org.openmrs.module.Module;
import org.openmrs.module.ModuleException;
import org.openmrs.module.ModuleFactory;
import org.openmrs.module.ModuleUtil;
import org.openmrs.util.OpenmrsUtil;
import org.openmrs.web.DispatcherServlet;
import org.openmrs.web.dwr.OpenmrsDWRServlet;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.context.support.XmlWebApplicationContext;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

public class WebModuleUtil {
	
	private static Log log = LogFactory.getLog(WebModuleUtil.class);
	
	private static DispatcherServlet dispatcherServlet = null;
	private static OpenmrsDWRServlet dwrServlet = null;
	
	// caches all of the modules' mapped servlets
	private static Map<Module, Map<String, HttpServlet>> moduleServlets = Collections.synchronizedMap(new HashMap<Module, Map<String, HttpServlet>>());
	
	/**
	 * Performs the webapp specific startup needs for modules
	 * 
	 * Normal startup is done in {@link ModuleFactory#startModule(Module)}
	 * 
	 * If delayContextRefresh is true, the spring context is not rerun.  This will
	 * save a lot of time, but it also means that the calling method is responsible
	 * for restarting the context if necessary.  If delayContextRefresh is true and 
	 * this module should have caused a context refresh, a true value is returned.
	 * Otherwise, false is returned
	 * 
	 * @param mod Module to start
	 * @param ServletContext the current ServletContext
	 * @param delayContextRefresh true/false whether or not to do the context refresh
	 * 
	 * @return boolean whether or not the spring context need to be refreshed
	 */
	public static boolean startModule(Module mod, ServletContext servletContext, boolean delayContextRefresh) {
		//register the module loggers
		if (mod.getLog4j() != null) {
			DOMConfigurator.configure(mod.getLog4j().getDocumentElement());
		}
		
		if (log.isDebugEnabled())
			log.debug("trying to start module " + mod);
		
		// only try and start this module if the api started it without a
		// problem.
		if (ModuleFactory.isModuleStarted(mod) && !mod.hasStartupError()) {
			
			String realPath = servletContext.getRealPath("");
			
			// copy the messages into the webapp
			String path = "/WEB-INF/module_messages@LANG@.properties";
			
			for (Entry<String, Properties> entry : mod.getMessages().entrySet()) {
				if (log.isDebugEnabled())
					log.debug("Copying message property file: " + entry.getKey());
{'fraction_non_alphanumeric': 0.09206623826000988, 'fraction_numerical': 0.0022244191794364805, 'mean_word_length': 4.6872803935347855, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 12}, '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': '819794', 'n_tokens_mistral': 2429, 'n_tokens_neox': 2299, 'n_words': 737}