Index: JDBCResource.java
===================================================================
--- JDBCResource.java	(revision 43019)
+++ JDBCResource.java	(revision 43020)
@@ -3,25 +3,23 @@
  *
  * Copyright (C) 2007-2013 gvSIG Association.
  *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; either version 3
- * of the License, or (at your option) any later version.
+ * This program is free software; you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License as published by the Free Software
+ * Foundation; either version 3 of the License, or (at your option) any later
+ * version.
  *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
+ * details.
  *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
- * MA  02110-1301, USA.
+ * You should have received a copy of the GNU General Public License along with
+ * this program; if not, write to the Free Software Foundation, Inc., 51
+ * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  *
- * For any additional information, do not hesitate to contact us
- * at info AT gvsig.com, or visit our website www.gvsig.com.
+ * For any additional information, do not hesitate to contact us at info AT
+ * gvsig.com, or visit our website www.gvsig.com.
  */
-
 package org.gvsig.fmap.dal.store.jdbc;
 
 import java.sql.Connection;
@@ -31,6 +29,7 @@
 import javax.sql.DataSource;
 
 import org.apache.commons.dbcp.BasicDataSource;
+import org.apache.commons.lang3.StringUtils;
 import org.gvsig.fmap.dal.exception.DataException;
 import org.gvsig.fmap.dal.exception.InitializeException;
 import org.gvsig.fmap.dal.resource.ResourceParameters;
@@ -44,218 +43,195 @@
 
 public class JDBCResource extends AbstractDBResourceNoBlocker {
 
-	final static private Logger logger = LoggerFactory
-			.getLogger(JDBCResource.class);
+    final static private Logger logger = LoggerFactory.getLogger(JDBCResource.class);
 
-	public final static String NAME = "JDBCResource";
-	public static final String DESCRIPTION = "JDBC Connection";
+    public final static String NAME = "JDBCResource";
+    public static final String DESCRIPTION = "JDBC Connection";
 
-	protected DataSource dataSource = null;
+    protected DataSource dataSource = null;
 
-	public JDBCResource(JDBCResourceParameters parameters)
-			throws InitializeException {
-		super(parameters);
-		registerJDBCDriver();
+    public JDBCResource(
+            JDBCResourceParameters parameters
+    ) throws InitializeException {
+        super(parameters);
+        registerJDBCDriver();
+    }
 
-	}
-
-	public String getName() throws AccessResourceException {
-		JDBCResourceParameters params = (JDBCResourceParameters) this
-				.getParameters();
-		return MessageFormat.format("JDBCResource({0},{1},{2},{3},{4})",
-				new Object[] {
-				params.getJDBCDriverClassName(),
-						params.getHost(), params.getPort(),
-				params.getUser(), params.getDBName() });
-	}
-
-	@SuppressWarnings("unchecked")
-	protected void registerJDBCDriver() throws InitializeException {
-		String className = ((JDBCResourceParameters) getParameters())
-				.getJDBCDriverClassName();
-		if (className == null) {
-			return;
-		}
-
-		Class theClass = null;
-		try {
-			theClass = Class.forName(className);
-		} catch (Exception e){
-			throw new InitializeException(e);
-		}
-		if (theClass == null) {
-			try {
-				throw new JDBCDriverClassNotFoundException(this.getName(),
-						className);
-			} catch (AccessResourceException e) {
-				throw new InitializeException(e);
-
-			}
-		}
-	}
-
-
-	public Connection getJDBCConnection() throws AccessResourceException {
-		return (Connection) get();
-	}
-        
-        public void closeConnection(Connection connection) {
-            try {
-                logger.debug("before-close connection"+ this.getStatusInformation());
-                connection.close();
-                logger.debug("after close connection "+ this.getStatusInformation());
-            } catch (Exception ex) {
-                logger.warn("Problems closing connection.",ex);
-            }            
+    /**
+     * Registra/Carga el driver de JDBC.
+     * 
+     * Debe ser sobreescrita en cada proveedor de datos para asegurar que se
+     * tegna acceso al driver.
+     * 
+     * @throws InitializeException 
+     */
+    protected void registerJDBCDriver() throws InitializeException {
+        String className = this.getParameters().getJDBCDriverClassName();
+        if (className == null) {
+            return;
         }
-
-	private void debugPoolStatus(String src) {
-		if (logger.isDebugEnabled() ) {
-			logger.debug(src + "  " + this.getStatusInformation());
-		}
-	}
-        
-        public String getStatusInformation() {
-		if ( !(dataSource instanceof BasicDataSource) ) {
-                    return "Connected: " + this.isConnected();
-                }
-        	BasicDataSource ds = (BasicDataSource) dataSource;
-                String s = "Connected: " + this.isConnected() + ", " +
-                        "actives: "+ ds.getNumActive() + "/"+ ds.getMaxActive() + ", " + 
-                        "idle: " + ds.getNumIdle() + "/"+ ds.getMaxIdle() ;
-                return s;
+        try {
+            Class theClass = Class.forName(className);
+            if (theClass == null) {
+                throw new JDBCDriverClassNotFoundException(this.getName(), className);
+            }
+        } catch (Exception e) {
+            throw new InitializeException(e);
         }
+    }
 
-	protected synchronized Object getTheConnection() throws DataException {
-		try {
-			Object conn = this.dataSource.getConnection();
-			debugPoolStatus("getTheConnection");
-			return conn;
-		} catch (SQLException e) {
-			throw new JDBCSQLException(e);
-		}
-	}
+    /**
+     * Crea el JDBC DataSource a partir de la informacion de los parametros
+     * del recurso.
+     * 
+     * Debe ser sobreescrita en cada proveedor de datos para asegurar que se
+     * tenga acceso al driver.
+     * 
+     * @return 
+     */
+    protected DataSource createDataSource() {
+        JDBCResourceParameters jdbcParams = this.getParameters();
+        BasicDataSource dataSource = new BasicDataSource();
+        dataSource.setDriverClassName(jdbcParams.getJDBCDriverClassName());
+        dataSource.setUsername(jdbcParams.getUser());
+        dataSource.setPassword(jdbcParams.getPassword());
+        dataSource.setUrl(jdbcParams.getUrl());
 
-	public boolean isThis(ResourceParameters parameters)
-			throws ResourceException {
-		if (!super.isThis(parameters)) {
-			return false;
-		}
+        dataSource.setMaxWait(60L * 1000); // FIXME
 
-		String dbName = ((JDBCResourceParameters) parameters).getDBName();
-		String myDbName = ((JDBCResourceParameters) getParameters())
-				.getDBName();
-		if (dbName != myDbName) {
-			if (!(dbName != null && dbName.equals(myDbName))) {
-				return false;
-			}
+//                FIXME Set Pool parameters:
+//		dataSource.setMaxActive(maxActive);
+//		dataSource.setMaxIdle(maxActive);
+//		dataSource.setMaxOpenPreparedStatements(maxActive);
+//		dataSource.setMaxWait(maxActive);
+//		dataSource.setInitialSize(initialSize);
+//		dataSource.setDefaultReadOnly(defaultReadOnly);
+//		dataSource.setDefaultTransactionIsolation(defaultTransactionIsolation);
+//		dataSource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
+//		dataSource.setMinIdle(minIdle);
+//		dataSource.setTestOnBorrow(testOnBorrow);
+//		dataSource.setTestOnReturn(testOnReturn);
+//		dataSource.setTestWhileIdle(testOnReturn);
+//		dataSource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
+//
+//		dataSource.setAccessToUnderlyingConnectionAllowed(allow);
+//		dataSource.setLoginTimeout(seconds);
+//		dataSource.setLogWriter(out);
+        return dataSource;
+    }
 
-		}
 
-		String driver = ((JDBCResourceParameters) parameters)
-				.getJDBCDriverClassName();
-		String myDriver = ((JDBCResourceParameters) getParameters())
-				.getJDBCDriverClassName();
-		if (driver != myDriver) {
-			if (!(driver != null && driver.equals(myDriver))) {
-				return false;
-			}
-		}
-		return true;
+    @Override
+    public JDBCResourceParameters getParameters() {
+        return (JDBCResourceParameters) super.getParameters();
+    }
 
-	}
+    @Override
+    public String getName() throws AccessResourceException {
+        JDBCResourceParameters params = this.getParameters();
+        return MessageFormat.format("JDBCResource({0},{1},{2},{3},{4})",
+                new Object[]{
+                    params.getJDBCDriverClassName(),
+                    params.getHost(),
+                    params.getPort(),
+                    params.getUser(),
+                    params.getDBName()
+                }
+        );
+    }
+    
+    public Connection getJDBCConnection() throws AccessResourceException {
+        return (Connection) get();
+    }
 
-	public boolean isConnected() {
-		if (dataSource == null) {
-			return false;
-		}
-		if (dataSource instanceof BasicDataSource) {
-			return ((BasicDataSource) dataSource).getNumActive() > 0
-					|| ((BasicDataSource) dataSource).getNumIdle() > 0;
-		}
-		return true;
-	}
+    public void closeConnection(Connection connection) {
+        try {
+            logger.debug("before-close connection" + this.getStatusInformation());
+            connection.close();
+            logger.debug("after close connection " + this.getStatusInformation());
+        } catch (Exception ex) {
+            logger.warn("Problems closing connection.", ex);
+        }
+    }
 
-	protected DataSource createDataSource() {
-		JDBCResourceParameters jdbcParams = (JDBCResourceParameters) this
-		.getParameters();
-		BasicDataSource dataSource = new BasicDataSource();
-		dataSource.setDriverClassName(jdbcParams.getJDBCDriverClassName());
-		dataSource.setUsername(jdbcParams.getUser());
-		dataSource.setPassword(jdbcParams.getPassword());
-		dataSource.setUrl(jdbcParams.getUrl());
+    protected void debugPoolStatus(String src) {
+        if (logger.isDebugEnabled()) {
+            logger.debug(src + "  " + this.getStatusInformation());
+        }
+    }
 
+    public String getStatusInformation() {
+        if (!(dataSource instanceof BasicDataSource)) {
+            return "Connected: " + this.isConnected();
+        }
+        BasicDataSource ds = (BasicDataSource) dataSource;
+        String s = MessageFormat.format("Conneted {0}, actives {1}/{2}, idle {3}/{4}",
+            this.isConnected(),
+            ds.getNumActive(),
+            ds.getMaxActive(),
+            ds.getNumIdle(),
+            ds.getMaxIdle()
+        );
+        return s;
+    }
 
-		dataSource.setMaxWait(60L * 1000); // FIXME
+    @Override
+    protected synchronized Object getTheConnection() throws DataException {
+        try {
+            Object conn = this.dataSource.getConnection();
+            debugPoolStatus("getTheConnection");
+            return conn;
+        } catch (SQLException e) {
+            throw new JDBCSQLException(e);
+        }
+    }
 
-		// FIXME Set Pool parameters:
-		/*
-		dataSource.setMaxActive(maxActive);
-		dataSource.setMaxIdle(maxActive);
-		dataSource.setMaxOpenPreparedStatements(maxActive);
-		dataSource.setMaxWait(maxActive);
-		dataSource.setInitialSize(initialSize);
-		dataSource.setDefaultReadOnly(defaultReadOnly);
-		dataSource.setDefaultTransactionIsolation(defaultTransactionIsolation);
-		dataSource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
-		dataSource.setMinIdle(minIdle);
-		dataSource.setTestOnBorrow(testOnBorrow);
-		dataSource.setTestOnReturn(testOnReturn);
-		dataSource.setTestWhileIdle(testOnReturn);
-		dataSource
-			.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
+    @Override
+    public boolean isThis(ResourceParameters parameters)
+            throws ResourceException {
+        if (!super.isThis(parameters)) {
+            return false;
+        }
+        JDBCResourceParameters params = (JDBCResourceParameters) parameters;
+        String dbName = params.getDBName();
+        String myDbName = this.getParameters().getDBName();
+        if ( !StringUtils.equals(dbName, myDbName) ) {
+            return false;
+        }
 
-		dataSource.setAccessToUnderlyingConnectionAllowed(allow);
-		dataSource.setLoginTimeout(seconds);
-		dataSource.setLogWriter(out);
-		 */
-		return dataSource;
-	}
+        String driver = params.getJDBCDriverClassName();
+        String myDriver = getParameters().getJDBCDriverClassName();
+        if ( !StringUtils.equals(driver, myDriver) ) {
+            return false;
+        }
+        return true;
+    }
 
-	protected void connectToDB() throws DataException {
-		if (this.dataSource != null) {
-			return;
-		}
-		JDBCResourceParameters jdbcParams = (JDBCResourceParameters) this
-				.getParameters();
-		BasicDataSource dataSource = new BasicDataSource();
-		dataSource.setDriverClassName(jdbcParams.getJDBCDriverClassName());
-		dataSource.setUsername(jdbcParams.getUser());
-		dataSource.setPassword(jdbcParams.getPassword());
-		dataSource.setUrl(jdbcParams.getUrl());
+    @Override
+    public boolean isConnected() {
+        if (dataSource == null) {
+            return false;
+        }
+        if (dataSource instanceof BasicDataSource) {
+            return ((BasicDataSource) dataSource).getNumActive() > 0
+                    || ((BasicDataSource) dataSource).getNumIdle() > 0;
+        }
+        return true;
+    }
 
-		dataSource.setMaxWait(60L * 1000); // FIXME
+    @Override
+    protected void connectToDB() throws DataException {
+        if (this.dataSource != null) {
+            return;
+        }
+        this.dataSource = this.createDataSource();
+    }
 
-		// FIXME Set Pool parameters:
-		/*
-		dataSource.setMaxActive(maxActive);
-		dataSource.setMaxIdle(maxActive);
-		dataSource.setMaxOpenPreparedStatements(maxActive);
-		dataSource.setMaxWait(maxActive);
-		dataSource.setInitialSize(initialSize);
-		dataSource.setDefaultReadOnly(defaultReadOnly);
-		dataSource.setDefaultTransactionIsolation(defaultTransactionIsolation);
-		dataSource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
-		dataSource.setMinIdle(minIdle);
-		dataSource.setTestOnBorrow(testOnBorrow);
-		dataSource.setTestOnReturn(testOnReturn);
-		dataSource.setTestWhileIdle(testOnReturn);
-		dataSource
-				.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
+    public void beginUse() {
+        this.executeBegins();
+    }
 
-		dataSource.setAccessToUnderlyingConnectionAllowed(allow);
-		dataSource.setLoginTimeout(seconds);
-		dataSource.setLogWriter(out);
-		*/
-
-		this.dataSource = dataSource;
-	}
-
-        public void beginUse() {
-            this.executeBegins();
-        }
-        
-        public void endUse() {
-           this.executeEnds();
-        }
+    public void endUse() {
+        this.executeEnds();
+    }
 }
