<commit_before>package boot;

import java.io.*;
import java.nio.channels.FileLock;
import java.nio.channels.FileChannel;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.HashMap;
import java.util.Properties;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.atomic.AtomicLong;
import org.projectodd.shimdandy.ClojureRuntimeShim;

@SuppressWarnings("unchecked")
public class App {
    private static File[]                  podjars     = null;
    private static File[]                  corejars    = null;
    private static File[]                  workerjars  = null;
    private static File                    bootdir     = null;
    private static File                    aetherfile  = null;
    private static HashMap<String, File[]> depsCache   = null;
    private static String                  cljversion  = null;
    private static String                  bootversion = null;
    private static String                  localrepo   = null;
    private static String                  appversion  = null;
    private static String                  channel     = "RELEASE";
    private static String                  booturl     = "https://github.com/boot-clj/boot";
    private static ClojureRuntimeShim      aethershim  = null;

    private static final String            aetherjar   = "aether.uber.jar";
    private static final AtomicLong        counter     = new AtomicLong(0);
    private static final ExecutorService   ex          = Executors.newCachedThreadPool();

    private static long  nextId()         { return counter.addAndGet(1); }

    public static File   getBootDir()     { return bootdir; }
    public static String getVersion()     { return appversion; }
    public static String getBootVersion() { return bootversion; }

    public static class Exit extends Exception {
        public Exit(String m) { super(m); }
        public Exit(String m, Throwable c) { super(m, c); }}
    
    private static String
    jarVersion(File f, String prefix) throws Exception {
        String n = f.getName();
        if (! n.startsWith(prefix))
            return null;
        else return n.substring(prefix.length()).replaceAll(".jar$", ""); }

    private static Properties
    writeProps(File f) throws Exception {
        ClojureRuntimeShim a = aetherShim();
        Properties         p = new Properties();
        String             c = cljversion;
        String             t = null;
        
        if (c == null) c = "1.6.0";
        
        if (bootversion != null) 
            p.setProperty("BOOT_VERSION", bootversion);
        else
            for (File x : resolveDepJars(a, "boot", channel, c))
                if (null != (t = jarVersion(x, "boot-")))
                    p.setProperty("BOOT_VERSION", t);

        p.setProperty("BOOT_CLOJURE_VERSION", c);

        try (FileOutputStream file = new FileOutputStream(f)) {
                p.store(file, booturl); }
        
        return p; }
    
    private static Properties
    readProps(File f, boolean create) throws Exception {
        FileLock lock = null;
        if (f.exists())
            lock = (new RandomAccessFile(f, "rw")).getChannel().lock();
        Properties p = new Properties();
        try {
            p.load(new FileInputStream(f));
            if (p.getProperty("BOOT_CLOJURE_VERSION") == null
                || p.getProperty("BOOT_VERSION") == null)
                throw new Exception("missing info");
            return p; }
        catch (Throwable e) {
            if (! create) return null;
            else return writeProps(f); }
        finally { if (lock != null) lock.release(); }}
        
    private static HashMap<String, File[]>
    seedCache() throws Exception {
        if (depsCache != null) return depsCache;
        else {
            ClojureRuntimeShim a = aetherShim();
        
            HashMap<String, File[]> cache = new HashMap<>();
        
            cache.put("boot/pod",    resolveDepJars(a, "boot/pod"));
            cache.put("boot/core",   resolveDepJars(a, "boot/core"));
            cache.put("boot/worker", resolveDepJars(a, "boot/worker"));

            return depsCache = cache; }}
    
    private static Object
    validateCache(File f, Object cache) throws Exception {
        for (File[] fs : ((HashMap<String, File[]>) cache).values())
            for (File d : fs)
                if (! d.exists() || f.lastModified() < d.lastModified())
                    throw new Exception("dep jar doesn't exist");
        return cache; }

    private static Object
    writeCache(File f, Object m) throws Exception {
        try (FileOutputStream file = new FileOutputStream(f)) {
                (new ObjectOutputStream(file)).writeObject(m); }
        return m; }
    
    private static Object
    readCache(File f) throws Exception {
        FileLock lock = (new RandomAccessFile(f, "rw")).getChannel().lock();
        try {
            long max = 18 * 60 * 60 * 1000;
            long age = System.currentTimeMillis() - f.lastModified();
            if (age > max) throw new Exception("cache age exceeds TTL");
            return validateCache(f, (new ObjectInputStream(new FileInputStream(f))).readObject()); }
        catch (Throwable e) { return writeCache(f, seedCache()); }
        finally { lock.release(); }}
    
    public static ClojureRuntimeShim
    newShim(File[] jarFiles) throws Exception {
        URL[] urls = new URL[jarFiles.length];
        
        for (int i=0; i<jarFiles.length; i++) urls[i] = jarFiles[i].toURI().toURL();
        
        ClassLoader cl = new URLClassLoader(urls, App.class.getClassLoader());
        ClojureRuntimeShim rt = ClojureRuntimeShim.newRuntime(cl);

        rt.require("boot.pod");
        rt.invoke("boot.pod/seal-app-classloader");
<commit_msg>Only explicitly lock files on non-Windows platforms. Windows always locks when the file is opened, and the explicit lock results in a subsequent exception.<commit_after>package boot;

import java.io.*;
import java.nio.channels.FileLock;
import java.nio.channels.FileChannel;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.HashMap;
import java.util.Properties;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.atomic.AtomicLong;
import org.projectodd.shimdandy.ClojureRuntimeShim;

@SuppressWarnings("unchecked")
public class App {
    private static File[]                  podjars     = null;
    private static File[]                  corejars    = null;
    private static File[]                  workerjars  = null;
    private static File                    bootdir     = null;
    private static File                    aetherfile  = null;
    private static HashMap<String, File[]> depsCache   = null;
    private static String                  cljversion  = null;
    private static String                  bootversion = null;
    private static String                  localrepo   = null;
    private static String                  appversion  = null;
    private static String                  channel     = "RELEASE";
    private static String                  booturl     = "https://github.com/boot-clj/boot";
    private static ClojureRuntimeShim      aethershim  = null;

    private static final String            aetherjar   = "aether.uber.jar";
    private static final AtomicLong        counter     = new AtomicLong(0);
    private static final ExecutorService   ex          = Executors.newCachedThreadPool();

    private static long  nextId()         { return counter.addAndGet(1); }

    public static File   getBootDir()     { return bootdir; }
    public static String getVersion()     { return appversion; }
    public static String getBootVersion() { return bootversion; }

    public static class Exit extends Exception {
        public Exit(String m) { super(m); }
        public Exit(String m, Throwable c) { super(m, c); }}
    
    private static String
    jarVersion(File f, String prefix) throws Exception {
        String n = f.getName();
        if (! n.startsWith(prefix))
            return null;
        else return n.substring(prefix.length()).replaceAll(".jar$", ""); }

    private static Properties
    writeProps(File f) throws Exception {
        ClojureRuntimeShim a = aetherShim();
        Properties         p = new Properties();
        String             c = cljversion;
        String             t = null;
        
        if (c == null) c = "1.6.0";
        
        if (bootversion != null) 
            p.setProperty("BOOT_VERSION", bootversion);
        else
            for (File x : resolveDepJars(a, "boot", channel, c))
                if (null != (t = jarVersion(x, "boot-")))
                    p.setProperty("BOOT_VERSION", t);

        p.setProperty("BOOT_CLOJURE_VERSION", c);

        try (FileOutputStream file = new FileOutputStream(f)) {
                p.store(file, booturl); }
        
        return p; }

    private static boolean
    isWindows() throws Exception {
        return (System.getProperty("os.name").toLowerCase().indexOf("win") >= 0);
    }
    private static Properties
    readProps(File f, boolean create) throws Exception {
        FileLock lock = null;
        if (!isWindows() && f.exists())
            lock = (new RandomAccessFile(f, "rw")).getChannel().lock();
        Properties p = new Properties();
        try {
            p.load(new FileInputStream(f));
            if (p.getProperty("BOOT_CLOJURE_VERSION") == null
                || p.getProperty("BOOT_VERSION") == null)
                throw new Exception("missing info");
            return p; }
        catch (Throwable e) {
            if (! create) return null;
            else return writeProps(f); }
        finally { if (lock != null) lock.release(); }}
        
    private static HashMap<String, File[]>
    seedCache() throws Exception {
        if (depsCache != null) return depsCache;
        else {
            ClojureRuntimeShim a = aetherShim();
        
            HashMap<String, File[]> cache = new HashMap<>();
        
            cache.put("boot/pod",    resolveDepJars(a, "boot/pod"));
            cache.put("boot/core",   resolveDepJars(a, "boot/core"));
            cache.put("boot/worker", resolveDepJars(a, "boot/worker"));

            return depsCache = cache; }}
    
    private static Object
    validateCache(File f, Object cache) throws Exception {
        for (File[] fs : ((HashMap<String, File[]>) cache).values())
            for (File d : fs)
                if (! d.exists() || f.lastModified() < d.lastModified())
                    throw new Exception("dep jar doesn't exist");
        return cache; }

    private static Object
    writeCache(File f, Object m) throws Exception {
        try (FileOutputStream file = new FileOutputStream(f)) {
                (new ObjectOutputStream(file)).writeObject(m); }
        return m; }
    
    private static Object
    readCache(File f) throws Exception {
        FileLock lock = null;
        if (!isWindows()){
            lock = (new RandomAccessFile(f, "rw")).getChannel().lock();
        }
        try {
            long max = 18 * 60 * 60 * 1000;
            long age = System.currentTimeMillis() - f.lastModified();
            if (age > max) throw new Exception("cache age exceeds TTL");
            return validateCache(f, (new ObjectInputStream(new FileInputStream(f))).readObject()); }
        catch (Throwable e) { return writeCache(f, seedCache()); }
        finally { if (lock != null) lock.release(); }}
    
    public static ClojureRuntimeShim
    newShim(File[] jarFiles) throws Exception {
        URL[] urls = new URL[jarFiles.length];
        
        for (int i=0; i<jarFiles.length; i++) urls[i] = jarFiles[i].toURI().toURL();
        
        ClassLoader cl = new URLClassLoader(urls, App.class.getClassLoader());
        ClojureRuntimeShim rt = ClojureRuntimeShim.newRuntime(cl);

        rt.require("boot.pod");
        rt.invoke("boot.pod/seal-app-classloader");

{'fraction_non_alphanumeric': 0.10940545808966862, 'fraction_numerical': 0.0026803118908382065, 'mean_word_length': 2.1693693693693694, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 16, 'https://': 2, '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': '1542543', 'n_tokens_mistral': 3356, 'n_tokens_neox': 3274, 'n_words': 922}