<commit_before>package org.eclipse.jetty.nested;

import java.io.File;
import java.io.IOException;

import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.eclipse.jetty.server.LocalConnector;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.bio.SocketConnector;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.thread.ThreadPool;
import org.eclipse.jetty.webapp.WebAppContext;
import org.eclipse.jetty.xml.XmlConfiguration;

/**
 * Nested Jetty Servlet.
 * <p>
 * This servlet runs Jetty as a nested server inside another servlet container.   The requests received by
 * this servlet are routed via a {@link NestedConnector} to the nested jetty servlet and handled by jetty contexts,
 * handlers, webapps and/or servlets.
 * <p>
 * The servlet can be configured with the following init parameters:<ul>
 * <li>debug - if true then jetty debugging is turned on</li>
 * <li>webapp - set to the resource path of the webapplication to deploy
 * <li>jetty.xml - set the the resource path of a jetty xml file used to configure the server
 * </ul>
 *
 */
public class NestedJettyServlet implements Servlet
{
    private Server _server;
    private ServletConfig _config;
    private ServletContext _context;
    private NestedConnector _connector;
    
    public void init(ServletConfig config) throws ServletException
    {    
        ClassLoader orig = Thread.currentThread().getContextClassLoader();
        try
        {
            Thread.currentThread().setContextClassLoader(NestedJettyServlet.class.getClassLoader());
            _config=config;
            _context=config.getServletContext();
            
            Log.getLog().setDebugEnabled(Boolean.parseBoolean(_config.getInitParameter("debug")));
            
            String jetty_xml=config.getInitParameter("jetty.xml");
            if (jetty_xml!=null)
            {
                XmlConfiguration xml_config = new XmlConfiguration(_context.getResourceAsStream(jetty_xml));
                _server=(Server)xml_config.configure();
            }
            if (_server==null)
                _server=new Server();
            
            if (_server.getConnectors().length==0)
            {
                _connector=new NestedConnector();
                _server.addConnector(_connector);
            }
            else
                _connector=(NestedConnector)_server.getConnectors()[0];
            
            SocketConnector socket = new SocketConnector();
            socket.setPort(0);
            _server.addConnector(socket);
            
            WebAppContext webapp = new WebAppContext();
            
            webapp.setContextPath(_context.getContextPath());
            webapp.setTempDirectory(new File((File)_context.getAttribute("javax.servlet.context.tempdir"),"jetty"));
            String docroot=config.getInitParameter("webapp");
           
            String realpath=_context.getRealPath(docroot);
            if (realpath!=null)
                webapp.setWar(realpath);
            else
                webapp.setWar(_context.getResource(docroot).toString());

            _server.setHandler(webapp);

            _server.start();
            _context.log("Started Jetty/"+_server.getVersion()+" for "+webapp.getWar()+" nested in "+_context.getServerInfo());
            webapp.setAttribute("socket",socket.toString());
        }
        catch(Exception e)
        {
            throw new ServletException(e);
        }
        finally
        {
            Thread.currentThread().setContextClassLoader(orig);
        }
    }

    public ServletConfig getServletConfig()
    {
        return _config;
    }

    public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException
    {
        _connector.service(req,res);
    }

    public String getServletInfo()
    {
        return this.toString();
    }

    public void destroy()
    {
        try
        {
            _server.stop();
        }
        catch(Exception e)
        {
            _context.log("stopping",e);
        }
    }
}
<commit_msg>removed debug<commit_after>package org.eclipse.jetty.nested;

import java.io.File;
import java.io.IOException;

import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.eclipse.jetty.server.LocalConnector;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.bio.SocketConnector;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.thread.ThreadPool;
import org.eclipse.jetty.webapp.WebAppContext;
import org.eclipse.jetty.xml.XmlConfiguration;

/**
 * Nested Jetty Servlet.
 * <p>
 * This servlet runs Jetty as a nested server inside another servlet container.   The requests received by
 * this servlet are routed via a {@link NestedConnector} to the nested jetty servlet and handled by jetty contexts,
 * handlers, webapps and/or servlets.
 * <p>
 * The servlet can be configured with the following init parameters:<ul>
 * <li>debug - if true then jetty debugging is turned on</li>
 * <li>webapp - set to the resource path of the webapplication to deploy
 * <li>jetty.xml - set the the resource path of a jetty xml file used to configure the server
 * </ul>
 *
 */
public class NestedJettyServlet implements Servlet
{
    private Server _server;
    private ServletConfig _config;
    private ServletContext _context;
    private NestedConnector _connector;
    
    public void init(ServletConfig config) throws ServletException
    {    
        ClassLoader orig = Thread.currentThread().getContextClassLoader();
        try
        {
            Thread.currentThread().setContextClassLoader(NestedJettyServlet.class.getClassLoader());
            _config=config;
            _context=config.getServletContext();
            
            Log.getLog().setDebugEnabled(Boolean.parseBoolean(_config.getInitParameter("debug")));
            
            String jetty_xml=config.getInitParameter("jetty.xml");
            if (jetty_xml!=null)
            {
                XmlConfiguration xml_config = new XmlConfiguration(_context.getResourceAsStream(jetty_xml));
                _server=(Server)xml_config.configure();
            }
            if (_server==null)
                _server=new Server();
            
            if (_server.getConnectors().length==0)
            {
                _connector=new NestedConnector();
                _server.addConnector(_connector);
            }
            else
                _connector=(NestedConnector)_server.getConnectors()[0];
            
            WebAppContext webapp = new WebAppContext();
            
            webapp.setContextPath(_context.getContextPath());
            webapp.setTempDirectory(new File((File)_context.getAttribute("javax.servlet.context.tempdir"),"jetty"));
            String docroot=config.getInitParameter("webapp");
           
            String realpath=_context.getRealPath(docroot);
            if (realpath!=null)
                webapp.setWar(realpath);
            else
                webapp.setWar(_context.getResource(docroot).toString());

            _server.setHandler(webapp);

            _server.start();
            _context.log("Started Jetty/"+_server.getVersion()+" for "+webapp.getWar()+" nested in "+_context.getServerInfo());
        }
        catch(Exception e)
        {
            throw new ServletException(e);
        }
        finally
        {
            Thread.currentThread().setContextClassLoader(orig);
        }
    }

    public ServletConfig getServletConfig()
    {
        return _config;
    }

    public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException
    {
        _connector.service(req,res);
    }

    public String getServletInfo()
    {
        return this.toString();
    }

    public void destroy()
    {
        try
        {
            _server.stop();
        }
        catch(Exception e)
        {
            _context.log("stopping",e);
        }
    }
}

{'fraction_non_alphanumeric': 0.0956859057246973, 'fraction_numerical': 0.0005877512636652169, 'mean_word_length': 2.4740710494079217, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 18}, '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': '4157345', 'n_tokens_mistral': 2254, 'n_tokens_neox': 2247, 'n_words': 482}