Skip to content

Commit

Permalink
Sonar cloud issue fix
Browse files Browse the repository at this point in the history
  • Loading branch information
fugerit79 committed Sep 15, 2023
1 parent 03e158f commit 91409ea
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 120 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Changed

- fj-daogen-version set to 1.2.1 in module fj-core-jvfs

## [8.3.1] - 2023-09-15

### Changed
Expand Down
2 changes: 1 addition & 1 deletion fj-core-jvfs/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
</organization>

<properties>
<fj-daogen-version>1.1.9</fj-daogen-version>
<fj-daogen-version>1.2.1</fj-daogen-version>
<gen.base.dir>${project.basedir}</gen.base.dir>
<generated.source.daogen>target/generated-sources/daogen</generated.source.daogen>
</properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@
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.BooleanUtils;
import org.fugerit.java.core.lang.helpers.StringUtils;
import org.fugerit.java.core.log.BasicLogObject;
import org.fugerit.java.core.util.PropsIO;
import org.fugerit.java.core.xml.dom.DOMUtils;
import org.fugerit.java.core.xml.dom.SearchDOM;
import org.w3c.dom.Element;
Expand Down Expand Up @@ -175,11 +177,11 @@ public Connection getConnection() throws DAOException {
/**
* Parse a configuration Element looking for ConnectionFactory configuration
*
* @param cfConfig the Element
* @return the CfConfig
* @throws Exception in case of issues
* @param cfConfig the Element
* @return the CfConfig
* @throws DAOException in case of issues
*/
public static CfConfig parseCfConfig( Element cfConfig ) throws Exception {
public static CfConfig parseCfConfig( Element cfConfig ) throws DAOException {
CfConfig config = new CfConfig();
SearchDOM searchDOM = SearchDOM.newInstance( true , true );
List<Element> cfConfigEntryList = searchDOM.findAllTags( cfConfig , "cf-config-entry" );
Expand Down Expand Up @@ -215,21 +217,6 @@ public static String getDriverInfo( ConnectionFactory cf ) throws DAOException {
});
}

/**
* Helper method to create a property with prefix
*
* @param prefix the prefix
* @param name the property base name
* @return the property full name ( prefix-name, or name if prefix == null)
*/
private static String getParamName( String prefix, String name ) {
String res = name;
if ( StringUtils.isNotEmpty( prefix ) ) {
res = prefix+"-"+name;
}
return res;
}

/**
* Creates a ConnectionFactory from a property object
*
Expand All @@ -256,25 +243,24 @@ public static ConnectionFactory newInstance( Properties props, String propsPrefi
}
ConnectionFactory cf = null;
String prefix = props.getProperty( PROP_CF_MODE_DC_PREFIX, propsPrefix );
String mode = props.getProperty( getParamName( prefix, PROP_CF_MODE ) );
Properties prefixProps = props;
if ( StringUtils.isNotEmpty( prefix ) ) {
prefixProps = PropsIO.subProps( props , prefix+"-" );
log.info( "subProps : {} -> {}", prefix, prefixProps );
}
String mode = prefixProps.getProperty( PROP_CF_MODE );
log.info( "ConnectionFactory.newInstance() mode : {}", mode );
if ( PROP_CF_MODE_DC.equalsIgnoreCase( mode ) ) {
if ( "true".equalsIgnoreCase( props.getProperty( getParamName( prefix, PROP_CF_EXT_POOLED ) ) ) ) {
int sc = Integer.parseInt( props.getProperty( getParamName( prefix, PROP_CF_EXT_POOLED_SC ), "3" ) );
int ic = Integer.parseInt( props.getProperty( getParamName( prefix, PROP_CF_EXT_POOLED_IC ), "10" ) );
int mc = Integer.parseInt( props.getProperty( getParamName( prefix, PROP_CF_EXT_POOLED_MC ), "30" ) );
cf = new DbcpConnectionFactory( props.getProperty( getParamName( prefix, PROP_CF_MODE_DC_DRV ) ),
props.getProperty( getParamName( prefix, PROP_CF_MODE_DC_URL ) ),
props.getProperty( getParamName( prefix, PROP_CF_MODE_DC_USR ) ),
props.getProperty( getParamName( prefix, PROP_CF_MODE_DC_PWD ) ), sc, ic, mc, cl );
if ( BooleanUtils.isTrue( prefixProps.getProperty(PROP_CF_EXT_POOLED ) ) ) {
cf = new DbcpConnectionFactory(prefixProps, cl);
} else {
cf = newInstance( props.getProperty( getParamName( prefix, PROP_CF_MODE_DC_DRV ) ),
props.getProperty( getParamName( prefix, PROP_CF_MODE_DC_URL ) ),
props.getProperty( getParamName( prefix, PROP_CF_MODE_DC_USR ) ),
props.getProperty( getParamName( prefix, PROP_CF_MODE_DC_PWD ) ), cl );
cf = newInstance( prefixProps.getProperty( PROP_CF_MODE_DC_DRV ),
prefixProps.getProperty(PROP_CF_MODE_DC_URL ),
prefixProps.getProperty(PROP_CF_MODE_DC_USR ),
prefixProps.getProperty(PROP_CF_MODE_DC_PWD ), cl );
}
} else if ( PROP_CF_MODE_DS.equalsIgnoreCase( mode ) || PROP_CF_MODE_DS2.equalsIgnoreCase( mode ) ) {
String dsName = props.getProperty( PROP_CF_MODE_DS_NAME );
String dsName = prefixProps.getProperty( PROP_CF_MODE_DS_NAME );
log.info( "dsName -> {}", dsName );
cf = newInstance( dsName );
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
package org.fugerit.java.core.db.connect;

import java.sql.Connection;
import java.util.Properties;

import org.apache.commons.dbcp2.BasicDataSource;
import org.fugerit.java.core.db.dao.DAOException;
Expand All @@ -36,41 +37,30 @@ public class DbcpConnectionFactory extends ConnectionFactoryImpl {
private BasicDataSource dataSource;

/**
* Constructor
*
* @param drv driver type
* @param url jdbc url
* @param usr user
* @param pwd password
* @param init initial connection
* @param min minimum connection
* @param max maximum connection
* @throws DAOException in case of issues
* @param cfProps connections parameters
* @throws DAOException in case of issues
*/
public DbcpConnectionFactory( String drv, String url, String usr, String pwd, int init, int min, int max ) throws DAOException {
this(drv, url, usr, pwd, init, min, max, null);
public DbcpConnectionFactory( Properties cfProps ) throws DAOException {
this(cfProps, null);
}

/**
* Constructor
*
* @param drv driver type
* @param url jdbc url
* @param usr user
* @param pwd password
* @param init initial connection
* @param min minimum connection
* @param max maximum connection
* @param cl the class loader
* @throws DAOException in case of issues
* @param cfProps connections parameters
* @param cl to use for driver class loading
* @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 {
public DbcpConnectionFactory( Properties cfProps, ClassLoader cl ) throws DAOException {
DAOException.apply( () -> {
int init = Integer.parseInt( cfProps.getProperty( PROP_CF_EXT_POOLED_SC , "3" ) );
int min = Integer.parseInt( cfProps.getProperty( PROP_CF_EXT_POOLED_IC , "10" ) );
int max = Integer.parseInt( cfProps.getProperty( PROP_CF_EXT_POOLED_MC , "30" ) );
this.dataSource = new BasicDataSource();
this.dataSource.setDriverClassName( drv );
this.dataSource.setUrl( url );
this.dataSource.setUsername( usr );
this.dataSource.setPassword( pwd );
this.dataSource.setDriverClassName( cfProps.getProperty( PROP_CF_MODE_DC_DRV ) );
this.dataSource.setUrl( cfProps.getProperty( PROP_CF_MODE_DC_URL ) );
this.dataSource.setUsername( cfProps.getProperty( PROP_CF_MODE_DC_USR ) );
this.dataSource.setPassword( cfProps.getProperty( PROP_CF_MODE_DC_PWD ) );
this.dataSource.setMaxTotal( max );
this.dataSource.setMaxIdle( min );
this.dataSource.setInitialSize( init );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ public static int indentifyDB( String productName ) {
dbType = DB_POSTGRESQL;
log.info( "IdGenerator configured for : POSTGRESQL ({}) was ({})", dbType, name );
} else {
dbType = DB_UNKNOWN;
log.info( "Unknown db type ({})", dbType );
}
return dbType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.fugerit.java.core.cfg.ConfigException;
import org.fugerit.java.core.cfg.ConfigRuntimeException;
import org.fugerit.java.core.function.SafeFunction;
import org.fugerit.java.core.lang.helpers.ClassHelper;
import org.fugerit.java.core.lang.helpers.StringUtils;
import org.fugerit.java.core.xml.dom.DOMIO;
Expand All @@ -22,20 +25,22 @@
*/
public class FixedFieldFileConfig {

private static void handleValidatorList( FixedFieldFileDescriptor fileDescriptor, Element currentFileTag ) throws Exception {
NodeList validatorListTagList = currentFileTag.getElementsByTagName( "validator-list" );
for ( int k=0; k<validatorListTagList.getLength(); k++ ) {
Element currentValidatorListTag = (Element) validatorListTagList.item( k );
NodeList validatorTagList = currentValidatorListTag.getElementsByTagName( "validator" );
for ( int j=0; j<validatorTagList.getLength(); j++ ) {
Element currentValidatorTag = (Element) validatorTagList.item( j );
String id = currentValidatorTag.getAttribute( "id" );
String type = currentValidatorTag.getAttribute( "type" );
FixedFileFieldValidator validator = (FixedFileFieldValidator)ClassHelper.newInstance( type );
validator.configure( currentValidatorTag );
fileDescriptor.getValidators().put( id , validator );
}
}
private static void handleValidatorList( FixedFieldFileDescriptor fileDescriptor, Element currentFileTag ) throws ConfigException {
ConfigException.apply( () -> {
NodeList validatorListTagList = currentFileTag.getElementsByTagName( "validator-list" );
for ( int k=0; k<validatorListTagList.getLength(); k++ ) {
Element currentValidatorListTag = (Element) validatorListTagList.item( k );
NodeList validatorTagList = currentValidatorListTag.getElementsByTagName( "validator" );
for ( int j=0; j<validatorTagList.getLength(); j++ ) {
Element currentValidatorTag = (Element) validatorTagList.item( j );
String id = currentValidatorTag.getAttribute( "id" );
String type = currentValidatorTag.getAttribute( "type" );
FixedFileFieldValidator validator = (FixedFileFieldValidator)ClassHelper.newInstance( type );
validator.configure( currentValidatorTag );
fileDescriptor.getValidators().put( id , validator );
}
}
} );
}

private static void handleFiledList( FixedFieldFileDescriptor fileDescriptor, Element currentFileTag ) {
Expand All @@ -60,42 +65,55 @@ private static void handleFiledList( FixedFieldFileDescriptor fileDescriptor, El
}
}

public static FixedFieldFileConfig parseConfig( InputStream is ) throws Exception {
FixedFieldFileConfig config = new FixedFieldFileConfig();
DocumentBuilderFactory dbf = DOMIO.newSafeDocumentBuilderFactory();
DocumentBuilder parser = dbf.newDocumentBuilder();
Document doc = parser.parse( is );
Element root = doc.getDocumentElement();
NodeList fileTagList = root.getElementsByTagName( "fixed-field-file" );
for ( int i=0; i<fileTagList.getLength(); i++ ) {
Element currentFileTag = (Element) fileTagList.item( i );
String idFile = currentFileTag.getAttribute( "id" );
String checkLength = currentFileTag.getAttribute( "check-length" );
String endline = currentFileTag.getAttribute( "endline" );
String encoding = currentFileTag.getAttribute( "encoding" );
String baseLocale = currentFileTag.getAttribute( "base-locale" );
FixedFieldFileDescriptor fileDescriptor = new FixedFieldFileDescriptor( idFile, "map", encoding );
if ( StringUtils.isNotEmpty( checkLength ) ) {
fileDescriptor.setCheckLengh( Integer.parseInt( checkLength.trim() ) );
}
if ( StringUtils.isNotEmpty( endline ) ) {
fileDescriptor.setEndline( endline );
}
if ( StringUtils.isNotEmpty( baseLocale ) ) {
fileDescriptor.setBaseLocale( baseLocale );
/**
* <p>Parse fixed filed file configuration.</p>
*
* <p>NOTE: starting from version 8.4.X java.lang.Exception removed in favor of org.fugerit.java.core.cfg.ConfigRuntimeException.</p>
*
* @see <a href="https://fuzzymemory.fugerit.org/src/docs/sonar_cloud/java-S112.html">Define and throw a dedicated exception instead of using a generic one.</a>
*
* @param is the configuration file to parse
* @return the parsed configuration
* @throws ConfigRuntimeException in case of issues
*/
public static FixedFieldFileConfig parseConfig( InputStream is ) {
return SafeFunction.get( () -> {
FixedFieldFileConfig config = new FixedFieldFileConfig();
DocumentBuilderFactory dbf = DOMIO.newSafeDocumentBuilderFactory();
DocumentBuilder parser = dbf.newDocumentBuilder();
Document doc = parser.parse( is );
Element root = doc.getDocumentElement();
NodeList fileTagList = root.getElementsByTagName( "fixed-field-file" );
for ( int i=0; i<fileTagList.getLength(); i++ ) {
Element currentFileTag = (Element) fileTagList.item( i );
String idFile = currentFileTag.getAttribute( "id" );
String checkLength = currentFileTag.getAttribute( "check-length" );
String endline = currentFileTag.getAttribute( "endline" );
String encoding = currentFileTag.getAttribute( "encoding" );
String baseLocale = currentFileTag.getAttribute( "base-locale" );
FixedFieldFileDescriptor fileDescriptor = new FixedFieldFileDescriptor( idFile, "map", encoding );
if ( StringUtils.isNotEmpty( checkLength ) ) {
fileDescriptor.setCheckLengh( Integer.parseInt( checkLength.trim() ) );
}
if ( StringUtils.isNotEmpty( endline ) ) {
fileDescriptor.setEndline( endline );
}
if ( StringUtils.isNotEmpty( baseLocale ) ) {
fileDescriptor.setBaseLocale( baseLocale );
}

// validator list
handleValidatorList(fileDescriptor, currentFileTag);

// field list
handleFiledList(fileDescriptor, currentFileTag);

// TODO: review this code
config.addFileDescriptor( idFile , fileDescriptor );
}

// validator list
handleValidatorList(fileDescriptor, currentFileTag);

// field list
handleFiledList(fileDescriptor, currentFileTag);

// TODO: review this code
config.addFileDescriptor( idFile , fileDescriptor );
}
is.close();
return config;
is.close();
return config;
} );
}

private FixedFieldFileConfig() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
import org.fugerit.java.core.lang.helpers.StringUtils;
import org.fugerit.java.core.lang.helpers.reflect.PathHelper;
import org.fugerit.java.core.util.collection.KeyString;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Element;

import lombok.extern.slf4j.Slf4j;

/**
* Default binding helper implementation
*
Expand All @@ -24,6 +24,7 @@
* @author Matteo a.k.a. Fugerit
*
*/
@Slf4j
public class BindingHelperDefault extends XMLConfigurableObject implements Serializable, BindingHelper, IdConfigType, KeyString {

public static final BindingHelper DEFAULT = new BindingHelperDefault( BindingCatalogConfig.ID_DEFAULT_HELPER );
Expand All @@ -34,15 +35,14 @@ public BindingHelperDefault() {
public BindingHelperDefault( String id ) {
this.setId( id );
}

private final static Logger logger = LoggerFactory.getLogger( BindingHelperDefault.class );

/**
*
*/
private static final long serialVersionUID = 1342342342323L;

public Object convertValue(BindingContext context, BindingConfig binding, BindingFieldConfig field, Object value) throws Exception {
log.trace( "convertValue() context {} , binding {}", context, binding );
log.trace( "convertValue() field {} , value {}", field, value );
return value;
}

Expand Down Expand Up @@ -82,7 +82,7 @@ public void bindingWorker(BindingContext context, BindingConfig binding, Binding
}
}
String messageTo = bindTo+" - "+bind;
logger.debug( "bindFrom {} to {}", bindFrom, messageTo );
log.debug( "bindFrom {} to {}", bindFrom, messageTo );
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ private boolean worker( ConnectionFactory wrapped ) throws Exception {
try ( ConnectionFactoryCloseable cf = ConnectionFactoryImpl.wrap( wrapped );
Connection conn = cf.getConnection() ) {
log.info( "db info : {}", cf.getDataBaseInfo() );
log.info( "db info second time : {}", cf.getDataBaseInfo() );
log.info( "driver info : {}", ConnectionFactoryImpl.getDriverInfo( cf ) );
}
ok = (wrapped != null);
Expand All @@ -50,15 +51,15 @@ private boolean worker( ConnectionFactory wrapped ) throws Exception {
public void testCFProps() throws Exception {
String name = "wrapped";
Properties props= PropsIO.loadFromClassLoaderSafe( BasicDBHelper.DEFAULT_DB_CONN_PATH );
props.setProperty( ConnectionFactoryImpl.PROP_CF_EXT_POOLED_IC , "1" );
props.setProperty( ConnectionFactoryImpl.PROP_CF_EXT_POOLED_SC , "1" );
props.setProperty( ConnectionFactoryImpl.PROP_CF_EXT_POOLED_MC , "3" );
ConnectionFactory wrapped = ConnectionFactoryImpl.newInstance( props );
ConnectionFacade.registerFactory( name , wrapped );
boolean ok = this.worker( ConnectionFacade.getFactory( name ) );
Assert.assertTrue(ok);
// pooled
ok = this.worker( new DbcpConnectionFactory( props.getProperty( ConnectionFactoryImpl.PROP_CF_MODE_DC_DRV ),
props.getProperty( ConnectionFactoryImpl.PROP_CF_MODE_DC_URL ),
props.getProperty( ConnectionFactoryImpl.PROP_CF_MODE_DC_USR ),
props.getProperty( ConnectionFactoryImpl.PROP_CF_MODE_DC_PWD ), 1, 1, 3 ) );
ok = this.worker( new DbcpConnectionFactory( props ) );
Assert.assertTrue(ok);
}

Expand Down

0 comments on commit 91409ea

Please sign in to comment.