Skip to content

Commit

Permalink
Clean Sonar bugs and smells
Browse files Browse the repository at this point in the history
  • Loading branch information
amrabed committed Nov 28, 2018
1 parent 08e4ad6 commit 7a3484f
Show file tree
Hide file tree
Showing 20 changed files with 401 additions and 623 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ build/
.gradle/
/bin/

.idea/
*.iml
*.class

.settings/
.classpath
.project
20 changes: 0 additions & 20 deletions analyzer/Aggregator.java

This file was deleted.

13 changes: 5 additions & 8 deletions analyzer/BoSC.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
package analyzer;

import java.util.ArrayList;
import java.util.Collections;

/**
* Bag of System Calls
*
* @author AmrAbed
*
*/
public class BoSC extends ArrayList<Byte>
{
public class BoSC extends ArrayList<Byte> {
private static final long serialVersionUID = 1L;

public BoSC(String string)
{
public BoSC(String string) {
string = string.replace("[", "");
string = string.replace("]", "");

for (String value : string.split(","))
{
for (String value : string.split(",")) {
add(Byte.parseByte(value.trim()));
}
}
Expand Down
20 changes: 0 additions & 20 deletions analyzer/Comparator.java

This file was deleted.

100 changes: 40 additions & 60 deletions analyzer/Database.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
package analyzer;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
Expand All @@ -10,118 +12,96 @@
* Normal-behavior Database
*
* @author AmrAbed
*
*/
public class Database extends HashMap<BoSC, Long>
{
public class Database extends HashMap<BoSC, Long> {
private static final long serialVersionUID = 1L;

/**
* Create normal-behavior database from a dump file
*
* @param filePath
* path to the dump file
* @throws NumberFormatException
* @param filePath path to the dump file
* @throws IOException
*/
public Database(String filePath) throws NumberFormatException, IOException
{
if (filePath != null)
{
final BufferedReader reader = new BufferedReader(new FileReader(filePath));
String line;
while ((line = reader.readLine()) != null)
{
final String[] words = line.split("\t");
put(new BoSC(words[0]), Long.parseLong(words[1]));
public Database(String filePath) throws IOException {
if (filePath != null) {
try (final BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
String line;
while ((line = reader.readLine()) != null) {
final String[] words = line.split("\t");
put(new BoSC(words[0]), Long.parseLong(words[1]));
}
} finally {
reader.close();
}
reader.close();
}
}

public void add(BoSC bosc)
{
if (bosc == null)
{
public void add(BoSC bosc) {
if (bosc == null) {
return;
}

if (containsKey(bosc))
{
if (containsKey(bosc)) {
replace(bosc, get(bosc) + 1);
}
else
{
} else {
put(bosc, 1L);
}
}

public void commit(Database change)
{
for (BoSC bosc : change.keySet())
{
public void commit(Database change) {
for (BoSC bosc : change.keySet()) {
long value = change.get(bosc);
if (this.containsKey(bosc))
{
if (this.containsKey(bosc)) {
value += this.get(bosc);
}
this.put(bosc, value);
}
}

private double getNorm()
{
private double getNorm() {
double norm = 0;
for (BoSC bosc : keySet())
{
for (BoSC bosc : keySet()) {
long val = get(bosc);
norm += val * val;
}
return Math.sqrt(norm);
}

public double calculateSimilarity(Database other)
{
if (this.isEmpty() || other.isEmpty())
{
public double calculateSimilarity(Database other) {
if (this.isEmpty() || other.isEmpty()) {
return 0;
}

double dot = 0;
double norm1 = this.getNorm(), norm2 = other.getNorm();
for (BoSC bosc : keySet())
{
// System.out.print(bosc + " -> ");
if (other.containsKey(bosc))
{
double norm1 = this.getNorm();
double norm2 = other.getNorm();
for (BoSC bosc : keySet()) {
if (other.containsKey(bosc)) {
long value1 = other.get(bosc);
long value2 = this.get(bosc);
// System.out.print("found (Frequencies: " + value2 + " & " + value1 + ")");
dot += value1 * value2;
}
}
return dot / (norm1 * norm2);
}

public void dump(String file) throws FileNotFoundException
{
try (PrintStream out = new PrintStream(file))
{
for (BoSC bosc : keySet())
{
public void dump(String file) throws FileNotFoundException {
try (PrintStream out = new PrintStream(file)) {
for (BoSC bosc : keySet()) {
out.println(bosc + "\t" + get(bosc));
}
}
}

@Override
public String toString()
{
String output = new String();
for (ArrayList<Byte> entry : keySet())
{
output += entry + " => " + get(entry) + "\n";
public String toString() {
final StringBuilder output = new StringBuilder();
for (Entry<BoSC, Long> entry : entrySet()) {
output.append(entry.getKey());
output.append(" => ");
output.append(entry.getValue());
output.append('\n');
}
return output;
return output.toString();
}
}
21 changes: 21 additions & 0 deletions analyzer/aggregate/Aggregator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package analyzer.aggregate;

import java.util.logging.Logger;
import java.util.logging.Level;

import analyzer.Database;

public class Aggregator {
public static void main(String[] args) {
try {
final Database db1 = new Database(args[0]);
final Database db2 = new Database(args[1]);

db1.commit(db2);
db1.dump(args[2]);

} catch (Exception e) {
Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).log(Level.SEVERE, e.toString());
}
}
}
20 changes: 20 additions & 0 deletions analyzer/compare/Comparator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package analyzer.compare;

import java.util.logging.Logger;
import java.util.logging.Level;

import analyzer.Database;

public class Comparator {
public static void main(String[] args) {
try {
final Database db1 = new Database(args[0]);
final Database db2 = new Database(args[1]);
final double similarity = db1.calculateSimilarity(db2);
Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).log(Level.INFO, () -> "Similaity: " + similarity);

} catch (Exception e) {
Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).log(Level.SEVERE, e.toString());
}
}
}
4 changes: 1 addition & 3 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'application'

mainClassName = 'edu.vt.rhids.main.RHIDS'
Expand All @@ -16,11 +15,10 @@ task createJar(type: Jar) {
with jar
}


repositories {
mavenCentral()
}

dependencies {
compile group: 'commons-cli', name: 'commons-cli', version: '1.2'
implementation 'commons-cli:commons-cli:1.2'
}
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Sun Mar 05 18:43:08 UTC 2017
#Wed Nov 28 12:06:22 EET 2018
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-3.4.1-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-3.4.1-all.zip
Loading

0 comments on commit 7a3484f

Please sign in to comment.