Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/issue 31 coverage 80 #34

Merged
merged 2 commits into from
Sep 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions fj-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,13 @@
</exclusions>
</dependency>

<dependency>
<groupId>com.github.h-thurow</groupId>
<artifactId>simple-jndi</artifactId>
<version>0.23.0</version>
<scope>test</scope>
</dependency>

</dependencies>

<profiles>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public static ConnectionFactory getFactory( String name ) {
* @return true if everything was closed with no errors
*/
public static boolean closeLoose( Connection conn, Statement stm, ResultSet rs ) {
return closeLoose( conn ) && closeLoose( stm ) && closeLoose( rs );
return closeLoose( conn ) & closeLoose( stm ) & closeLoose( rs );
}

/**
Expand All @@ -84,7 +84,7 @@ public static boolean closeLoose( Connection conn, Statement stm, ResultSet rs )
* @return true if everything was closed with no errors
*/
public static boolean closeLoose( Connection conn, Statement stm ) {
return closeLoose( conn ) && closeLoose( stm );
return closeLoose( conn ) & closeLoose( stm );
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package org.fugerit.java.core.db.connect;

public interface ConnectionFactoryCloseable extends ConnectionFactory, AutoCloseable {

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,18 @@
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.Driver;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

import org.fugerit.java.core.cfg.ConfigRuntimeException;
import org.fugerit.java.core.db.dao.DAOException;
import org.fugerit.java.core.db.metadata.DataBaseInfo;
import org.fugerit.java.core.function.UnsafeSupplier;
import org.fugerit.java.core.lang.helpers.StringUtils;
import org.fugerit.java.core.log.BasicLogObject;
import org.fugerit.java.core.xml.dom.DOMUtils;
import org.fugerit.java.core.xml.dom.SearchDOM;
Expand Down Expand Up @@ -152,6 +151,27 @@ public DataBaseInfo getDataBaseInfo() throws DAOException {
*/
public static final String PROP_CF_EXT_POOLED_MC = "db-ext-pooled-mc";

public static ConnectionFactoryCloseable wrap( ConnectionFactory cf ) {
return new ConnectionFactoryCloseable() {
@Override
public void close() throws Exception {
this.release();
}
@Override
public void release() throws DAOException {
cf.release();
}
@Override
public DataBaseInfo getDataBaseInfo() throws DAOException {
return cf.getDataBaseInfo();
}
@Override
public Connection getConnection() throws DAOException {
return cf.getConnection();
}
};
}

/**
* Parse a configuration Element looking for ConnectionFactory configuration
*
Expand All @@ -168,7 +188,7 @@ public static CfConfig parseCfConfig( Element cfConfig ) throws Exception {
Element currentEntryTag = (Element) cfConfigEntryIt.next();
Properties props = DOMUtils.attributesToProperties( currentEntryTag );
String id = props.getProperty( "id" );
if ( id == null || id.trim().length() == 0 ) {
if ( StringUtils.isEmpty( id ) ) {
throw new ConfigRuntimeException( "Connection factory id must be defined." );
} else if ( config.getCfMap().containsKey( id ) ) {
throw new ConfigRuntimeException( "Connection factory id already used : '"+id+"'" );
Expand All @@ -187,15 +207,12 @@ public static CfConfig parseCfConfig( Element cfConfig ) throws Exception {
* @throws DAOException in case of issues
*/
public static String getDriverInfo( ConnectionFactory cf ) throws DAOException {
String result = "";
try (Connection conn = cf.getConnection()) {
DatabaseMetaData databaseMetaData = conn.getMetaData();
result = databaseMetaData.getDriverName()+" "+databaseMetaData.getDriverVersion();
} catch (Exception e) {
throw DAOException.convertExMethod( "getDriverInfo", e );
}

return result;
return DAOException.get( () -> {
try (Connection conn = cf.getConnection()) {
DatabaseMetaData databaseMetaData = conn.getMetaData();
return databaseMetaData.getDriverName()+" "+databaseMetaData.getDriverVersion();
}
});
}

/**
Expand All @@ -207,7 +224,7 @@ public static String getDriverInfo( ConnectionFactory cf ) throws DAOException {
*/
private static String getParamName( String prefix, String name ) {
String res = name;
if ( prefix != null && !prefix.equals( "" ) ) {
if ( StringUtils.isNotEmpty( prefix ) ) {
res = prefix+"-"+name;
}
return res;
Expand Down Expand Up @@ -256,17 +273,10 @@ public static ConnectionFactory newInstance( Properties props, String propsPrefi
props.getProperty( getParamName( prefix, PROP_CF_MODE_DC_USR ) ),
props.getProperty( getParamName( prefix, PROP_CF_MODE_DC_PWD ) ), cl );
}
} else if ( PROP_CF_MODE_DS.equalsIgnoreCase( mode ) ) {
cf = newInstance( props.getProperty( PROP_CF_MODE_DS_NAME ) );
} else if ( PROP_CF_MODE_DS2.equalsIgnoreCase( mode ) ) {
} else if ( PROP_CF_MODE_DS.equalsIgnoreCase( mode ) || PROP_CF_MODE_DS2.equalsIgnoreCase( mode ) ) {
String dsName = props.getProperty( PROP_CF_MODE_DS_NAME );
try {
javax.naming.InitialContext ctx = new javax.naming.InitialContext();
DataSource dataSource = ( DataSource ) ctx.lookup( dsName );
cf = newInstance( dataSource );
} catch (Exception e) {
throw ( new DAOException( e ) );
}
log.info( "dsName -> {}", dsName );
cf = newInstance( dsName );
} else {
throw ( new DAOException( "Unsupported factory mode ( valid values ar 'dc', 'ds', 'ds2' )" ) );
}
Expand All @@ -284,7 +294,10 @@ public static ConnectionFactory newInstance( Properties props, String propsPrefi
* @throws DAOException in case of issues
*/
public static ConnectionFactory newInstance(Driver drv, String url, String usr, String pwd) throws DAOException {
return new DirectConnectionFactory( drv, url , usr, pwd );
Properties info = new Properties();
info.setProperty( "user", usr );
info.setProperty( "password", pwd );
return new SupplierConnectionFactory( "directConnectionSupplier" , () -> drv.connect( url, info ) );
}

/**
Expand Down Expand Up @@ -313,23 +326,19 @@ public static ConnectionFactory newInstance(String drv, String url, String usr,
* @throws DAOException in case of issues
*/
public static ConnectionFactory newInstance(String drv, String url, String usr, String pwd, ClassLoader cl) throws DAOException {
ConnectionFactory connectionFactory = null;
try {
log.info( "ConnectionFactoryImpl.newInstance() direct connection driver : {}", drv );
log.info( "ConnectionFactoryImpl.newInstance() direct connection url : {}", url );
log.info( "ConnectionFactoryImpl.newInstance() direct connection username : {}", usr );
log.info( "ConnectionFactoryImpl.newInstance() direct connection password : ******" );
log.info( "ConnectionFactoryImpl.newInstance() direct connection driver : {}", drv );
log.info( "ConnectionFactoryImpl.newInstance() direct connection url : {}", url );
log.info( "ConnectionFactoryImpl.newInstance() direct connection username : {}", usr );
log.info( "ConnectionFactoryImpl.newInstance() direct connection password : ******" );
return DAOException.get( () -> {
Driver driver = null;
if ( cl != null ) {
driver = (Driver)cl.loadClass( drv ).getDeclaredConstructor().newInstance();
} else {
driver= (Driver)Class.forName( drv ).asSubclass( Driver.class ).getDeclaredConstructor().newInstance();
}
connectionFactory = ( new DirectConnectionFactory( driver, url, usr, pwd ) );
} catch (Exception e) {
throw ( new DAOException( e ) );
}
return connectionFactory;
return newInstance(driver, url, usr, pwd);
});
}

/**
Expand All @@ -341,7 +350,7 @@ public static ConnectionFactory newInstance(String drv, String url, String usr,
*/
public static ConnectionFactoryImpl newInstance(String dsName) throws DAOException {
log.info( "ConnectionFactoryImpl.newInstance() data source name : {}", dsName );
return (new DSConnectionFactory(dsName));
return newInstance( DAOException.get( () -> (DataSource)new InitialContext().lookup( dsName ) ) );
}

/**
Expand All @@ -353,7 +362,7 @@ public static ConnectionFactoryImpl newInstance(String dsName) throws DAOExcepti
*/
public static ConnectionFactoryImpl newInstance(DataSource ds) throws DAOException {
log.info( "ConnectionFactoryImpl.newInstance() data source : {}", ds );
return (new DS2ConnectionFactory(ds));
return new SupplierConnectionFactory( "dataSourceSupplier" , () -> ds.getConnection() );
}

/*
Expand All @@ -374,112 +383,26 @@ public void release() throws DAOException {

}

/**
* ConnectionFactory implementations based on DriverManager
*
* @author Fugerit
*
*/
class DirectConnectionFactory extends ConnectionFactoryImpl {

private String url;
private Driver driver;
private Properties info;
class SupplierConnectionFactory extends ConnectionFactoryImpl {

public DirectConnectionFactory( Driver drv, String url, String usr, String pwd ) {
this.driver = drv;
this.url = url;
this.info = new Properties();
this.info.setProperty( "user", usr );
this.info.setProperty( "password", pwd );
}
private String description;

@Override
public Connection getConnection() throws DAOException {
Connection conn = null;
try {
conn = this.driver.connect( this.url, this.info );
} catch (Exception e) {
throw DAOException.convertExMethod( "getConnection", e );
}
return conn;
private UnsafeSupplier<Connection, Exception> supplier;

public SupplierConnectionFactory(String description, UnsafeSupplier<Connection, Exception> supplier) {
super();
this.description = description;
this.supplier = supplier;
}

}

/**
* ConnectionFactory implementation based on a Data Source
*
* @author Fugerit
*
*/
@Slf4j
class DSConnectionFactory extends ConnectionFactoryImpl {

@Override
public String toString() {
return this.getClass().getName()+"[dsName:"+this.dsName+",source:"+this.source+"]";
}

@Override
public Connection getConnection() throws DAOException {
Connection conn = null;
try {
conn = this.source.getConnection();
} catch (SQLException se) {
throw (new DAOException("Cannot create connection", se));
}
return conn;
return DAOException.get( supplier );
}

private String dsName;

private DataSource source;

public DSConnectionFactory(String dsName) throws DAOException {
log.info( "INIT START, dsName={}", dsName );
this.dsName = dsName;
try {
Context ctx = new InitialContext();
source = (DataSource) ctx.lookup(dsName);
} catch (NamingException ne) {
throw (new DAOException("Cannot create ConnectionFactory", ne));
} catch (Exception e) {
throw (new DAOException("Fatal Error", e));
}
log.info( "INIT END, source={}", source );
}

}

/**
* ConnectionFactory implementation based on a Data Source (v2)
*
* @author Fugerit
*
*/
class DS2ConnectionFactory extends ConnectionFactoryImpl {

@Override
public String toString() {
return this.getClass().getName()+"[source:"+this.source+"]";
}

@Override
public Connection getConnection() throws DAOException {
Connection conn = null;
try {
conn = this.source.getConnection();
} catch (SQLException se) {
throw (new DAOException("Cannot create Connection", se));
}
return conn;
return "SupplierConnectionFactory["+this.description+"]";
}

private DataSource source;

public DS2ConnectionFactory(DataSource ds) throws DAOException {
this.source = ds;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
package org.fugerit.java.core.db.connect;

import java.sql.Connection;
import java.sql.SQLException;

import org.apache.commons.dbcp2.BasicDataSource;
import org.fugerit.java.core.db.dao.DAOException;
Expand Down Expand Up @@ -66,7 +65,7 @@ public DbcpConnectionFactory( String drv, String url, String usr, String pwd, in
* @throws DAOException in case of issues
*/
public DbcpConnectionFactory( String drv, String url, String usr, String pwd, int init, int min, int max, ClassLoader cl ) throws DAOException {
try {
DAOException.apply( () -> {
this.dataSource = new BasicDataSource();
this.dataSource.setDriverClassName( drv );
this.dataSource.setUrl( url );
Expand All @@ -78,21 +77,12 @@ public DbcpConnectionFactory( String drv, String url, String usr, String pwd, in
if ( cl != null ) {
this.dataSource.setDriverClassLoader( cl );
}
} catch (Exception e) {
throw new DAOException( e );
}
});
}

@Override
public Connection getConnection() throws DAOException {
Connection conn = null;
try {
conn = this.dataSource.getConnection();
} catch (SQLException e) {
throw new DAOException( e );
}
return conn;
return DAOException.get( () -> this.dataSource.getConnection() );
}


}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.fugerit.java.core.db.connect;

import java.sql.Connection;
import java.sql.SQLException;

import org.fugerit.java.core.db.dao.DAOException;
import org.fugerit.java.core.db.metadata.DataBaseInfo;
Expand All @@ -24,11 +23,7 @@ public DataBaseInfo getDataBaseInfo() throws DAOException {

@Override
public void release() throws DAOException {
try {
this.getConnection().close();
} catch (SQLException e) {
throw new DAOException( "Error closing connection "+this.conn, e );
}
DAOException.apply( () -> this.getConnection().close() );
}

@Override
Expand Down
Loading
Loading