Skip to content

Commit ae0998e

Browse files
authored
Merge pull request #7 from secure-software-engineering/gradle-support
2 parents 9fc139a + 7d9313f commit ae0998e

File tree

12 files changed

+490
-420
lines changed

12 files changed

+490
-420
lines changed

swan_assist/build.gradle

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@ repositories {
1414

1515
dependencies {
1616
compile group: 'com.googlecode.json-simple', name: 'json-simple', version: '1.1'
17-
compile group:'de.upb.cs.swt', name: 'swan_core', version: '1.0.0'
17+
compile group:'de.upb.cs.swt', name: 'swan_core', version: '1.3.0'
1818
compile group: 'ca.mcgill.sable', name: 'soot', version: '3.3.0'
19+
compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.5'
20+
compile group: 'org.slf4j', name: 'slf4j-simple', version: '1.7.5'
1921
testCompile group: 'junit', name: 'junit', version: '4.12'
2022
}
2123

swan_assist/src/main/java/de/fraunhofer/iem/swan/assist/actions/LaunchSwanAction.java

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,20 @@
2020
import de.fraunhofer.iem.swan.assist.data.MethodWrapper;
2121
import de.fraunhofer.iem.swan.assist.ui.dialog.SwanLauncherDialog;
2222
import de.fraunhofer.iem.swan.assist.util.Constants;
23+
import de.fraunhofer.iem.swan.data.Method;
2324

2425
import java.io.File;
2526
import java.io.IOException;
2627
import java.io.InputStream;
27-
import java.util.ArrayList;
28-
import java.util.HashMap;
29-
import java.util.Objects;
30-
import java.util.Properties;
28+
import java.io.InputStreamReader;
29+
import java.util.*;
3130

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

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

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

73-
//Merge current list with training methods
74-
HashMap<String, MethodWrapper> methods = JSONFileLoader.getAllMethods();
73+
if(JSONFileLoader.isFileSelected()) {
74+
//Merge current list with training methods
75+
HashMap<String, MethodWrapper> methods = JSONFileLoader.getAllMethods();
7576

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

81-
HashMap<String, MethodWrapper> mergedMethods = new HashMap<>(methods);
82-
mergedMethods.putAll(trainingMethods);
80+
if (stream != null) {
8381

84-
//Export changes to configuration files
85-
JSONWriter exportFile = new JSONWriter();
86-
String newConfigFile = swanParameters.get(Constants.SWAN_OUTPUT_DIR) + File.separator + config.getProperty("input_json_suffix");
87-
try {
88-
exportFile.writeToJsonFile(new ArrayList<>(mergedMethods.values()), newConfigFile);
89-
} catch (IOException e) {
90-
e.printStackTrace();
91-
}
82+
JSONFileParser fileParser = new JSONFileParser();
83+
trainingMethods = fileParser.parseJSONFileStream(new InputStreamReader(stream));
84+
}
85+
86+
HashMap<String, MethodWrapper> mergedMethods = new HashMap<>(methods);
87+
mergedMethods.putAll(trainingMethods);
9288

93-
swanParameters.put(Constants.SWAN_CONFIG_FILE, newConfigFile);
89+
//Export changes to configuration files
90+
JSONWriter exportFile = new JSONWriter();
91+
String newConfigFile = swanParameters.get(Constants.SWAN_OUTPUT_DIR) + File.separator + config.getProperty("input_json_suffix");
92+
try {
93+
exportFile.writeToJsonFile(new ArrayList<>(mergedMethods.values()), newConfigFile);
94+
} catch (IOException e) {
95+
e.printStackTrace();
96+
}
97+
swanParameters.put(Constants.SWAN_CONFIG_FILE, newConfigFile);
98+
}
99+
else{
100+
swanParameters.put(Constants.SWAN_CONFIG_FILE, config.getProperty("swan_default_param_value"));
101+
}
94102

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

110118
//Disable/Enable action button
111-
if (JSONFileLoader.isReloading() || !JSONFileLoader.isFileSelected())
119+
if (JSONFileLoader.isReloading())
112120
event.getPresentation().setEnabled(false);
113121
else
114122
event.getPresentation().setEnabled(true);

swan_assist/src/main/java/de/fraunhofer/iem/swan/assist/actions/SwanProcessBuilder.java

Lines changed: 15 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99

1010
import com.intellij.openapi.project.Project;
1111
import com.intellij.util.messages.MessageBus;
12+
import de.fraunhofer.iem.swan.Main;
1213
import de.fraunhofer.iem.swan.assist.comm.SwanNotifier;
1314
import de.fraunhofer.iem.swan.assist.util.Constants;
1415

15-
import java.io.File;
16-
import java.io.IOException;
16+
import java.io.*;
1717
import java.time.LocalDateTime;
1818
import java.time.format.DateTimeFormatter;
1919
import java.util.HashMap;
@@ -46,51 +46,36 @@ public void run() {
4646

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

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

5151
File outputFolder = new File(parameters.get(Constants.SWAN_OUTPUT_DIR));
52-
outputFolder.mkdirs();
52+
53+
if(!outputFolder.exists())
54+
outputFolder.mkdir();
5355

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

61+
FileOutputStream fileOutputStream = new FileOutputStream(logFile.getAbsolutePath());
62+
63+
System.setOut(new PrintStream(fileOutputStream));
64+
5965
} catch (IOException e) {
6066
e.printStackTrace();
6167
}
6268

63-
ProcessBuilder processBuilder = new ProcessBuilder("java", "-jar",
64-
parameters.get(Constants.SWAN_JAR_DIR),
65-
parameters.get(Constants.SWAN_SOURCE_DIR),
69+
Main.main(new String[]{parameters.get(Constants.SWAN_SOURCE_DIR),
6670
parameters.get(Constants.SWAN_TRAIN_DIR),
6771
parameters.get(Constants.SWAN_CONFIG_FILE),
68-
parameters.get(Constants.SWAN_OUTPUT_DIR));
72+
parameters.get(Constants.SWAN_OUTPUT_DIR)});
6973

70-
processBuilder.redirectErrorStream(true);
71-
processBuilder.redirectOutput(ProcessBuilder.Redirect.appendTo(logFile));
72-
73-
String message;
74-
75-
try {
76-
Process swanProcess = processBuilder.start();
77-
int result = swanProcess.waitFor();
78-
79-
if (result == 0)
80-
message = resource.getString("Messages.Notification.Success");
81-
else
82-
message = resource.getString("Messages.Notification.Failure");
83-
84-
} catch (IOException | InterruptedException e) {
85-
86-
e.printStackTrace();
87-
message = resource.getString("Messages.Notification.Failure");
88-
}
74+
System.setOut(System.out);
8975

9076
HashMap<String, String> results = new HashMap<String, String>();
9177
results.put(Constants.SWAN_OUTPUT_FILE, parameters.get(Constants.SWAN_OUTPUT_FILE));
9278
results.put(Constants.SWAN_OUTPUT_LOG, parameters.get(Constants.SWAN_OUTPUT_LOG));
93-
results.put(Constants.SWAN_OUTPUT_MESSAGE, message);
9479

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

10085
/**
10186
* Get the timestamp in a specified format.
102-
* @param dateFormat Date format that should be used.
10387
* @return Formatted date
10488
*/
105-
private String getCurrentTimestamp(String dateFormat) {
89+
private String getCurrentTimestamp() {
10690

107-
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(dateFormat);
91+
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd-HHmmss");
10892
return LocalDateTime.now().format(formatter);
10993
}
11094
}

swan_assist/src/main/java/de/fraunhofer/iem/swan/assist/data/JSONFileLoader.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,20 @@ public static void loadInitialFile() {
7373
}
7474

7575
/**
76-
* Compares new JSON file with original file and updates list with merged results
76+
* Compares new JSON file with original file and updates list with merged results. Loads new file
77+
* is a JSON file was not selected.
7778
* @param newFilePath File path of new configuration file
7879
*/
7980
public static void loadUpdatedFile(String newFilePath) {
8081

81-
JSONFileComparator fileComparator = new JSONFileComparator(congFile, newFilePath);
82-
methods = fileComparator.compareJSONFile();
83-
setConfigurationFile(newFilePath);
82+
if(isFileSelected()){
83+
JSONFileComparator fileComparator = new JSONFileComparator(congFile, newFilePath);
84+
methods = fileComparator.compareJSONFile();
85+
setConfigurationFile(newFilePath);
86+
}else{
87+
setConfigurationFile(newFilePath);
88+
loadInitialFile();
89+
}
8490
}
8591

8692
/**
@@ -215,8 +221,6 @@ public static boolean methodExists(String methodSignature) {
215221
return methods.containsKey(methodSignature);
216222
}
217223

218-
//Returns method for the specified signature
219-
220224
/**
221225
* Returns an instance of the method
222226
* @param methodSignature Method Signature of requested method.
@@ -227,8 +231,6 @@ public static MethodWrapper getMethod(String methodSignature) {
227231
return methods.get(methodSignature);
228232
}
229233

230-
//Remove method from list
231-
232234
/**
233235
* Remove method from list
234236
* @param method Method to be removed

swan_assist/src/main/java/de/fraunhofer/iem/swan/assist/data/JSONFileParser.java

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@
1414
import de.fraunhofer.iem.swan.assist.util.Constants;
1515
import de.fraunhofer.iem.swan.data.Method;
1616

17+
import java.io.InputStreamReader;
1718
import java.util.HashMap;
1819
import java.util.ResourceBundle;
20+
import java.util.Set;
1921

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

37+
/**
38+
* Default constructor
39+
*/
40+
public JSONFileParser() {
41+
42+
}
43+
3544
/**
3645
* Returns file path for configuration file
3746
* @return File path as a string.
@@ -54,12 +63,30 @@ public void setCongFilePath(String congFilePath) {
5463
*/
5564
public HashMap<String, MethodWrapper> parseJSONFileMap() {
5665

57-
HashMap<String, MethodWrapper> methods = new HashMap<String, MethodWrapper>();
5866
Parser parser = new Parser(congFilePath);
67+
parser.parse(congFilePath);
68+
69+
return parseMethods(parser.methods());
70+
}
71+
72+
/**
73+
* Parses file and returns method.
74+
* @return HashMap of methods
75+
*/
76+
public HashMap<String, MethodWrapper> parseJSONFileStream(InputStreamReader streamReader) {
77+
78+
Parser parser = new Parser();
79+
parser.parseStream(streamReader);
80+
return parseMethods(parser.methods());
81+
}
82+
83+
private HashMap<String, MethodWrapper> parseMethods(Set<Method> methodsSet){
84+
5985
ResourceBundle resource = ResourceBundle.getBundle("dialog_messages");
86+
HashMap<String, MethodWrapper> methods = new HashMap<String, MethodWrapper>();
6087

6188
try {
62-
for (Method method : parser.parseFile(congFilePath)) {
89+
for (Method method :methodsSet ) {
6390

6491
MethodWrapper methodWrapper = new MethodWrapper(method);
6592

0 commit comments

Comments
 (0)