Skip to content

Provide utility for writing Properties to a file in a repeatable manner #23018

Closed

Description

@vpavic

Instances of java.util.Properties are typically written to file using Properties#store. Since this method also writes a timestamp in a comment, its use is problematic in projects that need to have reproducible builds.

If Spring Framework could provide utility for writing Properties to a file in a repeatable manner, this would IMO be of great benefit to all Spring projects. There are already several issues/PRs that are blocked due to the shortcomings of Properties#store:

/cc @dsyer @wilkinsona

Activity

  1. added
    status: waiting-for-triage
    on May 23, 2019
  2. added
    in: core
    status: pending-design-work
    and removed
    status: waiting-for-triage
    on May 23, 2019
  3. sbrannen commented on May 23, 2019

    @sbrannen
    Member

    As @jhoeller mentioned elsewhere, Spring already has a PropertiesPersister interface and DefaultPropertiesPersister implementation.

    So, we'll look into providing support to filter out comment lines from the output (likely enabled via a new boolean flag in DefaultPropertiesPersister), which should take care of the timestamp comment.

    @vpavic, would that meet your needs?

  4. added this to the 5.2 M3 milestone on May 23, 2019
  5. wilkinsona commented on May 23, 2019

    @wilkinsona
    Member

    I'm not sure that filtering comments alone is sufficient. To be guaranteed to be repeatable I believe it's also necessary to order the keys. Without ordering the keys, you are reliant on repeatable hashing of the keys which may not hold true under all circumstances.

    Gradle contains some code for writing properties in a repeatable manner which could serve as inspiration.

  6. vpavic commented on May 23, 2019

    @vpavic
    ContributorAuthor

    Thanks for the feedback @sbrannen, I wasn't aware of PropertiesPersister - a custom implementation would probably do.

    Thanks @wilkinsona, I forgot to mention there's also an ordering aspect to this.

  7. sbrannen commented on May 26, 2019

    @sbrannen
    Member

    I'm not sure that filtering comments alone is sufficient. To be guaranteed to be repeatable I believe it's also necessary to order the keys. Without ordering the keys, you are reliant on repeatable hashing of the keys which may not hold true under all circumstances.

    OK. Thanks for bringing that up. We'll then have to take the order into account as well.

  8. self-assigned this
    on May 26, 2019
  9. sbrannen commented on May 26, 2019

    @sbrannen
    Member

    Design Notes

    It will likely be easiest to support consistent ordering by extending Properties and overriding the implementation of keys().

    To remove the comments, we will likely need to override store(OutputStream, String) and store(Writer, String) to filter out all comments.

    Open Issues

    @vpavic, @dsyer, @wilkinsona:

    • Can you please confirm that you do not need this support for the storeToXML(...) variants?
    • Is it acceptable to ignore (i.e., filter out) any user-supplied comments (provided via the String comment argument to store(OutputStream, String) or store(Writer, String))?
  10. vpavic commented on May 27, 2019

    @vpavic
    ContributorAuthor

    I believe XML variants aren't needed.

    Regarding PropertiesPersister itself, it seems a bit obscure as code search doesn't reveal any (production) uses even in Framework's codebase - Properties#store is actually used in all places that write Properties instances.

    To me PropertiesPersister looks like an overhead for this, as simple property writing helper with two variants (one accepting OutputStream the other Writer) of static store method would be much simpler to consume across the Spring portfolio. Both load and XML related parts of the API don't seem particularly relevant to the core problem being discussed here.

  11. sbrannen commented on May 27, 2019

    @sbrannen
    Member

    Thanks for the feedback, @vpavic.

    I am actually considering only a custom subclass of Properties. If I get that to work, I don't think there will be a need to implement a custom PropertiesPersister, modify DefaultPropertiesPersister, or introduce a new helper class.

  12. sbrannen commented on May 27, 2019

    @sbrannen
    Member

    OK, this feature is basically done, but I'd like to share it with you here first to see if it meets your needs.

    New method in org.springframework.core.CollectionFactory (per recommendation by @jhoeller):

    /**
     * Create a variant of {@code java.util.Properties} that sorts properties
     * alphanumerically based on their keys.
     *
     * <p>This can be useful when storing the {@link Properties} instance in a
     * properties file, since it allows such files to be generated in a repeatable
     * manner with consistent ordering of properties. Comments in generated
     * properties files can also be optionally omitted.
     *
     * @param omitComments {@code true} if comments should be omitted when
     * storing properties in a file
     * @return a new {@code Properties} instance
     * @since 5.2
     */
    public static Properties createSortedProperties(boolean omitComments) {
    	return new SortedProperties(omitComments);
    }

    Implementation of SortedProperties:

    /**
     * Specialization of {@link Properties} that sorts properties alphanumerically
     * based on their keys.
     *
     * <p>This can be useful when storing the {@link Properties} instance in a
     * properties file, since it allows such files to be generated in a repeatable
     * manner with consistent ordering of properties.
     *
     * <p>Comments in generated properties files can also be optionally omitted
     * (see {@link SortedProperties#SortedProperties(boolean)}).
     *
     * @author Sam Brannen
     * @since 5.2
     * @see java.util.Properties
     */
    class SortedProperties extends Properties {
    
    	private static final String EOL = System.getProperty("line.separator");
    
    	private static final Comparator<Entry<Object, Object>> entryComparator = //
    		(entry1, entry2) -> String.valueOf(entry1.getKey()).compareTo(String.valueOf(entry2.getKey()));
    
    	private final boolean omitComments;
    
    
    	/**
    	 * Construct a new {@code SortedProperties} instance that honors the supplied
    	 * {@code omitComments} flag.
    	 *
    	 * @param omitComments {@code true} if comments should be omitted when
    	 * storing properties in a file
    	 */
    	SortedProperties(boolean omitComments) {
    		this.omitComments = omitComments;
    	}
    
    	@Override
    	public void store(OutputStream out, String comments) throws IOException {
    		ByteArrayOutputStream baos = new ByteArrayOutputStream();
    		super.store(baos, (this.omitComments ? null : comments));
    		String contents = new String(baos.toByteArray(), StandardCharsets.ISO_8859_1);
    		for (String line : StringUtils.tokenizeToStringArray(contents, EOL)) {
    			if (!omitComments || !line.startsWith("#")) {
    				out.write((line + EOL).getBytes(StandardCharsets.ISO_8859_1));
    			}
    		}
    	}
    
    	@Override
    	public void store(Writer writer, String comments) throws IOException {
    		StringWriter stringWriter = new StringWriter();
    		super.store(stringWriter, (this.omitComments ? null : comments));
    		String contents = stringWriter.toString();
    		for (String line : StringUtils.tokenizeToStringArray(contents, EOL)) {
    			if (!omitComments || !line.startsWith("#")) {
    				writer.write(line + EOL);
    			}
    		}
    	}
    
    	@Override
    	public void storeToXML(OutputStream out, String comments) throws IOException {
    		super.storeToXML(out, (this.omitComments ? null : comments));
    	}
    
    	@Override
    	public void storeToXML(OutputStream out, String comments, String encoding) throws IOException {
    		super.storeToXML(out, (this.omitComments ? null : comments), encoding);
    	}
    
    	/**
    	 * Return a sorted enumeration of the keys in this {@link Properties} object.
    	 * <p>The keys will be converted to strings if necessary using
    	 * {@link String#valueOf(Object)} and sorted alphanumerically according to
    	 * the natural order of strings.
    	 */
    	@Override
    	public synchronized Enumeration<Object> keys() {
    		Set<Object> sortedKeys = new TreeSet<>();
    		for (Object key : super.keySet()) {
    			sortedKeys.add(String.valueOf(key));
    		}
    		return Collections.enumeration(sortedKeys);
    	}
    
    	/**
    	 * Return a sorted set of the entries in this {@link Properties} object.
    	 * <p>The entries will be sorted based on their keys, and the keys will be
    	 * converted to strings if necessary using {@link String#valueOf(Object)}
    	 * and compared alphanumerically according to the natural order of strings.
    	 */
    	@Override
    	public Set<Entry<Object, Object>> entrySet() {
    		Set<Entry<Object, Object>> sortedEntries = new TreeSet<>(entryComparator);
    		sortedEntries.addAll(super.entrySet());
    		return Collections.synchronizedSet(sortedEntries);
    	}
    
    }

    @vpavic, @dsyer, @wilkinsona, please let me know your thoughts.

    Cheers,

    Sam

  13. added
    type: enhancement
    and removed
    status: pending-design-work
    on May 27, 2019
  14. vpavic commented on May 27, 2019

    @vpavic
    ContributorAuthor

    Thanks @sbrannen, looks good at first glance.

    My only concern is that this basically limits the use to SortedProperties instances, meaning you'd have to be control of both creation and persistence of properties. It would be nice if SortedProperties could wrap regular Properies instance so that we're be able to write it repeatable manner even when we're not in control of creation.

  15. sbrannen commented on May 28, 2019

    @sbrannen
    Member

    It would be nice if SortedProperties could wrap regular Properies instance so that we're be able to write it repeatable manner even when we're not in control of creation.

    OK. I just added that locally. The additional factory method looks like this:

    /**
     * Create a variant of {@link java.util.Properties} that sorts properties
     * alphanumerically based on their keys.
     *
     * <p>This can be useful when storing the {@code Properties} instance in a
     * properties file, since it allows such files to be generated in a repeatable
     * manner with consistent ordering of properties. Comments in generated
     * properties files can also be optionally omitted.
     *
     * <p>The returned {@code Properties} instance will be populated with
     * properties from the supplied {@code properties} object, but default
     * properties from the supplied {@code properties} object will not be copied.
     *
     * @param properties the {@code Properties} object from which to copy the
     * initial properties
     * @param omitComments {@code true} if comments should be omitted when
     * storing properties in a file
     * @return a new {@code Properties} instance
     * @since 5.2
     * @see #createSortedProperties(boolean)
     */
    public static Properties createSortedProperties(Properties properties, boolean omitComments) {
    	return new SortedProperties(properties, omitComments);
    }

    So, I hope that meets all of your needs now.

  16. vpavic commented on May 28, 2019

    @vpavic
    ContributorAuthor

    Looks good, thanks Sam! 👍

  17. wilkinsona commented on May 28, 2019

    @wilkinsona
    Member

    Thanks, @sbrannen. I can confirm that we have no need for storing to XML in a repeatable manner.

  18. sbrannen commented on May 28, 2019

    @sbrannen
    Member

    Oh... well... it turns out that my implementation fully supports XML as well. 😇

  19. sbrannen commented on May 28, 2019

    @sbrannen
    Member

    The new createSortedProperties(...) factory methods in CollectionFactory are now in place. See c39c421 for details, and feel free to try it out in an upcoming snapshot build.

  20. added a commit that references this issue on May 28, 2019
  21. added a commit that references this issue on Sep 17, 2026
    c39c421
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

in: coretype: enhancement

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions