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

org.mycore.common.config.MCRProperties Maven / Gradle / Ivy

There is a newer version: 2024.05
Show newest version
/*
 * This file is part of ***  M y C o R e  ***
 * See http://www.mycore.de/ for details.
 *
 * MyCoRe is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * MyCoRe 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with MyCoRe.  If not, see .
 */

package org.mycore.common.config;

import java.io.IOException;
import java.io.OutputStream;
import java.io.Writer;
import java.nio.charset.Charset;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.TreeSet;

import org.apache.logging.log4j.LogManager;

/**
 * Like {@link Properties} but with in-place replacement of properties that want to append a value.
 * 
 * Properties for System.getProperties() have always precedence for Properties defined here.
 * 
 * 
 * key=value1
 * key=%key%,value2
 * 
* * will be resolved to * *
 * key=value1,value2
 * 
* * @author Thomas Scheffler (yagee) * @since 2013.12 */ public class MCRProperties extends Properties { private static final long serialVersionUID = 8801587133852810123L; @Override public synchronized Object put(Object key, Object value) { return putString((String) key, (String) value); } private Object putString(String key, String value) { String systemProperty = System.getProperties().getProperty(key); if (systemProperty != null && !systemProperty.equals(value)) { LogManager.getLogger(getClass()).error("Cannot overwrite system property: {}={}", key, value); return systemProperty; } String oldValue = (String) super.get(key); String newValue = oldValue == null ? value : value.replaceAll('%' + key + '%', oldValue); if (!newValue.equals(value) && newValue.startsWith(",")) { //replacement took place, but starts with 'empty' value newValue = newValue.substring(1); } return super.put(key, newValue); } @Override public synchronized Object get(Object key) { String systemProperty = System.getProperties().getProperty((String) key); return systemProperty != null ? systemProperty : super.get(key); } Map getAsMap() { @SuppressWarnings("rawtypes") Map compileFix = this; @SuppressWarnings("unchecked") Map returns = compileFix; return returns; } /** * Creates a new MCRProperties instance with the values * of the given properties. */ public static MCRProperties copy(Properties properties) { MCRProperties p = new MCRProperties(); for (Map.Entry entry : properties.entrySet()) { p.put(entry.getKey(), entry.getValue()); } return p; } @Override public void store(OutputStream out, String comments) throws IOException { toSortedProperties().store(out, comments); } @Override public void store(Writer writer, String comments) throws IOException { toSortedProperties().store(writer, comments); } @Override public void storeToXML(OutputStream os, String comment, Charset charset) throws IOException { toSortedProperties().storeToXML(os, comment, charset); } private Properties toSortedProperties() { Properties sortedProps = new Properties() { @Override public Set> entrySet() { Set> sortedSet = new TreeSet<>( (o1, o2) -> o1.getKey().toString().compareTo(o2.getKey().toString())); sortedSet.addAll(super.entrySet()); return sortedSet; } @Override public Set keySet() { return new TreeSet(super.keySet()); } @Override public synchronized Enumeration keys() { return Collections.enumeration(new TreeSet(super.keySet())); } }; sortedProps.putAll(this); return sortedProps; } }