Dynamic configuration in a java/camel/spring/jpa application - java

I have written an application using java, camel, spring, shiro, c3p0 and jpa.
This application needs to connect to some web services and some db and it has now a static configuration using classic spring propertyplaceholders and .prop property files.
I inject properties in java classes using #Value annotations and I define datasources using spring with ${} placeholders.
In the configuration there are url,username,password for web services and database,url,username,password for datasource.
Now I need to do a dynamic/multi tenant configuration. I mean that each "customer" can have his set of passwords and that these login/passwords can change over time.
Using shiro I can add to the Subject some data, so I can add current properties to it and get them where I need.
But how can I continue to use #value annotations?
And, most important question, how can I change datasources parameters at runtime?
I see in c3p0 documentation that using getConnection(username,password) with a new pair of username and password creates a new pool and close the old. But I do not use getConnection because only the EntityManager uses datasource.
Please help me!
Thanks,
Mario
After a lot of searching I think I can do in this way:
for properties use DynamicCombinedConfiguration from commons configuration, but I do not know how to tell it to read the tenant id from Shiro Subject
for JPA use AbstractDataSource from spring, but again I do not know if I can read the tenant id from Shiro Subject
Can you tell me if I am pointing in the right direction?
Thanks again,
Mario

Related

Spring MVC - Distributed Database Transaction

I'm currently developing a web application using Spring MVC (without Maven).
What I need is to create a distributed transaction between two local databases, so that the code will update all of them in (theoretically) a 2phase commit.
Now, since I'm doing it for a school project, I'm in a simple environment which needs only to take a row from a table in one db and put it in a table on the other db, of course atomically (theoretically, such a transaction should be distributed because I'm using two different databases and not only one).
My question is, how can I deploy a Spring bean that firstly connects to both MySQL databases and then does that distributed transaction? Should I use some external library or could I achieve all with only using the Spring framework? In which case, could you please kindly link me an example or a guide to do this?
Thank you in advance for your help :)
Spring has an interface PlatformTransactionManager which is an
abstraction And it has many implementations like
DataSourceTransactionManager,HibernateTransactionManager etc.
Since you are using distributed transactions so you need to use
JTATransactionManager
These TransactionManagers provided by spring
are wrapper around the implementations provided by other frameworks
Now in case of JTA , you would be using either an application server
or a standalone JTA implementation like Atomikos
Following are the steps :-
Configure Transaction Manager in spring using application server
or standalone JTA implementation
Enable Transaction Management in spring
And then configure in your code the #Transactional annotation above
your method
Have a look at following links
http://docs.spring.io/spring/docs/current/spring-framework-reference/html/transaction.html
http://www.byteslounge.com/tutorials/spring-jta-multiple-resource-transactions-in-tomcat-with-atomikos-example

How Spring Security detects the password filed?

I am developing an web application using java with Spring MVC and Sprin-Security. I can see, Spring Security does the authentication and security related tasks by itself which is OK. I was wondering how Spring Security detects the password field from database on which the authentication is made!!!
I have stored the user passwords in a column named 'xyz'. Now, how can I ask Spring-Security to look for user password in the column 'xyz'???? I tried to find a clear answer for this, but couldn't. So, if you guys please help me to make a clear concept about this.... I appreciate that!!
First, you need to setup JDBC Connectivity. For that you can use Spring JDBC so that the connections are spring managed.
Then, you need to override UserDetailsService of Spring Security to your own queries. An example is given here
Then wire your services either via programmatic configuration or via XML configuration, whichever way you have done it in your project.

How to define a Shared DataSource in Spring Cloud Config

Would it be possible to set up a DataSource using Spring Cloud in which open JDBC connections could be injected into all of my Spring Boot applications?
Something kind of like a JNDI server lookup? If so, can someone provide some examples or a description on how to use this type of configuration?
You could use a Spring Cloud bootstrap configuration to create a DataSource. I don't see much value in doing it that way over a normal Spring Boot autoconfiguration though. Link: http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#boot-features-developing-auto-configuration.
One solution I found out was to set all datasources info into the properties files which will supplied by Spring Cloud Config Server to applications clients. So the aplications clients which creates DataSources gets from remote properties those values.

Spring Boot and Spring Data application with multiple DataSources created in runtime

I am developing a Spring Boot application that uses Spring Data JPA and will need to connect to many different databases e.g. PostreSQL, MySQL, MS-SQL, MongoDB.
I need to create all datasources in runtime i.e. user choose these data by GUI in started application:
-driver(one of the list),
-source,
-port,
-username,
-password.
And after all he writes native sql to choosen database and get results.
I read a lot of things about it in stack and spring forums(e.g. AbstractRoutingDataSource) but all of these tutorials show how to create datasources from xml configuration or static definition in java bean. It is possible to create many datsources in runtime? How to manage transactions and how to create many sessionFactories? It is possible to use #Transactional annotation? What is the best method to do this? Can someone explain me how to do this 'step by step'?
Hope it's not too late for an answer ;)
I developed a module which can be easily integrated in any spring project. It uses a meta-datasource to hold the tenant-datasource connection details.
For the tenant-datasource an AbstractRoutingDataSource is used.
Here you find my core implementation using the AbstractRoutingDataSource.
https://github.com/Dactabird/multitenancy
Here is an example to show how to integrate it. https://github.com/Dactabird/multitenancy-sample
In this example I'm using H2 embedded db. But of course you can use whatever you want.
Feel free to modify it for your purposes or to ask if questions are left!

How can I use JDBCTemplate from Spring JDBC in a Swing desktop application?

I use a lot of JDBC code in my Swing desktop application. Now I read about JDBCTemplate from Spring in Spring in Action and it looks like a nice API for working with JDBC.
But Spring JDBC seem to need some XML configuration files for beans. Is there any way I can use JDBCTemplate without these XML configuration files (e.g. with annotations)? Or how can I use this JdbcTemplate in a Swing desktop application for database access?
You can have a Spring Context without XML by creating a #Configuration anotated java class and create the Spring Context using the AnnotationConfigApplicationContext class to load the config
see Spring JavaConfig for a code sample.
While using Spring as the backbone of your application certainly has merit, and indeed annotation-based configuration can free you from 'XML hell', if you just want to use JdbcTemplate 'raw' there's nothing preventing you from doing so.
Just make sure you supply it with a valid DataSource, such as PGPoolingDataSource for example, if you're using PostgreSQL. If your JDBC vendor does not provide a DataSource implementation, then feel free to use Spring's SimpleDriverDataSource.
For example:
DataSource ds = new SimpleDriverDataSource(LegacyDriver.class,
"jdbc:legacy://database", "username", "password");
JdbcTemplate jdbc = new JdbcTemplate(ds);
// Use jdbc to do stuff
Even though its technically possible - it would defeat the purpose and design of Spring based application.
My suggestion would be to start using Spring framework as a backbone for you application. I promise you that your application will only benefit from using it (better design, clear separation of concerns, better testability etc ). Setting up JbdcTemplate using Spring context is almost trivial.
You already reading 'Spring in Action' - just start using it :)
Take a look at Spring Reference Documentation - the best Spring resource, period

Categories