Skip to content

Commit

Permalink
Merge pull request #7 from secure-software-engineering/gradle-support
Browse files Browse the repository at this point in the history
  • Loading branch information
oshando authored Jun 21, 2019
2 parents 9fc139a + 7d9313f commit ae0998e
Show file tree
Hide file tree
Showing 12 changed files with 490 additions and 420 deletions.
4 changes: 3 additions & 1 deletion swan_assist/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ repositories {

dependencies {
compile group: 'com.googlecode.json-simple', name: 'json-simple', version: '1.1'
compile group:'de.upb.cs.swt', name: 'swan_core', version: '1.0.0'
compile group:'de.upb.cs.swt', name: 'swan_core', version: '1.3.0'
compile group: 'ca.mcgill.sable', name: 'soot', version: '3.3.0'
compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.5'
compile group: 'org.slf4j', name: 'slf4j-simple', version: '1.7.5'
testCompile group: 'junit', name: 'junit', version: '4.12'
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,20 @@
import de.fraunhofer.iem.swan.assist.data.MethodWrapper;
import de.fraunhofer.iem.swan.assist.ui.dialog.SwanLauncherDialog;
import de.fraunhofer.iem.swan.assist.util.Constants;
import de.fraunhofer.iem.swan.data.Method;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Objects;
import java.util.Properties;
import java.io.InputStreamReader;
import java.util.*;

/**
* Action opens dialog for user to set parameters for running SWAN. After which thread is created to run SWAN.
*/
public class LaunchSwanAction extends AnAction {

protected Set<Method> methods = new HashSet<Method>();
/**
* Obtains application parameters from user, exports updated JSON file and starts thread to run SWAN.
* @param anActionEvent source event
Expand Down Expand Up @@ -70,27 +70,35 @@ public void actionPerformed(AnActionEvent anActionEvent) {

HashMap<String, String> swanParameters = dialog.getParameters();

//Merge current list with training methods
HashMap<String, MethodWrapper> methods = JSONFileLoader.getAllMethods();
if(JSONFileLoader.isFileSelected()) {
//Merge current list with training methods
HashMap<String, MethodWrapper> methods = JSONFileLoader.getAllMethods();

//Load training methods
String trainingFile = Objects.requireNonNull(getClass().getClassLoader().getResource(config.getProperty("train_config_file"))).getPath() ;
JSONFileParser fileParser = new JSONFileParser(trainingFile);
HashMap<String, MethodWrapper> trainingMethods = fileParser.parseJSONFileMap();
InputStream stream = getClass().getClassLoader().getResourceAsStream(config.getProperty("train_config_file"));
HashMap<String, MethodWrapper> trainingMethods = new HashMap<>();

HashMap<String, MethodWrapper> mergedMethods = new HashMap<>(methods);
mergedMethods.putAll(trainingMethods);
if (stream != null) {

//Export changes to configuration files
JSONWriter exportFile = new JSONWriter();
String newConfigFile = swanParameters.get(Constants.SWAN_OUTPUT_DIR) + File.separator + config.getProperty("input_json_suffix");
try {
exportFile.writeToJsonFile(new ArrayList<>(mergedMethods.values()), newConfigFile);
} catch (IOException e) {
e.printStackTrace();
}
JSONFileParser fileParser = new JSONFileParser();
trainingMethods = fileParser.parseJSONFileStream(new InputStreamReader(stream));
}

HashMap<String, MethodWrapper> mergedMethods = new HashMap<>(methods);
mergedMethods.putAll(trainingMethods);

swanParameters.put(Constants.SWAN_CONFIG_FILE, newConfigFile);
//Export changes to configuration files
JSONWriter exportFile = new JSONWriter();
String newConfigFile = swanParameters.get(Constants.SWAN_OUTPUT_DIR) + File.separator + config.getProperty("input_json_suffix");
try {
exportFile.writeToJsonFile(new ArrayList<>(mergedMethods.values()), newConfigFile);
} catch (IOException e) {
e.printStackTrace();
}
swanParameters.put(Constants.SWAN_CONFIG_FILE, newConfigFile);
}
else{
swanParameters.put(Constants.SWAN_CONFIG_FILE, config.getProperty("swan_default_param_value"));
}

SwanProcessBuilder processBuilder = new SwanProcessBuilder(project, dialog.getParameters());
processBuilder.start();
Expand All @@ -108,7 +116,7 @@ public void actionPerformed(AnActionEvent anActionEvent) {
public void update(AnActionEvent event) {

//Disable/Enable action button
if (JSONFileLoader.isReloading() || !JSONFileLoader.isFileSelected())
if (JSONFileLoader.isReloading())
event.getPresentation().setEnabled(false);
else
event.getPresentation().setEnabled(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@

import com.intellij.openapi.project.Project;
import com.intellij.util.messages.MessageBus;
import de.fraunhofer.iem.swan.Main;
import de.fraunhofer.iem.swan.assist.comm.SwanNotifier;
import de.fraunhofer.iem.swan.assist.util.Constants;

import java.io.File;
import java.io.IOException;
import java.io.*;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
Expand Down Expand Up @@ -46,51 +46,36 @@ public void run() {

ResourceBundle resource = ResourceBundle.getBundle("dialog_messages");

String currentTimestamp = getCurrentTimestamp("yyyy-MM-dd-HHmmss");
String currentTimestamp = getCurrentTimestamp();

File outputFolder = new File(parameters.get(Constants.SWAN_OUTPUT_DIR));
outputFolder.mkdirs();

if(!outputFolder.exists())
outputFolder.mkdir();

File logFile = new File(outputFolder, currentTimestamp + parameters.get(Constants.SWAN_OUTPUT_LOG));
try {
logFile.createNewFile();
parameters.replace(Constants.SWAN_OUTPUT_LOG, logFile.getPath());

FileOutputStream fileOutputStream = new FileOutputStream(logFile.getAbsolutePath());

System.setOut(new PrintStream(fileOutputStream));

} catch (IOException e) {
e.printStackTrace();
}

ProcessBuilder processBuilder = new ProcessBuilder("java", "-jar",
parameters.get(Constants.SWAN_JAR_DIR),
parameters.get(Constants.SWAN_SOURCE_DIR),
Main.main(new String[]{parameters.get(Constants.SWAN_SOURCE_DIR),
parameters.get(Constants.SWAN_TRAIN_DIR),
parameters.get(Constants.SWAN_CONFIG_FILE),
parameters.get(Constants.SWAN_OUTPUT_DIR));
parameters.get(Constants.SWAN_OUTPUT_DIR)});

processBuilder.redirectErrorStream(true);
processBuilder.redirectOutput(ProcessBuilder.Redirect.appendTo(logFile));

String message;

try {
Process swanProcess = processBuilder.start();
int result = swanProcess.waitFor();

if (result == 0)
message = resource.getString("Messages.Notification.Success");
else
message = resource.getString("Messages.Notification.Failure");

} catch (IOException | InterruptedException e) {

e.printStackTrace();
message = resource.getString("Messages.Notification.Failure");
}
System.setOut(System.out);

HashMap<String, String> results = new HashMap<String, String>();
results.put(Constants.SWAN_OUTPUT_FILE, parameters.get(Constants.SWAN_OUTPUT_FILE));
results.put(Constants.SWAN_OUTPUT_LOG, parameters.get(Constants.SWAN_OUTPUT_LOG));
results.put(Constants.SWAN_OUTPUT_MESSAGE, message);

MessageBus messageBus = project.getMessageBus();
SwanNotifier publisher = messageBus.syncPublisher(SwanNotifier.END_SWAN_PROCESS_TOPIC);
Expand All @@ -99,12 +84,11 @@ public void run() {

/**
* Get the timestamp in a specified format.
* @param dateFormat Date format that should be used.
* @return Formatted date
*/
private String getCurrentTimestamp(String dateFormat) {
private String getCurrentTimestamp() {

DateTimeFormatter formatter = DateTimeFormatter.ofPattern(dateFormat);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd-HHmmss");
return LocalDateTime.now().format(formatter);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,20 @@ public static void loadInitialFile() {
}

/**
* Compares new JSON file with original file and updates list with merged results
* Compares new JSON file with original file and updates list with merged results. Loads new file
* is a JSON file was not selected.
* @param newFilePath File path of new configuration file
*/
public static void loadUpdatedFile(String newFilePath) {

JSONFileComparator fileComparator = new JSONFileComparator(congFile, newFilePath);
methods = fileComparator.compareJSONFile();
setConfigurationFile(newFilePath);
if(isFileSelected()){
JSONFileComparator fileComparator = new JSONFileComparator(congFile, newFilePath);
methods = fileComparator.compareJSONFile();
setConfigurationFile(newFilePath);
}else{
setConfigurationFile(newFilePath);
loadInitialFile();
}
}

/**
Expand Down Expand Up @@ -215,8 +221,6 @@ public static boolean methodExists(String methodSignature) {
return methods.containsKey(methodSignature);
}

//Returns method for the specified signature

/**
* Returns an instance of the method
* @param methodSignature Method Signature of requested method.
Expand All @@ -227,8 +231,6 @@ public static MethodWrapper getMethod(String methodSignature) {
return methods.get(methodSignature);
}

//Remove method from list

/**
* Remove method from list
* @param method Method to be removed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@
import de.fraunhofer.iem.swan.assist.util.Constants;
import de.fraunhofer.iem.swan.data.Method;

import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.ResourceBundle;
import java.util.Set;

/**
* Parses JSON file and returns methods.
Expand All @@ -32,6 +34,13 @@ public JSONFileParser(String path) {
congFilePath = path;
}

/**
* Default constructor
*/
public JSONFileParser() {

}

/**
* Returns file path for configuration file
* @return File path as a string.
Expand All @@ -54,12 +63,30 @@ public void setCongFilePath(String congFilePath) {
*/
public HashMap<String, MethodWrapper> parseJSONFileMap() {

HashMap<String, MethodWrapper> methods = new HashMap<String, MethodWrapper>();
Parser parser = new Parser(congFilePath);
parser.parse(congFilePath);

return parseMethods(parser.methods());
}

/**
* Parses file and returns method.
* @return HashMap of methods
*/
public HashMap<String, MethodWrapper> parseJSONFileStream(InputStreamReader streamReader) {

Parser parser = new Parser();
parser.parseStream(streamReader);
return parseMethods(parser.methods());
}

private HashMap<String, MethodWrapper> parseMethods(Set<Method> methodsSet){

ResourceBundle resource = ResourceBundle.getBundle("dialog_messages");
HashMap<String, MethodWrapper> methods = new HashMap<String, MethodWrapper>();

try {
for (Method method : parser.parseFile(congFilePath)) {
for (Method method :methodsSet ) {

MethodWrapper methodWrapper = new MethodWrapper(method);

Expand Down
Loading

0 comments on commit ae0998e

Please sign in to comment.