Spring: locate the folder with properties using systemEnvironment variable - spring

I have a Spring project named 'finman'.
The directory structure
As you can see on the image the folder with properties (named 'config') is on the same level as 'src'. So I need to locate that path using
systemEnvironment['FINMAN_ROOT']
The code fragment from 'spring-mvc.xml' responsible for the resource bundle location:
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"
p:cacheSeconds="60"
p:defaultEncoding="UTF-8">
<property name="basenames" value="file:///#{systemEnvironment['FINMAN_ROOT']}/config/messages/app"/>
</bean>
The problem is that I don't know how to set that variable.

Environment variables set in OS:
Windows: https://www.chem.gla.ac.uk/~louis/software/faq/q1.html
Linux: http://www.cyberciti.biz/faq/set-environment-variable-linux/
After it do not forget to restart java (or IDE), as they get environments, when started.

Related

How to get the host name in spring configuration file?

Is there any easy way to get the host name in spring configuration file ? Currently I am using Java code to get the host name and and auto wire the property in the bean . But looking for less coding approach if any !
Thanks
The following will give you the hostname in java
return InetAddress.getLocalHost().getHostName();
where InetAddress belongs to the java.net package. You can add that to your java configuration file. If you want to do it in xml, you can do the following
<bean id="localhostInetAddress"
class="java.net.InetAddress"
factory-method="getLocalHost"/>
<bean id="hostname"
factory-bean="localhostInetAddress"
factory-method="getHostName"/>

Using property placeholder with Spring Expression Language

When my FTP mput transfer is successfully complete, I want to rename the file in the local directory. For this, I need to use the local dir path from PropertyPlaceholderConfigurer. But this doesn't seem to be working. Please can you suggest the syntaxt to expand the value of the property? ${local.request.dir} represents a directory path like /home/jainr/REQUEST.
<int-ftp:request-handler-advice-chain>
<bean id="requestFileRename" class="org.springframework.integration.handler.advice.ExpressionEvaluatingRequestHandlerAdvice">
<property name="trapException" value="true" />
<property name="onSuccessExpression" value="T(org.apache.commons.io.FileUtils).moveFile(new java.io.File(#{${local.request.dir}} + '/' + headers['RequestFileName']), new java.io.File(#{${local.request.dir}} + '/' + headers['RequestFileName'] + '.processed'))" />
</bean>
</int-ftp:request-handler-advice-chain>
You need to define the resolved placeholder as a literal, from a SpEL perspective...
new java.io.File('${local.request.dir}/' + headers...
Notice I also removed the #{...} - that is initialization time SpEL - this is runtime SpEL.
In future, instead of statements like...
But this doesn't seem to be working.
...provide the error message and/or stack trace.

How can I specify location of property file in OSGi blueprint?

I would like to specify location of property file, from which OSGi blueprint property placeholder should read properties values:
<cm:property-placeholder persistent-id="myBundle"/>
<bean
id="myCoolBean"
class="test.properties.MyCoolBean">
<property
name="echo"
value="${echo}"/>
</bean>
UPDATE:
Configuration felix.configadmin + felix.fileinstall works for me.
I installed:
org.apache.felix.configadmin-1.8.0.jar
org.apache.felix.fileinstall-3.1.4.jar
org.eclipse.equinox.cm-3.2.0.jar
I specified VM argument -Dfelix.fileinstall.dir=C:/eclipse/config
The myBundle.cfg file has value:
echo=Echo
The property placeholder in blueprint does not work with files. Instead it uses the persistent id to retreive a config from ConfigurationAdmin service.
So the solution is to install felix config admin together with felixfileinstall. So configs will be retrieved from a folder and updated in ConfigurationAdmin.
In apache karaf this is already configured but you can also do it on your own. See my karaf tutorial about config admin.
If you want to go with plain felix then you can take a look what karaf does to solve it. So for example in config.properties there are the settings for felix fileinstall. There you have to e.g. set he directory containing your configs. In plain felix that would be framework properties.
This is what karaf sets:
felix.fileinstall.enableConfigSave = true
felix.fileinstall.dir = ${karaf.etc}
felix.fileinstall.filter = .*\\.cfg
felix.fileinstall.poll = 1000
felix.fileinstall.noInitialDelay = true
felix.fileinstall.log.level = 3

Configuring Spring batch xml

Issue: Using spring batch, i need to read a file which has todays date. E.g test_02032015.txt.This file will be in a directory /test/example. Its an unix environment that i need to fetch file from.
question is how to configure spring batch xml so that above mentioned file is read
Any pointers to relevant website or solution would be of great help.
You have a few ways to address a requirement like this:
If you don't need to worry about the other files in a directory, you can just use a wild card in the file name like this:
<property name="resource" value="data/iosample/input/*.xml" />
Another alternative would be to pass the value into the job as a parameter and reference it like this:
<property name="resource" value="#{jobParameters['input.file']}" />
Finally you could use SpEL to build the file name (Sorry I don't have an example of that handy).

Loading property file from filesystem using util:properties?

When i try to load a property file from the filesystem using util:properties, the property file is not found. I placed it on my root drive and all kinds of places but i seem not to be able to load a property file from the filesystem using util:properties.
Is it possible to do this?
<util:properties id="configProperties" location="file:///d:/config.properties"/>
Loading it from the classpath works ok.. but i want to configure the application so a system admin can change the properties.
<util:properties id="configProperties" location="classpath:/config.properties"/>
I have this in a Linux project and it works fine:
<util:properties id="jdbcConfiguration" location="file:///home/reporting/jdbc.properties"/>
so the problem is probably with the way you specify the path. Try it this way:
<util:properties id="configProperties" location="file:d:/config.properties"/>

Resources