You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hop.apache.org by ha...@apache.org on 2021/12/29 17:00:51 UTC
[hop] branch master updated: Sonar cleanup (#1248)
This is an automated email from the ASF dual-hosted git repository.
hansva pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/hop.git
The following commit(s) were added to refs/heads/master by this push:
new 8647a22 Sonar cleanup (#1248)
8647a22 is described below
commit 8647a22f824ddf328ef73dc6ac137d9e341d0d10
Author: Gabriel Dutra <gv...@gmail.com>
AuthorDate: Wed Dec 29 09:00:45 2021 -0800
Sonar cleanup (#1248)
* fixed sonar vulnerabities issues.
* fixed sonar issues
* [SONAR] cleanup
---
.../org/apache/hop/core/database/Database.java | 35 +++++++++---------
.../hop/core/database/DatabaseMetaInformation.java | 20 ++++++-----
.../apache/hop/core/fileinput/CharsetToolkit.java | 34 +++++++-----------
.../java/org/apache/hop/laf/OverlayProperties.java | 2 +-
.../java/org/apache/hop/www/AsyncRunServlet.java | 41 +++++++++++++---------
5 files changed, 68 insertions(+), 64 deletions(-)
diff --git a/core/src/main/java/org/apache/hop/core/database/Database.java b/core/src/main/java/org/apache/hop/core/database/Database.java
index 0beadb0..34243b2 100644
--- a/core/src/main/java/org/apache/hop/core/database/Database.java
+++ b/core/src/main/java/org/apache/hop/core/database/Database.java
@@ -173,7 +173,11 @@ public class Database implements IVariables, ILoggingObject {
@Override
public boolean equals(Object obj) {
Database other = (Database) obj;
- return this.databaseMeta.equals(other.databaseMeta);
+ if (other == null) {
+ return false;
+ } else {
+ return this.databaseMeta.equals(other.databaseMeta);
+ }
}
/**
@@ -407,10 +411,7 @@ public class Database implements IVariables, ILoggingObject {
connection = DriverManager.getConnection(url, properties);
}
- } catch (SQLException e) {
- throw new HopDatabaseException(
- "Error connecting to database: (using class " + classname + ")", e);
- } catch (Throwable e) {
+ } catch (Exception e) {
throw new HopDatabaseException(
"Error connecting to database: (using class " + classname + ")", e);
}
@@ -917,7 +918,7 @@ public class Database implements IVariables, ILoggingObject {
try {
keys.close();
} catch (SQLException e) {
- throw new HopDatabaseException("Unable to close resultset of auto-generated keys", e);
+ log.logError("Unable to close resultset of auto-generated keys", e);
}
}
}
@@ -1279,10 +1280,10 @@ public class Database implements IVariables, ILoggingObject {
prepStmt.close();
} else {
String sqlStripped = databaseMeta.stripCR(sql);
- Statement stmt = connection.createStatement();
- resultSet = stmt.execute(sqlStripped);
- count = stmt.getUpdateCount();
- stmt.close();
+ try (Statement stmt = connection.createStatement()) {
+ resultSet = stmt.execute(sqlStripped);
+ count = stmt.getUpdateCount();
+ }
}
String upperSql = sql.toUpperCase();
if (!resultSet && count > 0) {
@@ -2019,7 +2020,7 @@ public class Database implements IVariables, ILoggingObject {
}
// Store in cache!!
- if (dbcache != null && entry != null && fields != null) {
+ if (dbcache != null && fields != null) {
dbcache.put(entry, fields);
}
@@ -3079,14 +3080,14 @@ public class Database implements IVariables, ILoggingObject {
try {
rs.close();
} catch (Exception e) {
- throw new HopDatabaseException("Unable to close resultset", e);
+ log.logError("Unable to close resultset", e);
}
if (pstmt != null) {
try {
pstmt.close();
} catch (Exception e) {
- throw new HopDatabaseException("Unable to close prepared statement pstmt", e);
+ log.logError("Unable to close prepared statement pstmt", e);
}
pstmt = null;
}
@@ -3094,7 +3095,7 @@ public class Database implements IVariables, ILoggingObject {
try {
selStmt.close();
} catch (Exception e) {
- throw new HopDatabaseException("Unable to close prepared statement sel_stmt", e);
+ log.logError("Unable to close prepared statement sel_stmt", e);
}
selStmt = null;
}
@@ -4207,7 +4208,7 @@ public class Database implements IVariables, ILoggingObject {
* @param tableName The table to create
* @throws HopDatabaseException
*/
- public String getDDLCreationTable(String tableName, IRowMeta fields) throws HopDatabaseException {
+ public String getDDLCreationTable(String tableName, IRowMeta fields) {
String retval;
// First, check for reserved SQL in the input row r...
@@ -4404,12 +4405,12 @@ public class Database implements IVariables, ILoggingObject {
allkeys.close();
}
} catch (SQLException e) {
- throw new HopDatabaseException(
+ log.logError(
"Error closing connection while searching primary keys in table [" + tableName + "]",
e);
}
}
- return names.toArray(new String[names.size()]);
+ return names.toArray(new String[0]);
}
/**
diff --git a/core/src/main/java/org/apache/hop/core/database/DatabaseMetaInformation.java b/core/src/main/java/org/apache/hop/core/database/DatabaseMetaInformation.java
index 3437571..c051b13 100644
--- a/core/src/main/java/org/apache/hop/core/database/DatabaseMetaInformation.java
+++ b/core/src/main/java/org/apache/hop/core/database/DatabaseMetaInformation.java
@@ -178,14 +178,16 @@ public class DatabaseMetaInformation {
//
String sql = databaseMeta.getSqlListOfSchemas();
if (!Utils.isEmpty(sql)) {
- Statement schemaStatement = db.getConnection().createStatement();
- ResultSet schemaResultSet = schemaStatement.executeQuery(sql);
- while (schemaResultSet != null && schemaResultSet.next()) {
- String schemaName = schemaResultSet.getString("name");
- schemaList.add(new Schema(schemaName));
+ try (Statement schemaStatement = db.getConnection().createStatement()) {
+ ResultSet schemaResultSet = schemaStatement.executeQuery(sql);
+ while (schemaResultSet != null && schemaResultSet.next()) {
+ String schemaName = schemaResultSet.getString("name");
+ schemaList.add(new Schema(schemaName));
+ }
+ if (schemaResultSet != null) {
+ schemaResultSet.close();
+ }
}
- schemaResultSet.close();
- schemaStatement.close();
} else {
ResultSet schemaResultSet = dbmd.getSchemas();
while (schemaResultSet != null && schemaResultSet.next()) {
@@ -194,7 +196,9 @@ public class DatabaseMetaInformation {
}
// Close the schema ResultSet immediately
//
- schemaResultSet.close();
+ if (schemaResultSet != null) {
+ schemaResultSet.close();
+ }
}
}
for (Schema schema : schemaList) {
diff --git a/core/src/main/java/org/apache/hop/core/fileinput/CharsetToolkit.java b/core/src/main/java/org/apache/hop/core/fileinput/CharsetToolkit.java
index 48b7502..ae43e52 100644
--- a/core/src/main/java/org/apache/hop/core/fileinput/CharsetToolkit.java
+++ b/core/src/main/java/org/apache/hop/core/fileinput/CharsetToolkit.java
@@ -24,6 +24,7 @@ import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
import java.util.Collection;
/**
@@ -176,13 +177,13 @@ public class CharsetToolkit {
// if the file has a Byte Order Marker, we can assume the file is in UTF-xx
// otherwise, the file would not be human readable
if (hasUTF8Bom(buffer)) {
- return Charset.forName("UTF-8");
+ return StandardCharsets.UTF_8;
}
if (hasUTF16LEBom(buffer)) {
- return Charset.forName("UTF-16LE");
+ return StandardCharsets.UTF_16LE;
}
if (hasUTF16BEBom(buffer)) {
- return Charset.forName("UTF-16BE");
+ return StandardCharsets.UTF_16BE;
}
// if a byte has its most significant bit set, the file is in UTF-8 or in the default encoding
@@ -276,40 +277,29 @@ public class CharsetToolkit {
if (this.enforce8Bit) {
return this.defaultCharset;
} else {
- return Charset.forName("US-ASCII");
+ return StandardCharsets.US_ASCII;
}
}
// if no invalid UTF-8 were encountered, we can assume the encoding is UTF-8,
// otherwise the file would not be human readable
if (validU8Char) {
- return Charset.forName("UTF-8");
+ return StandardCharsets.UTF_8;
}
// finally, if it's not UTF-8 nor US-ASCII, let's assume the encoding is the default encoding
return this.defaultCharset;
}
- public static Charset guessEncoding(File f, int bufferLength)
- throws FileNotFoundException, IOException {
- FileInputStream fis = new FileInputStream(f);
- byte[] buffer = new byte[bufferLength];
- fis.read(buffer);
- fis.close();
+ public static Charset guessEncoding(File f, int bufferLength) throws IOException {
+ byte[] buffer;
+ try (FileInputStream fis = new FileInputStream(f)) {
+ buffer = new byte[bufferLength];
+ fis.read(buffer);
+ }
CharsetToolkit toolkit = new CharsetToolkit(buffer);
toolkit.setDefaultCharset(getDefaultSystemCharset());
return toolkit.guessEncoding();
}
- public static Charset guessEncoding(File f, int bufferLength, Charset defaultCharset)
- throws FileNotFoundException, IOException {
- FileInputStream fis = new FileInputStream(f);
- byte[] buffer = new byte[bufferLength];
- fis.read(buffer);
- fis.close();
- CharsetToolkit toolkit = new CharsetToolkit(buffer);
- toolkit.setDefaultCharset(defaultCharset);
- return toolkit.guessEncoding();
- }
-
/**
* If the byte has the form 10xxxxx, then it's a continuation byte of a multiple byte character;
*
diff --git a/core/src/main/java/org/apache/hop/laf/OverlayProperties.java b/core/src/main/java/org/apache/hop/laf/OverlayProperties.java
index 2b8646a..a3507c6 100644
--- a/core/src/main/java/org/apache/hop/laf/OverlayProperties.java
+++ b/core/src/main/java/org/apache/hop/laf/OverlayProperties.java
@@ -26,7 +26,7 @@ import java.util.Properties;
public class OverlayProperties extends Properties implements IPropertyHandler {
private static final long serialVersionUID = 1L;
- private String name = null;
+ private final String name = null;
public OverlayProperties(String file) throws IOException {
load(file);
diff --git a/plugins/misc/async/src/main/java/org/apache/hop/www/AsyncRunServlet.java b/plugins/misc/async/src/main/java/org/apache/hop/www/AsyncRunServlet.java
index 67d1f86..53ac371 100644
--- a/plugins/misc/async/src/main/java/org/apache/hop/www/AsyncRunServlet.java
+++ b/plugins/misc/async/src/main/java/org/apache/hop/www/AsyncRunServlet.java
@@ -26,6 +26,7 @@ import org.apache.hop.core.logging.LogLevel;
import org.apache.hop.core.logging.LoggingObjectType;
import org.apache.hop.core.logging.SimpleLoggingObject;
import org.apache.hop.core.metadata.SerializableMetadataProvider;
+import org.apache.hop.core.parameters.UnknownParamException;
import org.apache.hop.core.variables.IVariables;
import org.apache.hop.i18n.BaseMessages;
import org.apache.hop.metadata.api.IHopMetadataSerializer;
@@ -171,7 +172,11 @@ public class AsyncRunServlet extends BaseHttpServlet implements IHopServerPlugin
if (Const.indexOfString(requestParameter, pipelineParameters) < 0) {
workflow.setVariable(requestParameter, Const.NVL(requestParameterValue, ""));
} else {
- workflow.setParameterValue(requestParameter, Const.NVL(requestParameterValue, ""));
+ try {
+ workflow.setParameterValue(requestParameter, Const.NVL(requestParameterValue, ""));
+ } catch (UnknownParamException e) {
+ log.logError("Error running asynchronous web service", e);
+ }
}
}
workflow.activateParameters(workflow);
@@ -196,30 +201,34 @@ public class AsyncRunServlet extends BaseHttpServlet implements IHopServerPlugin
//
new Thread(workflow::startExecution).start();
- final OutputStream outputStream = response.getOutputStream();
-
- // Report the ID in a JSON block
- //
- JSONObject json = new JSONObject();
- json.put("name", workflowMeta.getName());
- json.put("id", serverObjectId);
+ try (OutputStream outputStream = response.getOutputStream()) {
- String jsonString = json.toJSONString();
- outputStream.write(jsonString.getBytes(StandardCharsets.UTF_8));
- outputStream.write("\n".getBytes(StandardCharsets.UTF_8));
- outputStream.flush();
+ // Report the ID in a JSON block
+ //
+ JSONObject json = new JSONObject();
+ json.put("name", workflowMeta.getName());
+ json.put("id", serverObjectId);
+ String jsonString = json.toJSONString();
+ outputStream.write(jsonString.getBytes(StandardCharsets.UTF_8));
+ outputStream.write("\n".getBytes(StandardCharsets.UTF_8));
+ outputStream.flush();
+ } catch (IOException e) {
+ log.logError("Error running asynchronous web service", e);
+ }
response.setStatus(HttpServletResponse.SC_OK);
-
} catch (IOException | HopException e) {
log.logError("Error running asynchronous web service", e);
}
}
@Override
- protected void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- super.doPost(request, response);
+ protected void doPost(HttpServletRequest request, HttpServletResponse response) {
+ try {
+ super.doPost(request, response);
+ } catch (ServletException | IOException e) {
+ log.logError("Error running asynchronous web service", e);
+ }
}
public String toString() {