How to define a second environment variable when the first one wasn't resolved in Spring? - spring

Suppose I have configuration property called app.database.url. I need to search for the URL in two places, so I created resolvable property like that:
app.database.url=${APP_DATABASE_URL:DEFAULT_URL}
However instead of searching for the second environment variable with key DEFAULT_URL, spring resolves this property as a string with value DEFAULT_URL. Is is possible to tell spring that the second argument is also should be resolved by environment variables?

I think you can just use this:
app.database.url=${APP_DATABASE_URL:${DEFAULT_URL}}

Related

JMeter Custom Plugin Variable Substitution

Context
I am developing a custom JMeter plugin which generates test data dynamically from a tree like structure.
The editor for the tree generates GUI input fields as needed, and therefore I have no set of defined configuration properties which are set in the respective TestElement. Instead, I serialize the tree as a whole in the GUI class, set the result as one property and deserialize it in the config element where it is processed further during test execution.
Problem
This works just fine, except that JMeter variable/function expressions like ${foo} or ${_bar(..)} in the dynamic input fields are not evaluated. As far as I understand the JMeter source code, the evaluation is triggered somehow if the respective property setters in org.apache.jmeter.testelement.TestElement are used which is not possible for my plugin.
Unfortunately, I was not able to find a proper implementation which can be used in my config element to evaluate such expressions explicitly after deserialization.
Question
I need a pointer to JMeter source code or documentation for evaluating variable/function expressions explicitly.
After I manages to setup the JMeter-Project properly in my IDE, I found org.apache.jmeter.engine.util.CompoundVariable which can be used like this:
CompoundVariable compoundVariable = new CompoundVariable();
compoundVariable.setParameters("${foo}");
// returns the value of the expression in the current context
compoundVariable.execute();

Nifi: Reading external properties in custom Processor

I have updated the variable registry to point to a custom properties file and i am able to read those in my processors using expression language with out any issues.
How ever i want to read them in my Custom Processor's (extending the AbstractProcessor) onTrigger()
I tried flowFile.getAttributes() and context.getAllProperties() and it is not getting picked up.
Appreciate any inputs.
Thanks
To clarify, you want to reference the value of these externally-defined variables inside the application logic of your CustomProcessor#onTrigger() method?
You can:
Load the variable definitions by querying NiFiProperties#getVariableRegistryProperties() or NiFiProperties#getVariableRegistryPropertiesPaths. Once you have a reference to the variable definitions, you can parse and use them as you wish.
You can reference them via the flowfile attributes or processor properties if those attributes or properties support Expression Language and it is appropriately scoped. The PropertyDescriptor will list expressionLanguageSupported() and return an ExpressionLanguageScope, which is an enum consisting of NONE, VARIABLE_REGISTRY, and FLOWFILE_ATTRIBUTES (which also includes the VR).
I don't understand the scenario where you want your code to load custom variables that aren't controllable by the flow administrator, which would be populated via processor properties or flowfile attributes. If you really feel you need to access custom variables that aren't available via the context or flowfile, you can use Option 1 above, but you could also theoretically store those variables in environment variables, System properties, etc.

Exposing attributes in a defined type in puppet

I'd like to access attributes that are inside instances of defined types from other classes/instances.
This is very similar to a question asked on SO before - In Puppet, how can I access a variable/attribute inside a defined type?, however from what I understood the answer was specifically related to accessing parameters as opposed to arbitrary variables.
For example, given the following defined type:
define server (
$server_name = 'my_server'
){
$server_history = 'A long story'
}
I can successfully use getparam(...) to fetch server_name but I cannot do the same for server_history.
Also, if server was a class as opposed to to a defined type, accessing this variable is straightforward using something like server::serverhistory
Does anyone have any ideas on how to expose these variables? Or am I approaching this completely the wrong way?
Edit: For some higher level context on what I'm trying to do my server type gets instantiated by 3 other classes. A variable in the server type builds out some directory paths based on parameters provided to it by these classes (which naturally, are specific to those classes). There are some other classes that would like to use the directory path variable to place files there.
You ask
I'd like to access attributes that are inside instances of defined types from other classes/instances.
and you go on to clarify that you're after
arbitrary variables.
In fact, ordinary variables in the body of a defined type are not attributes of that type, nor of any instance thereof. They are not part of the accessible persistent state of instances of such types at all.
More generally, Puppet treats defined types just like native types in almost every observable way, but by the same token, it does not provide any features that serve to distinguish defined types as a special case. You are looking for such a feature, and it does not exist.
Since your design idea will not work, you'll need to think of an alternative. You say
my server type gets instantiated by 3 other classes. A variable in the server type builds out some directory paths based on parameters provided to it by these classes (which naturally, are specific to those classes). There are some other classes that would like to use the directory path variable to place files there.
Since the paths you're after are characteristic of specific classes, it makes sense for them to be accessible directly via those classes. It seems odd to me that you would even want to access them indirectly via resources declared by those classes.

How to define #Value as optional

I have the following in a Spring bean:
#Value("${myValue}")
private String value;
The value is correctly injected. However, the variable needs to be optional, it is passed in as a command line parameter (which is then added to the Spring context using a SimpleCommandLinePropertySource), and this argument will not always exist.
I have tried both the following in order to provide a default value:
#Value("${myValue:}")
#Value("${myValue:DEFAULT}")
but in each case, the default argument after the colon is injected even when there is an actual value - this appears override what Spring should inject.
What is the correct way to specify that #Value is not required?
Thanks
What is the correct way to specify that #Value is not required?
Working on the assumption that by 'not required' you mean null then...
You have correctly noted that you can supply a default value to the right of a : character. Your example was #Value("${myValue:DEFAULT}").
You are not limited to plain strings as default values. You can use SPEL expressions, and a simple SPEL expression to return null is:
#Value("${myValue:#{null}}")
If you are using Java 8, you can take advantage of its java.util.Optional class.
You just have to declare the variable following this way:
#Value("${myValue:#{null}}")
private Optional<String> value;
Then, you can check whether the value is defined or not in a nicer way:
if (value.isPresent()) {
// do something cool
}
Hope it helps!
If you want to make the configuration property optional just pass an empty string like this:
#Value("${app.optional.value:}")
I guess you are you using multiple context:property-placeholder/ declarations?
If so, this is a known issue since 2012, but not fixed, apparently due to both lack of interest and no clean way of fixing it. See https://github.com/spring-projects/spring-framework/issues/14623 for discussion and some ways to work around it. It's explained in an understandable way by http://www.michelschudel.nl/wp/2017/01/25/beware-of-multiple-spring-propertyplaceholderconfigurers-and-default-values/

Which method is called to initialize a Maven plugin before its execution?

I'm writing a Maven plugin and would like to convert a set of strings inputted as parameter excludes to a pattern before the plugin execution. I implemented the interface org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable but when I access in the initialize method the parameter excludes is null in the execute method is not null.
Which method is called to initialize a Maven plugin before its execution and has access to the parameters?
The best recommendation i can give is to take a deep look into the documentation which will give you the advice to define some kind of parameters and may be some default values for different parameters. The first method is usually the execute() method of the AbstractMojo class which you have to inherit from. If you need some kind of default values just check the parameters if they haven't been initialized (which means being null) and give the values you like for them.

Resources