Reading configuration from config files#1722
Conversation
|
I think that this approach makes sense in that it lets configuration come from the environment, system properties, file, or classpath. I think that's a good thing and that we should follow this pattern. For actually checking the setting of various flags, however, I think that the config class should parse all this input and set boolean (or enum) values that can be checked directly, rather than relying on a hash lookup. I think that can give us two advantages:
|
|
I think I can implement this next week or so... |
|
If you're looking for the kinds of debug flags I'd like to potentially replace with a real debugging mechanism, I would look at these: And as for feature flags, the first one IMO should be for the reflect and proxy support that's currently languishing in a PR by @rbri |
|
Unfortunately, I didn't have as much time as I thought, but I wanted to update the draft before Christmas of how I want to do it now. The idea is, to check all sources (classpath, configfile, system-properties, env - the last one wins) for the settings and parse them immediately in the correct datatype. When a variable is bound to the property @gbrail I hope, this goes in the right direction... Unfortunately (or fortunately) I won't be able to continue until next year due to vacation. :) |
|
So, I found some time to update this PR. What do we have now The
When checking for a value, There is a helper class
I tried that, but did not like the idea to add configs from different packages in one class:
Other findings, I identified:
it would be good, if I get some feedback, if this goes in a way we want. @gbrail FYI |
|
@gbrail ping... Did you find already time to give some feedback? |
gbrail
left a comment
There was a problem hiding this comment.
I think this is good progress, and minus the attached minor comments, I think that this is the right implementation.
I also want to know how we can test this -- there are a lot of options with properties files, system properties, environment variables, and a service loader.
I really think that we should go forward with this, but only if we can put in some way to exercise more of this codebase in our test suite.
| if (ret != null) { | ||
| Class<T> enumType = (Class<T>) defaultValue.getClass(); | ||
| // We assume, that enums all are in UPPERCASES | ||
| return Enum.valueOf(enumType, ret.toString().toUpperCase(Locale.ROOT)); |
There was a problem hiding this comment.
Should we catch "IllegalArgumentException" in case the input value is not valid and return the default in that case?
| if (ret instanceof Boolean) { | ||
| return (Boolean) ret; | ||
| } else { | ||
| return "1".equals(ret) || "true".equals(ret); |
There was a problem hiding this comment.
I wonder if you'd consider numeric values other than 1 as "true," and doing a case-insensitive comparison to "true"
| props.load(new InputStreamReader(in, StandardCharsets.UTF_8)); | ||
| addConfig(props); | ||
| } catch (IOException e) { | ||
| System.err.println( |
There was a problem hiding this comment.
I don't love that we "println" anywhere, but I don't see that we have a choice in this case. But if it happens, it'll be buried in someone's big log file and they won't know what it means -- should we consider adding "Rhino:" or something to identify this message so that people can know why it's appearing?
| props.load(new InputStreamReader(in, StandardCharsets.UTF_8)); | ||
| addConfig(props); | ||
| } catch (IOException e) { | ||
| System.err.println( |
There was a problem hiding this comment.
Same here -- we should add "Rhino" or something to this.
c09c606 to
5799ee2
Compare
rPraml
left a comment
There was a problem hiding this comment.
From my perspective, this is ready for merge.
I would like to add some documentation to https://rhino.github.io
Can anyone tell me, how? (i did not find the repo/instruction how to contribute)
@gbrail FYI
| public static void main(String args[]) { | ||
| try { | ||
| if (Boolean.getBoolean("rhino.use_java_policy_security")) { | ||
| if (RhinoConfig.get("rhino.use_java_policy_security", false)) { |
There was a problem hiding this comment.
Q: Should we change this to
| if (RhinoConfig.get("rhino.use_java_policy_security", false)) { | |
| if (RhinoConfig.get("rhino.useJavaPolicySecurity", false)) { |
A: Probably not, as this will break other things!
And JavaPolicySecurity has to be removed anyway in the future (see #1353)
| private static String[] CONFIG_FILES = {"rhino.config", "rhino-test.config"}; | ||
| private List<Map<?, ?>> configs = new ArrayList<>(); | ||
| // this allows debugging via system property "rhino.debugConfig" | ||
| private final boolean debug = Boolean.getBoolean("rhino.debugConfig"); |
There was a problem hiding this comment.
I've added this flag in a separate commit in this PR. It will help to trouble-shoot any loading issues. So I find this useful and would be happy, if we can keep this
| * this module will also use this loader. <br> | ||
| * The loader does NOT load the properties from the default locations as mentioned in the | ||
| * documentation. So, do not wonder, if setting config values in 'rhino.config' or | ||
| * 'rhino-test.config' do not take effect. Instead use `rhino-config-for-this-module.config`. |
There was a problem hiding this comment.
Testing is a bit difficult, so I tried to cover all aspects in this tests
| if (k.startsWith("rhino.")) { | ||
| systemProperty k, v | ||
| } | ||
| } |
There was a problem hiding this comment.
I copy all rhino system properties to the sub process. So you can pass properties with -Drhino.xyz from a gradle buld
There was a problem hiding this comment.
I think this PR is looking very good, but why do we have this file and the other .config files in the top-level directory? I suppose that we need one just to test the file reading part of the codebase, but since they won't be used by the runtime or tests otherwise, do we really need all of them? That's really my only concern with this PR at this point. Thanks!
There was a problem hiding this comment.
Hello @gbrail
I also want to know how we can test this -- there are a lot of options with properties files, system properties, environment variables, and a service loader.
This is the problem, why I need so much test files. Let me explain for what they are.
rhino/rhino-config-for-this-module.config
The file rhino-config-for-this-module.config is used internally by RhinoPropertiesTest.Loader and is the only config-file loaded in this module for tests.
This file, along with rhino/rhino-test.config, serves as a placeholder for where properties should be placed.
For example, if there is a new experimental feature 'xyz' that can be enabled with rhino.experimental.xyz=true, it can be enabled in rhino-config-for-this-module.config or rhino-test.config, so that is available for test cases.
As these files are not included in the build jars, the feature stays disabled, unless someone enables it.
rhino/rhino-test.config
rhino/src/test/resources/rhino-testcase.config
rhino/src/test/resources/rhino.config
These files are used for various tests
tests/rhino-test.config
This file is mainly for documentation/example (there are only comments) and can be used to enable features for testing.
I see the problem, that this is now a bit confusing, but the files are required for the (complex) tests.
I see the following options:
- keep everything as it is (this will confuse people)
- create one (or more)
rhino-config-testsmodule, to test various config ways (with/without ServiceLoader) - remove/rewrite the complex test cases (especially the one with serviceLoader), which means, that the one or other properties-file will be obsolete.
I will prefer option 3 ;)
rPraml
left a comment
There was a problem hiding this comment.
I've refactored the tests a bit. There are currently two top level rhino-test.config files, which are mainly used for documentation (and one test)
I can delete these files, if you want.
Should I add the documentation to https://github.com/rhino/rhino.github.io/blob/main/docs/_docs/configuration.md or is there a better/preferred place?
| # | ||
| # Please keep this values for RhinoPropertiesTest | ||
| test.file.rhino-test-config.loaded=true | ||
| test.config.bar=value4-mod |
There was a problem hiding this comment.
This file is a placeholder/for documentation and is also used to demonstrate, that a top-level file can override values in a classpath file.
Should I delete it (and the related test)?
| # rhino.printTrees=false | ||
| # rhino.printICode=false | ||
| # rhino.debugLinker=false | ||
| # rhino.use_java_policy_security=false |
There was a problem hiding this comment.
This file is really only a placeholder/for documentation. Should I delete it?
|
Sorry, lost track of where in the thread to comment. I think it's fine to leave "rhino.test-config" files in the "rhino" and "tests" modules. Could you please add a comment explaining why they're there, and say that it's an example of how to use these files, or that people could uncomment them in order to do further testing? Once you do that I think that we should merge this. As for the docs, I think it'd be great if you updated the docs on "rhino.io". @p-bakker owns that repo and I'm sure he'd accept a PR. |
|
Do you have time to look at the two warnings that come from ErrorProne? /home/runner/work/rhino/rhino/rhino/src/main/java/org/mozilla/javascript/config/RhinoProperties.java:173: warning: [AnnotateFormatMethod] This method uses a pair of parameters as a format string and its arguments, but the enclosing method wasn't annotated @FormatMethod. Doing so gives compile-time rather than run-time protection against malformed format strings. I can look at them myself later if you'd like, but I'm trying to keep the code clean of those warnings so I'd like to see if you have a chance to fix the underlying issue first. Thanks! |
|
Hello @gbrail, tried to fix this, but
In this case I would accept the offer and leave the fix to you ;) Roland |
|
OK -- I'll dig deeper into ErrorProne later in the week and fix it, and
will merge what you already have soon. Thanks!
…On Mon, Feb 10, 2025 at 10:39 PM Roland Praml ***@***.***> wrote:
Hello @gbrail <https://github.com/gbrail>,
tried to fix this, but com.google.errorprone.annotations.FormatMethod is
not on the classpath.
I see two options:
- add errorprone dependency (maybe in an extra PR?)
- add @SuppressWarnings("AnnotateFormatMethod") in the meantime.
I can look at them myself later if you'd like, but I'm trying to keep the
code clean of those warnings so I'd like to see if you have a chance to fix
the underlying issue first. Thanks!
In this case I would accept the offer and leave the fix to you ;)
Roland
—
Reply to this email directly, view it on GitHub
<#1722 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAD7I23G2OY7CFXQDHET3XT2PGLKLAVCNFSM6AAAAABRNBCUF2VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDMNBZHEZTQNJUGM>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
|
Thanks for your work on this and I'll look at the warning soon. |
Documentation for this PR mozilla/rhino#1722
This could be a first draft for #1720