Java Code Examples for org.springframework.jdbc.datasource.UserCredentialsDataSourceAdapter

Following code examples demonstrate how to use org.springframework.jdbc.datasource.UserCredentialsDataSourceAdapterfrom spring-framework. These examples are extracted from various highly rated open source projects. You can directly use these code snippets or view their entire linked source code. These snippets are extracted to provide contextual information about how to use this class in the real world. These samples also let you understand some good practices on how to use org.springframework.jdbc.datasource.UserCredentialsDataSourceAdapterand various code implementation of this class.

    public static UserCredentialsDataSourceAdapter getDataSource(String user, String password, String dsConfigName) {
        String dsConfig = "oracle-ds";
        if(dsConfigName != null && !dsConfigName.isEmpty()){
            dsConfig = dsConfigName;
        }

        try {
            InitialContext context = new InitialContext();
            logger.info("Using Datasource = " + dsConfig + " ConfName=" + dsConfigName);
            DataSource ds = (DataSource) context.lookup("java:jboss/datasources/" + dsConfig);
            if (ds == null) {
                logger.error("No Datasource found");

            } else {
                UserCredentialsDataSourceAdapter adapter = new UserCredentialsDataSourceAdapter();
                adapter.setTargetDataSource(ds);
                adapter.setUsername(user);
                adapter.setPassword(password);
                return adapter;
            }
        } catch (NamingException e) {
            logger.error("Error while creating a datasource for the user: " + user, e);
        }
        return null;
    } 


	public JdbcTemplate getJdbcTemplate() {
		lastAccessDB = new Date();
		UserCredentialsDataSourceAdapter ds = (UserCredentialsDataSourceAdapter) this.ds;
		ds.setCredentialsForCurrentThread(userName, password);
		SessionManager sessionManager = (SessionManager) SpringUtil.getBean("sessionManager");
		id.co.collega.v7.ef.service.Session session = sessionManager.getSession(sessionId);
		if (session != null) {
			session.touch();
			return new BackendJdbcTemplate(ds);
		} else {
			Sessions.getCurrent().invalidate();
			throw new RuntimeException("Session expired");
		}
	} 

	public DataSource dataSource() {
		DriverManagerDataSource dataSource = new DriverManagerDataSource();
		UserCredentialsDataSourceAdapter user = new UserCredentialsDataSourceAdapter();
		dataSource.setDriverClassName(environment.getRequiredProperty("jdbc.driverClassName"));
		dataSource.setUrl(environment.getRequiredProperty("jdbc.url"));
		dataSource.setUsername(environment.getRequiredProperty("jdbc.username"));
		dataSource.setPassword(environment.getRequiredProperty("jdbc.password"));
		user.setTargetDataSource(dataSource);
		return user;
	} 

Advertisement
Javadoc
An adapter for a target JDBC javax.sql.DataSource, applying the specified user credentials to every

standard getConnection() call, implicitly invoking getConnection(username, password) on the target. All other methods simply delegate to the corresponding methods of the target DataSource.

Can be used to proxy a target JNDI DataSource that does not have user credentials configured. Client code can work with this DataSource as usual, using the standard getConnection() call.

In the following example, client code can simply transparently work with the preconfigured "myDataSource", implicitly accessing "myTargetDataSource" with the specified user credentials. <bean id="myTargetDataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> <property name="jndiName" value="java:comp/env/jdbc/myds"/> </bean> <bean id="myDataSource" class="org.springframework.jdbc.datasource.UserCredentialsDataSourceAdapter"> <property name="targetDataSource" ref="myTargetDataSource"/> <property name="username" value="myusername"/> <property name="password" value="mypassword"/> </bean>

If the "username" is empty, this proxy will simply delegate to the standard getConnection() method of the target DataSource. This can be used to keep a UserCredentialsDataSourceAdapter bean definition just for the option of implicitly passing in user credentials if the particular target DataSource requires it. @author Juergen Hoeller @since 1.0.2 @see #getConnection

Read More
Advertisement