<commit_before>/*
 * Copyright (C) 2013 salesforce.com, inc.
 *
 * Licensed under the Apache License, Version 2.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://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.auraframework.impl.source.file;

import org.apache.log4j.Logger;
import org.auraframework.def.DefDescriptor;
import org.auraframework.service.CachingService;
import org.auraframework.system.SourceListener;
import org.auraframework.util.FileChangeEvent;
import org.auraframework.util.FileListener;
import org.auraframework.util.FileMonitor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;

import javax.inject.Inject;
import java.io.IOException;
import java.lang.ref.WeakReference;
import java.nio.file.FileSystems;
import java.nio.file.FileVisitOption;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.WatchEvent;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentLinkedQueue;

import static java.nio.file.LinkOption.NOFOLLOW_LINKS;
import static java.nio.file.StandardWatchEventKinds.ENTRY_CREATE;
import static java.nio.file.StandardWatchEventKinds.ENTRY_DELETE;
import static java.nio.file.StandardWatchEventKinds.ENTRY_MODIFY;
import static java.nio.file.StandardWatchEventKinds.OVERFLOW;

/**
 * File monitor allowing to ability to add watched directory. Used to update files and clear caches on source changes
 * during development
 */
public final class FileMonitorImpl implements FileMonitor, Runnable {
    private final static Logger LOG = Logger.getLogger(FileMonitorImpl.class);

    @Configuration
    public static class BeanConfiguration {
        private static final FileMonitorImpl INSTANCE;
        
        static {
            synchronized (BeanConfiguration.class) {
                INSTANCE = new FileMonitorImpl();
                INSTANCE.start();
            }
        }
        
        @Lazy
        @Bean
        public FileMonitor fileMonitorImpl() {
            return INSTANCE;
        }
    }
    
    @Inject
    private CachingService cachingService;

    private final ConcurrentLinkedQueue<WeakReference<SourceListener>> listeners = new ConcurrentLinkedQueue<>();

    // keep track of directories monitored
    private final Set<String> monitoredDirs;

    // keep track of watch service keys for directories monitored,
    // for the purpose of reporting with the same directory root as the original
    private final Map<WatchKey, Path> monitoredKeys;

    private WatchService watchService;
    private Thread watchServiceThread;
    private boolean terminateThread;<commit_msg>force side effects expected in core<commit_after>/*
 * Copyright (C) 2013 salesforce.com, inc.
 *
 * Licensed under the Apache License, Version 2.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://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.auraframework.impl.source.file;

import org.apache.log4j.Logger;
import org.auraframework.def.DefDescriptor;
import org.auraframework.impl.CachingServiceImpl;
import org.auraframework.service.CachingService;
import org.auraframework.system.SourceListener;
import org.auraframework.util.FileChangeEvent;
import org.auraframework.util.FileListener;
import org.auraframework.util.FileMonitor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;

import javax.inject.Inject;
import java.io.IOException;
import java.lang.ref.WeakReference;
import java.nio.file.FileSystems;
import java.nio.file.FileVisitOption;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.WatchEvent;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentLinkedQueue;

import static java.nio.file.LinkOption.NOFOLLOW_LINKS;
import static java.nio.file.StandardWatchEventKinds.ENTRY_CREATE;
import static java.nio.file.StandardWatchEventKinds.ENTRY_DELETE;
import static java.nio.file.StandardWatchEventKinds.ENTRY_MODIFY;
import static java.nio.file.StandardWatchEventKinds.OVERFLOW;

/**
 * File monitor allowing to ability to add watched directory. Used to update files and clear caches on source changes
 * during development
 */
public final class FileMonitorImpl implements FileMonitor, Runnable {
    private final static Logger LOG = Logger.getLogger(FileMonitorImpl.class);

    @Configuration
    public static class BeanConfiguration {
        private static final FileMonitorImpl INSTANCE;
        
        static {
            synchronized (BeanConfiguration.class) {
                INSTANCE = new FileMonitorImpl();
                INSTANCE.start();
                
                //force side effects.
                new CachingServiceImpl.BeanConfiguration();
            }
        }
        
        @Lazy
        @Bean
        public FileMonitor fileMonitorImpl() {
            return INSTANCE;
        }
    }
    
    @Inject
    private CachingService cachingService;

    private final ConcurrentLinkedQueue<WeakReference<SourceListener>> listeners = new ConcurrentLinkedQueue<>();

    // keep track of directories monitored
    private final Set<String> monitoredDirs;

    // keep track of watch service keys for directories monitored,
    // for the purpose of reporting with the same directory root as the original
    private final Map<WatchKey, Path> monitoredKeys;

    private WatchService watchService;
    private Thread watchServiceThread;
    private boolean terminateThread;
{'fraction_non_alphanumeric': 0.07755102040816327, 'fraction_numerical': 0.002533427163969036, 'mean_word_length': 3.8973121984838044, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '6443088', 'n_tokens_mistral': 1909, 'n_tokens_neox': 1832, 'n_words': 563}