All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.metaeffekt.mirror.PropertyFileAccess Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2021-2024 the original author or authors.
 *
 * 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 com.metaeffekt.mirror;

import com.metaeffekt.artifact.analysis.utils.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.swing.Timer;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.stream.Collectors;

public class PropertyFileAccess {

    private final static Logger LOG = LoggerFactory.getLogger(PropertyFileAccess.class);

    private final String namespace;

    private final Map> cachedAePropertyFiles = new HashMap<>();
    private final Timer flushCacheTimer = new Timer(3 * 60 * 1000, e -> flushCachedAePropertyFiles());

    public PropertyFileAccess(String namespace) {
        this.namespace = namespace;
        Runtime.getRuntime().addShutdownHook(new Thread(this::flushCachedAePropertyFiles));
    }

    public PropertyFileAccess() {
        this("properties");
    }

    private Map readOrGetCachedAePropertyFile(File directory, String fileIdentifier) {
        final File file = buildPropertyFilename(directory, fileIdentifier);

        if (cachedAePropertyFiles.containsKey(file)) {
            return cachedAePropertyFiles.get(file);
        } else {
            final Map contents = getAePropertyFileContents(file, fileIdentifier);
            cachedAePropertyFiles.put(file, contents);
            return contents;
        }
    }

    public void set(File directory, String fileIdentifier, String key, Object value) {
        final Map contents = readOrGetCachedAePropertyFile(directory, fileIdentifier);

        final String valueString;
        if (value == null) {
            valueString = "";
        } else if (value instanceof List || value instanceof Set) {
            valueString = ((Collection) value).stream()
                    .map(Object::toString)
                    .map(this::escapeValue)
                    .collect(Collectors.joining(","));
        } else {
            valueString = String.valueOf(value);
        }

        if (valueString.length() == 0) {
            contents.remove(key);
            LOG.info("Clearing key [{}] in {} / {}", key, directory.getName(), fileIdentifier);
        } else {
            contents.put(key, valueString);
            LOG.info("Setting [{}] to [{}] in {} / {}", key, valueString, directory.getName(), fileIdentifier);
        }

        refreshFlushCacheTimer();
    }

    public void addCsv(File directory, String fileIdentifier, String key, String value) {
        final Map contents = readOrGetCachedAePropertyFile(directory, fileIdentifier);
        contents.merge(key, value, (a, b) -> a + ", " + escapeValue(b));

        refreshFlushCacheTimer();
    }

    public void remove(File directory, String fileIdentifier, String key) {
        final Map contents = readOrGetCachedAePropertyFile(directory, fileIdentifier);
        contents.remove(key);
    }

    public Optional getString(File directory, String fileIdentifier, String key) {
        final Map contents = readOrGetCachedAePropertyFile(directory, fileIdentifier);
        return Optional.ofNullable(unescapeValue(contents.get(key)));
    }

    public List getCsv(File directory, String fileIdentifier, String key) {
        final Map contents = readOrGetCachedAePropertyFile(directory, fileIdentifier);
        final String value = contents.get(key);
        if (value == null) {
            return new ArrayList<>();
        } else {
            return Arrays.stream(value.split(", ?"))
                    .map(this::unescapeValue)
                    .collect(Collectors.toList());
        }
    }

    public Optional getBoolean(File directory, String fileIdentifier, String key) {
        final Map contents = readOrGetCachedAePropertyFile(directory, fileIdentifier);
        final String value = contents.get(key);
        if (value == null) {
            return Optional.empty();
        } else {
            return Optional.of(Boolean.parseBoolean(value));
        }
    }

    public Optional getLong(File directory, String fileIdentifier, String key) {
        final Map contents = readOrGetCachedAePropertyFile(directory, fileIdentifier);
        final String value = contents.get(key);
        if (value == null) {
            return Optional.empty();
        } else {
            return Optional.of(Long.parseLong(value));
        }
    }

    public void flushCachedAePropertyFiles() {
        clearFlushCacheTimer();

        for (Map.Entry> propertyFile : cachedAePropertyFiles.entrySet()) {
            writePropertyMapToFile(propertyFile.getKey(), propertyFile.getValue());
        }

        cachedAePropertyFiles.clear();
    }

    public void discardCachedChangesForFileType(String fileIdentifier) {
        final String searchString = ".ae-" + namespace + "-" + fileIdentifier;
        cachedAePropertyFiles.entrySet().removeIf(entry -> entry.getKey().getName().contains(searchString));
    }

    private void clearFlushCacheTimer() {
        synchronized (flushCacheTimer) {
            flushCacheTimer.stop();
        }
    }

    private void refreshFlushCacheTimer() {
        synchronized (flushCacheTimer) {
            flushCacheTimer.restart();
        }
    }

    private String escapeKey(String key) {
        return key
                .replace("#", "ESC_HASH")
                .replace("=", "ESC_EQUAL");
    }

    private String unescapeKey(String key) {
        return key
                .replace("ESC_HASH", "#")
                .replace("ESC_EQUAL", "=");
    }

    private String escapeValue(String value) {
        if (value == null) return null;
        return value
                .replace(",", "ESC_COMMA");
    }

    private String unescapeValue(String value) {
        if (value == null) return null;
        return value
                .replace("ESC_COMMA", ",");
    }

    private void writePropertyMapToFile(File file, Map propertyMap) {
        if (propertyMap.size() == 0) {
            file.delete();
        } else {
            final List lines = new ArrayList<>();
            for (Map.Entry property : propertyMap.entrySet()) {
                lines.add(escapeKey(property.getKey()) + "=" + property.getValue().replace("ESC_COMMA", ","));
            }

            try {
                FileUtils.writeLines(file, lines);
            } catch (IOException e) {
                throw new RuntimeException("Could not write property file " + file.getAbsolutePath(), e);
            }
        }
    }

    private Map getAePropertyFileContents(File file, String fileIdentifier) {
        final Map contents = new HashMap<>();

        if (file.exists()) {
            try {
                final List lines = FileUtils.readLines(file, StandardCharsets.UTF_8);

                for (String line : lines) {
                    final String[] keyValue = line.split("=", 2);
                    if (keyValue.length == 2) {
                        contents.put(unescapeKey(keyValue[0]), keyValue[1]);
                    }
                }
            } catch (IOException e) {
                throw new RuntimeException("Could not read property file " + file.getAbsolutePath(), e);
            }
        } else {
            LOG.debug("Creating new property file [{}] for [{}]", file.getAbsolutePath(), fileIdentifier);
        }

        return contents;
    }

    private File buildPropertyFilename(File directory, String fileIdentifier) {
        return new File(directory, ".ae-" + namespace + "-" + fileIdentifier);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy