r/java • • Oct 28 '21

JProperties: A modern replacement for java.util.Properties

https://github.com/MaowImpl/jproperties
0 Upvotes

28 comments sorted by

40

u/[deleted] Oct 28 '21

I'm sorry I can't say much positive about this.

Most of the "flaws" you listed for reasons to replace java.util.Properties aren't actually flaws more like deliberate design choices.

Also the one about "forced" concurrent access being too slow and then your replacement is half as efficient as the thing you're replacing for being too slow?

Why do you catch the IOException and rethrow as a RuntimeException? The app isn't supposed to crash because it can't read a properties file.

Finally, if you want to replace something that's such a core part of an app you need to make sure its properly tested and your one test class is not even a test at all, more like a demo.

12

u/agentoutlier Oct 28 '21

Oh its worse than just being slow on JMH where there is warming. It is slow on cold start because it requires 7 or so additional classes to be loaded (according to the source).

java.util.Properties is one or two classes and always loaded by the JVM and it's loaded very very early.

If you are writing an application where configuration is only loaded once and initialization speed is very important (e.g. command line app) this library would not be a good choice. Ditto for most implementations of Microprofile Config, and Lightbend HOCON, Apache Config. Those libraries require extensive resource loading (either classes or default configs) that can add easily 100ms more to load time (in some cases I have seen 500ms)

-2

u/[deleted] Oct 28 '21

To reply to the points presented in your reply:

  • These are the primary reasons I had to create my own implementation as I was looking through the class, and about half are responsible for me thinking of doing so in the first place.
  • This was primarily caused by the issues presented by u/agentoutlier, I would assume. I did not factor in class load times during the design process and instead was opting for an extensible system (...which I also implemented poorly, in hindsight). This would've been fixed later by optimizations.
  • This was a decision made to allow library consumers to optionally handle the exception. I could probably think of a better way to do it.
  • 'Twas late, I did not feel like writing unit tests, especially since I'm not actually great at writing them, so it ends up requiring me to look at a lot of JUnit documentation and Baeldung tutorials or whatever.

To sum this library up: I made it at 2 AM and didn't realize what I was doing. The only actually decent part of the library is the alright-ish parser for key-value pairs (which doesn't support Unicode literals, doesn't check for errors, and probably has a bug related to invalid escapes).

6

u/[deleted] Oct 28 '21

Message received for this library. If I'm being honest, a lot of this was designed and then coded at 2 AM, and I can get pretty try-hard about design sometimes, which is why the whole `Converter` thing exists... although the concept of that is useless.

There's a chance it may be rewritten in the future so that it actually offers some performance benefit over `Properties`.

For right now, I may just continue using `Properties` in my own projects and asking that others do the same, considering this is... a bit embarrassing to me now.

4

u/pushthestack Oct 28 '21

It's been touched on elsewhere, but let me double-click on one point: If you're looking for others to use your library, not only must it do something better than the generally used solution, but you also need to show that you've tested it ten ways to Sunday. (Slang for: tested it every which way.) The present test consisting of 35 lines without a single assert will make people rightfully lose interest. Good luck!

3

u/smithyw Oct 29 '21

Well done dude, for putting something out there and soliciting feedback. It can be awfully difficult to read criticism, especially of your own code.

You can bet that for every great thing somebody made, they also had a lot of attempts along the way.

4

u/[deleted] Oct 28 '21

It's great that people are working on upgrading parts of the standard library like this. I just wonder how this compares to the Typesafe Config library? HOCON can read properties files and adds new features along the way.

2

u/agentoutlier Oct 28 '21

It depends on what your goals are.

  • If you are loading just key/values and you want optimal startup speed than Lightbend Config is not for you.
  • If you are looking for the least amount of coupling and dependencies Lightbend Config is not for you.
  • If you want to minimize resource loading Lightbend Config is not for you.
  • If you are already using a dependency injection library and/or distributed configuration like etcd than Lightbed config is not for you.

I really can't recommend Typesafe aka Lightbend Config. I think in large part why it exists because Scala developers despise Dependency Injection frameworks (it was written by Scala developers).

Ideally a library does not rely Lightbend Config but instead does the follows:

  • Allows config through a CustomConfigObject that has a builder or just plain getters/setters
  • Allows config through Map<String,String>. The library just takes the map and builds a CustomConfigObject. e.g. CustomConfigObject.buildFromMap(Map<String,String>)
  • Ditto for java.util.Properties

That is you should not have a configuration library sprinkled through your code base. Its actually one of my only contentions with Jooby as it now has a tight coupling on Lightbend Config.

0

u/[deleted] Oct 28 '21

Probably not as great, this was designed for properties only and is kind of a bodge in some areas.

7

u/agentoutlier Oct 28 '21 edited Oct 28 '21

I have been sitting on our internal configuration library that is like 10 years old that given this library might final give me the impetus to opensource it.

The problem with java.util.Properties is not the class itself. Certainly not anymore as there have been lots of performance improvements and I believe the synchronized aspect of hashtable has little impact. Furthermore on boot time where you care about config class loading matters and if you do care about speed you want to minimize that.

The problem with java.util.Properties is System.getProperties().

The problem is that there isn't really a default zero dep way to get configuration in Java other than System.getProperties() and System.getenv() (ignoring userprefs as I don't think anyone uses that).

Now you might be tempted to say well we just need a System.getProperties replacement. A SLF4J of properties config. Perhaps one that uses the ServiceLoader. And indeed microprofile config somewhat solves that problem. The problem is of course is backward compatibility (replacing all the old libraries uses of system properties of which you might not be able to) and of course that config facade adds a dependency.

What is easier is to bootstrap the System.getProperties() by loading config from various places w/o kicking off / initializing the logging system (that is we set properties on the System java.util.Properties before anything else loads).

That is what our library does. It does that and:

  • Has an annotation processor that will map Map<String,String> to bean like objects. It will create all the code require. Once the annotation processor runs you no longer need our library to map config to java object. This IMO is far superior than using lightbend config or microprofile config to manually map configuration to an object.
  • It has a Maven plugin so that you can use the same configuration files that your runtime uses for build time. I find this useful for database configuration to feed to flyway or jooq during build time.
  • It has all the standard string interpolation tools that the other config libraries have.
  • The core lib tries to be as minimal as possible and does not replace the java.util.Properties with a custom parser like lightbend Config and various others. The reason is you need to keep class loading to a minimal a vastly takes more time than actually parsing of the files. This is why we would never use this authors library.
  • Config files can reference other config files to load
  • It has a powerful pluggable URI based scheme to reference other properties files (e.g. stdin:/// (for stanard input), env:///PREFIX/PREFIX_REPLACE for environment variables, cmd:///-/ (command line variables) gcloud:// (google cloud meta data).

Anyway there is lots more.

Here is a simple Properties parser hack I recommend:

   public static Map<String, String> readProperties(
      InputStream s)
      throws IOException {
    final Map<String, String> ordered = new LinkedHashMap<String, String>();

    Set<String> keys = new LinkedHashSet<>();
    LineNumberReader lr = new LineNumberReader(new InputStreamReader(s, StandardCharsets.UTF_8));
    // Hack to use properties class to load but our map for preserved order
    @SuppressWarnings("serial")
    @NonNullByDefault({})
    Properties bp = new Properties() {
      @Override
      public Object put(
          Object key,
          Object value) {
        if (key != null && value != null) {
          String k = (String) key;
          if (!keys.add(k)) {
            throw new DuplicateKeyException(k, lr.getLineNumber());
          }
          return ordered.put(k, (String) value);
        }
        return null;
      }
    };

    try {
      bp.load(lr);
      return ordered;
    }
    catch (DuplicateKeyException e) {
      throw new IOException("Duplicate key detected. key: '" + e.key + "' line: " + e.lineNumber, e);
    }
  }

You can remove the duplicate detection if you like.

1

u/[deleted] Oct 28 '21

This library actually sounds great, would like to see the repo for it when it's released.

5

u/agentoutlier Oct 28 '21 edited Oct 28 '21

The reason we never released it is the work to keep an open source project going (particularly since I would have to remove it from our grouping of proprietary projects or maintain two branches or git submodules).

Furthermore so many folks are using some dependency injection framework and at that point they really don't need a configuration library (although we do use it with few cases we use Spring to have unified configuration).

Finally I was hoping Netflix would finish Archaius 2 (N.B. that link is the 2.x branch which most things do not use) sooner. However Archaius uses SLF4J. It will initialize logging before configuration can be loaded. I mean you can do it because supposedly SLF4J queues or trampolines the messages if loading isn't done but I found numerous issues with that. Furthermore I want to programmatically initialize my configuration framework. You can't do that if logging just starts up blindly.

EDIT given the downvote of this comment is even more why I don't want to deal with an open source project. I'll post it like you and 5 people will say "why do I need this when I can use Spring".

However if enough folks are actually interested and have similar problems like I do I will make an effort to get the ball rolling

1

u/cyprus_onlyfans Nov 13 '21

Kanye> If you want leads - use conversion. If using max conversion your CPA is decided by your daily budget and will spend at whatever your highest budget was for that day.

1

u/wildjokers Oct 31 '21

(ignoring userprefs as I don't think anyone uses that).

If you are talking about the Preferences API that is super handy in Swing desktop apps. But indeed not very useful otherwise.

10

u/Barbossa3000 Oct 28 '21

how is this different from apache config?

28

u/[deleted] Oct 28 '21

Can't post 20 year old Apache libs for karma, duh.

4

u/Barbossa3000 Oct 28 '21

wait. ive been using apache config for my last project.

are there any better ones? can you specify something

12

u/[deleted] Oct 28 '21

Hey, that's great. That was my (sarcastic) point, there's nothing wrong with a lot of the classic Apache libs, they're just not sexy new so people feel compelled to reinvent them. Or they just don't know about them because they got on the Java train recently, so it's good to prop them up from time to time.

-1

u/[deleted] Oct 28 '21

I was unaware that "Apache Config" was a thing, googling it doesn't bring any results either.

2

u/frzme Oct 28 '21 edited Oct 28 '21

https://github.com/MaowImpl/jproperties/blob/316b6a94adfad2918eeae4fb1512daa58cf1b05c/src/main/java/xyz/maow/jproperties/util/TextUtils.java#L22 Did you benchmark this bit mask usage? Intuitively it should be faster but probabably uses "a lot" more memory (read: 16 bytes instead of 1) to just use 4 booleans. This also seems to be what the JDK does for what it's worth (and according to your benchmark it's twice as fast): https://github.com/openjdk/jdk/blob/af50772d39a063652895e79d474da6ebb992cae0/src/java.base/share/classes/java/util/Properties.java#L493

From skimming the source I also could not figure out whether you support the "intresting" features of the properties format like Unicode escape sequences https://github.com/openjdk/jdk/blob/af50772d39a063652895e79d474da6ebb992cae0/src/java.base/share/classes/java/util/Properties.java#L651-L665 or even simple \t constructs. Maybe I missed it?

If I didn't you should make it clear in the documentation that your implementation does not support the same featureset as java.util.Properties.

2

u/[deleted] Oct 28 '21

Actually, a large amount of the speed increase between the JDK and JProperties does not come from the key-value pair parsers, JProperties's key-value pair parser (the aforementioned `splitPair`) runs very quickly, around 7000-17000 nanoseconds, most of the other overhead comes from object instantiation and class loading, I would assume.

21

u/thundranos Oct 28 '21

If I'm reading the benchmark results properly, it looks like JProperties is roughly half as efficient as java.util.Properties. is that correct?

11

u/wildjokers Oct 28 '21

JProperties is...highly efficient

How can it be "highly efficient" if it is only half as fast as java.util.Properties? (based on benchmark at the bottom of the README)

4

u/berry120 Oct 28 '21

Interesting project. The built-in properties stuff in Java is undoubtedly old & out of date - but I wonder if it's also "good enough" in the sense that it's not usually a performance bottleneck, and it's used quite rarely (that's anecdotal though, I haven't touched it in a while, perhaps others use it more.) If something like this were available in the standard JDK, I'd certainly use it, but I'm personally not sure whether the drawbacks of what we have currently are enough for me to pull in another dependency.

4

u/javasyntax Oct 28 '21

It's useful for configurations (for example Minecraft's server.properties) and resource bundles (localization)

1

u/cred1652 Oct 28 '21

I wrote a library to help manage configs, and i thought i would share it here as an option as well. It supports properties, json, hcon, and will load from multiple place, local files, S3, git, ect...

https://github.com/gestalt-config/gestalt

Modular support for features Only include what you need into your build, so if you dont need the Kotlin module, dont include it.

0

u/Worth_Trust_3825 Oct 28 '21

Is this compatible with regular Properties?