How to use property-placeholder for file on filesystem - java

We used to have a way to load properties from a file on the classpath:
<context:property-placeholder location="classpath:myConfigFile.properties" />
and it worked great. But now we want to load properties from a specific file on the system that is NOT in the classpath. We wanted to be able to dynamically load the file, so we are using a Java environment variable to populate it. I'll give a simple example below:
In Java:
System.setProperty("my.prop.file", "/path/to/myConfigFile.properties");
In Spring XML:
<context:property-placeholder location="${my.prop.file}" />
I've also tried it this way, thanks to an idea from Luciano:
<context:property-placeholder properties-ref="prop" />
<util:properties id="prop" location="reso"/>
<bean id="reso" class="org.springframework.core.io.FileSystemResource">
<constructor-arg index="0" value="${my.prop.file}" />
</bean>
Everything I've tried has failed. No matter what I set my.prop.file to. Greatest hits include:
<context:property-placeholder location="/path/to/myConfigFile.properties" />
(ClassNotFoundException: .path.to.myConfigFile.properties)
<context:property-placeholder location="file:/path/to/myConfigFile.properties" />
(ClassNotFoundException: file:.path.to.myConfigFile.properties)
<context:property-placeholder location="file:///path/to/myConfigFile.properties" />
(ClassNotFoundException: file:...path.to.myConfigFile.properties)
How do you use property-placeholders with a location that is on the file system and NOT on the classpath? We are using Spring 3.0.5.
It turns out there was a problem with the script running the Java program that loads the spring file. Thank you for helping. I am going to request that this question be deleted, as the original code works after all.
Thank you for your help.

This did work for me:
<context:property-placeholder location="file:/path/to/myConfigFile.properties" />
But this (interestingly) did not:
<context:property-placeholder location="#{ systemProperties['foo'] }" />
I got
java.io.FileNotFoundException: Could not open ServletContext resource [/#{ systemProperties['foo'] }]
You're not going to be able to use a property placeholder ${..} with the definition of a PropertyPlaceholderConfigurer. It's the chicken before the egg.
You can always subclass PropertyPlaceholderConfigurer and have it do whatever you want (for example, call setLocations() in a #PostConstruct method. Then, instead of using <context:property-placeholder>, use:
<bean class="com.foo.MyPropertyPlaceholderConfigurer"/>

What about this way?
<context:property-placeholder properties-ref="prop" />
<util:properties id="prop" location="reso"/>
<bean id="reso" class="org.springframework.core.io.FileSystemResource">
<constructor-arg index="0" value="/yourpathandfile" />
</bean>

Related

Read PropertyPlaceholderConfigurer classpath Spring with Jboss

Using PropertyPlaceholderConfigurer to externalize spring-configuration
properties.
Added following code to spring-servlet.xml
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:environment.properties</value>
</property>
</bean>
Filter to be externalized from spring-security.xml
<security:custom-filter position="AUTH_FILTER" ref="${filter}" />
filter value is present in environment.properties
environment.properties file is present inside Jboss modules and is readable from code using resource bundle.
But, with these changes somehow properties file is not getting loaded and following error is thrown while publishing code.
Caused by: java.lang.IllegalArgumentException: Could not resolve
placeholder 'filter' in string value "${filter}"
PS:
Also tried hardcoding path as <value>file:${jboss.home.dir}/modules/system/layers/base/configuration/main/environment.properties</value> but, dosen't seems to be working.
I think your problem is that your spring-servlet.xml is not linked to your spring-security.xml. So spring-security.xml has no knowledged of the PropertyPlaceholderConfigurer.
IMO, you should configure PropertyPlaceholderConfigurer in a properties-context.xml (for instance) so you can import this new file into your spring-servlet.xml and spring-security.xml as following:
<import resource="classpath:properties-context.xml" />

SFTP Inbound channel Adapter does not copy file when local directory has any content

I am trying to use Spring Batch Integration with Spring Batch programs that I have constructed already, but at this point I am just trying to get a working sample of Spring Integration with help from Spring IO and the Spring Example on GITHUB.
I have customized the sample program to copy all the files on the remote directory to local at one shot, and few other subtle changes. The program works pretty much fine, though it works if and only if the local directory I am copying to has no content in it.
I tried to rename the files and run the program but still the same issue. Even if the local directory has the hidden .DS_Store file the program does not copy the contents i.e., files from the remote (SFTP) directory. I would like to try out few scenarios:
Copy files even if there are hidden files
Overwrite the files with the same name
Copy only a subset of files i.e., copy only the files that are not already with the same name
Pardon my ignorance, I seem to miss out setting some property on the tag.
My XML file is pretty much as below:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:int="http://www.springframework.org/schema/integration"
xmlns:int-sftp="http://www.springframework.org/schema/integration/sftp"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/integration/sftp http://www.springframework.org/schema/integration/sftp/spring-integration-sftp.xsd">
<context:property-placeholder location="classpath:user.properties"/>
<bean id="sftpSessionFactory" class="org.springframework.integration.sftp.session.DefaultSftpSessionFactory">
<property name="host" value="${host}"/>
<property name="port" value="22"/>
<property name="user" value="${user}"/>
<property name="password" value="${password}"/>
</bean>
<int-sftp:inbound-channel-adapter id="sftpInbondAdapter"
channel="receiveChannel"
session-factory="sftpSessionFactory"
local-directory="file:${local.directory}"
remote-directory="${remote.directory}"
auto-create-local-directory="true"
delete-remote-files="false"
filename-pattern="*.txt">
<int:poller max-messages-per-poll="-1" fixed-rate="1000" />
</int-sftp:inbound-channel-adapter>
<int:channel id="receiveChannel">
<int:queue/>
</int:channel>
</beans>
Here's the Test program:
public class SftpInboundReceiveSampleTest {
#Test
public void runDemo(){
ApplicationContext context = new ClassPathXmlApplicationContext("/META-INF/spring/integration/SftpInboundReceiveSample-context.xml", this.getClass());
PollableChannel localFileChannel = context.getBean("receiveChannel", PollableChannel.class);
System.out.println("Received first file message: " + localFileChannel.receive());
}
}
Update:
Operating System: Mac OS X 10.9.3
Spring Integration version: 4.0.0.RELEASE
Log: here
It has been reported before that files are not copied if the local directory is not empty, but I don't see in the code how that's possible and we have never been able to reproduce the issue. I don't recall the outcome of the previous question(s).
So:
Should just work - can you turn on DEBUG logging to see if it provides any clues? Please also indicate operating system and Spring Integration version.
Files are always copied (by default) and overwrite the local version; however, they won't generate messages if the file is already processed by this instance of the app (the default local-filter is an AcceptOnceFileListFilter). Since 3.0, you can set the local-filter to an AcceptAllFileListFilter to always generate a message - but you need to remove the file when it's processed to avoid it being found over and over again on every poll. Or, use a FileSystemPersistentAcceptOnceFileListFilter which stores the lastModified date as well as the file name, so can detect the file has changed.
In order to prevent duplicate files being fetched (regardless of #2), instead of a filename-pattern, use a CompositeFileListFilter that delegates to (a) an SftpPersistentAcceptOnceFileListFilter and an SftpSimplePatternFileListFilter; the file will only be fetched if it passes both filters.
PS: I just ran a test on OS X to a directory /Users/foo/bar where bar has a .DS_Store and it worked just fine.
EDIT:
Here is my config that does 1, 2, and 3...
<int-sftp:inbound-channel-adapter id="sftpInbondAdapter"
channel="receiveChannel"
session-factory="sftpSessionFactory"
local-directory="/Users/.../Development/tmp"
remote-directory="foo"
auto-create-local-directory="true"
delete-remote-files="false"
filter="filter">
<int:poller fixed-rate="1000" max-messages-per-poll="1"/>
</int-sftp:inbound-channel-adapter>
<int:channel id="receiveChannel">
<int:queue/>
</int:channel>
<bean id="filter" class="org.springframework.integration.file.filters.CompositeFileListFilter">
<constructor-arg>
<list>
<bean class="org.springframework.integration.sftp.filters.SftpPersistentAcceptOnceFileListFilter">
<constructor-arg>
<bean class="org.springframework.integration.metadata.PropertiesPersistingMetadataStore" />
</constructor-arg>
<constructor-arg value="foo" />
</bean>
<bean class="org.springframework.integration.sftp.filters.SftpSimplePatternFileListFilter">
<constructor-arg value="*.txt" />
</bean>
</list>
</constructor-arg>
</bean>
...
// logs showing existing present files sent to the channel
...
16:44:54.465 WARN [task-scheduler-3][com.jcraft.jsch] Permanently added '10.0.0.3' (RSA) to the list of known hosts.
16:45:06.372 DEBUG [task-scheduler-3][org.springframework.integration.sftp.inbound.SftpInboundFileSynchronizer] 2 files transferred
16:45:12.704 DEBUG [task-scheduler-3][org.springframework.integration.endpoint.SourcePollingChannelAdapter] Received no Message during the poll, returning 'false'
16:45:40.056 DEBUG [task-scheduler-4][org.springframework.integration.sftp.inbound.SftpInboundFileSynchronizer] 0 files transferred
Notice the 2 files transferred and 0 files transferred.
I don't see any logs for SftpInboundFileSynchronizer in your logs. Note that I had to put a sleep in the test app - if you are using the same app; it terminates before synchronizing any files if files exist already; put a Thread.sleep() in there to stop it terminating.

Load internal (Classpath) and external property file in a Spring Application

I need to load both external and internal property files in my spring application. Once I declare the external file as below
<context:property-placeholder location="file:${JBOSS_HOME}/123.properties" />
I can access the properties defined in this external file. But all properties related to the property file in my class path Could not resolved.
My Application Context
** <!--Refer External File --> **
<context:property-placeholder location="file:${JBOSS_HOME}/123.properties" />
<!--Refer Internal File -->
<bean id="helloWorldBean"
class="com.javacodegeeks.snippets.enterprise.services.HelloWorld">
<property name="internalProperty1" value="${internalProperty1}" />
<property name="**externalProperty**" value="${**externalProperty**}" />
</bean>
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>constants.properties</value>
</property>
</bean>
I am getting property value of the external property file but not the value of the internal property file.
Exception in thread "main"
Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name 'helloWorldBean' defined in class path resource [applicationContext.xml]: Could not resolve placeholder 'internalProperty1' in string value "${internalProperty1}"
at org.springframework.beans.factory.config.PlaceholderConfigurerSupport.doProcessProperties(PlaceholderConfigurerSupport.java:209)
at org.springframework.context.support.PropertySourcesPlaceholderConfigurer.processProperties(PropertySourcesPlaceholderConfigurer.java:174)
at org.springframework.context.support.PropertySourcesPlaceholderConfigurer.postProcessBeanFactory(PropertySourcesPlaceholderConfigurer.java:151)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:694)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:669)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:461)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplsamicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at com.javacodegeeks.snippets.enterprise.App.main(App.java:13)
Cannot I load external(non-class path) and internal (Class path) property file together ?
What you need is something like this:
<!--Order matters, properties in the second file will override the first -->
<context:property-placeholder location="file:${JBOSS_HOME}/123.properties,classpath:configuration.properties"
ignore-unresolvable="false" ignore-resource-not-found="true" />

Using/converting an XML file to a Spring bean definition

I have defined a Spring application context xml which will be edited by end users to add new beans.Something like:
<bean name="myBeanName1" class="com.xxx.Yyy">
<property name="type" value="type1" />
<property name="name" value="name1" />
<property name="arguments" value="arg1" />
</bean>
<bean name="myBeanName2" class="com.xxx.Yyy">
<property name="type" value="type2" />
<property name="name" value="name2" />
<property name="arguments" value="arg2" />
</bean>
.
.
.
Now I am asked to change this to a normal xml so that users wont be bothered by bean property and class names:
<def name="Name1">
<type>type1</type>
<argument>arg1</argument
</def>
<def name="Name2">
<type>type2</type>
<argument>arg2</argument
</def>
As my code is using the bean, how can I use the new xml with minimal code change so that it is converted to a bean definition as earlier and things work as before?.
I dont know if Spring has a solution for this out of the box. What I thought was applying stylesheet to the new xml to create my older bean. Any other better/elegant solution for this?
Edit: Now that user input is not inside the bean anymore, is it possible to use the new xml to inject to a #Component class?
Spring supports creating custom tags. You need to create xsd schema, NamespaceHandlerm, implement BeanDefinitionParsers and make spring aware of these by creating spring.handlers & spring.schemas special files.
Have a look at Extensible XML authoring
Example:
<beans xmlns declaration goes here>
<yournamespace:yourcustomtag id="some id">
<yournamespace:some_other_tag your-custom-attribute="some value" />
</yournamespace:yourcustomtag>
</beans>

finding a property using Spring and Webapp properties placeholder

This is a "simple" problem and I am seeking both a how-to and/or a you're-dumb-don't-do-that. I am open to both.
I am building a war file and want the structure to be:
WEB-INF/
properties/
<my properties files>
classes/
...
spring/
<my spring files>
Is that dumb? I know that I can access the properties files though the property-placeholder but I'd rather not nest the properties in the classes section - it doesn't make sense to me.
So the Spring file looks like this:
<context:property-placeholder location="classpath:properties/*.properties" />
if I want to access them in the classes area. I thought that
<context:property-placeholder location="properties/*.properties" />
would let me just put the directory under WEB-INF directly...am I wrong (ps I think I am :) ).
Any advice?
This should work
<context:property-placeholder location="WEB-INF/properties/*.properties" />
WEB-INF is not the root of the of the web-app, so you need to add WEB-INF to the path.
spring-context-3.1.xsd
<xsd:attribute name="location" type="xsd:string">
<xsd:annotation>
<xsd:documentation>
<![CDATA[
The location of the properties file to resolve placeholders against, as a Spring
resource location: a URL, a "classpath:" pseudo URL, or a relative file path.
Multiple locations may be specified, separated by commas. If neither location nor properties-ref is
specified, placeholders will be resolved against system properties.
]]>
</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
You can't do it the way you want since the classpath for the Classloader will be the /classes directory and any jars in the /lib directory. This is the standard configuration for a war file.
Wars and ears have specific configurations which you have to follow for the files to be valid. If you think about it, it would make it difficult to have different vendors provide web containers that could deploy the same war file if there was no standard format. There is a pretty informative page here.
To achieve something similar to what you want, you can simply have directories of /classes/properties and /classes/spring and look them up appropriately from your classpath ("classpath:properties/myfile.properties).
I am not sure what you want to achieve. Here the method I use to inject the properties from a basic properties file to a bean:
In the spring files (XML bean definitions), I add the reference to my property file (myfile.properties):
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:myfile.properties" />
</bean>
and then I add my references to the properties (db.url is the URL address for my database connection, I kept only the bean properties referenced in my property file).
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<!-- results in a setDriverClassName(String) call -->
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url"><value>${db.url}</value></property>
<property name="username"><value>${db.login}</value></property>
<property name="password"><value>${db.password}</value></property>
</bean>
By default, if the property is not defined in the property file, Spring uses the System Properties (this behaviour can be changed).

Categories