/*
 * Copyright (c) 2016, The Dattack team (http://www.dattack.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.dattack.jtoolbox.jdbc;

import java.sql.Connection;
import java.sql.SQLException;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

/**
 * Implementation of a <code>DataSource</code> that references a object registered in a JNDI context.
 *
 * @author cvarela
 * @since 0.1
 */
@SuppressWarnings("checkstyle:AbbreviationAsWordInName")
public final class JNDIDataSource extends AbstractDataSource {

    private final String jndiName;

    public JNDIDataSource(final String jndiName) {
        this.jndiName = jndiName;
    }

    @Override
    public Connection getConnection() throws SQLException {

        try {
            final InitialContext context = new InitialContext();
            final DataSource dataSource = (DataSource) context.lookup(jndiName);
            if (dataSource == null) {
                throw new SQLException("Unknown JNDI resource '" + jndiName + "'");
            }
            return dataSource.getConnection();
        } catch (final NamingException e) {
            throw new SQLException(
                    String.format("Unable to get a connection from JNDI name '%s': %s", jndiName, e.getMessage()), e);
        }
    }

    @Override
    public Connection getConnection(final String username, final String pwd) throws SQLException {
        throw new UnsupportedOperationException("Not implemented");
    }
}

@@ -996,17 +996,17 @@
 ckstyle:
-A
+a
 bbreviat
@@ -1121,16 +1121,60 @@
 ndiName;
+%0A    private volatile DataSource dataSource;
 %0A%0A    pu
@@ -1267,53 +1267,41 @@
     
-@Override%0A    public Connection getConnection
+private void initializeDataSource
 () t
@@ -1321,17 +1321,16 @@
 ption %7B%0A
-%0A
         
@@ -1416,50 +1416,20 @@
     
-final DataSource dataSource = (DataSource)
+Object obj =
  con
@@ -1467,26 +1467,19 @@
     if (
-dataSource
+obj
  == null
@@ -1576,24 +1576,25 @@
           %7D%0A
+%0A
             
@@ -1597,251 +1597,919 @@
     
-return dataSource.getConnection();%0A        %7D catch (final NamingException e) %7B%0A            throw new SQLException(%0A                    String.format(%22Unable to get a connection from JNDI name '%25s': %25s%22, jndiName, e.getMessage()), e);%0A        %7D
+if (obj instanceof DataSource) %7B%0A                this.dataSource = (DataSource) obj;%0A            %7D else %7B%0A                throw new SQLException(String.format(%22Unable to get a Connection (JNDI-name: %25s, Class: %25s)%22, jndiName,%0A                        obj.getClass()));%0A            %7D%0A%0A        %7D catch (final NamingException e) %7B%0A            throw new SQLException(%0A                    String.format(%22Unable to get a connection from JNDI name '%25s': %25s%22, jndiName, e.getMessage()), e);%0A        %7D%0A    %7D%0A%0A    private DataSource getDataSource() throws SQLException %7B%0A        if (dataSource == null) %7B%0A            synchronized (this) %7B%0A                if (dataSource == null) %7B%0A                    initializeDataSource();%0A                %7D%0A            %7D%0A        %7D%0A        return dataSource;%0A    %7D%0A%0A    @Override%0A    public Connection getConnection() throws SQLException %7B%0A        return getDataSource().getConnection();
 %0A   

Fix to handle error when the JNDI resource isn't a DataSource