<commit_before>package de.elydon.fragments.application.simple.tomcat;

import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.FileVisitor;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributes;

import javax.servlet.ServletException;

import org.apache.catalina.LifecycleException;
import org.apache.catalina.core.StandardHost;
import org.apache.catalina.startup.Tomcat;

/**
 * <p>
 * A {@link Runnable} that starts a {@link Tomcat} server and deploys all WAR
 * archives that are in the current path.
 * </p>
 * 
 * @author elydon
 *
 */
public class TomcatRunnable implements Runnable {

	private Tomcat tomcat;

	@Override
	public void run() {
		tomcat = new Tomcat();

		// do not unpack the WARs, just run them
		((StandardHost) tomcat.getHost()).setUnpackWARs(false);

		// find all WARs and "deploy" them
		try {
			deployWARs();
		} catch (final IOException e) {
			throw new IllegalStateException("Unable to deploy WAR archives", e);
		}

		try {
			tomcat.start();
		} catch (final LifecycleException e) {
			throw new IllegalStateException("Unable to start tomcat", e);
		}
		tomcat.getServer().await();
	}

	private void deployWARs() throws IOException {
		final Path root = Paths.get(".");
		Files.walkFileTree(root, new FileVisitor<Path>() {

			@Override
			public FileVisitResult postVisitDirectory(final Path dir, final IOException exc) throws IOException {
				return FileVisitResult.CONTINUE;
			}

			@Override
			public FileVisitResult preVisitDirectory(final Path dir, final BasicFileAttributes attrs)
					throws IOException {
				if (dir.equals(root)) {
					return FileVisitResult.CONTINUE;
				}
				
				// not scanning recursively
				return FileVisitResult.SKIP_SUBTREE;
			}

			@Override
			public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs) throws IOException {
				// is it a WAR archive?
				if (file.toString().endsWith(".war")) {
					// extract webapp name
					String webappName = file.getFileName().toString();
					webappName = webappName.substring(0, webappName.length() - ".war".length());

					try {
						System.out.println("deploying " + webappName);
						tomcat.addWebapp("/" + webappName, file.toString());
					} catch (final ServletException e) {
						System.err.println("Unable to deploy [" + file + "], reason: " + e.getMessage());
					}<commit_msg>TomcatRunnable now also outputs the absolute file name of the WAR which is deployed<commit_after>package de.elydon.fragments.application.simple.tomcat;

import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.FileVisitor;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributes;

import javax.servlet.ServletException;

import org.apache.catalina.LifecycleException;
import org.apache.catalina.core.StandardHost;
import org.apache.catalina.startup.Tomcat;

/**
 * <p>
 * A {@link Runnable} that starts a {@link Tomcat} server and deploys all WAR
 * archives that are in the current path.
 * </p>
 * 
 * @author elydon
 *
 */
public class TomcatRunnable implements Runnable {

	private Tomcat tomcat;

	@Override
	public void run() {
		tomcat = new Tomcat();

		// do not unpack the WARs, just run them
		((StandardHost) tomcat.getHost()).setUnpackWARs(false);

		// find all WARs and "deploy" them
		try {
			deployWARs();
		} catch (final IOException e) {
			throw new IllegalStateException("Unable to deploy WAR archives", e);
		}

		try {
			tomcat.start();
		} catch (final LifecycleException e) {
			throw new IllegalStateException("Unable to start tomcat", e);
		}
		tomcat.getServer().await();
	}

	private void deployWARs() throws IOException {
		final Path root = Paths.get(".");
		Files.walkFileTree(root, new FileVisitor<Path>() {

			@Override
			public FileVisitResult postVisitDirectory(final Path dir, final IOException exc) throws IOException {
				return FileVisitResult.CONTINUE;
			}

			@Override
			public FileVisitResult preVisitDirectory(final Path dir, final BasicFileAttributes attrs)
					throws IOException {
				if (dir.equals(root)) {
					return FileVisitResult.CONTINUE;
				}
				
				// not scanning recursively
				return FileVisitResult.SKIP_SUBTREE;
			}

			@Override
			public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs) throws IOException {
				// is it a WAR archive?
				String filename = file.toAbsolutePath().normalize().toString();
				if (filename.endsWith(".war")) {
					// extract webapp name
					String webappName = file.getFileName().toString();
					webappName = webappName.substring(0, webappName.length() - ".war".length());

					try {
						System.out.println("deploying " + webappName + " [" + filename + "]");
						tomcat.addWebapp("/" + webappName, filename);
					} catch (final ServletException e) {
						System.err.println("Unable to deploy [" + file + "], reason: " + e.getMessage());
					}
{'fraction_non_alphanumeric': 0.11494252873563218, 'fraction_numerical': 0.00039635354736424893, 'mean_word_length': 3.7884250474383303, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, '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': '7493743', 'n_tokens_mistral': 1773, 'n_tokens_neox': 1498, 'n_words': 409}