How to access Java system properties from Freemarker templates? - java

I started using Freemarker for assembling simple HTML pages, using FMPP Maven plugin. So far so good.
But one thing I need to do is to include value of a system property (one of system properties Maven provides) on a page. Is there a way to access system properties from Freemarker templates?
(if not, I may just have to hack plugin to allow passing values from Maven)

cf https://community.jivesoftware.com/thread/14820
You can access it like this :
${statics['java.lang.System'].getProperty("my.property")}
cf documentation here :
http://freemarker.sourceforge.net/docs/pgui_misc_beanwrapper.html

FMPP has a setting called data that specifies the variables that all templates will see, so that's where you should put the system properties. To put values into there, unless the value can be specified as a simple literal, you need a so called data-loader. So in this case you need a data-loader that returns the system properties as a java.util.Properties object. While there's no data-loader specifically for that, you can use the eval data-loader like this (in your config.fmpp):
data: {
...
sysProps: eval('System.getProperties()')
...
}
Now in your templates you can access the system properties like sysProps["os.name"].
Alternatively, you could write a custom FMPP data-loader. See http://fmpp.sourceforge.net/dataloader.html#sect19.

Related

Cucumber variable parameter store separately

I have scripts written in java, Cucumber gherkin (intellij IDE) .
I would want to revamp it to store all input value in separate parameter file.
So that I can easily replace configurable value at that specific parameter file.
Is that best approach?
How does cucumber handle it, any additional library needed?
You can add qaf-cucumber dependency that will enable this feature you are looking for. It also will add support to resource management for different environment as well as examples from external data-providers. Provided user.name and user.password in properties you can use it in step as below:
Given user login with '${user.name}' and '${user.password}'
Your property file (or xml file) will have following properties
user.name=testUser
encrypted.user.password=encriptedpassword

java.util.Properties ignoring ${...} placeholders

Anyone happen to come come across a use case where one has to stick to java.util.Properties.load method to read all the key-value pairs from a .properties file but at the same time to be environment/profile specific, placeholders, ${...} are used?
I'm building a spring boot app. and have profile specific properties files and placeholders work fine in them. However, the app. is dependent on a relatively older app that reads a property file from java.util.Properties.load method and in doing so the placeholders are being ignored. Since this is an old app. and do not want to change at this point in time, anyone has any suggestions on how do I go about?
If you're using Maven, you can write a generic properties file as such:
prop.1=${val1}
prop.2=${val2}
...
Then using the Filtering feature of the Maven Resources Plugin, you can do the replace your placeholders depdending on your maven profile.

How do I pass variables to JVM that are referenced in .property file?

I have following in my .properties file:
connection1.username=${cloud.db.username}
connection1.password=${cloud.db.password}
I'd like to pass these variables in Run/Debug Configuration
I thought all I need to do is to pass following parameters in JVM command line: -Dcloud.db.username=JOHN -Dcloud.db.password=SECRET.
But in the code call to Properties.getProperty() still returns ${cloud.db.username}.
I'm running it as junit under IntelliJ IDEA.
Passing properties into a jvm using -D puts them into the system properties (accessed via System.getProperties()).
Creating a java.util.Properties from a file in code gives you a completely separate set of properties.
Lots of applications allow you to specify properties via the command line -D option and in a file. To do this they have to implement some sort of fallback mechanism between the two and decide which gets preference.
Also, some applications allow you to define a property as referring to another, they generally do this either by extending or wrapping java.util.Properties.
You're expecting both of these things to work but neither are provided by Java out of the box.
If you want to implement these features, take a look at some open source projects that support them.
I don't fully understand why would you want to do something like that. Why do you need those values in the property file if you are not really using it?
If you want to pass those two parameters as custom command line parameters (-D) then can't you just do:
System.getProperty("cloud.db.username");
System.getProperty("cloud.db.password");
The value for connection1.username will alway be "${cloud.db.username}", java does not replace it with the value of another property or an environment variable.
If you want such a replacement, then you'll have to implement it (iterate through the entire value set of the properties object and replace all values, that match this pattern with the values of the referenced properties)
Trying accessing them via System.getProperty(...) instead

How to edit a velocimacro without restarting velocity?

My velocity macros are being cached and I don't want them to be... not during development at least.
I've set the following properties in my properties file...
velocimacro.library.autoreload=true
file.resource.loader.cache=false
velocity.engine.resource.manager.cache.enabled=false
... but this doesn't seem to have done the trick
Using velocity properties, how can I configure velocity to not cache macros?
(I'm using velocity 1.6.4)
EDIT:
I don't think the line...
velocity.engine.resource.manager.cache.enabled=false
...is relevant to velocity
I have been having the same issue with NVelocity (C# port of velocity). Digging through their souce I found that the re-loading of the macros in the global name space are controlled by the following property.
properties.SetProperty(RuntimeConstants.VM_PERM_ALLOW_INLINE_REPLACE_GLOBAL, true);
I havn't tested this with velocity but looking at their documentation the property exists and seem to do exactly what you need.
Looks like you cant do what you want. The only way I could get macro definitions to reload is to put them in their own library file and set the velocimacro.library.autoreload = true.
From http://velocity.apache.org/engine/devel/developer-guide.html
velocimacro.library = VM_global_library.vm
Multi-valued key. Will accept CSV for value. Filename(s) of Velocimacro library to be loaded when the Velocity Runtime engine starts. These Velocimacros are accessable to all templates. The file is assumed to be relative to the root of the file loader resource path.
velocimacro.library.autoreload = false
Controls Velocimacro library autoloading. When set to true the source Velocimacro library for an invoked Velocimacro will be checked for changes, and reloaded if necessary. This allows you to change and test Velocimacro libraries without having to restart your application or servlet container, just like you can with regular templates. This mode only works when caching is off in the resource loaders (e.g. file.resource.loader.cache = false ). This feature is intended for development, not for production.
I'm not sure this is possible if the macros are not in a velocity library and just in some template file.
However, in this case, if you just want to make development easier, you can just rename the macro (by doing a find/replace all and just adding a number to the end or something). Then you should be able to see the change straight away. You just have to remember to rename it back to what it's supposed to be when your finished!
You might need to set
file.resource.loader.modificationCheckInterval
This tells velocity how often to check if the file has changed. I can't tell from the docs what the default is, but we have ours set to 2 in our dev env. It might just be that the default value for this prop is a high number or less than 0 which is essentially off, meaning it will never check for changes in your macro file.
You may want to use the #define directive instead of #macro. Theses references can change.
With it you can also name a block of VTL code and call it how many times you need it.
Define the macro arguments as variables in the same context and use them within the named block as if it were a macro.
Both can solve common situations; but they are not equivalent.
https://velocity.apache.org/engine/1.7/user-guide.html#define

How can I pass a system properties file to GWT?

Is there a system property or a program argument that I can use to pass the name of a properties file to GWT, to tell GWT to load these into the system properties?
I'm trying to change something like this:
com.google.gwt.dev.GWTShell -Dsysprop1=value1 -Dsysprop2=value2 ...
to something like this:
com.google.gwt.dev.GWTShell -Dgwt.system.properties=/my/properties/file ...
The motivation is to be able to change easily between sets of properties according the runtime environment, for example so that I can have dev.properties and prod.properties property without having to set the properties individually on the startup command.
Aside: there are two other options that will work, but which I'd like to avoid for now:
adding a layer of indirection through which to pick up all of the system properties (because this will be a pain to retrofit)
adding a bootstrapper to load the property set before delegating everything GWT (because I'm lazy, though this might be a reasonable longer term option if GWT doesn't support this out of the box).
There's a great example of how to do this using GWT Generators here.

Categories

Resources