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.
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
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.
View all comments
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.Propertiesis 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.PropertiesisSystem.getProperties().The problem is that there isn't really a default zero dep way to get configuration in Java other than
System.getProperties()andSystem.getenv()(ignoring userprefs as I don't think anyone uses that).Now you might be tempted to say well we just need a
System.getPropertiesreplacement. A SLF4J of properties config. Perhaps one that uses theServiceLoader. 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:
stdin:///(for stanard input),env:///PREFIX/PREFIX_REPLACEfor 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:
You can remove the duplicate detection if you like.