Skip to content

Commit

Permalink
feat: serialization in properties of database nods
Browse files Browse the repository at this point in the history
  • Loading branch information
Nolife999 committed Oct 8, 2023
1 parent 302effe commit b16bb87
Show file tree
Hide file tree
Showing 10 changed files with 417 additions and 283 deletions.
8 changes: 0 additions & 8 deletions arc-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,6 @@
<artifactId>opencsv</artifactId>
<version>3.9</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.json/json -->
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>${project.org.json}</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.mockito/mockito-core -->
<dependency>
<groupId>org.mockito</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,7 @@ private void copyMetadataToSandbox() throws ArcException {

/**
* Instanciate the metadata required into all executors pod
*
* @param envExecution
* @return number of executor pod
* @throws ArcException
*/
protected int copyMetadataToExecutorsAllNods() throws ArcException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,15 @@ public void retrieveRulesTablesFromSchemaTest() throws ArcException {
assertEquals(4, result.size());

result = BddPatcher.retrieveModelTablesFromSchema(c, testSandbox3);
assertTrue(result.contains("arc.ihm_famille"));
assertTrue(result.contains("arc.ihm_mod_table_metier"));
assertTrue(result.contains("arc.ihm_mod_variable_metier"));
assertEquals(3, result.size());

result = BddPatcher.retrieveMappingTablesFromSchema(c, testSandbox3);
assertTrue(result.contains(testSandbox3+".mapping_dsn_employeur_ok"));
assertEquals(1, result.size());


u.executeImmediate(c, "DROP SCHEMA IF EXISTS "+testSandbox3+" CASCADE;");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public void copyMetadataToExecutorsTestScalable() throws SQLException, ArcExcept
int result=synchronizationInstance.copyMetadataToExecutorsAllNods();

// copy should be a success
// return that there is 1 executor nod
assertEquals(1, result);

u.executeImmediate(c, "DROP SCHEMA IF EXISTS "+BddPatcherTest.testSandbox3+" CASCADE;");
Expand Down
27 changes: 7 additions & 20 deletions arc-utils/src/main/java/fr/insee/arc/utils/dao/UtilitaireDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import fr.insee.arc.utils.exception.ArcException;
import fr.insee.arc.utils.exception.ArcExceptionMessage;
import fr.insee.arc.utils.files.CompressedUtils;
import fr.insee.arc.utils.ressourceUtils.ConnectionAttribute;
import fr.insee.arc.utils.ressourceUtils.PropertiesHandler;
import fr.insee.arc.utils.structure.GenericBean;
import fr.insee.arc.utils.textUtils.IConstanteCaractere;
Expand All @@ -53,9 +54,6 @@ public class UtilitaireDao implements IConstanteNumerique, IConstanteCaractere {
private static final Logger LOGGER = LogManager.getLogger(UtilitaireDao.class);


public static final String CONNECTION_SEPARATOR_RAW = "|||";
private static final String CONNECTION_SEPARATOR = "\\|\\|\\|";

/**
* execute request returns a table with headers, type and data provide the
* indexes of these elements
Expand Down Expand Up @@ -90,20 +88,8 @@ public static final UtilitaireDao get(Integer aPool) {
* @return
*/
public int numberOfNods() {
return numberOfNods(properties.getDatabaseUsername());
return properties.getConnectionProperties().size();
}

/**
* Compute the number of element split ||| . See regexp
* {@value #CONNECTION_SEPARATOR}
*
* @param databaseUserName
* @return
*/
public static int numberOfNods(String databaseUserName) {
return databaseUserName.split(CONNECTION_SEPARATOR).length;
}


/** return a valid connection index according to the given connection in properties
* @param aPool
Expand All @@ -130,10 +116,11 @@ public final Connection getDriverConnexion() throws ArcException {

int validConnectionIndex=validConnectionIndex(this.pool);

String driver = properties.getDatabaseDriverClassName().split(CONNECTION_SEPARATOR)[validConnectionIndex];
String uri = properties.getDatabaseUrl().split(CONNECTION_SEPARATOR)[validConnectionIndex];
String user = properties.getDatabaseUsername().split(CONNECTION_SEPARATOR)[validConnectionIndex];
String password = properties.getDatabasePassword().split(CONNECTION_SEPARATOR)[validConnectionIndex];
ConnectionAttribute currentConnectionAttributes = properties.getConnectionProperties().get(validConnectionIndex);
String driver = currentConnectionAttributes.getDatabaseDriverClassName();
String uri = currentConnectionAttributes.getDatabaseUrl();
String user = currentConnectionAttributes.getDatabaseUsername();
String password = currentConnectionAttributes.getDatabasePassword();

Class.forName(driver);
Connection c = null;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package fr.insee.arc.utils.ressourceUtils;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONObject;

public class ConnectionAttribute {

private String databaseUrl;
private String databaseUsername;
private String databasePassword;
private String databaseDriverClassName;

public ConnectionAttribute(String databaseUrl, String databaseUsername, String databasePassword,
String databaseDriverClassName) {
super();
this.databaseUrl = databaseUrl;
this.databaseUsername = databaseUsername;
this.databasePassword = databasePassword;
this.databaseDriverClassName = databaseDriverClassName;
}



/**
* Ruby map from ci/cd : {0=>"zzz"},{1=>"xxxx"},{2=>"pass\"ji\""}
* No ruby parser in java :(
* Change string to json {0:"zzz",1:"xxxx",2:"pass\"ji\""} to be parsable in java
* replace },{ by ,
* replace => by :
* then cast to json
* @param rubyMapToken
* @return
*/

public static String[] unserialize(String rubyMapToken)
{
// the rubyMapToken must begin with starString or it is not an expected rubyMapToken
String startString = "{0=>";
if (!rubyMapToken.startsWith(startString))
{
// it is not a ruby map key, no parsing required, return the provided string
return new String[] {rubyMapToken};
}

String inputToken = "{0:" + rubyMapToken.substring(startString.length());

// transform ruby in json to parse that correctly
// replace },{ by ,
// replace => by :
int numberOfToken=1;
for (int tokenId=1; tokenId < Integer.MAX_VALUE; tokenId++)
{
String toFind="\"},{"+tokenId+"=>\"";
if (inputToken.contains(toFind))
{
inputToken = inputToken.replace(toFind, "\","+tokenId+":\"");
}
else
{
numberOfToken= numberOfToken + tokenId -1;
break;
}
}

// cast to json
JSONObject parsedMapTokens = new JSONObject(inputToken);

// extract tokens
List<String> resultToken = new ArrayList<>();
for (int tokenId=0; tokenId < numberOfToken; tokenId++)
{
resultToken.add(parsedMapTokens.getString(tokenId+""));
}

return resultToken.toArray(new String[0]);

}




public String getDatabaseUrl() {
return databaseUrl;
}
public void setDatabaseUrl(String databaseUrl) {
this.databaseUrl = databaseUrl;
}
public String getDatabaseUsername() {
return databaseUsername;
}
public void setDatabaseUsername(String databaseUsername) {
this.databaseUsername = databaseUsername;
}
public String getDatabasePassword() {
return databasePassword;
}
public void setDatabasePassword(String databasePassword) {
this.databasePassword = databasePassword;
}
public String getDatabaseDriverClassName() {
return databaseDriverClassName;
}
public void setDatabaseDriverClassName(String databaseDriverClassName) {
this.databaseDriverClassName = databaseDriverClassName;
}




}
Loading

0 comments on commit b16bb87

Please sign in to comment.