How to use different configuration for deployment? - maven

I am currently dealing with the deployment of my web-server which is why I am facing a presumably very common problem: Configuration files
As a very straight forward example let's take a database. I have a local database.xml which contains the data source which I use on my local machine:
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.postgresql.Driver" />
<property name="url" value="jdbc:postgresql://localhost:5432/mz_db" />
<property name="username" value="postgres" />
<property name="password" value="pg" />
</bean>
but of course, I am connecting to a different database once I deploy the server. So what are my options here? I would like to have this as centralized as possible to prevent wrong files being deployed here. I am sure that maven can be used for this somehow.
I was thinking about doing something like creating two config/ directories
WEB-INF/config/
WEB-INF/config-dev/
and use config-dev/ per default without the exception of the deploy phase or something like this. I am not sure if this can be done or if this is the best way of doing it.
Any suggestions?

Related

How to make JNDI look up optional in Spring

Is there anyway where we can make the JNDI lookup optional in spring appllicationcontext xml configuration.
I want to deploy the same application which has JNDI setting(Data base connection) on two different environments. In one environment we need DB connection and another environment we don't need the DB connection. Could you please suggest if there is a away we can achieve this without modifying the applicationcontext.xml(I mean without commenting out the JNDI configuration and other related bean injection for DB connection).
Use profiles, something like:
<beans profile="prod">
<jee:jndi-lookup id="dbDataSource" jndi-name="jdbc/DatabaseName"expected-type="javax.sql.DataSource" />
</beans>
<beans profile="dev,default">
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="connectionCachingEnabled" value="true"/>
</bean>type="javax.sql.DataSource" />
</beans>
Then when you start you app say which profile with system argument:
-Dspring.profiles.actibe=prod
The default profile will be dev.

Trouble with overriding properties in Spring-batch

We are using Spring 4.3.9 with Spring Batch 3. We are using maven to copy resources with filtering to merge profile-based properties into the configs at build time. I want to allow my DevOps engineers to override property file settings (db passwords) at deployment time to the environment specific property, so I've setup things as shown below, but the overrides don't work:
<bean class="org.springframework.beans.factory.config.PropertyOverrideConfigurer">
<property name="location" value="classpath:bluecost-OVERRIDE.properties" />
</bean>
<bean id="dataSource"
class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="${datasource.database.driverClassName}" />
<property name="jdbcUrl" value="${datasource.database.url}" />
<property name="user" value="${datasource.database.username}" />
<property name="password" value="${datasource.database.password}" />
<property name="maxPoolSize" value="${datasource.maxPoolSize}" />
<property name="minPoolSize" value="${datasource.minPoolSize}" />
</bean>
The property file that maven merges with all default values looks like this:
datasource.database.driverClassName=com.ibm.db2.jcc.DB2Driver
datasource.database.url=jdbc:db2://localhost:50000/bluecost
datasource.database.username=not-real-id
datasource.database.password=not-real-pwd
datasource.maxPoolSize=50
datasource.minPoolSize=10
And finally, my bluecost-OVERRIDE.properties file has the correct values for just the username and password, configured like this:
# Overriding values for the datasource property values
datasource.database.username=db2inst
datasource.database.password=db2inst1
The override file is surely in the classpath (it wouldn't start without finding it anyway). It's throwing errors at runtime because of the invalid (default) userid/pwd.
Why doesn't it override the userid/pwd like I want it to?

H2 Multiple connection

I have two applications : app1 and app2 , and i want that this two application use the same H2 file as a database .
I test this configuration in the two sides but it didn't work :
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="org.h2.Driver" />
<property name="url" value="jdbc:h2:~/dBTrunk;MODE=Oracle;AUTO_SERVER=TRUE" />
<property name="username" value="sa" />
<property name="password" value="" />
</bean>
I am open to all kind of solution my only goal is that app1 and app2 can work in parralel mode with the same H2 database .
Regards
This should work. The first app should open to the database file in embedded mode, but then start a database server. The second app will then try to connect to the first app's database server.
If it is not possible for the two apps to talk to each other using TCP/IP, then it wont work. Is there a firewall between them? Are there certain ports blocked?

Update Views With Spring MVC And Thymeleaf Without Redeploy The App

I am using Spring MVC 3.2, Thymeleaf, Thymeleaf dialect with Tomcat and every time that I change a view I don't want redeploy my app. As suggested from others I am setting:
<property name="cacheable" value="false"/>
in the template resolver, but it not working.
Also a full reload (CTRL + F5) has not effect.
Here my full Thymeleaf configuration:
<!-- Thymeleaf template engine -->
<bean id="templateResolver"
class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
<property name="prefix" value="/WEB-INF/templates/" />
<property name="suffix" value=".html" />
<property name="templateMode" value="HTML5" />
<property name="cacheable" value="false"/>
</bean>
<bean id="templateEngine"
class="org.thymeleaf.spring3.SpringTemplateEngine">
<property name="templateResolver" ref="templateResolver" />
<!-- These lines add the dialect to Thymeleaf -->
<property name="additionalDialects">
<set>
<bean class="nz.net.ultraq.thymeleaf.LayoutDialect"/>
</set>
</property>
</bean>
<bean class="org.thymeleaf.spring3.view.ThymeleafViewResolver">
<property name="templateEngine" ref="templateEngine" />
</bean>
Is there something else that I can do to resolve this issue?
It depends on many many things and mainly on "where and how" you update your view. If you are using a IDE (Netbeans or Eclipse) it main depend on the IDE itself and on the deployement process.
Current organization on disk :
Source directories => [ on build ] => target or build directories => [ on deploy ] => tomcat directories
But IDE often tries to be developper's friendly and it can happen that target directories and tomcat directories are the same. But it also may depend of the IDE and its configuration.
Also when you save files under source webapp, the IDE may automatically copy them to target.
But this is not guaranteed by <property name="cacheable" value="false"/> of Thymeleaf config. All what it guarantees is that if a template is changed in tomcat directories, next request will use it.
So to be sure where the problem really comes, you will have to find where tomcat actually gets the templates and if those templates are modified.
(and I didn't even talked of browsers cache ...)
In Tomcat "Server Options", check "Serve modules without publishing" did the trick for me.

Passing encrypted properties to spring context

I never seen this but I wondering if somebody has come across. Having a web server which access a database. I want to pass the database password encrypted and have spring context decrypting it before setting the datasource. I know the spring security can do some of this like using a salt file in the web server, etc.
The challenge here is that I don't want to give a clear user,password,url to the web server team. Just an encrypted password and have spring decrypted before using it.
Is there something like this already? I know I could code something but is it already done?
Thanks
By using an org.jasypt.properties.EncryptableProperties object, an application would be able to correctly read and use a .properties file like this:
datasource.driver=com.mysql.jdbc.Driver
datasource.url=jdbc:mysql://localhost/reportsdb
datasource.username=reportsUser
datasource.password=ENC(G6N718UuyPE5bHyWKyuLQSm02auQPUtm)
Note that the database password is encrypted (in fact, any other property could also be encrypted, be it related with database configuration or not).
More information :
http://www.jasypt.org/encrypting-configuration.html
I actually found exactly what I was looking for in this thread:
How to use encrypted password in apache BasicDataSource?
Here are the details from jasyp http://www.jasypt.org/spring3.html
This problem and solution to it is explained here..(link)
db.Properties.
#driverClassName=oracle.jdbc.driver.OracleDriver
#url=jdbc:oracle:thin:#localhost:1521:XE
#username=ITEM_INVENTORY
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/ITEM_INVENTORY?zeroDateTimeBehavior=convertToNull
username=root
Encrypt db.Properties
##password=cGFzc3dvcmQ=
password=cm9vdA==
The spring beans configuration for the datasource would look like this
(here you may use only password part)
spring-beans.xml
<bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="db#[driverClassName]" />
<property name="url" value="db#[url]" />
<property name="username" value="db#[username]" />
<property name="password" value="encryptedDb#[password]" />
</bean>
<bean id="dbPropertyPlaceholder" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:db.properties</value>
</list>
</property>
<property name="placeholderPrefix" value="db#[" />
<property name="placeholderSuffix" value="]" />
</bean>
<bean id="encryptedDbPropertyPlaceholder" class="com.inventory.api.util.DecryptPropertyConfigurer">
<property name="locations">
<list>
<value>classpath:encryped_db.properties</value>
</list>
</property>
<property name="placeholderPrefix" value="encryptedDb#[" />
<property name="placeholderSuffix" value="]" />
</bean>
And so on.. please refer given link for more information..

Resources