Skip to content

Commit

Permalink
Remove arch dependant bson lib (#1023)
Browse files Browse the repository at this point in the history
* Integrate new bson lib

Due to the nature of not knowing what systems the Java libs will run on, it makes sense to use a pure java based implementation of BSON encoding/decoding until the Livio C lib can be compiled for more architectures, or made on the fly.
  • Loading branch information
joeygrover committed Mar 29, 2019
1 parent e76e75f commit b9bf643
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 2 deletions.
87 changes: 87 additions & 0 deletions base/src/main/java/com/livio/BSON/BsonEncoder.java
@@ -0,0 +1,87 @@
package com.livio.BSON;

import com.smartdevicelink.util.DebugTool;
import org.bson.*;

import java.util.*;

public class BsonEncoder {


public static byte[] encodeToBytes(HashMap<String, Object> map) throws ClassCastException {
if(map != null) {
BasicBSONObject bson = new BasicBSONObject();
bson.putAll(sanitizeMap(map));
BasicBSONEncoder encoder = new BasicBSONEncoder();

return encoder.encode(bson);
}
DebugTool.logError("Something went wrong encoding the map into BSON bytes");

return null;
}

public static HashMap<String, Object> decodeFromBytes(byte[] bytes) {
if(bytes != null) {
BasicBSONDecoder decoder = new BasicBSONDecoder();
BSONObject object = decoder.readObject(bytes);
if (object != null) {
Map<String, Object> map = object.toMap();
if (map != null) {
return sanitizeMap(new HashMap<>(map));
}
}
}
DebugTool.logError("Something went wrong decoding bytes into BSON");
return null;
}

/**
* Goes thorugh the map and ensures that all the values included are supported by SDL. If they are not of a supported
* value it is removed from the map
* @param map the map to be sanitized
* @return a sanitized HashMap with non-supported object type removes
*/
private static HashMap<String, Object> sanitizeMap(HashMap<String, Object> map){
Set<String> keys = map.keySet();
Object value;
for(String key : keys){
value = map.get(key);

//First check to see if it value is a valid type used in SDL
if(isSupportedType(value)){
continue;
}else if(value instanceof List){ //Next, check to see if it is a list of values
List list = (List)value;

//If the list is empty, there shouldn't be a problem leaving it in the map
if(list.isEmpty()){
continue;
}

//Since the list isn't empty, check the first item
if(isSupportedType(list.get(0))){
continue;
}
}
//If the item isn't valid, remove it from the map
map.remove(key);

}
return map;
}

/**
* Checks the value to ensure it is of a supported type
* @param value the generic object value
* @return if the object is an instanceOf one of the supported SDL BSON objects
*/
private static boolean isSupportedType(Object value){
return value instanceof Integer
|| value instanceof Long
|| value instanceof Double
|| value instanceof String
|| value instanceof Boolean;
}

}
2 changes: 1 addition & 1 deletion javaEE/build.gradle
Expand Up @@ -27,7 +27,7 @@ configurations {

dependencies {
extraLibs fileTree(dir: 'libs', include: ['*.jar'])
extraLibs fileTree(dir: '../javaSE/libs', include: ['*.jar']) //BSON lib
extraLibs 'org.mongodb:bson:3.10.1'
extraLibs 'com.android.support:support-annotations:28.0.0'
extraLibs 'org.java-websocket:Java-WebSocket:1.3.9'
configurations.api.extendsFrom(configurations.extraLibs)
Expand Down
1 change: 1 addition & 0 deletions javaSE/build.gradle
Expand Up @@ -28,6 +28,7 @@ configurations {

dependencies {
extraLibs fileTree(dir: 'libs', include: ['*.jar'])
extraLibs 'org.mongodb:bson:3.10.1'
extraLibs 'com.android.support:support-annotations:28.0.0'
extraLibs 'org.java-websocket:Java-WebSocket:1.3.9'
configurations.api.extendsFrom(configurations.extraLibs)
Expand Down
Binary file removed javaSE/libs/bson_java_lib.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion javaSE/src/main/java/com/smartdevicelink/BuildConfig.java
@@ -1,5 +1,5 @@
package com.smartdevicelink;

public final class BuildConfig {
public static final String VERSION_NAME = "RC1-4.8.0";
public static final String VERSION_NAME = "RC2-4.8.0";
}
6 changes: 6 additions & 0 deletions third_party.md
Expand Up @@ -74,6 +74,12 @@ The third party software included and used by this project is:
* Licensed under Apache License, Version 2.0
* See [https://services.gradle.org/distributions/gradle-4.4-all.zip](https://services.gradle.org/distributions/gradle-4.4-all.zip)

**BSON - MongoDB**

* `org.mongodb:bson`
* Licensed under Apache License, Version 2.0
* See [https://mvnrepository.com/artifact/org.mongodb/bson/3.10.1](https://mvnrepository.com/artifact/org.mongodb/bson/3.10.1)


### SDL JavaEE

Expand Down

0 comments on commit b9bf643

Please sign in to comment.