Commit 105ea70e authored by Pierre-Yves Gibello's avatar Pierre-Yves Gibello
Browse files

Add PKM logs, and align with latest versions of pkm-api

parent ccd61b9e
......@@ -35,19 +35,20 @@ Concerning the project configuration, the properties file provided should provid
```
# Properties file example, to inject JML into PKM using JmlGen
# PKM location, database name and credentials
baseUrl: https://pkm-api_pkm_1:8080
certificate: src/main/resources/pkm_docker.crt
# PKM location, project name and credentials
baseUrl: http://pkm-api_pkm_1:8080
#baseUrl: https://pkm-api_pkm_1:8080
#certificate: src/main/resources/pkm_docker.crt
# Sample based on MyThaiStar, available on https://github.com/devonfw/my-thai-star
# To check, query RawSourceCode collection in DB OW2-MyThaiStar as follows:
# db.getCollection('RawSourcecode').find({'filename':'java/mtsj/api/src/main/java/com/devonfw/application/mtsj/general/logic/api/to/BinaryObjectEto.java'})
database: OW2-MyThaiStar
project: OW2-MyThaiStar
user: admin
password: admin
#deleteTempFiles: true
# Mock test: if true, database is deleted at logout (default false)
# Mock test: if true, project is deleted at logout (default false)
#mockTest: true
```
package eu.decoder.jmlgen;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintStream;
import java.io.StringReader;
import java.nio.file.Files;
import java.nio.file.Path;
......@@ -18,6 +21,7 @@ import org.openapitools.client.model.PkmFile;
import eu.decoder.jmlgen.generator.JmlGenerator;
import eu.decoder.pkm.client.PkmClient;
import eu.decoder.pkm.client.PkmLogItems;
public class PkmJmlGen {
......@@ -32,29 +36,30 @@ public class PkmJmlGen {
}
/**
* Connect to PKM on specified database
* @param database the PKM database name (null if specified in configuration)
* @param createDbIfNotExists If true, create database if not exists
* Connect to PKM on specified project
* @param project the PKM project name (null if specified in configuration)
* @param createDbIfNotExists If true, create project if not exists
* @return The connected PKM client
* @throws ApiException
*/
public PkmClient login(String database, boolean createDbIfNotExists) throws ApiException {
if(database == null) database = getProperty("database");
if(database == null) {
public PkmClient login(String project, boolean createDbIfNotExists) throws ApiException {
if(project == null) project = getProperty("project");
if(project == null) project = getProperty("database"); // backward compatibility with previous versions
if(project == null) {
// error
throw new RuntimeException("Error: missing database property");
throw new RuntimeException("Error: missing project property");
}
//String codePath = getProperty("codePath");
//if(codePath == null) codePath = tmpDir.toString();
String baseUrl = getProperty("baseUrl");
if(baseUrl == null) baseUrl = "https://pkm-api_pkm_1:8080";
if(baseUrl == null) baseUrl = "http://pkm-api_pkm_1:8080";
// Initialize PKM client
PkmClient client;
try {
client = new PkmClient(baseUrl, database, getProperty("certificate"));
client = new PkmClient(baseUrl, project, getProperty("certificate"));
} catch (FileNotFoundException e) {
throw new ApiException(e);
}
......@@ -69,14 +74,15 @@ public class PkmJmlGen {
/**
* Inject JML into the PKM, according to configuration
* @param database the PKM database name (null if specified in configuration)
* @param project the PKM project name (null if specified in configuration)
* @param logout true to log out after injection, false otherwise
* @throws Exception
*/
public void injectJmlIntoPKM(String database, boolean logout) throws Exception {
public void injectJmlIntoPKM(String project, boolean logout) throws Exception {
PkmClient client = login(database, true);
PkmClient client = login(project, true);
Path tmpDir = Files.createTempDirectory("pkm-").toAbsolutePath();
PkmLogItems pkmLog = new PkmLogItems("jmlgen");
// Extract data from PKM, and copy them to temporary dir
List<PkmFile> pkmFiles = client.getAllSourceFiles(true, ".java");
......@@ -85,7 +91,9 @@ public class PkmJmlGen {
for(PkmFile pkmFile : pkmFiles) {
Path parent = Paths.get(pkmFile.getRelPath()).getParent();
if(parent == null) {
System.err.println("Ignoring file " + pkmFile.getRelPath() + ": parent path unknown");
String message = "Ignoring file " + pkmFile.getRelPath() + ": parent path unknown";
System.err.println(message);
pkmLog.addError(message); // Log error in PKM Logs
} else {
String relFileDir = parent.toString().trim();
// If relative path is absolute, simply make it relative by removing first /
......@@ -115,19 +123,40 @@ public class PkmJmlGen {
}
if(codePath.length() < 1) codePath.append("src/main/java");
// Generate JML
JmlGenerator.generateJml(tmpDir.toString(), tmpDir.toString(),
codePath.toString(),
System.out);
PrintStream out = null;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
// Prepare JML generation logging
out = new PrintStream(baos, true);
// Generate JML
JmlGenerator.generateJml(tmpDir.toString(), tmpDir.toString(),
codePath.toString(),
out);
} catch(IOException e) {
pkmLog.addError(e.getMessage());
throw e;
} finally {
if(out != null) {
// Log all JML generation traces into PKM
String jmlLogs = new String(baos.toByteArray());
out.close();
System.out.println(jmlLogs);
pkmLog.addLog(jmlLogs); // Single log with all stuff...
//addPkmLogs(pkmLog, jmlLogs); // multiline log
}
}
// Put JML-annotated data back to the PKM
System.out.print("Injecting JML into PKM...");
pkmLog.addLog("Start injecting JML into PKM");
for(PkmFile pkmFile : pkmFiles) {
client.postSourceFile(tmpDir.toFile(),
Paths.get(tmpDir.toString(), pkmFile.getRelPath()).toFile(), true);
}
System.out.println("...done!");
pkmLog.addLog("Done injecting JML into PKM");
if(! "false".equalsIgnoreCase(getProperty("deleteTempFiles"))) {
// Delete temp directory recursively
// See https://www.baeldung.com/java-delete-directory
......@@ -141,8 +170,11 @@ public class PkmJmlGen {
if("true".equalsIgnoreCase(getProperty("mockTest"))) {
System.out.println("Deleting DB (mockTest set to true in configuration)");
client.setDbTransient();
client.setProjectTransient();
}
client.logItems(pkmLog); // Flush PKM log items into database
if(logout) client.logout();
}
......@@ -200,4 +232,22 @@ public class PkmJmlGen {
System.exit(0);
}
/**
* Log each line of a multiline string into the PKM
* @param pkmLogs
* @param logs
*/
private void addPkmLogs(PkmLogItems pkmLogs, String logs) {
BufferedReader in = null;
try {
in = new BufferedReader(new StringReader(logs));
String log;
while((log = in.readLine()) != null) {
pkmLogs.addLog(log);
}
in.close();
} catch(IOException e) {
if(in != null) try { in.close(); } catch(Exception ignore) { }
}
}
}
# Properties file example, to inject JML into PKM using JmlGen
# PKM location, database name and credentials
baseUrl: https://pkm-api_pkm_1:8080
certificate: src/main/resources/pkm_docker.crt
# PKM location, project name and credentials
baseUrl: http://pkm-api_pkm_1:8080
#baseUrl: https://pkm-api_pkm_1:8080
#certificate: src/main/resources/pkm_docker.crt
# Sample based on MyThaiStar, available on https://github.com/devonfw/my-thai-star
# To check, query RawSourceCode collection in DB OW2-MyThaiStar as follows:
# db.getCollection('RawSourcecode').find({'filename':'java/mtsj/api/src/main/java/com/devonfw/application/mtsj/general/logic/api/to/BinaryObjectEto.java'})
database: OW2-MyThaiStar
project: OW2-MyThaiStar
user: admin
password: admin
#deleteTempFiles: true
# Mock test: if true, database is deleted at logout (default false)
# Mock test: if true, project is deleted at logout (default false)
#mockTest: true
-----BEGIN CERTIFICATE-----
MIIDfDCCAmSgAwIBAgIJANpNsWWgHx8GMA0GCSqGSIb3DQEBCwUAMEExCzAJBgNV
MIIDjjCCAnagAwIBAgIJAKlupRgyJcVnMA0GCSqGSIb3DQEBCwUAMEExCzAJBgNV
BAYTAmV1MQ8wDQYDVQQIDAZGcmFuY2UxEzARBgNVBAoMCkRlY29kZXIuZXUxDDAK
BgNVBAsMA0NFQTAeFw0yMDExMTMxNTEzMTVaFw0zMDExMTExNTEzMTVaMEExCzAJ
BgNVBAsMA0NFQTAeFw0yMTAyMDIwNTQzMTNaFw0zMTAxMzEwNTQzMTNaMEExCzAJ
BgNVBAYTAmV1MQ8wDQYDVQQIDAZGcmFuY2UxEzARBgNVBAoMCkRlY29kZXIuZXUx
DDAKBgNVBAsMA0NFQTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAMLH
/K3ERZSRDZd9QcFgGCLnMVVLF/MXZLrqbMYDrBZp91qhrFD8FWTgLBL2BiJIVgPN
lm7+WbJMXgZGKWM623cgzdmNzq5Sz8GvLXsoYxsoRmAXY4jsb9D3veTrGapVANay
InJaBEMkiIl8xTmjlzI/WlJeoCXoxUNWXg5XVDeVa4jNSTDUaq6UzbWDpzXN+ncU
n4RH2TP11ekpXIEm9ceLujxc06DYMi3e9VCg55ZZRfVX5ZJ424GBPLRZoYHLGqou
o7NLQR0JtqL0lr64pBHubHduc9+gAlhYHUZuq8FaI6dnm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pX3BrbV8xghFwa20tYXBpX21vbmdv
ZGJfMYIRcGttLWFwaV9mcmFtYS1jXzGCFXBrbS1hcGlfZnJhbWEtY2xhbmdfMYIO
cGttLWFwaV9hc2ZtXzEwDQYJKoZIhvcNAQELBQADggEBAJWGi4bVNyyuvYWtoy3w
YsuyfZlkMCcwpJwBnWc2+58yVoqBtuM59cdFwFUG0rZ0c0gz5PUdnU8Ke4HBNZQ3
JQ5Zds26dU2jxt9gr/XEsp7vKZQm1F5F7OLLLKm1QfJHofESQbupO15/cwjYX8a+
GGFj4h3UbhhhUeQfLUDbVaBefa+utzDqA4KFTFYT/l6WjCrxN0piL6ggWZ7WQGII
G4ZU7l1eTEnlAKc8mULMsBhr6ZEI4tghYprszc/tgisnA6Ixy9FNqqJ7MN+K7xWn
CfgTNX2k2IN96ms6SfTGGa4t8qQyyPdAbX2T9eeq+WsvOHfLnozOxe5lb7//+Rgn
YsE=
-----END CERTIFICATE-----
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment