<commit_before>package org.apache.commons.jcs3.auxiliary.remote;

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.
 */

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.URL;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.RMISocketFactory;
import java.util.Properties;

import org.apache.commons.jcs3.log.Log;
import org.apache.commons.jcs3.log.LogManager;

/**
 * This class provides some basic utilities for doing things such as starting
 * the registry properly.
 */
public class RemoteUtils
{
    /** The logger. */
    private static final Log log = LogManager.getLog(RemoteUtils.class);

    /** No instances please. */
    private RemoteUtils()
    {
    }

    /**
     * Creates and exports a registry on the specified port of the local host.
     * <p>
     *
     * @param port
     * @return the registry
     */
    public static Registry createRegistry(int port)
    {
        Registry registry = null;

        // if ( log.isInfoEnabled() )
        // {
        // log.info( "createRegistry> Setting security manager" );
        // }
        //
        // System.setSecurityManager( new RMISecurityManager() );

        if (port < 1024)
        {
            log.warn("createRegistry> Port chosen was less than 1024, will use default [{0}] instead.",
                    Registry.REGISTRY_PORT);
            port = Registry.REGISTRY_PORT;
        }

        try
        {
            registry = LocateRegistry.createRegistry(port);
            log.info("createRegistry> Created the registry on port {0}", port);
        }
        catch (final RemoteException e)
        {
            log.warn("createRegistry> Problem creating registry. It may already be started.",
                    e);
        }
        catch (final Throwable t)
        {
            log.error("createRegistry> Problem creating registry.", t);
        }

        if (registry == null)
        {
            try
            {
                registry = LocateRegistry.getRegistry(port);
            }
            catch (final RemoteException e)
            {
                log.error("createRegistry> Problem getting a registry reference.", e);
            }
        }

        return registry;
    }

    /**
     * Loads properties for the named props file.
     * First tries class path, then file, then URL
     * <p>
     *
     * @param propFile
     * @return The properties object for the file
     * @throws IOException
     */
    public static Properties loadProps(final String propFile)
            throws IOException
    {
        InputStream is = RemoteUtils.class.getResourceAsStream(propFile);

        // Try root of class path
        if ((null == is) && (propFile != null && !propFile.startsWith("/")))
        {
            is = RemoteUtils.class.getResourceAsStream("/" + propFile);
        }

        if (null == is) // not found in class path
        {
            if (new File(propFile).exists())
            {
                // file found
                is = new FileInputStream(propFile);
            }
            else
            {
                // try URL
                is = new URL(propFile).openStream();
            }
        }

        final Properties props = new Properties();
        try
        {
            props.load(is);
            log.debug("props.size={0}", () -> props.size());<commit_msg>Apply modified version of #31<commit_after>package org.apache.commons.jcs3.auxiliary.remote;

import java.io.IOException;
import java.io.InputStream;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.RMISocketFactory;
import java.util.Properties;

import org.apache.commons.jcs3.log.Log;
import org.apache.commons.jcs3.log.LogManager;

/**
 * This class provides some basic utilities for doing things such as starting
 * the registry properly.
 */
public class RemoteUtils
{
    /** The logger. */
    private static final Log log = LogManager.getLog(RemoteUtils.class);

    /** No instances please. */
    private RemoteUtils()
    {
    }

    /**
     * Creates and exports a registry on the specified port of the local host.
     * <p>
     *
     * @param port
     * @return the registry
     */
    public static Registry createRegistry(int port)
    {
        Registry registry = null;

        // if ( log.isInfoEnabled() )
        // {
        // log.info( "createRegistry> Setting security manager" );
        // }
        //
        // System.setSecurityManager( new RMISecurityManager() );

        if (port < 1024)
        {
            log.warn("createRegistry> Port chosen was less than 1024, will use default [{0}] instead.",
                    Registry.REGISTRY_PORT);
            port = Registry.REGISTRY_PORT;
        }

        try
        {
            registry = LocateRegistry.createRegistry(port);
            log.info("createRegistry> Created the registry on port {0}", port);
        }
        catch (final RemoteException e)
        {
            log.warn("createRegistry> Problem creating registry. It may already be started.",
                    e);
        }
        catch (final Throwable t)
        {
            log.error("createRegistry> Problem creating registry.", t);
        }

        if (registry == null)
        {
            try
            {
                registry = LocateRegistry.getRegistry(port);
            }
            catch (final RemoteException e)
            {
                log.error("createRegistry> Problem getting a registry reference.", e);
            }
        }

        return registry;
    }

    /**
     * Loads properties for the named props file.
     * First tries class path, then file, then URL
     * <p>
     *
     * @param propFile
     * @return The properties object for the file
     * @throws IOException
     */
    public static Properties loadProps(final String propFile)
            throws IOException
    {
        InputStream is = RemoteUtils.class.getResourceAsStream(propFile);

        // Try root of class path
        if ((null == is) && (propFile != null && !propFile.startsWith("/")))
        {
            is = RemoteUtils.class.getResourceAsStream("/" + propFile);
        }

        if (null == is) // not found in class path
        {
            Path propPath = Paths.get(propFile);
            if (Files.exists(propPath))
            {
                // file found
                is = Files.newInputStream(propPath);
            }
            else
            {
                // try URL
                is = new URL(propFile).openStream();
            }
        }

        final Properties props = new Properties();
        try
        {
            props.load(is);
            log.debug("props.size={0}", () -> props.size());
{'fraction_non_alphanumeric': 0.09957200402819738, 'fraction_numerical': 0.0042799597180261835, 'mean_word_length': 1.9123900293255132, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 1, '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': '5456028', 'n_tokens_mistral': 2115, 'n_tokens_neox': 2042, 'n_words': 626}