Unverified Commit 03bc010c authored by Romain Bioteau's avatar Romain Bioteau Committed by GitHub
Browse files

feat(license) use license-maven-plugin (#13)

* check license headers at build time
* use the license-maven-plugin to apply the header to new files
parent 679f6ad2
......@@ -147,6 +147,7 @@
<nexus-staging-maven-plugin.version>1.6.8</nexus-staging-maven-plugin.version>
<maven-javadoc-plugin.version>3.2.0</maven-javadoc-plugin.version>
<maven-source-plugin.version>3.2.1</maven-source-plugin.version>
<license-maven-plugin.version>4.0.rc2</license-maven-plugin.version>
<!-- Sonar -->
<sonar.projectKey>bonitasoft_bonita-connector-database</sonar.projectKey>
......@@ -221,6 +222,21 @@
</resources>
<pluginManagement>
<plugins>
<plugin>
<groupId>com.mycila</groupId>
<artifactId>license-maven-plugin</artifactId>
<version>${license-maven-plugin.version}</version>
<configuration>
<licenseSets>
<licenseSet>
<header>${project.basedir}/src/license/header.txt</header>
<includes>
<include>**/*.java</include>
</includes>
</licenseSet>
</licenseSets>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
......@@ -323,6 +339,19 @@
</pluginManagement>
<plugins>
<plugin>
<groupId>com.mycila</groupId>
<artifactId>license-maven-plugin</artifactId>
<executions>
<execution>
<id>license-check-file-header</id>
<goals>
<goal>check</goal>
</goals>
<phase>validate</phase>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.gmaven</groupId>
<artifactId>groovy-maven-plugin</artifactId>
......
Copyright (C) 2009 - 2020 Bonitasoft S.A.
Bonitasoft, 32 rue Gustave Eiffel - 38000 Grenoble
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 2.0 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.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
\ No newline at end of file
/**
* Copyright (C) 2009-2011 BonitaSoft S.A.
* BonitaSoft, 31 rue Gustave Eiffel - 38000 Grenoble
/*
* Copyright (C) 2009 - 2020 Bonitasoft S.A.
* Bonitasoft, 32 rue Gustave Eiffel - 38000 Grenoble
* 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 2.0 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package org.bonitasoft.connectors.database;
......@@ -40,7 +43,7 @@ public class Database {
private Context ctx;
private Statement selectStatement;
private Statement selectStatement;
public Database(final String driver, final String url, final String username, final String password)
throws ClassNotFoundException, SQLException {
......@@ -55,10 +58,10 @@ public class Database {
}
public void disconnect() throws SQLException, NamingException {
if(selectStatement != null) {
selectStatement.close();
selectStatement = null;
}
if (selectStatement != null) {
selectStatement.close();
selectStatement = null;
}
if (connection != null && !connection.isClosed()) {
connection.close();
}
......@@ -68,9 +71,9 @@ public class Database {
}
public ResultSet select(final String query) throws ConnectorException, SQLException {
if(selectStatement != null) {
throw new ConnectorException("A Statement is already opened.");
}
if (selectStatement != null) {
throw new ConnectorException("A Statement is already opened.");
}
selectStatement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
return selectStatement.executeQuery(query);
......@@ -78,7 +81,7 @@ public class Database {
public boolean executeCommand(final String command) throws SQLException, ConnectorException {
Statement statement = null;
boolean isExecuted = false;
boolean isExecuted = false;
try {
statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
isExecuted = statement.execute(command);
......@@ -94,12 +97,14 @@ public class Database {
/**
* Does not produce an output result. A Statement is created, executed and closed.
*
* @param commands, the list of SQL command to execute
* @param commit , commit after the batch execution
* @throws SQLException
* @throws ConnectorException
*/
public void executeBatch(final List<String> commands, final boolean commit) throws SQLException, ConnectorException {
public void executeBatch(final List<String> commands, final boolean commit)
throws SQLException, ConnectorException {
Statement statement = null;
try {
connection.setAutoCommit(false);
......
/**
* Copyright (C) 2012 BonitaSoft S.A.
* BonitaSoft, 31 rue Gustave Eiffel - 38000 Grenoble
/*
* Copyright (C) 2009 - 2020 Bonitasoft S.A.
* Bonitasoft, 32 rue Gustave Eiffel - 38000 Grenoble
* 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 2.0 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package org.bonitasoft.connectors.database.datasource;
......@@ -38,121 +41,120 @@ public class DatasourceConnector implements Connector {
private static final Logger LOGGER = Logger.getLogger(DatasourceConnector.class.getName());
public static final String DATASOURCE_INPUT = "dataSourceName";
public static final String SCRIPT_INPUT = "script";
public static final String SEPARATOR_INPUT = "separator";
public static final String PROPERTIES_INPUT = "properties";
private String datasource;
private String script;
private String separator;
private Properties properties;
private Database database;
@Override
public Map<String, Object> execute() throws ConnectorException {
if (hasMultipleQuery()) {
return executeBatch();
} else {
return executeSingleQuery();
}
}
public static final String SCRIPT_INPUT = "script";
public static final String SEPARATOR_INPUT = "separator";
public static final String PROPERTIES_INPUT = "properties";
private String datasource;
private String script;
private String separator;
private Properties properties;
private Database database;
@Override
public Map<String, Object> execute() throws ConnectorException {
if (hasMultipleQuery()) {
return executeBatch();
} else {
return executeSingleQuery();
}
}
private boolean hasMultipleQuery() {
return separator != null;
}
@Override
public void setInputParameters(final Map<String, Object> parameters) {
datasource = (String) parameters.get(DATASOURCE_INPUT);
LOGGER.info(DATASOURCE_INPUT + " " + datasource);
script = (String) parameters.get(SCRIPT_INPUT);
LOGGER.info(SCRIPT_INPUT + " " + script);
separator = (String) parameters.get(SEPARATOR_INPUT);
LOGGER.info(SEPARATOR_INPUT + " " + separator);
@Override
public void setInputParameters(final Map<String, Object> parameters) {
datasource = (String) parameters.get(DATASOURCE_INPUT);
LOGGER.info(DATASOURCE_INPUT + " " + datasource);
script = (String) parameters.get(SCRIPT_INPUT);
LOGGER.info(SCRIPT_INPUT + " " + script);
separator = (String) parameters.get(SEPARATOR_INPUT);
LOGGER.info(SEPARATOR_INPUT + " " + separator);
@SuppressWarnings("unchecked")
List<List<Object>> propertiesList = (List<List<Object>>) parameters.get(PROPERTIES_INPUT);
if (propertiesList != null) {
properties = new Properties();
for (List<Object> line : propertiesList) {
if(line.size() == 1){
LOGGER.info("Property " + line.get(0) + " null");
properties.put(line.get(0), null);
}else if(line.size() == 2){
LOGGER.info("Property " + line.get(0) + " " + line.get(1));
properties.put(line.get(0), line.get(1));
}//else ignore line
}
}
}
@Override
public void validateInputParameters() throws ConnectorValidationException {
if (propertiesList != null) {
properties = new Properties();
for (List<Object> line : propertiesList) {
if (line.size() == 1) {
LOGGER.info("Property " + line.get(0) + " null");
properties.put(line.get(0), null);
} else if (line.size() == 2) {
LOGGER.info("Property " + line.get(0) + " " + line.get(1));
properties.put(line.get(0), line.get(1));
} //else ignore line
}
}
}
@Override
public void validateInputParameters() throws ConnectorValidationException {
final List<String> messages = new ArrayList<>(0);
if (datasource == null || datasource.isEmpty()) {
messages.add("Datasource can't be empty");
}
if (script == null || script.isEmpty()) {
messages.add("Script is not set");
}
if (!messages.isEmpty()) {
throw new ConnectorValidationException(this, messages);
}
}
@Override
public void connect() throws ConnectorException {
try {
database = new Database(datasource, properties);
} catch (final Exception e) {
throw new ConnectorException(e);
}
}
@Override
public void disconnect() throws ConnectorException {
if (database != null) {
try {
database.disconnect();
} catch (final Exception e) {
throw new ConnectorException(e);
}
}
}
private Map<String, Object> executeSingleQuery() throws ConnectorException {
try {
final String command = script.toUpperCase().trim();
if (datasource == null || datasource.isEmpty()) {
messages.add("Datasource can't be empty");
}
if (script == null || script.isEmpty()) {
messages.add("Script is not set");
}
if (!messages.isEmpty()) {
throw new ConnectorValidationException(this, messages);
}
}
@Override
public void connect() throws ConnectorException {
try {
database = new Database(datasource, properties);
} catch (final Exception e) {
throw new ConnectorException(e);
}
}
@Override
public void disconnect() throws ConnectorException {
if (database != null) {
try {
database.disconnect();
} catch (final Exception e) {
throw new ConnectorException(e);
}
}
}
private Map<String, Object> executeSingleQuery() throws ConnectorException {
try {
final String command = script.toUpperCase().trim();
final Map<String, Object> result = new HashMap<>(2);
if (command.startsWith("SELECT")) {
result.put("resultset", database.select(script));
} else {
database.executeCommand(script);
}
return result;
} catch (final SQLException sqle) {
throw new ConnectorException(sqle);
}
}
private Map<String, Object> executeBatch() throws ConnectorException {
final List<String> commands = getScriptCommands();
try {
database.executeBatch(commands, true);
return null;
} catch (final Exception e) {
throw new ConnectorException(e);
}
}
private List<String> getScriptCommands() {
if (command.startsWith("SELECT")) {
result.put("resultset", database.select(script));
} else {
database.executeCommand(script);
}
return result;
} catch (final SQLException sqle) {
throw new ConnectorException(sqle);
}
}
private Map<String, Object> executeBatch() throws ConnectorException {
final List<String> commands = getScriptCommands();
try {
database.executeBatch(commands, true);
return null;
} catch (final Exception e) {
throw new ConnectorException(e);
}
}
private List<String> getScriptCommands() {
final List<String> commands = new ArrayList<>();
final StringTokenizer tokenizer = new StringTokenizer(script, separator);
while (tokenizer.hasMoreTokens()) {
final String command = tokenizer.nextToken();
commands.add(command.trim());
}
return commands;
}
final StringTokenizer tokenizer = new StringTokenizer(script, separator);
while (tokenizer.hasMoreTokens()) {
final String command = tokenizer.nextToken();
commands.add(command.trim());
}
return commands;
}
}
/**
* Copyright (C) 2012 BonitaSoft S.A.
* BonitaSoft, 31 rue Gustave Eiffel - 38000 Grenoble
/*
* Copyright (C) 2009 - 2020 Bonitasoft S.A.
* Bonitasoft, 32 rue Gustave Eiffel - 38000 Grenoble
* 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 2.0 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package org.bonitasoft.connectors.database.jdbc;
......@@ -36,244 +39,253 @@ import org.bonitasoft.engine.connector.ConnectorValidationException;
*/
public class JdbcConnector implements Connector {
public static final String RESULTSET_OUTPUT = "resultset";
public static final String RESULTSET_OUTPUT = "resultset";
public static final String SINGLE_RESULT_OUTPUT = "singleResult";
public static final String SINGLE_RESULT_OUTPUT = "singleResult";
public static final String ONEROW_NCOL_RESULT_OUTPUT = "oneRowNColResult";
public static final String ONEROW_NCOL_RESULT_OUTPUT = "oneRowNColResult";
public static final String NROW_ONECOL_RESULT_OUTPUT = "nRowOneColResult";
public static final String NROW_ONECOL_RESULT_OUTPUT = "nRowOneColResult";
public static final String TABLE_RESULT_OUTPUT = "tableResult";
public static final String TABLE_RESULT_OUTPUT = "tableResult";
public static final String USERNAME = "username";
public static final String USERNAME = "username";
public static final String PASSWORD = "password";
public static final String PASSWORD = "password";
public static final String SCRIPT = "script";
public static final String SCRIPT = "script";
public static final String SEPARATOR = "separator";
public static final String SEPARATOR = "separator";
public static final String DRIVER = "driver";
public static final String DRIVER = "driver";
public static final String URL = "url";
public static final String URL = "url";
public static final String OUTPUT_TYPE = "outputType";
public static final String OUTPUT_TYPE = "outputType";
//Output types
public static final String SINGLE = "single";
public static final String N_ROW = "n_row";
public static final String ONE_ROW = "one_row";
public static final String TABLE = "table";
private String url;
private String userName;
private String password;
private String driver;
private String separator;
private String script;
private String outputType;
private Database database;
private ResultSet data;
private Logger LOGGER = Logger.getLogger(this.getClass().getName());
@Override
public Map<String, Object> execute() throws ConnectorException {
if (separator != null) {
return executeBatch();
} else {
return executeSingleQuery();
}
}
@Override
public void setInputParameters(final Map<String, Object> parameters) {
userName = (String) parameters.get(USERNAME);
LOGGER.info(USERNAME + " " + userName);
final String paswordString = (String) parameters.get(PASSWORD);
LOGGER.info(PASSWORD + " ******");
if (paswordString != null && !paswordString.isEmpty()) {
password = paswordString;
} else {
password = null;
}
script = (String) parameters.get(SCRIPT);
LOGGER.info(SCRIPT + " " + script);
separator = (String) parameters.get(SEPARATOR);
LOGGER.info(SEPARATOR + " " + separator);
driver = (String) parameters.get(DRIVER);
LOGGER.info(DRIVER + " " + driver);
url = (String) parameters.get(URL);
LOGGER.info(URL + " " + url);
outputType = (String) parameters.get(OUTPUT_TYPE);
LOGGER.info(OUTPUT_TYPE + " " + outputType);
}
@Override
public void validateInputParameters() throws ConnectorValidationException {
final List<String> messages = new ArrayList<String>(0);
if (url == null || url.isEmpty()) {
messages.add("Url can't be empty");
}
if (driver == null || driver.isEmpty()) {
messages.add("Driver is not set");
}
if (script == null || script.isEmpty()) {
messages.add("Script is not set");
}
if (!messages.isEmpty()) {
throw new ConnectorValidationException(this, messages);
}
}
@Override
public void connect() throws ConnectorException {
try {
database = new Database(driver, url, userName, password);
} catch (final Exception e) {
throw new ConnectorException(e);
}
}
@Override
public void disconnect() throws ConnectorException {
if (script.toUpperCase().trim().startsWith("SELECT")) {
try {
if(data != null){
data.close();
}
} catch (Exception e) {
throw new ConnectorException(e);
}
}
if (database != null) {
try {
database.disconnect();
} catch (final Exception e) {
throw new ConnectorException(e);
}
}
}
private Map<String, Object> executeSingleQuery() throws ConnectorException {
try {
final String command = script.toUpperCase().trim();
//Output types
public static final String SINGLE = "single";
public static final String N_ROW = "n_row";
public static final String ONE_ROW = "one_row";
public static final String TABLE = "table";