SFTP with DefaultSftpSessionFactory by SSH trust throws exception - java

I have requirement to download/upload all txt file from SFTP server.
I am using Spring configuration as org.springframework.integration.sftp.session.DefaultSftpSessionFactory
and inbound
its throws ServletContext resource not found.
Here password would be blank
<bean id="acceptAllFileListFilter" class="org.springframework.integration.file.filters.AcceptAllFileListFilter" />
<bean id="inboundSftpSessionFactory" class="org.springframework.integration.file.remote.session.CachingSessionFactory">
<constructor-arg ref="inboundDefaultSftpSessionFactory" />
</bean>
<bean id="inboundDefaultSftpSessionFactory" class="org.springframework.integration.sftp.session.DefaultSftpSessionFactory">
<property name="host" value="${sftp.host}" />
<property name="privateKey" value="/home/tech/id_rsa"/>
<property name="privateKeyPassphrase" value="${sftp.private.key.passphrase}"/>
<property name="port" value="${sftp.port}" />
<property name="user" value="${sftp.user}" />
<property name="password" value="${sftp.password}" />
</bean>
....
Caused by: java.io.FileNotFoundException: Could not open ServletContext resource [/home/tech/id_rsa]
at org.springframework.web.context.support.ServletContextResource.getInputStream(ServletContextResource.java:141)
at org.springframework.integration.sftp.session.DefaultSftpSessionFactory.initJschSession(DefaultSftpSessionFactory.java:371)
at org.springframework.integration.sftp.session.DefaultSftpSessionFactory.getSession(DefaultSftpSessionFactory.java:347)
... 27 more
File is exist at specified location
when i tried with password configuration then its working fine.

The file isn't at the specified location, as it tries to load the file from the root of the application and it isn't located there. It is located on the file system but that isn't what you specified.
Prefix the property value with file:.
<bean id="inboundDefaultSftpSessionFactory" class="org.springframework.integration.sftp.session.DefaultSftpSessionFactory">
<property name="host" value="${sftp.host}" />
<property name="privateKey" value="file:/home/tech/id_rsa"/>
<property name="privateKeyPassphrase" value="${sftp.private.key.passphrase}"/>
<property name="port" value="${sftp.port}" />
<property name="user" value="${sftp.user}" />
<property name="password" value="${sftp.password}" />
</bean>
See the reference guide for more information on resource loading.

Related

Java Spring MVC Project external database configuration with .properties file

I am trying external configuration with properties file. dataSource bean is defined in security-context-xml file and hibernate.cfg.xml use them for database informations and sessionFactory.
I want externally dataSource configuration with encrypted password.
Used code following:
<context:property-placeholder
location="classpath:sample.properties" ignore-unresolvable="true" />
<bean id="dataSource"
class="org.apache.commons.dbcp2.BasicDataSource"
destroy-method="close">
<property name="driverClassName"
value="oracle.jdbc.OracleDriver" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation"
value="classpath:hibernate.cfg.xml" />
</bean>
And sample.properties file is:
jdbc.url="*****"
jdbc.username="****"
jdbc.password="****"
This didn't work and throws exception:
Caused by: java.sql.SQLException: Cannot create JDBC driver of class 'oracle.jdbc.OracleDriver' for connect URL '${jdbc.url}'
at org.apache.commons.dbcp2.BasicDataSource.createConnectionFactory(BasicDataSource.java:2023)
at org.apache.commons.dbcp2.BasicDataSource.createDataSource(BasicDataSource.java:1897)
at org.apache.commons.dbcp2.BasicDataSource.getConnection(BasicDataSource.java:1413)
at org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl.getConnection(DatasourceConnectionProviderImpl.java:139)
at org.hibernate.internal.AbstractSessionImpl$NonContextualJdbcConnectionAccess.obtainConnection(AbstractSessionImpl.java:380)
at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.obtainConnection(LogicalConnectionImpl.java:228)
... 56 more

ActiveMQ Data persistent Issue in Java Spring

I have an application which uses embedded activeMQ 5.11. At the start of the application it creates activemq-data\producerBroker\KahaDB folder at the class path location. I do want to change the location but spring.xml doesn't take a location.
Spring.xml as given,
<bean id="producerBroker" class="org.apache.activemq.broker.SslBrokerService">
<property name="brokerName" value="producerBroker" />
<property name="persistent" value="true" />
<property name="persistenceAdapter" ref="persistenceAdapter"/>
<property name="transportConnectors">
<list>
<bean class="org.apache.activemq.broker.TransportConnector">
<property name="name" value="xxx"></property>
<property name="uri" value="${transportConnectorURIs}"></property>
</bean>
</list>
</property>
<property name="jmsBridgeConnectors">
<list>
<bean class="org.apache.activemq.network.jms.JmsQueueConnector">
<property name="outboundQueueConnectionFactory">
<bean class="org.apache.activemq.ActiveMQSslConnectionFactory">
<property name="brokerURL" value="${brokerURL}" />
<property name="userName" value="${username}" />
<property name="password" value="${password}" />
<property name="trustStore" value="${trust.store.path}" />
<property name="trustStorePassword" value="${trust.store.password}" />
<!-- <property name="keyStore" value="${key.store.path}"/> -->
<!-- <property name="keyStorePassword" value="${key.store.password}"/> -->
</bean>
</property>
<property name="outboundQueueBridges">
<list>
<bean class="org.apache.activemq.network.jms.OutboundQueueBridge">
<constructor-arg value="${screenshotQueueName}" />
</bean>
<bean class="org.apache.activemq.network.jms.OutboundQueueBridge">
<constructor-arg value="${resultXmlQueueName}" />
</bean>
</list>
</property>
</bean>
</list>
</property>
</bean>
<bean id="persistenceAdapter" class="org.apache.activemq.store.kahadaptor.KahaPersistenceAdapter">
<property name="directory" value="E:\test"/>
Current issue is it throws an error as "exception is org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [org.apache.activemq.store.kahadaptor.KahaPersistenceA
apter] for bean with name 'kahaPersistenceAdapter' defined in class path resource [spring/resultupload/resultupload.xml]; nested exception is java.la
g.ClassNotFoundException: org.apache.activemq.store.kahadaptor.KahaPersistenceAdapter"
Anyone has experience in the directory change in activeMQ 5.11 in java spring?
The destination of the persistence location must be defined at the broker level.
The kahaPersistenceAdapter (which was file based) was removed with version 5.9. You should use the kahaDB.
kahaDB - uses KahaDB an embedded lightweight non-relational database
<broker brokerName="broker" persistent="true" useShutdownHook="false">
<transportConnectors>
<transportConnector uri="tcp://localhost:61616"/>
</transportConnectors>
<persistenceAdapter>
<kahaDB directory="e:/temp" ... />
</persistenceAdapter>
</broker>
all valid attributes: http://activemq.apache.org/schema/core/activemq-core-5.11.0-schema.html#kahaDB

Creating multiple instances of PropertyPlaceHolderConfigurer - Spring

I have 2 different applications deployed in application server (glassfish). One is a jar file and other is a war application. Both the applications refer to a single properties file (data.properties). To read the properties file, I have created a instance of Springs PropertyPlaceholderConfigurer in respective context files (business-beans.xml and applicationContext.xml). After deploying the applications, I am able to load the properties file in one application while the other web application throws "Could not resolve placeholder 'sw.throttle.enable'
Question -
How to solve the issue?
Is it incorrect load the same properties file at two locations?
Is there a way I load the properties file in one context and in the other bean definition file use the reference of the first one?
SnapShot of business.beans
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
<property name="placeholderPrefix" value="${sw." />
<property name="location" value="file:///etc/data.properties" />
<property name="ignoreResourceNotFound" value="true" />
</bean>
Property referenced as below in business.beans
<bean id="mService" class=" com.test.business.mService">
<property name="throttlingEnabled" value="${sw.throttle.enable}"/>
</bean>
Snapshot of applicationContext.xml
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
<property name="placeholderPrefix" value="${sw." />
<property name="location" value="file:///etc/data.properties" />
<property name="ignoreResourceNotFound" value="true" />
</bean>
Property referenced as below in applicationContext.xml
<bean id="downloadService" class="com.test.downloadService"
init-method="startUp" destroy-method="shutDown"
p:throttlingEnabled="${sw.throttle.enable}" />
The application containing business.beans deploys well, but the application containing applicationContext.xml throw run time error "could not resolve placeholder sw.throttle.enable"
Note -
Both the applications are deployed in a OsGi Context.
Spring version is 3.0.1
Edit -
The applicationContext.xml has another bean defined as below. Could this be the cause?
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>
</bean>
The issue was resolved by setting "ignoreUnresolvablePlaceholders" to "true". Apparently business.beans had nothing to do with the issue
Below is the modified configuration which solved the issue
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
<property name="placeholderPrefix" value="${sw." />
<property name="location" value="file:///etc/data.properties" />
<property name="ignoreResourceNotFound" value="true" />
<property name="ignoreUnresolvablePlaceHolders" value="true"
</bean>
Thanks StackOverflow for the answer +1

Converted JNDI name [java:comp/env/***] not found before reading the properties file

Good Mornig.
I have an application in tomcat 8, java 1.7, Spring 3.2.2 and primefaces 5.0. When the application starts, it shows the next debug message in console:
Looking up JNDI object with name [java:comp/env/etl.csv.token]
localhost-startStop-1: org.springframework.jndi.JndiLocatorDelegate
Converted JNDI name [java:comp/env/etl.csv.token] not found
But the key isn't in the JNDI context. It is in a properties file. The application works fine and takes the property from the properties file, but i can't understand why spring is trying to find the property in the JNDI context
The properties file is like this:
database.driverClassName=org.postgresql.Driver
database.initialSize=3
database.maxActive=15
database.maxIdle=10
database.minIdle=3
database.maxWait=10000
etl.csv.token=,
And its properties are used like this:
<bean id="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource">
<property name="driverClassName" value="${database.driverClassName}" />
<property name="url" value="${database.url}" />
<property name="username" value="${database.username}" />
<property name="password" value="${database.password}" />
<property name="initialSize" value="${database.initialSize}" />
<property name="maxActive" value="${database.maxActive}" />
<property name="maxIdle" value="${database.maxIdle}" />
<property name="maxWait" value="${database.maxWait}" />
<property name="minIdle" value="${database.minIdle}" />
<property name="testOnBorrow" value="true" />
<property name="validationQuery" value="SELECT CURRENT_TIMESTAMP" />
<property name="removeAbandoned" value="true" />
</bean>
For all properties, the behaviour is the same. I would like that message does not appear
Sorry for the english, is not my native language
Any help will be apreciated.

org.springframework.beans.factory.NoSuchBeanDefinitionException exception in UNIX

I have my java spring standalone project which works fine in windows but when I create jar file and executes that jar file by using shell script, it gives me org.springframework.beans.factory.NoSuchBeanDefinitionException. It seems that in unix it is unable to load the beans and unable to do component scan.
I have application contex file as below:
<context:annotation-config />
<context:component-scan base-package="com.ubs.lazar" />
<context:property-placeholder location="oracle.properties" />
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${batch.jdbc.driver}" />
<property name="url" value="${batch.jdbc.url}" />
<property name="username" value="${batch.jdbc.user}" />
<property name="password" value="${batch.jdbc.password}" />
</bean>
<bean id="daoFactory" class="com.ubs.mzq.xen.db.XenDaoFactory">
<property name="databaseName" value="oracle" />
<property name="dataSource" ref="dataSource" />
</bean>
<tx:annotation-driven/>
And I am invoking and load the configuration from java as below:
ApplicationContext context = new ClassPathXmlApplicationContext("classpath*:META-INF/application-context.xml");
AwardEventService awardEventService = (AwardEventService) context.getBean("awardEventServiceImpl");
Can you please somebody helps to how to overcome this issue in UNIX.
Thanks

Categories