<commit_before>/*
 * /////////////////////////////////////////////////////////////////////////////
 * // This file is part of the "Hyrax Data Server" project.
 * //
 * //
 * // Copyright (c) 2013 OPeNDAP, Inc.
 * // Author: <NAME>  <<EMAIL>>
 * //
 * // This library is free software; you can redistribute it and/or
 * // modify it under the terms of the GNU Lesser General Public
 * // License as published by the Free Software Foundation; either
 * // version 2.1 of the License, or (at your option) any later version.
 * //
 * // This library is distributed in the hope that it will be useful,
 * // but WITHOUT ANY WARRANTY; without even the implied warranty of
 * // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * // Lesser General Public License for more details.
 * //
 * // You should have received a copy of the GNU Lesser General Public
 * // License along with this library; if not, write to the Free Software
 * // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
 * //
 * // You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
 * /////////////////////////////////////////////////////////////////////////////
 */

package opendap.coreServlet;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServlet;
import java.io.*;
import java.util.concurrent.locks.ReentrantLock;

/**
 * Provides methods for detecting the presence of a localized configuration directory and moving an initial
 * copy of the default configuration to that directory if it does not already exist.
 */
public class PersistentConfigurationHandler {

    private static Logger log;

    private static String defaultContentLocation="WEB-INF/conf/";

    private static final ReentrantLock _installDefaultConfigLock = new ReentrantLock();



    static{
        log = org.slf4j.LoggerFactory.getLogger(PersistentConfigurationHandler.class);
    }

        /**
         * If the following are true:
         *  - The local configuration directory exists.
         *  - The semaphore file does not exist in the configuration directory.
         *  Then the default configuration will be copied into
         *  the configuration directory.
         *  Otherwise, nothing will be done.
         * @param servlet
         */
    public static void installDefaultConfiguration(HttpServlet servlet, String semaphore) {


        _installDefaultConfigLock.lock();     // Only one thread allowed in at a time.
        try {
            Logger my_log = LoggerFactory.getLogger(PersistentConfigurationHandler.class);
            my_log.debug("PersistentContentHandler:");
            my_log.debug("    configPath:               {}",ServletUtil.getConfigPath(servlet));
            my_log.debug("    defaultConfigurationPath: {}",getDefaultConfigurationPath(servlet));
            my_log.debug("    semaphore:                {}",semaphore);
            my_log.debug("    ThreadName:               {}",Thread.currentThread().getName());
            
            // -------------
            // first time, create content directory
            String defaultConfigurationPath = getDefaultConfigurationPath(servlet);
            File defaultConfigurationFile = new File(defaultConfigurationPath);

            if (defaultConfigurationFile.exists()) {
                try {
                    if (copyDirIfSemaphoreNotPresent(defaultConfigurationPath, ServletUtil.getConfigPath(servlet), semaphore)) {
                        my_log.info("Copied default configuration from " + defaultConfigurationPath + " to directory " + ServletUtil.getConfigPath(servlet));
                    } else {
                        my_log.info("Located configuration directory semaphore ('{}') in directory '{}'", semaphore, ServletUtil.getConfigPath(servlet));
                    }
                } catch (IOException ioe) {
                    my_log.error("Failed to copy default content directory " + defaultConfigurationPath + " to " + ServletUtil.getConfigPath(servlet), ioe);
                }
            }
            //-------------
        } finally {
            _installDefaultConfigLock.unlock();
        }

    }


    private static  boolean copyDirIfSemaphoreNotPresent(
            String fromDir,
            String toDir,
            String semaphore)
            throws IOException {

            File contentFile = new File(toDir + semaphore);
            if (!contentFile.exists()) {
                copyDirTree(fromDir, toDir);
                if (!contentFile.exists()) {
                    String msg = "FAILED to locate semaphore file '" + contentFile.getAbsolutePath() + "' after copy completed.";
                    LoggerFactory.getLogger(PersistentConfigurationHandler.class).error("copyDirIfSemaphoreNotPresent() - {}", msg);
                    throw new IOException(msg);

                }
                return true;
            }
            return false;
    }


 /* #################################################################################################################
  *
  * FILE UTILITY METHODS
  *
  *
  */

    private static String getDefaultConfigurationPath(HttpServlet servlet) {
      return ServletUtil.getRootPath(servlet) + defaultContentLocation;
    }



    /**
     * Copy an entire directory tree.
     * @param fromDirName from this directory (do nothing if not exist)
     * @param toDirName to this directory (will create if not exist)
     * @throws java.io.IOException on io error
     */
    static public void copyDirTree(String fromDirName, String toDirName) throws IOException {
        File fromDir = new File(fromDirName);
        File toDir = new File(toDirName);
        if (!fromDir.exists())
            return;
        if (!toDir.exists()) {
            if(!toDir.mkdirs()){
                String msg = "FAILED to create target directory '"+toDir.getAbsolutePath()+
                              "' Unable to copy content from '"+fromDir.getAbsolutePath()+"'";
                LoggerFactory.getLogger(PersistentConfigurationHandler.class).error("copyDirTree() - {}",msg);
                throw new IOException(msg);
            }
        }

        File[] files = fromDir.listFiles();
        if(files==null){
            LoggerFactory.getLogger(PersistentConfigurationHandler.class).error("copyDirTree() - Unable to locate directory {}. Not content will be copied. THIS IS BAD.",fromDirName);
            return;
        }
        for (int i=0; i<files.length; i++) {
            File f = files[i];
            if (f.isDirectory())
                copyDirTree(f.getAbsolutePath(), toDir.getAbsolutePath() + "/" + f.getName());
            else
                copyFile( f.getAbsolutePath(), toDir.getAbsolutePath() + "/" + f.getName());
        }
    }
    /**
     * copy one file to another.
     * @param fileInName copy from this file, which must exist.
     * @param fileOutName copy to this file, which is overrwritten if already exists.
     * @throws java.io.IOException on io error
     */<commit_msg>olfs: Added user name to install fail message.<commit_after>/*
 * /////////////////////////////////////////////////////////////////////////////
 * // This file is part of the "Hyrax Data Server" project.
 * //
 * //
 * // Copyright (c) 2013 OPeNDAP, Inc.
 * // Author: <NAME>  <<EMAIL>>
 * //
 * // This library is free software; you can redistribute it and/or
 * // modify it under the terms of the GNU Lesser General Public
 * // License as published by the Free Software Foundation; either
 * // version 2.1 of the License, or (at your option) any later version.
 * //
 * // This library is distributed in the hope that it will be useful,
 * // but WITHOUT ANY WARRANTY; without even the implied warranty of
 * // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * // Lesser General Public License for more details.
 * //
 * // You should have received a copy of the GNU Lesser General Public
 * // License along with this library; if not, write to the Free Software
 * // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
 * //
 * // You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
 * /////////////////////////////////////////////////////////////////////////////
 */

package opendap.coreServlet;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServlet;
import java.io.*;
import java.util.concurrent.locks.ReentrantLock;

/**
 * Provides methods for detecting the presence of a localized configuration directory and moving an initial
 * copy of the default configuration to that directory if it does not already exist.
 */
public class PersistentConfigurationHandler {

    private static Logger log;

    private static String defaultContentLocation="WEB-INF/conf/";

    private static final ReentrantLock _installDefaultConfigLock = new ReentrantLock();



    static{
        log = org.slf4j.LoggerFactory.getLogger(PersistentConfigurationHandler.class);
    }

        /**
         * If the following are true:
         *  - The local configuration directory exists.
         *  - The semaphore file does not exist in the configuration directory.
         *  Then the default configuration will be copied into
         *  the configuration directory.
         *  Otherwise, nothing will be done.
         * @param servlet
         */
    public static void installDefaultConfiguration(HttpServlet servlet, String semaphore) {


        _installDefaultConfigLock.lock();     // Only one thread allowed in at a time.
        try {
            Logger my_log = LoggerFactory.getLogger(PersistentConfigurationHandler.class);
            my_log.debug("PersistentContentHandler:");
            my_log.debug("    configPath:               {}",ServletUtil.getConfigPath(servlet));
            my_log.debug("    defaultConfigurationPath: {}",getDefaultConfigurationPath(servlet));
            my_log.debug("    semaphore:                {}",semaphore);
            my_log.debug("    ThreadName:               {}",Thread.currentThread().getName());
            
            // -------------
            // first time, create content directory
            String defaultConfigurationPath = getDefaultConfigurationPath(servlet);
            File defaultConfigurationFile = new File(defaultConfigurationPath);

            if (defaultConfigurationFile.exists()) {
                try {
                    if (copyDirIfSemaphoreNotPresent(defaultConfigurationPath, ServletUtil.getConfigPath(servlet), semaphore)) {
                        my_log.info("Copied default configuration from " + defaultConfigurationPath + " to directory " + ServletUtil.getConfigPath(servlet));
                    } else {
                        my_log.info("Located configuration directory semaphore ('{}') in directory '{}'", semaphore, ServletUtil.getConfigPath(servlet));
                    }
                } catch (IOException ioe) {
                    my_log.error("Failed to copy default content directory " + defaultConfigurationPath + " to " + ServletUtil.getConfigPath(servlet), ioe);
                }
            }
            //-------------
        } finally {
            _installDefaultConfigLock.unlock();
        }

    }


    private static  boolean copyDirIfSemaphoreNotPresent(
            String fromDir,
            String toDir,
            String semaphore)
            throws IOException {

            File contentFile = new File(toDir + semaphore);
            if (!contentFile.exists()) {
                copyDirTree(fromDir, toDir);
                if (!contentFile.exists()) {
                    String msg = "FAILED to locate semaphore file '" + contentFile.getAbsolutePath() + "' after copy completed.";
                    LoggerFactory.getLogger(PersistentConfigurationHandler.class).error("copyDirIfSemaphoreNotPresent() - {}", msg);
                    throw new IOException(msg);

                }
                return true;
            }
            return false;
    }


 /* #################################################################################################################
  *
  * FILE UTILITY METHODS
  *
  *
  */

    private static String getDefaultConfigurationPath(HttpServlet servlet) {
      return ServletUtil.getRootPath(servlet) + defaultContentLocation;
    }



    /**
     * Copy an entire directory tree.
     * @param fromDirName from this directory (do nothing if not exist)
     * @param toDirName to this directory (will create if not exist)
     * @throws java.io.IOException on io error
     */
    static public void copyDirTree(String fromDirName, String toDirName) throws IOException {
        File fromDir = new File(fromDirName);
        File toDir = new File(toDirName);
        if (!fromDir.exists())
            return;
        if (!toDir.exists()) {
            if(!toDir.mkdirs()){
                String msg = "FAILED to create target directory '"+toDir.getAbsolutePath()+
                              "' Unable to copy content from '"+fromDir.getAbsolutePath()+"' user.name: " +
                        System.getProperty("user.name");

                LoggerFactory.getLogger(PersistentConfigurationHandler.class).error("copyDirTree() - {}",msg);
                throw new IOException(msg);
            }
        }

        File[] files = fromDir.listFiles();
        if(files==null){
            LoggerFactory.getLogger(PersistentConfigurationHandler.class).error("copyDirTree() - Unable to locate directory {}. Not content will be copied. THIS IS BAD.",fromDirName);
            return;
        }
        for (int i=0; i<files.length; i++) {
            File f = files[i];
            if (f.isDirectory())
                copyDirTree(f.getAbsolutePath(), toDir.getAbsolutePath() + "/" + f.getName());
            else
                copyFile( f.getAbsolutePath(), toDir.getAbsolutePath() + "/" + f.getName());
        }
    }
    /**
     * copy one file to another.
     * @param fileInName copy from this file, which must exist.
     * @param fileOutName copy to this file, which is overrwritten if already exists.
     * @throws java.io.IOException on io error
     */
{'fraction_non_alphanumeric': 0.13331926269874772, 'fraction_numerical': 0.004643309413254538, 'mean_word_length': 2.1098227958871143, 'pattern_counts': {'":': 0, '<': 11, '<?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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '978926', 'n_tokens_mistral': 3522, 'n_tokens_neox': 3375, 'n_words': 1055}