<commit_before>package com.github.persapiens.jsfboot.jetty;

import java.io.IOException;
import java.net.URL;
import java.net.URLClassLoader;
import java.security.AccessController;
import java.security.PrivilegedAction;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.util.resource.ResourceCollection;
import org.eclipse.jetty.webapp.WebAppContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.boot.context.embedded.jetty.JettyEmbeddedServletContainerFactory;
import org.springframework.boot.context.embedded.jetty.JettyServerCustomizer;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.context.web.SpringBootServletInitializer;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;

/**
 * Taken from https://github.com/ghillert/spring-boot-jsp-demo/blob/jetty/src/main/java/com/hillert/JspDemoApplication.java#L78
 * and from https://github.com/eclipse/jetty.project/issues/420
 * and from https://github.com/spring-projects/spring-boot/pull/5290
 */
@Configuration
@EnableConfigurationProperties({ JettyProperties.class })
@ConditionalOnClass(JettyEmbeddedServletContainerFactory.class)
public class JettySpringBootAutoConfiguration extends SpringBootServletInitializer implements EmbeddedServletContainerCustomizer {

    @Autowired
    private JettyProperties jettyProperties;
    
	private static final Logger LOGGER = LoggerFactory.getLogger(JettySpringBootAutoConfiguration.class);

	private JettyServerCustomizer jettyServerCustomizer() {
		return new JettyServerCustomizerImpl();
	}

	@Override
	public void customize(ConfigurableEmbeddedServletContainer container) {
		if (container instanceof JettyEmbeddedServletContainerFactory) {
            ((JettyEmbeddedServletContainerFactory) container).addServerCustomizers(jettyServerCustomizer());
		}
	}

    private class JettyServerCustomizerImpl implements JettyServerCustomizer {
        @Override
        public void customize(Server server) {
            Handler[] childHandlersByClass = server.getChildHandlersByClass(WebAppContext.class);
            WebAppContext webAppContext = (WebAppContext) childHandlersByClass[0];
            
            try {
                ClassPathResource classPathResource = new ClassPathResource(jettyProperties.getClassPathResource());
                webAppContext.setBaseResource(new ResourceCollection(classPathResource.getURI().toString()));
                
                AccessController.doPrivileged(new PrivilegedAction<Void>() {
                    @Override
                    public Void run() {<commit_msg>fix #50 changed to @ConditionalOnClass(Server.class)<commit_after>package com.github.persapiens.jsfboot.jetty;

import java.io.IOException;
import java.net.URL;
import java.net.URLClassLoader;
import java.security.AccessController;
import java.security.PrivilegedAction;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.util.resource.ResourceCollection;
import org.eclipse.jetty.webapp.WebAppContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.boot.context.embedded.jetty.JettyEmbeddedServletContainerFactory;
import org.springframework.boot.context.embedded.jetty.JettyServerCustomizer;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.context.web.SpringBootServletInitializer;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;

/**
 * Taken from https://github.com/ghillert/spring-boot-jsp-demo/blob/jetty/src/main/java/com/hillert/JspDemoApplication.java#L78
 * and from https://github.com/eclipse/jetty.project/issues/420
 * and from https://github.com/spring-projects/spring-boot/pull/5290
 */
@Configuration
@EnableConfigurationProperties({ JettyProperties.class })
@ConditionalOnClass(Server.class)
public class JettySpringBootAutoConfiguration extends SpringBootServletInitializer implements EmbeddedServletContainerCustomizer {

    @Autowired
    private JettyProperties jettyProperties;
    
	private static final Logger LOGGER = LoggerFactory.getLogger(JettySpringBootAutoConfiguration.class);

	private JettyServerCustomizer jettyServerCustomizer() {
		return new JettyServerCustomizerImpl();
	}

	@Override
	public void customize(ConfigurableEmbeddedServletContainer container) {
		if (container instanceof JettyEmbeddedServletContainerFactory) {
            ((JettyEmbeddedServletContainerFactory) container).addServerCustomizers(jettyServerCustomizer());
		}
	}

    private class JettyServerCustomizerImpl implements JettyServerCustomizer {
        @Override
        public void customize(Server server) {
            Handler[] childHandlersByClass = server.getChildHandlersByClass(WebAppContext.class);
            WebAppContext webAppContext = (WebAppContext) childHandlersByClass[0];
            
            try {
                ClassPathResource classPathResource = new ClassPathResource(jettyProperties.getClassPathResource());
                webAppContext.setBaseResource(new ResourceCollection(classPathResource.getURI().toString()));
                
                AccessController.doPrivileged(new PrivilegedAction<Void>() {
                    @Override
                    public Void run() {
{'fraction_non_alphanumeric': 0.08318352672005229, 'fraction_numerical': 0.004249060303971237, 'mean_word_length': 5.132264529058117, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 6, 'lorem ipsum': 0, 'www.': 0, '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': '6653876', 'n_tokens_mistral': 1628, 'n_tokens_neox': 1563, 'n_words': 246}