<commit_before>/*
 *  Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
 *
 *  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.wso2.carbon.uuf.internal.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.wso2.carbon.uuf.exception.MalformedConfigurationException;
import org.wso2.carbon.uuf.exception.UUFException;

import java.io.IOException;
import java.io.InputStream;
import java.util.Optional;
import java.util.Properties;

/**
 * This class lazily loads the 'mime-map.properties' file and maps file extension to mime type using the file.
 */
public class MimeMapper {

    private static volatile Properties MIME_MAP = null;
    private static final Logger log = LoggerFactory.getLogger(MimeMapper.class);

    private static Properties loadMimeMap() {
        Properties mimeMap = new Properties();
        String mimePropertyFileName = "mime-map.properties";
        InputStream inputStream = MimeMapper.class.getClassLoader().getResourceAsStream(mimePropertyFileName);
        if (inputStream == null) {
            throw new UUFException("Cannot find MIME types property file '" + mimePropertyFileName + "'");
        }
        try {
            mimeMap.load(inputStream);
        } catch (IllegalArgumentException e) {
            throw new MalformedConfigurationException(
                    "MIME types property file is '" + mimePropertyFileName + "' is invalid.", e);
        } catch (IOException e) {
            throw new UUFException("Cannot read MIME types property file '" + mimePropertyFileName + "'.", e);
        }
        try {
            inputStream.close();
        } catch (IOException e) {
            log.warn("Could not close input stream of MIME types property file '" + mimePropertyFileName + "'.", e);
        }
        return mimeMap;
    }

    public static Optional<String> getMimeType(String extension) {
        if (MIME_MAP == null) {
            // Here, class object 'MimeMapper.class' is used as the synchronization lock
            // because 'getMimeType()' is the only is public method.
            synchronized (MimeMapper.class) {
                if (MIME_MAP == null) {
                    MIME_MAP = loadMimeMap();
                }
            }
        }
        return Optional.ofNullable(MIME_MAP.getProperty(extension));
    }
}<commit_msg>code refactoring in MimeMapper class<commit_after>/*
 *  Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
 *
 *  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.wso2.carbon.uuf.internal.util;

import org.wso2.carbon.uuf.exception.FileOperationException;
import org.wso2.carbon.uuf.exception.MalformedConfigurationException;
import org.wso2.carbon.uuf.exception.UUFException;

import java.io.IOException;
import java.io.InputStream;
import java.util.Optional;
import java.util.Properties;

/**
 * This class lazily loads the 'mime-map.properties' file and maps file extension to mime type using the file.
 */
public class MimeMapper {

    private static final String MIME_PROPERTY_FILE = "mime-map.properties";
    private static volatile Properties MIME_MAP = null;

    private MimeMapper() {
    }

    private static Properties loadMimeMap() {
        Properties mimeMap = new Properties();
        try (InputStream inputStream = MimeMapper.class.getClassLoader().getResourceAsStream(MIME_PROPERTY_FILE)) {
            if (inputStream == null) {
                throw new UUFException("Cannot find MIME types property file '" + MIME_PROPERTY_FILE + "'");
            }
            mimeMap.load(inputStream);
        } catch (IllegalArgumentException e) {
            throw new MalformedConfigurationException(
                    "MIME types property file is '" + MIME_PROPERTY_FILE + "' is invalid.", e);
        } catch (IOException e) {
            throw new FileOperationException("Cannot read MIME types property file '" + MIME_PROPERTY_FILE + "'.", e);
        }
        return mimeMap;
    }

    public static Optional<String> getMimeType(String extension) {
        if (MIME_MAP == null) {
            /* Here, class object 'MimeMapper.class' is used as the synchronization lock because 'getMimeType()' is
            the only is public method. */
            synchronized (MimeMapper.class) {
                if (MIME_MAP == null) {
                    MIME_MAP = loadMimeMap();
                }
            }
        }
        return Optional.ofNullable(MIME_MAP.getProperty(extension));
    }
}
{'fraction_non_alphanumeric': 0.0913861744480637, 'fraction_numerical': 0.005247918928700687, 'mean_word_length': 2.783025325119781, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, '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': '5013978', 'n_tokens_mistral': 1515, 'n_tokens_neox': 1482, 'n_words': 480}